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.

fix(webgpu): a normal map keeps its frame along a triangle's edge

yhh04be128d660b
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2022b3d49..ed2029372 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -54,6 +54,7 @@ published separately; it ships inside the editor.
### Fixed
+- **Normal maps along a triangle's edge on WebGPU.** On WebGPU with multisampling, every pixel a triangle only partly covers lost its normal map: on Metal the screen-space derivatives that build the frame were moved behind the branch that picks it, where those pixels have no neighbours to difference against. A normal-mapped quad drew a dotted line down its diagonal and round its rim, and the Skin shader's detail layer left those pixels without their highlight.
- **Cut-out chains of coverage.** A cut-out's mip chain holds, as alpha, the share of texels its cut keeps, and its colour is the colour of what the cut keeps. The minified alpha is what a cut-out is drawn with, and a plain average of a soft mask (a painted band of eyelashes fading out) averaged to more than the cut keeps: the probe's lash card drew a pink veil over the iris where Blender draws a few lashes. The chain no longer rescales alpha to keep a cutoff's coverage, and an imported cut-out's clear texels take the colour of the solid ones nearest them, so even a mip the GPU makes does not average in what the painter left under alpha 0.
- **Cut-out shadows.** A cut-out's shadow keeps the share of light it lets through, and is the same shape on both backends. The shadow pass read a card's alpha at level 0 on WebGPU (mips on WebGL2), and cut at the cutoff on both; where the card is smaller than a texel from the light it now cuts against a per-texel threshold through the mip chain, so a fringe of hair throws a shadow as deep as it is dense.
- **Imported eyes.** An imported eye shades as the Principled shader it was authored against: the Eye shader's tinted white, its warm wrap of light past the terminator (which reddened a painted iris) and its lid darkening are written as off, and both its glossy layers take the source's roughness instead of a near-mirror cornea.
diff --git a/src/esengine/resource/ShaderParser.cpp b/src/esengine/resource/ShaderParser.cpp
index 29efe3fbb..928ecd567 100644
--- a/src/esengine/resource/ShaderParser.cpp
+++ b/src/esengine/resource/ShaderParser.cpp
@@ -1487,22 +1487,23 @@ fn perturbNormal(N : vec3f, worldPos : vec3f, uv : vec2f, tangentNormal : vec3f)
let dp2 = -dpdy(worldPos);
let duv1 = dpdx(uv);
let duv2 = -dpdy(uv);
- // The frame the normal map was baked in, when the geometry carries it: the tangent made square to the interpolated normal.
- if (dot(g_worldTangent.xyz, g_worldTangent.xyz) > 1e-8) {
- let Tg = normalize(g_worldTangent.xyz - N * dot(N, g_worldTangent.xyz));
- let Bg = cross(N, Tg) * g_worldTangent.w;
- return normalize(mat3x3f(Tg, Bg, N) * tangentNormal);
- }
let dp2perp = cross(dp2, N);
let dp1perp = cross(N, dp1);
let T = dp2perp * duv1.x + dp1perp * duv2.x;
let B = dp2perp * duv1.y + dp1perp * duv2.y;
let m = max(dot(T, T), dot(B, B));
let scale = max(dot(dp1, dp1), dot(dp2, dp2)) * max(dot(duv1, duv1), dot(duv2, duv2));
- if (!(m > scale * 1e-6)) { return N; }
// Each axis to unit length on its own: a texture stretched more along one axis than the other (a face unwrapped into an atlas) has
// a short T beside a long B, and scaling both by the longer one would leave the normal map flat along the short one.
- return normalize(mat3x3f(T * inverseSqrt(max(dot(T, T), 1e-20)), B * inverseSqrt(max(dot(B, B), 1e-20)), N) * tangentNormal);
+ let derived = select(N, normalize(mat3x3f(T * inverseSqrt(max(dot(T, T), 1e-20)), B * inverseSqrt(max(dot(B, B), 1e-20)), N) * tangentNormal),
+ m > scale * 1e-6);
+ // The frame the normal map was baked in, when the geometry carries it: the tangent made square to the interpolated normal.
+ // Chosen by select, not by returning early: behind a branch Metal sinks the derivatives above into it, where a quad the
+ // triangle only partly covers has lost its helpers, and every pixel along an edge lost its normal map (skin-detail).
+ let Tg = normalize(g_worldTangent.xyz - N * dot(N, g_worldTangent.xyz));
+ let Bg = cross(N, Tg) * g_worldTangent.w;
+ let baked = normalize(mat3x3f(Tg, Bg, N) * tangentNormal);
+ return select(derived, baked, dot(g_worldTangent.xyz, g_worldTangent.xyz) > 1e-8);
}
// Where a world point lands in this camera's part of a screen-space mask: on the Z = 0
// plane, which is the plane a 2D occluder hides things in, and through this same camera