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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(render): normal maps stay on up close
esengine3ac17f3c058a
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 0bb28dd10..0497e2c5b 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -17,6 +17,7 @@ published separately; it ships inside the editor.### Fixed- **A shipped game samples normal maps and roughness maps as data.** The engine's own upload — a web export, a mini-game, a native build, the editor's Play — made every image an sRGB colour texture in a linear project whatever its import settings said, so every normal map was bent (a flat one by about fifty degrees) and every packed occlusion, roughness and metal map gamma-darkened, while the editor's viewport, which uploads through its own path, looked right. ORCA Bistro's Vespa came out as dark chrome in an exported game and blue in the editor. Khronos's NormalTangentTest now draws each normal-mapped bump as its geometric twin does.+- Normal maps stay on up close. The tangent frame taken from screen derivatives was refused below a fixed size, which shrinks with the fourth power of distance and scale, so in a metre-scale scene a surface a few metres from the camera silently lost its normal map.## [0.75.0] - 2026-09-27diff --git a/fixtures/scenes/mesh-normalmap-tiny.esscene b/fixtures/scenes/mesh-normalmap-tiny.esscenenew file mode 100644index 000000000..f19478613--- /dev/null+++ b/fixtures/scenes/mesh-normalmap-tiny.esscene@@ -0,0 +1,71 @@+{+ "version": 4,+ "name": "MeshNormalMap",+ "entities": [+ {+ "id": 0,+ "name": "Camera",+ "parent": null,+ "children": [],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": 0,+ "y": 0,+ "z": 0.001+ }+ }+ },+ {+ "type": "Camera",+ "data": {+ "projectionType": 1,+ "orthoSize": 0.03,+ "isActive": true,+ "priority": 0+ }+ }+ ]+ },+ {+ "id": 1,+ "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": 1,+ "y": 0+ },+ "enabled": true+ }+ }+ ]+ }+ ]+}diff --git a/fixtures/scenes/mesh-normalmap-tiny.textures.json b/fixtures/scenes/mesh-normalmap-tiny.textures.jsonnew file mode 100644index 000000000..0967ef424--- /dev/null+++ b/fixtures/scenes/mesh-normalmap-tiny.textures.json@@ -0,0 +1 @@+{}diff --git a/fixtures/scenes/normalmap-quad-tiny.esprefab b/fixtures/scenes/normalmap-quad-tiny.esprefabnew file mode 100644index 000000000..b128fcaef--- /dev/null+++ b/fixtures/scenes/normalmap-quad-tiny.esprefab@@ -0,0 +1,43 @@+{+ "version": "2",+ "name": "normalmap-quad-tiny",+ "rootEntityId": "n0",+ "entities": [+ {+ "prefabEntityId": "n0",+ "name": "Quad",+ "parent": null,+ "children": [],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {+ "scale": {+ "x": 0.0001,+ "y": 0.0001,+ "z": 0.0001+ }+ }+ },+ {+ "type": "MeshRenderer",+ "data": {+ "mesh": "scenes/normalmap-quad.esmesh",+ "lit": true,+ "material": "scenes/normalmap-quad_m0.esmaterial",+ "color": {+ "r": 1,+ "g": 1,+ "b": 1,+ "a": 1+ },+ "opaque": true,+ "cullBackfaces": true,+ "enabled": true+ }+ }+ ]+ }+ ]+}diff --git a/src/esengine/resource/ShaderParser.cpp b/src/esengine/resource/ShaderParser.cppindex 2c713b469..827c43b5f 100644--- a/src/esengine/resource/ShaderParser.cpp+++ b/src/esengine/resource/ShaderParser.cpp@@ -1229,8 +1229,9 @@ fn perturbNormal(N : vec3f, worldPos : vec3f, uv : vec2f, tangentNormal : vec3f)let dp1perp = cross(N, dp1);let T = dp2perp * duv1.x + dp1perp * duv2.x;let B = dp2perp * duv1.y + dp1perp * duv2.y;- let m = max(max(dot(T, T), dot(B, B)), 1e-12);- if (m <= 1e-11) { return N; }+ 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; }let invmax = inverseSqrt(m);return normalize(mat3x3f(T * invmax, B * invmax, N) * tangentNormal);}@@ -2068,8 +2069,11 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p" highp vec3 dp1perp = cross(N, dp1);\n"" highp vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;\n"" highp vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;\n"+ // Degenerate against the surface's own scale: m falls with the fourth power+ // of distance, so a fixed floor turned normal maps off up close." highp float m = max(dot(T, T), dot(B, B));\n"- " if (m <= 1e-11) return N;\n"+ " highp float scale = max(dot(dp1, dp1), dot(dp2, dp2)) * max(dot(duv1, duv1), dot(duv2, duv2));\n"+ " if (!(m > scale * 1e-6)) return N;\n"" highp float invmax = inversesqrt(m);\n"" return normalize(mat3(T * invmax, B * invmax, N) * tangentNormal);\n""}\n"diff --git a/tools/renderScenes.mjs b/tools/renderScenes.mjsindex fa5f8ddd9..0bc038d43 100644--- a/tools/renderScenes.mjs+++ b/tools/renderScenes.mjs@@ -692,6 +692,10 @@ export const SCENES = [// both halves take the same light. Its texels point -X and +X, so the lit half// and the unlit one are the tangent frame, derived per pixel from derivatives.{ id: "mesh-normalmap", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_PREFAB: "/scenes/normalmap-quad.esprefab", ESTELLA_VERIFY_SCENE: "/scenes/mesh-normalmap.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/mesh-normalmap.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "4", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.375,\"y\":0.5,\"rgb\":[180,180,180],\"tol\":45},{\"x\":0.625,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":30}]" } },+ // The same quad ten thousand times smaller, seen as large. A tangent frame from+ // derivatives shrinks with the fourth power of scale, so a degenerate-UV floor+ // fixed in world units switched normal maps off on anything small up close.+ { id: "mesh-normalmap-tiny", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_PREFAB: "/scenes/normalmap-quad-tiny.esprefab", ESTELLA_VERIFY_SCENE: "/scenes/mesh-normalmap-tiny.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/mesh-normalmap-tiny.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "4", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.375,\"y\":0.5,\"rgb\":[180,180,180],\"tol\":45},{\"x\":0.625,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":30}]" } },// The same quad under a two-channel normal map (B = 0, as BC5 and some converters// store it), lit from the front. Rebuilding Z from X and Y turns both halves toward// the light; unpacking B as Z points them into the surface and both go black.