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): a cutout material casts its cut-out shape into shadow maps, and Hair cards cut out by alpha

yhh98f53bc49296
diff --git a/CHANGELOG.md b/CHANGELOG.md
index beec65fe6..4a1709799 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -34,6 +34,7 @@ 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.
+- **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 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 dd55cfe21..bde7066e8 100644
--- a/docs/astro/src/content/docs/graphics/materials.mdx
+++ b/docs/astro/src/content/docs/graphics/materials.mdx
@@ -84,7 +84,7 @@ texture), the diffuse light is Kajiya-Kay (a strand is lit by how far the light
highlights along the strand, shifted against each other by `u_shiftMap` (`u_shiftAmount`, `u_shiftTile`): a white one near the surface and a coloured
one under it (`u_secondaryTint`). `u_roughness` sets how tight the lines are. Light comes through the strands from behind (`u_translucency`),
`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.
-A hair model is usually cards or ribbons, not strands; this shader gives the shading of a strand to what the geometry gives it.
+`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.
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 0170201d2..241be7344 100644
--- a/docs/astro/src/content/docs/zh-cn/graphics/materials.mdx
+++ b/docs/astro/src/content/docs/zh-cn/graphics/materials.mdx
@@ -65,7 +65,7 @@ std140 uniform 块,于是 CPU 能按名字设置、并安全地送到 GPU。一
**皮肤**(`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:hair`)把发丝当发丝来打光。`u_flowMap` 说明发丝朝哪个方向(红、绿是贴图上的一个方向),漫反射是 Kajiya-Kay(发丝被光照亮的程度取决于光离它的走向有多远),高光是两条沿发丝的窄高光,由 `u_shiftMap` 把它们错开(`u_shiftAmount`、`u_shiftTile`):一条白的靠近表面,一条带颜色的在下面(`u_secondaryTint`)。`u_roughness` 决定线条有多紧。光会从发丝背面透过来(`u_translucency`),`u_sharpen` 让发片上密布的细发丝在斜着看时不被冲淡,`u_rootDark` 按遮蔽图把发根压暗。头发模型通常是发片或发带而不是真正的发丝;这个着色器把发丝的明暗给了几何本身给不了的部分。
+**头发**(`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`),所以每盏灯和天空都会变成一个眼神光。网格上画的眼睛贴图会与结果相乘。
diff --git a/fixtures/scenes/mesh-shadow-cutout.esscene b/fixtures/scenes/mesh-shadow-cutout.esscene
new file mode 100644
index 000000000..491363a8c
--- /dev/null
+++ b/fixtures/scenes/mesh-shadow-cutout.esscene
@@ -0,0 +1,151 @@
+{
+ "version": 4,
+ "name": "MeshShadowCutout",
+ "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": "Ground",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": 0,
+ "z": -200
+ },
+ "scale": {
+ "x": 600,
+ "y": 600,
+ "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
+ }
+ }
+ ]
+ },
+ {
+ "id": 2,
+ "name": "Blocker",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": 0,
+ "z": 0
+ },
+ "scale": {
+ "x": 150,
+ "y": 150,
+ "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/mesh-shadow-cutout/hair.esmaterial",
+ "texture": "@uuid:7c3e9a41-2b5d-4e8f-a1c6-9d0f4b2e7a54"
+ }
+ }
+ ]
+ },
+ {
+ "id": 3,
+ "name": "Sun",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": 0,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Light",
+ "data": {
+ "type": 1,
+ "color": {
+ "r": 1,
+ "g": 1,
+ "b": 1,
+ "a": 1
+ },
+ "intensity": 1,
+ "direction": {
+ "x": 0.5,
+ "y": 0
+ },
+ "meshShadows": true
+ }
+ }
+ ]
+ }
+ ]
+}
\ No newline at end of file
diff --git a/fixtures/scenes/mesh-shadow-cutout.png b/fixtures/scenes/mesh-shadow-cutout.png
new file mode 100644
index 000000000..157fc04eb
Binary files /dev/null and b/fixtures/scenes/mesh-shadow-cutout.png differ
diff --git a/fixtures/scenes/mesh-shadow-cutout.textures.json b/fixtures/scenes/mesh-shadow-cutout.textures.json
new file mode 100644
index 000000000..315fba85e
--- /dev/null
+++ b/fixtures/scenes/mesh-shadow-cutout.textures.json
@@ -0,0 +1,3 @@
+{
+ "7c3e9a41-2b5d-4e8f-a1c6-9d0f4b2e7a54": "/scenes/mesh-shadow-cutout.png"
+}
\ No newline at end of file
diff --git a/fixtures/scenes/mesh-shadow-cutout/hair.esmaterial b/fixtures/scenes/mesh-shadow-cutout/hair.esmaterial
new file mode 100644
index 000000000..374c72c2c
--- /dev/null
+++ b/fixtures/scenes/mesh-shadow-cutout/hair.esmaterial
@@ -0,0 +1,11 @@
+{
+ "version": "1.0",
+ "type": "material",
+ "shader": "builtin:hair",
+ "blendMode": 0,
+ "depthTest": true,
+ "depthWrite": true,
+ "properties": {
+ "u_alphaCutoff": 0.5
+ }
+}
\ No newline at end of file
diff --git a/sdk/src/render/builtinShaders.ts b/sdk/src/render/builtinShaders.ts
index 8508ef680..868565b40 100644
--- a/sdk/src/render/builtinShaders.ts
+++ b/sdk/src/render/builtinShaders.ts
@@ -1396,6 +1396,9 @@ const HAIR = `#pragma shader "Hair"
#pragma param u_sharpen float default(-0.8) range(-3,1) ui(slider)
// How much darker the roots are than the tips: the occlusion map's red, pushed.
#pragma param u_rootDark float default(0.5) range(0,1) ui(slider)
+// A card's alpha cut at this level, and sharpened to about a texel so alpha-to-coverage feathers the edge of every strand instead of stair-stepping it; 0 keeps the card opaque.
+#pragma param u_alphaCutoff float default(0) range(0,1) ui(slider)
+#pragma coverage u_alphaCutoff
#pragma fragment
precision highp float;
@@ -1441,6 +1444,13 @@ void main() {
highp vec3 T = vec3(0.0, 1.0, 0.0);
#endif
vec4 base = texture(u_textures[0], v_texCoord, u_sharpen) * v_color * u_tint;
+ // Before any discard: a derivative needs every fragment of its quad.
+ float alpha = 1.0;
+ if (u_alphaCutoff > 0.0) {
+ alpha = (base.a - u_alphaCutoff) / max(fwidth(base.a), 1e-4) + 0.5;
+ if (alpha <= 0.0) discard;
+ alpha = min(alpha, 1.0);
+ }
float root = texture(u_ormMap, v_texCoord).r;
float ao = mix(1.0, root, u_occlusionStrength);
base.rgb *= mix(1.0, 0.35 + 0.65 * root, u_rootDark);
@@ -1452,7 +1462,7 @@ void main() {
ext.trans = vec4(u_translucency, 0.25, 0.0, 0.0);
vec3 viewDir = viewDirection(P);
vec3 lit = applyLightingSurface(base.rgb, N, P, viewDir, 0.0, u_roughness, u_specular, ao, ext);
- fragColor = vec4(lit, 1.0);
+ fragColor = vec4(lit, alpha);
#ifdef ES_GBUFFER
fragSurface = vec4(packNormal(normalize(N)), u_roughness, 0.75);
#endif
@@ -1488,6 +1498,7 @@ fn packNormal(n : vec3f) -> vec2f {
g_worldTangent = v.v_worldTangent;
#endif
var base = textureSampleBias(t0, s0, v.v_texCoord, mc.u_sharpen) * v.v_color * mc.u_tint;
+ let edge = max(abs(dpdx(base.a)) + abs(dpdy(base.a)), 1e-4);
let flowTex = textureSampleGrad(u_flowMap, u_flowMap_s, v.v_texCoord, duvx, duvy).rg * 2.0 - vec2f(1.0);
let orm = textureSampleGrad(u_ormMap, u_ormMap_s, v.v_texCoord, duvx, duvy);
let shiftTex = textureSampleGrad(u_shiftMap, u_shiftMap_s, v.v_texCoord * mc.u_shiftTile, duvx * mc.u_shiftTile, duvy * mc.u_shiftTile).r;
@@ -1509,6 +1520,12 @@ fn packNormal(n : vec3f) -> vec2f {
let N = vec3f(0.0, 0.0, 1.0);
let T = vec3f(0.0, 1.0, 0.0);
#endif
+ var alpha = 1.0;
+ if (mc.u_alphaCutoff > 0.0) {
+ alpha = (base.a - mc.u_alphaCutoff) / edge + 0.5;
+ if (alpha <= 0.0) { discard; }
+ alpha = min(alpha, 1.0);
+ }
let ao = mix(1.0, orm.r, mc.u_occlusionStrength);
base = vec4f(base.rgb * mix(1.0, 0.35 + 0.65 * orm.r, mc.u_rootDark), base.a);
let noise = (shiftTex - 0.5) * mc.u_shiftAmount;
@@ -1521,11 +1538,11 @@ fn packNormal(n : vec3f) -> vec2f {
let lit = applyLightingSurface(base.rgb, N, P, viewDir, 0.0, mc.u_roughness, mc.u_specular, ao, ext);
#ifdef ES_GBUFFER
var frag : FragOut;
- frag.color = vec4f(lit, 1.0);
+ frag.color = vec4f(lit, alpha);
frag.surface = vec4f(packNormal(normalize(N)), mc.u_roughness, 0.75);
return frag;
#else
- return vec4f(lit, 1.0);
+ return vec4f(lit, alpha);
#endif
}
#pragma end
diff --git a/src/esengine/data/shaders/mesh.esshader b/src/esengine/data/shaders/mesh.esshader
index c1df31196..f1a65a556 100644
--- a/src/esengine/data/shaders/mesh.esshader
+++ b/src/esengine/data/shaders/mesh.esshader
@@ -42,6 +42,11 @@
// land where its skinned self lands, and one copy of that is one that cannot drift.
#pragma feature SHADOW_DEPTH
+// A caster cut out by its alpha: the shadow program of a material whose alpha is coverage discards where the
+// base texture is below the material's u_alphaCutoff, so a card of hair or a leaf casts its own shape and not
+// the rectangle it is drawn on.
+#pragma feature SHADOW_ALPHA
+
// A skinned surface coloured by how much ONE joint moves each vertex: the weight a rig is
// painted with, read off the surface instead of a table. The joint is the record's tint x
// (a diagnostic draw has no tint to carry), the colour goes out through the same varying the
@@ -314,6 +319,9 @@ void main() {
highp vec2 was = v_velPrev.xy / max(v_velPrev.w, 1e-6);
fragColor = vec4((was - now) * 0.5, gl_FragCoord.z, v_velPrev.w > 1e-6 ? 1.0 : 0.0);
#elif defined(SHADOW_DEPTH)
+#ifdef SHADOW_ALPHA
+ if (texture(u_texture, v_texCoord).a * v_color.a < u_alphaCutoff) discard;
+#endif
// The expression the receiver evaluates on u_shadowMatrix, evaluated here on the
// same matrix and at the same point: whatever each backend's clip z means, both
// sides mean it alike.
@@ -635,6 +643,9 @@ fn packNormal(n : vec3f) -> vec2f {
let was = v.v_velPrev.xy / max(v.v_velPrev.w, 1e-6);
return vec4f((was - now) * 0.5, v.pos.z, select(0.0, 1.0, v.v_velPrev.w > 1e-6));
#elif defined(SHADOW_DEPTH)
+#ifdef SHADOW_ALPHA
+ if (textureSampleLevel(t0, s0, v.v_texCoord, 0.0).a * v.v_color.a < mc.u_alphaCutoff) { discard; }
+#endif
// The expression the receiver evaluates on u_shadowMatrix, evaluated here on the
// same matrix and at the same point: whatever each backend's clip z means, both
// sides mean it alike.
diff --git a/src/esengine/renderer/plugins/MeshPlugin.cpp b/src/esengine/renderer/plugins/MeshPlugin.cpp
index 1c5d5f83a..fd2406749 100644
--- a/src/esengine/renderer/plugins/MeshPlugin.cpp
+++ b/src/esengine/renderer/plugins/MeshPlugin.cpp
@@ -923,9 +923,11 @@ void MeshPlugin::collect(RenderCollectContext& collect_ctx) {
}
}
key.shaderId = materialProgram != 0 ? materialProgram : residentShader;
- // A caster whose material bends it casts the bent shape: the depth program gets the
- // material's hook, and the draw keeps the material so its params are bound.
- if (shadowDepth && material && ctx.materials && ctx.materials->hasVertexHook(mesh.material)) {
+ // A caster whose material bends it casts the bent shape, and one whose alpha is coverage casts
+ // only where it is opaque: the depth program gets the material's hook and cutoff, and the draw
+ // keeps the material so its params are bound.
+ if (shadowDepth && material && ctx.materials
+ && (ctx.materials->hasVertexHook(mesh.material) || ctx.materials->castsCutoutShadow(mesh.material))) {
const u32 bentDepth = ctx.materials->shadowProgram(mesh.material, ctx.resources,
resident->hasNormals, skinned, resident->hasTangents);
if (bentDepth != 0) {
diff --git a/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp b/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp
index feb15dafa..1b206e47c 100644
--- a/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp
+++ b/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp
@@ -520,6 +520,11 @@ inline constexpr const char* MESH = R"esshader(#pragma shader "Mesh"
+#pragma feature SHADOW_ALPHA
+
+
+
+
#pragma feature WEIGHT_HEAT
@@ -789,6 +794,9 @@ void main() {
highp vec2 was = v_velPrev.xy / max(v_velPrev.w, 1e-6);
fragColor = vec4((was - now) * 0.5, gl_FragCoord.z, v_velPrev.w > 1e-6 ? 1.0 : 0.0);
#elif defined(SHADOW_DEPTH)
+#ifdef SHADOW_ALPHA
+ if (texture(u_texture, v_texCoord).a * v_color.a < u_alphaCutoff) discard;
+#endif
@@ -1110,6 +1118,9 @@ fn packNormal(n : vec3f) -> vec2f {
let was = v.v_velPrev.xy / max(v.v_velPrev.w, 1e-6);
return vec4f((was - now) * 0.5, v.pos.z, select(0.0, 1.0, v.v_velPrev.w > 1e-6));
#elif defined(SHADOW_DEPTH)
+#ifdef SHADOW_ALPHA
+ if (textureSampleLevel(t0, s0, v.v_texCoord, 0.0).a * v.v_color.a < mc.u_alphaCutoff) { discard; }
+#endif
diff --git a/src/esengine/renderer/store/MaterialStore.cpp b/src/esengine/renderer/store/MaterialStore.cpp
index ee4d3e6a9..9449c2382 100644
--- a/src/esengine/renderer/store/MaterialStore.cpp
+++ b/src/esengine/renderer/store/MaterialStore.cpp
@@ -124,7 +124,9 @@ u32 MaterialStore::shadowProgram(u32 materialId, resource::ResourceManager& reso
// material's own fragment is not used: a shadow map wants depth, not colour.
const resource::ParsedShader material = resource::ShaderParser::parse(src->second.source);
resource::ParsedShader depth = resource::ShaderParser::parse(ShaderEmbeds::MESH);
- if (!material.valid || !depth.valid || material.vertexHook.empty()) {
+ const bool hooked = !material.vertexHook.empty();
+ const bool cutout = material.coverageParam == "u_alphaCutoff";
+ if (!material.valid || !depth.valid || (!hooked && !cutout)) {
variants[shape] = {};
return 0;
}
@@ -138,7 +140,8 @@ u32 MaterialStore::shadowProgram(u32 materialId, resource::ResourceManager& reso
if (tangents && withNormals) features.emplace_back("MESH_TANGENTS");
if (skinned) features.emplace_back("SKINNED");
features.emplace_back("SHADOW_DEPTH");
- features.emplace_back("ES_MATERIAL_VERTEX");
+ if (hooked) features.emplace_back("ES_MATERIAL_VERTEX");
+ if (cutout) features.emplace_back("SHADOW_ALPHA");
const auto target = resources.preferredShaderTarget();
const std::string vert = resource::ShaderParser::assembleStage(depth, resource::ShaderStage::Vertex, "", features, target);
const std::string frag = resource::ShaderParser::assembleStage(depth, resource::ShaderStage::Fragment, "", features, target);
diff --git a/src/esengine/renderer/store/MaterialStore.hpp b/src/esengine/renderer/store/MaterialStore.hpp
index ec7b1d5a6..7585752c1 100644
--- a/src/esengine/renderer/store/MaterialStore.hpp
+++ b/src/esengine/renderer/store/MaterialStore.hpp
@@ -222,6 +222,14 @@ public:
return layout && layout->vertexHook;
}
+ /// Whether @p materialId's caster has to be cut out by its alpha: its alpha is coverage now and the
+ /// coverage param is the one the depth shader reads.
+ bool castsCutoutShadow(u32 materialId) const {
+ const MaterialRecord* m = find(materialId);
+ const MaterialUniformLayout* layout = m ? layoutFor(m->shaderRef) : nullptr;
+ return layout && layout->coverageParam == "u_alphaCutoff" && alphaIsCoverage(materialId);
+ }
+
/// The program that draws @p materialId's CASTER into a shadow map: the engine's depth shader for
/// resident meshes with the material's vertex hook and params grafted on, so a bent mesh casts the
/// shadow of the bent shape. 0 when the source was never kept or the variant fails — the caller
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 9c1ee6305..1169c4cc5 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -2541,6 +2541,8 @@ export const NOTED = {
{"note":"Imported meshes now carry a tangent at each vertex (a glTF's own direction, or one made from the UVs), and normal maps on them are read against that frame instead of one derived per pixel, which was wrong at UV seams and on stretched textures. Re-import a model to get them; one imported before keeps its old shading."},
"feat(render): an Eye shader draws the iris behind a refracting cornea, with a pupil, a limbal ring and a catchlight":
{"note":"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."},
+ "feat(render): a cutout material casts its cut-out shape into shadow maps, and Hair cards cut out by alpha":
+ {"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."},
"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 a0194df57..49e39dfac 100644
--- a/tools/renderScenes.mjs
+++ b/tools/renderScenes.mjs
@@ -205,6 +205,9 @@ export const SCENES = [
// linear gates cast nothing and the other shadow gates are gamma. The atlas takes
// the target away mid-frame; whether the capture is handed back is what this pins.
{ 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.
+ { 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}]" } },
// The same question through a PERSPECTIVE camera, which is what a 3D scene uses: