Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat(render): the Skin shader takes a tiled detail normal map for pores and creases

yhh7cb53182b8fd
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 48f4d33eb..8d8a7c472 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -35,7 +35,10 @@ published separately; it ships inside the editor.
- **Contact shadows and screen-space bounce light.** Contact Shadows and Screen Space GI effects add small sharp shadows at contacts and one bounce of colour between surfaces, and the weather softens the sun's shadows. `contactShadows` marches a short ray toward the scene's sun in the depth buffer; `ssgi` gathers eight rays a pixel over the G-buffer's normals at half size into a float target and averages the neighbours on the same surface before laying the light, times the receiver's colour, over the picture. The Weather component raises the sun's `sourceAngle` and lowers its `shadowStrength` as the sky clouds over.
- **Terrain and Foliage.** A Terrain component makes ground from a heightmap or noise, paints it with grass, dirt, snow and rock layers and draws it in chunks that coarsen with distance; a Foliage component scatters grass or a mesh over it, swaying in the wind and thinning out with distance.
- **Separate material maps packed on import.** Importing an FBX with separate metalness, roughness and occlusion images now packs them into the one map the Model shader samples, instead of dropping them. Occlusion goes in red, roughness in green and metal in blue, each from its image's red channel (a glossiness map is inverted); a channel with no image stays at full so the material's own value speaks. Only PNG and DDS can be read.
+- **Curvature-shaped skin scattering.** The Skin shader can take its wrap past the terminator per colour and shape it by curvature: `u_preIntegrate` (red travels furthest, every colour further where the surface turns sharply) and `u_curvature`, so a nose or an ear glows at its edge while a flat cheek stays crisp. Off by default.
+- **Skin detail normal layer.** The Skin shader has a detail normal layer: `u_detailMap` is a fine normal map tiled over the surface (`u_detailTile`) for pores and creases, laid on the sharp normal the shine reads by `u_detailStrength` (0 leaves it out) and not on the soft diffuse normals.
- **Cutout shadows for hair cards and leaves.** A material whose alpha is coverage (`u_alphaCutoff` above 0, now also on the Hair shader) casts its cut-out shape into shadow maps instead of the rectangle it is drawn on, and Hair cards feather their edges with alpha-to-coverage.
+- **Eye lid shadow.** The Eye shader shades the eye the lids sit on: `u_lidMap` is an authored occlusion map, and `u_lidShadow` darkens, and fades the cornea's reflections, where the surface faces up from `u_lidStart`, which is the upper lid on an upright head.
- **Eye shader.** A new Eye shader (`builtin:eye`) draws an eye from a mask and a dome normal map: the iris sits behind a refracting cornea so it moves as the eye turns, with a pupil, fibres and a limbal ring, a white lit with wrapped light, and a near-mirror cornea that shows every light as a catchlight.
- **Skin and hair.** Two new shaders for characters: Skin softens diffuse light by colour, warms the terminator and lets light through thin places without blurring the picture, and Hair lights strands along a flow map with two shifted highlights. Normal maps on stretched UVs (a face unwrapped into an atlas) now keep their shape, and mipmapped textures are sampled with anisotropic filtering where the device has it. Both read a normal map and an occlusion/roughness map as data textures (import them with sRGB off); the Skin shader shades each pixel with three lighting passes, so it is for characters in view. Hair is shaded as strands but a hair model is still cards or ribbons.
- **Anisotropic filtering.** Mipmapped textures are now sampled with up to 16x anisotropic filtering where the device has it (WebGL2 with the extension, WebGPU always), so a floor, a hair card or a sleeve seen at a slant keeps its detail instead of washing out to grey.
diff --git a/docs/astro/src/content/docs/graphics/materials.mdx b/docs/astro/src/content/docs/graphics/materials.mdx
index bde7066e8..760b70d8e 100644
--- a/docs/astro/src/content/docs/graphics/materials.mdx
+++ b/docs/astro/src/content/docs/graphics/materials.mdx
@@ -77,7 +77,7 @@ curve, and it does that here per colour: the diffuse light is taken from a softe
a thin or strongly curved place lets light through (`u_translucency`, `u_thinness`: an ear, a nostril, a finger), the shine is two
lobes (`u_specular`, `u_lobe2`, `u_lobe2Roughness`) and a faint fuzz lights the rim (`u_fuzz`). Turn `u_subsurface` on to also mark it for the
[Subsurface Scattering](/docs/graphics/post-processing/#subsurface-scattering) effect, which spreads what is left on a larger scale at the cost of
-blurring the picture. It shades each pixel with three lighting passes, so it is for the skin of a character in view, not for a crowd.
+blurring the picture. `u_preIntegrate` takes the wrap past the terminator per colour (red furthest) and widens it where the surface turns sharply, so a nose or an ear glows at its edge while a flat cheek stays crisp; `u_curvature` is how fast a turn counts as sharp, per world unit. `u_detailMap` is a second, fine normal map tiled over the surface (`u_detailTile`) for pores and creases that a baked map has no texels for: it is laid on the sharp normal the shine reads, by `u_detailStrength` (0 leaves it out), and not on the soft diffuse normals. Import it with sRGB off and wrap repeat. It shades each pixel with three lighting passes, so it is for the skin of a character in view, not for a crowd.
**Hair** (`builtin:hair`) lights a strand as a strand. `u_flowMap` says which way the strands run (red and green are a direction across the
texture), the diffuse light is Kajiya-Kay (a strand is lit by how far the light is from along it), and the shine is two narrow
@@ -86,7 +86,7 @@ one under it (`u_secondaryTint`). `u_roughness` sets how tight the lines are. Li
`u_sharpen` keeps the fine strands a card is covered in from washing out at a slant, and `u_rootDark` darkens the roots by the occlusion map.
`u_alphaCutoff` cuts a card out by its texture's alpha, feathered by alpha-to-coverage under MSAA, and the card then casts its own shape into shadow maps, not the rectangle it is drawn on. A hair model is usually cards or ribbons, not strands; this shader gives the shading of a strand to what the geometry gives it.
-**Eye** (`builtin:eye`) builds an eye from two maps and a few numbers. `u_depthMap` is white over the iris and the dome of the cornea, and `u_irisNormalMap` holds that dome's normals (import both with sRGB off). The view ray is bent into the cornea (`u_ior`) and carried down to the iris (`u_irisDepth`), so the iris moves behind the surface as the eye turns. The iris itself is drawn from `u_irisColor` to `u_irisColor2` with fibres, a dark limbal ring (`u_limbal`) and a pupil (`u_pupilSize`), against the white (`u_scleraColor`), which is lit with light that wraps past the terminator (`u_scleraWrap`). The cornea is a near-mirror (`u_corneaRoughness`, `u_specular`), so every light and the sky show as a catchlight. A painted eye texture on the mesh multiplies the result.
+**Eye** (`builtin:eye`) builds an eye from two maps and a few numbers. `u_depthMap` is white over the iris and the dome of the cornea, and `u_irisNormalMap` holds that dome's normals (import both with sRGB off). The view ray is bent into the cornea (`u_ior`) and carried down to the iris (`u_irisDepth`), so the iris moves behind the surface as the eye turns. The iris itself is drawn from `u_irisColor` to `u_irisColor2` with fibres, a dark limbal ring (`u_limbal`) and a pupil (`u_pupilSize`), against the white (`u_scleraColor`), which is lit with light that wraps past the terminator (`u_scleraWrap`). The cornea is a near-mirror (`u_corneaRoughness`, `u_specular`), so every light and the sky show as a catchlight. The lids shade the eye: `u_lidMap` is an authored occlusion map (white where the eye is open), and `u_lidShadow` darkens, and fades the cornea's reflections, where the surface faces up (from `u_lidStart`, a dot with the world's up, to straight up), which is the upper lid on an upright head. A painted eye texture on the mesh multiplies the result.
Both read the same lighting core as every Lit shader, so shadows, image-based light and the other effects apply.
diff --git a/docs/astro/src/content/docs/zh-cn/graphics/materials.mdx b/docs/astro/src/content/docs/zh-cn/graphics/materials.mdx
index 241be7344..0446bf4c1 100644
--- a/docs/astro/src/content/docs/zh-cn/graphics/materials.mdx
+++ b/docs/astro/src/content/docs/zh-cn/graphics/materials.mdx
@@ -63,11 +63,11 @@ std140 uniform 块,于是 CPU 能按名字设置、并安全地送到 GPU。一
两个着色器,给金属/粗糙度这一对参数描述不了的角色部位用。它们都和 Model 着色器一样,从网格自己的纹理取基础颜色,并把 `u_normalMap` 和 `u_ormMap`(红色是遮蔽,绿色是粗糙度)当作**数据**纹理:导入时要把 sRGB **关掉**,否则法线贴图会被颜色曲线扭曲,表面会出现斑块。
-**皮肤**(`builtin:skin`)不模糊画面。光进入皮肤又在不远处出来,会让弯曲表面的受光一侧变软,这里按颜色分开做:漫反射光取自比表面自己的法线更软的法线,红色最软(`u_blurRed`、`u_blurGreen`,是法线贴图的 mip 层级),所以毛孔和高光保持清晰。明暗交界线被染暖(`u_sssColor`、`u_sssWrap`),薄的或曲率大的地方会透光(`u_translucency`、`u_thinness`:耳朵、鼻翼、手指),高光是两层(`u_specular`、`u_lobe2`、`u_lobe2Roughness`),另有一层很淡的绒毛光照亮边缘(`u_fuzz`)。打开 `u_subsurface` 还会把它标记给[次表面散射](/docs/zh-cn/graphics/post-processing/#次表面散射)特效,在更大的尺度上扩散剩下的光,代价是会模糊画面。它对每个像素做三次光照计算,所以适合镜头里的角色皮肤,不适合一群人。
+**皮肤**(`builtin:skin`)不模糊画面。光进入皮肤又在不远处出来,会让弯曲表面的受光一侧变软,这里按颜色分开做:漫反射光取自比表面自己的法线更软的法线,红色最软(`u_blurRed`、`u_blurGreen`,是法线贴图的 mip 层级),所以毛孔和高光保持清晰。明暗交界线被染暖(`u_sssColor`、`u_sssWrap`),薄的或曲率大的地方会透光(`u_translucency`、`u_thinness`:耳朵、鼻翼、手指),高光是两层(`u_specular`、`u_lobe2`、`u_lobe2Roughness`),另有一层很淡的绒毛光照亮边缘(`u_fuzz`)。打开 `u_subsurface` 还会把它标记给[次表面散射](/docs/zh-cn/graphics/post-processing/#次表面散射)特效,在更大的尺度上扩散剩下的光,代价是会模糊画面。`u_preIntegrate` 让明暗交界线之后的环绕光按颜色分开(红色最远),并在表面急转的地方变宽,所以鼻翼和耳朵边缘会透出光,而平坦的脸颊保持清晰;`u_curvature` 是每个世界单位内多快的转折算作急转。`u_detailMap` 是平铺在表面上的第二张细法线图(`u_detailTile`),用来补烘焙贴图没有足够像素表现的毛孔和皮纹:它按 `u_detailStrength`(0 表示不用)叠在高光读取的锐利法线上,不叠在柔和的漫反射法线上。导入时关闭 sRGB,并把环绕方式设为重复。它对每个像素做三次光照计算,所以适合镜头里的角色皮肤,不适合一群人。
**头发**(`builtin:hair`)把发丝当发丝来打光。`u_flowMap` 说明发丝朝哪个方向(红、绿是贴图上的一个方向),漫反射是 Kajiya-Kay(发丝被光照亮的程度取决于光离它的走向有多远),高光是两条沿发丝的窄高光,由 `u_shiftMap` 把它们错开(`u_shiftAmount`、`u_shiftTile`):一条白的靠近表面,一条带颜色的在下面(`u_secondaryTint`)。`u_roughness` 决定线条有多紧。光会从发丝背面透过来(`u_translucency`),`u_sharpen` 让发片上密布的细发丝在斜着看时不被冲淡,`u_rootDark` 按遮蔽图把发根压暗。`u_alphaCutoff` 按贴图的 alpha 把发片抠出形状,在 MSAA 下由 alpha-to-coverage 柔化边缘,发片投进阴影图里的也是这个形状,而不是它所在的矩形。头发模型通常是发片或发带而不是真正的发丝;这个着色器把发丝的明暗给了几何本身给不了的部分。
-**眼睛**(`builtin:eye`)用两张贴图和几个数值做出一只眼睛。`u_depthMap` 在虹膜和角膜穹顶上为白色,`u_irisNormalMap` 是这个穹顶的法线(两张都要以关闭 sRGB 的方式导入)。视线在角膜处折射(`u_ior`)再下行到虹膜(`u_irisDepth`),所以眼球转动时虹膜在表面后面移动。虹膜本身由 `u_irisColor` 到 `u_irisColor2` 绘制,带纤维纹理、深色角膜缘(`u_limbal`)和瞳孔(`u_pupilSize`),衬在眼白(`u_scleraColor`)上,眼白的光会绕过明暗交界线(`u_scleraWrap`)。角膜近似镜面(`u_corneaRoughness`、`u_specular`),所以每盏灯和天空都会变成一个眼神光。网格上画的眼睛贴图会与结果相乘。
+**眼睛**(`builtin:eye`)用两张贴图和几个数值做出一只眼睛。`u_depthMap` 在虹膜和角膜穹顶上为白色,`u_irisNormalMap` 是这个穹顶的法线(两张都要以关闭 sRGB 的方式导入)。视线在角膜处折射(`u_ior`)再下行到虹膜(`u_irisDepth`),所以眼球转动时虹膜在表面后面移动。虹膜本身由 `u_irisColor` 到 `u_irisColor2` 绘制,带纤维纹理、深色角膜缘(`u_limbal`)和瞳孔(`u_pupilSize`),衬在眼白(`u_scleraColor`)上,眼白的光会绕过明暗交界线(`u_scleraWrap`)。角膜近似镜面(`u_corneaRoughness`、`u_specular`),所以每盏灯和天空都会变成一个眼神光。眼睑会给眼睛投下阴影:`u_lidMap` 是手绘的遮蔽图(眼睛睁开处为白色),`u_lidShadow` 会在表面朝上的地方(从 `u_lidStart` 即与世界上方向的点积起,到正上方)变暗并淡化角膜的反射,在头部直立时这就是上眼睑。网格上画的眼睛贴图会与结果相乘。
两者读的是和所有 Lit 着色器相同的光照核心,所以阴影、基于图像的光照和其他特效都适用。
diff --git a/fixtures/scenes/skin-detail.esscene b/fixtures/scenes/skin-detail.esscene
new file mode 100644
index 000000000..8d0a1cbb7
--- /dev/null
+++ b/fixtures/scenes/skin-detail.esscene
@@ -0,0 +1,153 @@
+{
+ "version": 4,
+ "name": "SkinDetail",
+ "entities": [
+ {
+ "id": 0,
+ "name": "Camera",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": 0,
+ "z": 10
+ }
+ }
+ },
+ {
+ "type": "Camera",
+ "data": {
+ "projectionType": 1,
+ "orthoSize": 300,
+ "isActive": true,
+ "priority": 0
+ }
+ }
+ ]
+ },
+ {
+ "id": 1,
+ "name": "Off",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": -150,
+ "y": 0,
+ "z": 0
+ },
+ "scale": {
+ "x": 250,
+ "y": 250,
+ "z": 1
+ }
+ }
+ },
+ {
+ "type": "MeshRenderer",
+ "data": {
+ "mesh": "/scenes/shadow-quad.esmesh",
+ "color": {
+ "r": 1,
+ "g": 1,
+ "b": 1,
+ "a": 1
+ },
+ "lit": true,
+ "opaque": true,
+ "enabled": true,
+ "material": "/scenes/skin-detail/off.esmaterial"
+ }
+ }
+ ]
+ },
+ {
+ "id": 2,
+ "name": "On",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 150,
+ "y": 0,
+ "z": 0
+ },
+ "scale": {
+ "x": 250,
+ "y": 250,
+ "z": 1
+ }
+ }
+ },
+ {
+ "type": "MeshRenderer",
+ "data": {
+ "mesh": "/scenes/shadow-quad.esmesh",
+ "color": {
+ "r": 1,
+ "g": 1,
+ "b": 1,
+ "a": 1
+ },
+ "lit": true,
+ "opaque": true,
+ "enabled": true,
+ "material": "/scenes/skin-detail/on.esmaterial"
+ }
+ }
+ ]
+ },
+ {
+ "id": 3,
+ "name": "Sun",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": 0,
+ "z": 0
+ },
+ "rotation": {
+ "x": 0,
+ "y": 0.6293203910498374,
+ "z": 0,
+ "w": 0.7771459614569709
+ }
+ }
+ },
+ {
+ "type": "Light",
+ "data": {
+ "type": 1,
+ "color": {
+ "r": 1,
+ "g": 1,
+ "b": 1,
+ "a": 1
+ },
+ "intensity": 1,
+ "meshShadows": false
+ }
+ }
+ ]
+ }
+ ]
+}
\ No newline at end of file
diff --git a/fixtures/scenes/skin-detail.png b/fixtures/scenes/skin-detail.png
new file mode 100644
index 000000000..26305c177
Binary files /dev/null and b/fixtures/scenes/skin-detail.png differ
diff --git a/fixtures/scenes/skin-detail.textures.json b/fixtures/scenes/skin-detail.textures.json
new file mode 100644
index 000000000..d60eb0c5f
--- /dev/null
+++ b/fixtures/scenes/skin-detail.textures.json
@@ -0,0 +1,3 @@
+{
+ "7c3e9a41-2b5d-4e8f-a1c6-9d0f4b2e7a55": "/scenes/skin-detail.png"
+}
\ No newline at end of file
diff --git a/fixtures/scenes/skin-detail/off.esmaterial b/fixtures/scenes/skin-detail/off.esmaterial
new file mode 100644
index 000000000..7cd88b4cc
--- /dev/null
+++ b/fixtures/scenes/skin-detail/off.esmaterial
@@ -0,0 +1,17 @@
+{
+ "version": "1.0",
+ "type": "material",
+ "shader": "builtin:skin",
+ "blendMode": 0,
+ "depthTest": true,
+ "depthWrite": true,
+ "properties": {
+ "u_detailMap": "/scenes/skin-detail.png",
+ "u_detailStrength": 0,
+ "u_detailTile": 1,
+ "u_roughness": 0.35,
+ "u_specular": 0.6,
+ "u_lobe2": 0,
+ "u_fuzz": 0
+ }
+}
\ No newline at end of file
diff --git a/fixtures/scenes/skin-detail/on.esmaterial b/fixtures/scenes/skin-detail/on.esmaterial
new file mode 100644
index 000000000..97b39ea9a
--- /dev/null
+++ b/fixtures/scenes/skin-detail/on.esmaterial
@@ -0,0 +1,17 @@
+{
+ "version": "1.0",
+ "type": "material",
+ "shader": "builtin:skin",
+ "blendMode": 0,
+ "depthTest": true,
+ "depthWrite": true,
+ "properties": {
+ "u_detailMap": "/scenes/skin-detail.png",
+ "u_detailStrength": 1,
+ "u_detailTile": 1,
+ "u_roughness": 0.35,
+ "u_specular": 0.6,
+ "u_lobe2": 0,
+ "u_fuzz": 0
+ }
+}
\ No newline at end of file
diff --git a/sdk/src/render/builtinShaders.ts b/sdk/src/render/builtinShaders.ts
index 868565b40..b20ff5670 100644
--- a/sdk/src/render/builtinShaders.ts
+++ b/sdk/src/render/builtinShaders.ts
@@ -1557,6 +1557,11 @@ const SKIN = `#pragma shader "Skin"
#pragma param u_normalMap texture default(flatnormal) texel(data)
#pragma param u_ormMap texture default(white) texel(data)
#pragma param u_occlusionStrength float default(1) range(0,1) ui(slider)
+// A second, fine normal map tiled over the surface (pores, creases) laid on the sharp normal the shine reads: how strongly, and how many times it repeats.
+// The soft diffuse normals do not carry it, as they do not carry the baked pores either. Import it with sRGB off and wrap repeat; 0 leaves it out.
+#pragma param u_detailMap texture default(flatnormal) texel(data)
+#pragma param u_detailStrength float default(0) range(0,2) ui(slider)
+#pragma param u_detailTile float default(16) range(1,128) ui(slider)
// How rough the surface is (a scale on the map's own roughness), how much it shines, and the second, tighter lobe.
#pragma param u_roughness float default(1) range(0,2) ui(slider)
#pragma param u_specular float default(1) range(0,2) ui(slider)
@@ -1571,6 +1576,10 @@ const SKIN = `#pragma shader "Skin"
// Light through thin places: how much, and how much of a surface's curvature counts as thin.
#pragma param u_translucency float default(0.5) range(0,1) ui(slider)
#pragma param u_thinness float default(0.5) range(0,1) ui(slider)
+// Pre-integrated scattering: the wrap past the terminator is taken per colour (red furthest) and widens where the surface turns sharply, which
+// is what makes a nose or an ear glow at its edge while a flat cheek stays crisp. u_curvature is how fast a turn counts as sharp, per world unit.
+#pragma param u_preIntegrate float default(0) range(0,1) ui(slider)
+#pragma param u_curvature float default(8) range(0,64) ui(slider)
// The fine fuzz that lights a rim.
#pragma param u_fuzz float default(0.1) range(0,1) ui(slider)
// Marks the surface for the Subsurface Scattering effect, which spreads what is left of the light on a larger scale. Off by default.
@@ -1606,23 +1615,32 @@ vec3 skinNormalAt(float bias) {
return normalize(texture(u_normalMap, v_texCoord, bias).xyz * 2.0 - 1.0);
}
+// The baked normal with the detail's slope added to it, in the tangent frame both are read in.
+vec3 skinDetail(vec3 baked, vec2 detail) {
+ return normalize(vec3(baked.xy + detail * u_detailStrength, baked.z));
+}
+
void main() {
+ // Taken before any branch: a derivative needs every fragment of its quad.
+ vec2 detail = texture(u_detailMap, v_texCoord * u_detailTile).xy * 2.0 - 1.0;
#ifdef MESH_NORMALS
highp vec3 Ng = normalize(v_worldNormal);
highp vec3 P = v_worldXYZ;
if (!gl_FrontFacing) Ng = -Ng;
- highp vec3 N = perturbNormal(Ng, P, v_texCoord, skinNormalAt(0.0));
+ highp vec3 N = perturbNormal(Ng, P, v_texCoord, skinDetail(skinNormalAt(0.0), detail));
highp vec3 Nmid = perturbNormal(Ng, P, v_texCoord, skinNormalAt(u_blurGreen));
highp vec3 Nsoft = perturbNormal(Ng, P, v_texCoord, skinNormalAt(u_blurRed));
// How fast the surface turns across a pixel, against how big a pixel is: the thin, strongly curved places.
float turn = (length(dFdx(Ng)) + length(dFdy(Ng))) / max(length(dFdx(P)) + length(dFdy(P)), 1e-5);
float thick = 1.0 - clamp(turn * 12.0 * u_thinness, 0.0, 1.0);
+ float curv = clamp(turn * u_curvature, 0.0, 1.0);
#else
highp vec3 P = vec3(v_worldPos, 0.0);
highp vec3 N = vec3(0.0, 0.0, 1.0);
highp vec3 Nmid = N;
highp vec3 Nsoft = N;
float thick = 1.0;
+ float curv = 0.0;
#endif
vec4 base = texture(u_textures[0], v_texCoord) * v_color * u_tint;
vec4 orm = texture(u_ormMap, v_texCoord);
@@ -1633,7 +1651,7 @@ void main() {
// The diffuse light, from two softnesses of normal, with the terminator warmed and light let through a thin place.
SurfaceExt wrapped = surfaceExtNone(Nsoft);
wrapped.scatter = vec4(u_sssColor.rgb * base.rgb * 1.4, u_sssWrap);
- wrapped.trans = vec4(u_translucency, thick, 0.0, 0.0);
+ wrapped.trans = vec4(u_translucency, thick, curv, u_preIntegrate);
vec3 soft = applyLightingSurface(base.rgb, Nsoft, P, viewDir, 0.0, 1.0, 0.0, ao, wrapped);
SurfaceExt wrappedMid = surfaceExtNone(Nmid);
wrappedMid.scatter = wrapped.scatter;
@@ -1685,7 +1703,9 @@ fn packNormal(n : vec3f) -> vec2f {
#endif
let base = textureSample(t0, s0, v.v_texCoord) * v.v_color * mc.u_tint;
let orm = textureSample(u_ormMap, u_ormMap_s, v.v_texCoord);
- let tangentSharp = normalize(textureSampleBias(u_normalMap, u_normalMap_s, v.v_texCoord, 0.0).xyz * 2.0 - vec3f(1.0));
+ let detail = textureSample(u_detailMap, u_detailMap_s, v.v_texCoord * mc.u_detailTile).xy * 2.0 - vec2f(1.0);
+ let bakedSharp = normalize(textureSampleBias(u_normalMap, u_normalMap_s, v.v_texCoord, 0.0).xyz * 2.0 - vec3f(1.0));
+ let tangentSharp = normalize(vec3f(bakedSharp.xy + detail * mc.u_detailStrength, bakedSharp.z));
let tangentMid = normalize(textureSampleBias(u_normalMap, u_normalMap_s, v.v_texCoord, mc.u_blurGreen).xyz * 2.0 - vec3f(1.0));
let tangentSoft = normalize(textureSampleBias(u_normalMap, u_normalMap_s, v.v_texCoord, mc.u_blurRed).xyz * 2.0 - vec3f(1.0));
#ifdef MESH_NORMALS
@@ -1697,12 +1717,14 @@ fn packNormal(n : vec3f) -> vec2f {
let Nsoft = perturbNormal(Ng, P, v.v_texCoord, tangentSoft);
let turn = (length(dpdx(Ng)) + length(dpdy(Ng))) / max(length(dpdx(P)) + length(dpdy(P)), 1e-5);
let thick = 1.0 - clamp(turn * 12.0 * mc.u_thinness, 0.0, 1.0);
+ let curv = clamp(turn * mc.u_curvature, 0.0, 1.0);
#else
let P = vec3f(v.v_worldPos.x, 0.0, v.v_worldPos.y);
let N = vec3f(0.0, 0.0, 1.0);
let Nmid = N;
let Nsoft = N;
let thick = 1.0;
+ let curv = 0.0;
#endif
let ao = mix(1.0, orm.r, mc.u_occlusionStrength);
let rough = clamp(orm.g * mc.u_roughness, 0.05, 1.0);
@@ -1710,7 +1732,7 @@ fn packNormal(n : vec3f) -> vec2f {
var wrapped = surfaceExtNone(Nsoft);
wrapped.scatter = vec4f(mc.u_sssColor.rgb * base.rgb * 1.4, mc.u_sssWrap);
- wrapped.trans = vec4f(mc.u_translucency, thick, 0.0, 0.0);
+ wrapped.trans = vec4f(mc.u_translucency, thick, curv, mc.u_preIntegrate);
let soft = applyLightingSurface(base.rgb, Nsoft, P, viewDir, 0.0, 1.0, 0.0, ao, wrapped);
var wrappedMid = surfaceExtNone(Nmid);
wrappedMid.scatter = wrapped.scatter;
@@ -1761,6 +1783,11 @@ const EYE = `#pragma shader "Eye"
#pragma param u_scleraRoughness float default(0.45) range(0.1,1) ui(slider)
#pragma param u_specular float default(1) range(0,2) ui(slider)
#pragma param u_scleraWrap float default(0.35) range(0,1) ui(slider)
+// The lids shade the eye they sit on. u_lidMap is an authored occlusion map (white where the eye is open); u_lidShadow also darkens, and fades the
+// cornea's reflections, where the surface faces up from u_lidStart (a dot with the world's up) to straight up: the upper lid, for an upright head.
+#pragma param u_lidMap texture default(white) texel(data)
+#pragma param u_lidShadow float default(0.5) range(0,1) ui(slider)
+#pragma param u_lidStart float default(0.2) range(-1,1) ui(slider)
#pragma fragment
precision highp float;
@@ -1792,6 +1819,7 @@ void main() {
vec2 uv = v_texCoord;
// Every sample that takes part in a derivative is taken here, before any branch.
float h = texture(u_depthMap, uv).r;
+ float lid = texture(u_lidMap, uv).r;
vec3 dome = normalize(texture(u_irisNormalMap, uv).xyz * 2.0 - 1.0);
#ifdef MESH_NORMALS
highp vec3 Ng = normalize(v_worldNormal);
@@ -1828,6 +1856,7 @@ void main() {
vec3 albedo = mix(u_scleraColor.rgb, iris, irisMask) * painted.rgb;
float ao = 1.0;
+ float lidShade = lid * (1.0 - u_lidShadow * smoothstep(u_lidStart, 1.0, Ng.y));
vec3 Nd = normalize(mix(Ng, Ndome, 0.5 * surfaceMask));
SurfaceExt body = surfaceExtNone(Nd);
body.scatter = vec4(vec3(0.9, 0.5, 0.4) * albedo, u_scleraWrap * (1.0 - surfaceMask));
@@ -1840,7 +1869,7 @@ void main() {
shine.coatN = vec4(Ng, 0.0);
vec3 spec = applyLightingSurface(vec3(0.0), Nc, P, viewDir, 0.0, u_corneaRoughness, u_specular, ao, shine);
- fragColor = vec4(diffuse + spec, 1.0);
+ fragColor = vec4((diffuse + spec) * lidShade, 1.0);
#ifdef ES_GBUFFER
fragSurface = vec4(packNormal(normalize(Nc)), u_corneaRoughness, 0.75);
#endif
@@ -1876,6 +1905,7 @@ fn packNormal(n : vec3f) -> vec2f {
let uv = v.v_texCoord;
// Every sample is taken here, before any branch.
let h = textureSample(u_depthMap, u_depthMap_s, uv).r;
+ let lid = textureSample(u_lidMap, u_lidMap_s, uv).r;
let dome = normalize(textureSample(u_irisNormalMap, u_irisNormalMap_s, uv).xyz * 2.0 - vec3f(1.0));
#ifdef MESH_NORMALS
var Ng = normalize(v.v_worldNormal);
@@ -1910,6 +1940,7 @@ fn packNormal(n : vec3f) -> vec2f {
let albedo = mix(mc.u_scleraColor.rgb, iris, irisMask) * painted.rgb;
let ao = 1.0;
+ let lidShade = lid * (1.0 - mc.u_lidShadow * smoothstep(mc.u_lidStart, 1.0, Ng.y));
let Nd = normalize(mix(Ng, Ndome, 0.5 * surfaceMask));
var body = surfaceExtNone(Nd);
body.scatter = vec4f(vec3f(0.9, 0.5, 0.4) * albedo, mc.u_scleraWrap * (1.0 - surfaceMask));
@@ -1923,11 +1954,11 @@ fn packNormal(n : vec3f) -> vec2f {
#ifdef ES_GBUFFER
var frag : FragOut;
- frag.color = vec4f(diffuse + spec, 1.0);
+ frag.color = vec4f((diffuse + spec) * lidShade, 1.0);
frag.surface = vec4f(packNormal(normalize(Nc)), mc.u_corneaRoughness, 0.75);
return frag;
#else
- return vec4f(diffuse + spec, 1.0);
+ return vec4f((diffuse + spec) * lidShade, 1.0);
#endif
}
#pragma end
diff --git a/src/esengine/resource/ShaderParser.cpp b/src/esengine/resource/ShaderParser.cpp
index 4eae76aaf..689b5bb7a 100644
--- a/src/esengine/resource/ShaderParser.cpp
+++ b/src/esengine/resource/ShaderParser.cpp
@@ -1886,8 +1886,11 @@ fn shadeLight(pd : vec4f, col : vec4f, sp : vec4f, sh : vec4f, sm : vec4f, fo :
* (distributionCharlie(max(dot(N, Hx), 0.0), sheenA) * visibilityNeubelt(NdotV, ndotl) * 3.14159265);
}
if (ext.scatter.a > 0.0) {
- let wrapped = clamp((dot(N, L) + ext.scatter.a) / (1.0 + ext.scatter.a), 0.0, 1.0);
- extra += irr * ext.scatter.rgb * max(wrapped - ndotl, 0.0);
+ // The wrap is per channel once trans.w asks for it: red travels furthest, and every channel further where the
+ // surface turns sharply (trans.z, a curvature 0..1), as a pre-integrated skin ramp would give.
+ let wrapWidth = vec3f(ext.scatter.a) * mix(vec3f(1.0), vec3f(1.0, 0.45, 0.25) * (0.3 + 1.7 * ext.trans.z), ext.trans.w);
+ let wrapped = clamp((vec3f(dot(N, L)) + wrapWidth) / (vec3f(1.0) + wrapWidth), vec3f(0.0), vec3f(1.0));
+ extra += irr * ext.scatter.rgb * max(wrapped - vec3f(ndotl), vec3f(0.0));
let Hb = normalize(L + N * 0.3);
extra += irr * ext.scatter.rgb
* (pow(clamp(dot(V, -Hb), 0.0, 1.0), 4.0) * ext.trans.x * (1.0 - ext.trans.y));
@@ -2772,7 +2775,8 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p
// coat x strength, y perceptual roughness coatN the coat's own normal (the geometric one, not the bump map's)
// sheen rgb colour, a perceptual roughness aniso xyz tangent (unit, across N), w strength -1..1
// scatter rgb colour scattered light takes, a wrap 0..1
- // trans x light passing through, y thickness 0..1 film x strength, y film index, z thickness in nm
+ // trans x light passing through, y thickness 0..1, z curvature 0..1, w how far the wrap is per channel and curvature-shaped
+ // film x strength, y film index, z thickness in nm
// misc x dielectric reflectance at normal incidence (0.04 for an index of 1.5)
"struct SurfaceExt {\n"
" highp vec4 coat;\n"
@@ -3011,8 +3015,9 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p
" * (distributionCharlie(max(dot(N, Hx), 0.0), sheenA) * visibilityNeubelt(NdotV, ndotl) * 3.14159265);\n"
" }\n"
" if (ext.scatter.a > 0.0) {\n"
- " highp float wrapped = clamp((dot(N, L) + ext.scatter.a) / (1.0 + ext.scatter.a), 0.0, 1.0);\n"
- " extra += irr * ext.scatter.rgb * max(wrapped - ndotl, 0.0);\n"
+ " highp vec3 wrapWidth = vec3(ext.scatter.a) * mix(vec3(1.0), vec3(1.0, 0.45, 0.25) * (0.3 + 1.7 * ext.trans.z), ext.trans.w);\n"
+ " highp vec3 wrapped = clamp((vec3(dot(N, L)) + wrapWidth) / (vec3(1.0) + wrapWidth), vec3(0.0), vec3(1.0));\n"
+ " extra += irr * ext.scatter.rgb * max(wrapped - vec3(ndotl), vec3(0.0));\n"
" highp vec3 Hb = normalize(L + N * 0.3);\n"
" extra += irr * ext.scatter.rgb\n"
" * (pow(clamp(dot(V, -Hb), 0.0, 1.0), 4.0) * ext.trans.x * (1.0 - ext.trans.y));\n"
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index ff237412d..7983e0fbf 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -2545,6 +2545,12 @@ export const NOTED = {
{"note":"A material whose alpha is coverage (`u_alphaCutoff` above 0, now also on the Hair shader) casts its cut-out shape into shadow maps instead of the rectangle it is drawn on, and Hair cards feather their edges with alpha-to-coverage."},
"fix(editor): a selected plane seen from close by no longer draws its diagonal as an outline":
{"note":"The selection outline of a mesh no longer draws a stray line across a large plane seen from close by: a triangle with a corner behind the camera was judged by a different rule than its neighbour, so their shared diagonal showed as an edge."},
+ "feat(render): the Skin shader takes a tiled detail normal map for pores and creases":
+ {"note":"The Skin shader has a detail normal layer: `u_detailMap` is a fine normal map tiled over the surface (`u_detailTile`) for pores and creases, laid on the sharp normal the shine reads by `u_detailStrength` (0 leaves it out) and not on the soft diffuse normals."},
+ "feat(render): the Skin shader shapes its scattering per colour and by curvature":
+ {"note":"The Skin shader can take its wrap past the terminator per colour and shape it by curvature: `u_preIntegrate` (red travels furthest, every colour further where the surface turns sharply) and `u_curvature`, so a nose or an ear glows at its edge while a flat cheek stays crisp. Off by default."},
+ "feat(render): the Eye shader takes a lid shadow, authored or from the surface facing up":
+ {"note":"The Eye shader shades the eye the lids sit on: `u_lidMap` is an authored occlusion map, and `u_lidShadow` darkens, and fades the cornea's reflections, where the surface faces up from `u_lidStart`, which is the upper lid on an upright head."},
"chore(tools): the gates run in parallel with --jobs, and the reds a full run gathered are cleared":
{"internal":"tooling for the static gates, and fixes to checks that had drifted from the code they guard"},
"fix(render): a wire narrower than a pixel is no longer painted over by the sky in dashes":
diff --git a/tools/renderScenes.mjs b/tools/renderScenes.mjs
index 49e39dfac..eb0342e6c 100644
--- a/tools/renderScenes.mjs
+++ b/tools/renderScenes.mjs
@@ -207,6 +207,14 @@ export const SCENES = [
{ id: "mesh-shadow-linear", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_COLORSPACE: "linear", ESTELLA_VERIFY_SCENE: "/scenes/mesh-shadow.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.708,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":20},{\"x\":0.167,\"y\":0.5,\"rgb\":[243,243,243],\"tol\":20},{\"x\":0.5,\"y\":0.5,\"rgb\":[243,243,243],\"tol\":20}]" } },
// A caster cut out by its alpha: the left half of the blocker is opaque and the right half clear, so only the left half casts. The floor
// at x=60 is in its shadow (and seen through the clear half), the floor at x=150 is where the right half would have shaded it.
+ // The Skin shader's detail layer: two quads under a light nearly along the surface, the right one with a detail normal map tilted toward the light
+ // at strength 1 and the left with strength 0. Only the shine reads the detail, and it lights up where the tilt turns the normal toward the half vector.
+ // The Skin shader's per-channel, curvature-shaped wrap: two spheres lit from the side, the right with u_preIntegrate 1. Past the terminator
+ // the wrap carries red further than green and blue, so the right sphere's shade is redder where the left's is the same in every channel.
+ { id: "skin-preintegrate", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/skin-preint.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.3125,\"y\":0.5,\"rgb\":[77,32,24],\"tol\":15},{\"x\":0.8125,\"y\":0.5,\"rgb\":[94,13,0],\"tol\":15}]" } },
+ // The Eye shader's lid shadow: two quads facing up under a light from above, the right with u_lidShadow 1, so the surface the upper lid sits over is darker.
+ { id: "eye-lid", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/eye-lid.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.117,\"y\":0.5,\"rgb\":[159,157,154],\"tol\":15},{\"x\":0.859,\"y\":0.5,\"rgb\":[64,63,62],\"tol\":15}]" } },
+ { id: "skin-detail", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/skin-detail.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/skin-detail.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.25,\"y\":0.5,\"rgb\":[137,89,80],\"tol\":20},{\"x\":0.75,\"y\":0.5,\"rgb\":[253,205,197],\"tol\":20}]" } },
{ id: "mesh-shadow-cutout", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/mesh-shadow-cutout.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/mesh-shadow-cutout.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "3", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.6,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":20},{\"x\":0.75,\"y\":0.5,\"rgb\":[228,228,228],\"tol\":20},{\"x\":0.167,\"y\":0.5,\"rgb\":[228,228,228],\"tol\":20}]" } },
{ id: "mesh-shadow-material", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/mesh-shadow-material.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "3", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.5,\"y\":0.5,\"rgb\":[255,0,0],\"tol\":40},{\"x\":0.708,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":20},{\"x\":0.167,\"y\":0.5,\"rgb\":[228,228,228],\"tol\":20}]" } },