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(render): the stock lit material reads a mesh's own normals

esengined10d15f99983
diff --git a/desktop/public/scenes/mesh-material-lit/builtinlit.esmaterial b/desktop/public/scenes/mesh-material-lit/builtinlit.esmaterial
new file mode 100644
index 000000000..7c3ed0cc2
--- /dev/null
+++ b/desktop/public/scenes/mesh-material-lit/builtinlit.esmaterial
@@ -0,0 +1,10 @@
+{
+ "version": "1.0",
+ "type": "material",
+ "shader": "builtin:sprite-lit",
+ "blendMode": 0,
+ "depthTest": false,
+ "properties": {
+ "u_tint": { "r": 1, "g": 0, "b": 0, "a": 1 }
+ }
+}
diff --git a/sdk/src/render/builtinShaders.ts b/sdk/src/render/builtinShaders.ts
index 23ce42a6a..f999ece09 100644
--- a/sdk/src/render/builtinShaders.ts
+++ b/sdk/src/render/builtinShaders.ts
@@ -87,15 +87,26 @@ precision mediump float;
in vec4 v_color;
in vec2 v_texCoord;
in highp vec2 v_worldPos;
+#ifdef MESH_NORMALS
+in highp vec3 v_worldNormal;
+in highp vec3 v_worldXYZ;
+#endif
uniform sampler2D u_textures[8];
out vec4 fragColor;
// Unset u_normalMap = flat normal, so lighting works with no normal map assigned.
+// Geometry that CARRIES normals is lit by them, and the map then perturbs that
+// frame instead of replacing it with the flat one a sprite has.
void main() {
vec4 base = texture(u_textures[0], v_texCoord) * v_color * u_tint;
+#ifdef MESH_NORMALS
+ highp vec3 N = perturbNormal(normalize(v_worldNormal), v_worldXYZ, v_texCoord,
+ sampleNormal(u_normalMap, v_texCoord));
+#else
vec3 N = sampleNormal(u_normalMap, v_texCoord);
+#endif
fragColor = vec4(applyLighting2D(base.rgb, N, v_worldPos), base.a);
}
#pragma end
@@ -103,7 +114,12 @@ void main() {
#pragma fragment wgsl
@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
let base = textureSampleLevel(t0, s0, v.v_texCoord, 0.0) * v.v_color * mc.u_tint;
+#ifdef MESH_NORMALS
+ let N = perturbNormal(normalize(v.v_worldNormal), v.v_worldXYZ, v.v_texCoord,
+ sampleNormal(u_normalMap, u_normalMap_s, v.v_texCoord));
+#else
let N = sampleNormal(u_normalMap, u_normalMap_s, v.v_texCoord);
+#endif
return vec4f(applyLighting2D(base.rgb, N, v.v_worldPos), base.a);
}
#pragma end
diff --git a/tools/renderScenes.mjs b/tools/renderScenes.mjs
index cf8ff5f46..3ba6b22a6 100644
--- a/tools/renderScenes.mjs
+++ b/tools/renderScenes.mjs
@@ -140,6 +140,10 @@ export const SCENES = [
// normals the engine's vertex stage hands it. RED, so the fallback (the mesh
// shader, taken when a variant fails) cannot pass for it — that draws white.
{ id: "mesh-material-lit", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_MESH_ASSET: "/scenes/lit-triangles.esmesh", ESTELLA_VERIFY_MESH_MATERIAL: "/scenes/mesh-material-lit/litmat.esmaterial", ESTELLA_VERIFY_SCENE: "/scenes/mesh-lit.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/mesh-lit.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "4", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.30,\"y\":0.556,\"rgb\":[255,0,0],\"tol\":40},{\"x\":0.70,\"y\":0.556,\"rgb\":[0,0,0],\"tol\":40}]" } },
+ // The STOCK lit template on the same geometry: a flat normal there would light
+ // an imported model like a sprite — a wrong picture, not a missing one. Red
+ // tint, so the engine's own mesh shader (white) cannot pass for it.
+ { id: "mesh-builtin-lit", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_MESH_ASSET: "/scenes/lit-triangles.esmesh", ESTELLA_VERIFY_MESH_MATERIAL: "/scenes/mesh-material-lit/builtinlit.esmaterial", ESTELLA_VERIFY_SCENE: "/scenes/mesh-lit.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/mesh-lit.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "4", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.30,\"y\":0.556,\"rgb\":[255,0,0],\"tol\":40},{\"x\":0.70,\"y\":0.556,\"rgb\":[0,0,0],\"tol\":40}]" } },
// A mesh assigned IN THE EDITOR: a cold ref reaches the World only through the
// live loader + re-projection. The quad covers the gap between the scene's own
// two triangles, so the frame says which geometry is drawn — and the hit test