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): a double-sided mesh is lit on the side the camera sees
esengine86d027ac80a9
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex fdb575fcc..251d382f3 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -38,6 +38,7 @@ published separately; it ships inside the editor.### Fixed+- A double-sided mesh is lit on the side the camera sees. Its back faces took the front's normal, so a leaf, a flag or an awning turned away from the light went black on the side the light actually reached; the normal now flips for a back face, as glTF specifies, in the engine's mesh shader and in the material shaders an imported model uses.- Normal maps stored in two channels light correctly. The shader rebuilds Z from X and Y instead of reading blue, so a BC5 map, or one a converter wrote with blue at 0 — every normal map of Bistro's glTF conversion — no longer points its surface's normals inward, which left normal-mapped surfaces unlit by their sun.- Compressed (KTX2) textures on web and mini-games are sampled with their mip chain. The cook has always written one into every texture it compresses, and only level 0 reached the GPU: a compressed texture shimmered as it shrank into the distance, and the rest of its bytes shipped for nothing. A texture decoded to RGBA on a device with no compressed format gets its chain generated.- The package size report on Windows attributes the SDK's bytes to the directories they come from again: every module read as "project", since the source map's paths were spelled with backslashes.diff --git a/fixtures/scenes/backface-quad-plain.esprefab b/fixtures/scenes/backface-quad-plain.esprefabnew file mode 100644index 000000000..e2337a558--- /dev/null+++ b/fixtures/scenes/backface-quad-plain.esprefab@@ -0,0 +1,43 @@+{+ "version": "2",+ "name": "backface-quad-plain",+ "rootEntityId": "n0",+ "entities": [+ {+ "prefabEntityId": "n0",+ "name": "Quad",+ "parent": null,+ "children": [],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {+ "rotation": {+ "x": 0,+ "y": 1,+ "z": 0,+ "w": 0+ }+ }+ },+ {+ "type": "MeshRenderer",+ "data": {+ "mesh": "scenes/normalmap-quad.esmesh",+ "lit": true,+ "color": {+ "r": 1,+ "g": 1,+ "b": 1,+ "a": 1+ },+ "opaque": true,+ "cullBackfaces": false,+ "enabled": true+ }+ }+ ]+ }+ ]+}diff --git a/fixtures/scenes/backface-quad-plain.esprefab.meta b/fixtures/scenes/backface-quad-plain.esprefab.metanew file mode 100644index 000000000..b8cdb2582--- /dev/null+++ b/fixtures/scenes/backface-quad-plain.esprefab.meta@@ -0,0 +1,8 @@+{+ "uuid": "6194f462-f45a-4d3f-af57-144558fecc25",+ "version": "2.0",+ "type": "prefab",+ "importer": {+ "autoMigrate": true+ }+}diff --git a/fixtures/scenes/backface-quad.esprefab b/fixtures/scenes/backface-quad.esprefabnew file mode 100644index 000000000..d5632aae1--- /dev/null+++ b/fixtures/scenes/backface-quad.esprefab@@ -0,0 +1,44 @@+{+ "version": "2",+ "name": "backface-quad",+ "rootEntityId": "n0",+ "entities": [+ {+ "prefabEntityId": "n0",+ "name": "Quad",+ "parent": null,+ "children": [],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {+ "rotation": {+ "x": 0,+ "y": 1,+ "z": 0,+ "w": 0+ }+ }+ },+ {+ "type": "MeshRenderer",+ "data": {+ "mesh": "scenes/normalmap-quad.esmesh",+ "lit": true,+ "material": "scenes/backface-quad_m0.esmaterial",+ "color": {+ "r": 1,+ "g": 1,+ "b": 1,+ "a": 1+ },+ "opaque": true,+ "cullBackfaces": false,+ "enabled": true+ }+ }+ ]+ }+ ]+}diff --git a/fixtures/scenes/backface-quad.esprefab.meta b/fixtures/scenes/backface-quad.esprefab.metanew file mode 100644index 000000000..67ffa72d8--- /dev/null+++ b/fixtures/scenes/backface-quad.esprefab.meta@@ -0,0 +1,8 @@+{+ "uuid": "62cba391-cd2a-4711-8235-9018c0357b03",+ "version": "2.0",+ "type": "prefab",+ "importer": {+ "autoMigrate": true+ }+}diff --git a/fixtures/scenes/backface-quad_m0.esmaterial b/fixtures/scenes/backface-quad_m0.esmaterialnew file mode 100644index 000000000..2a86d9bcd--- /dev/null+++ b/fixtures/scenes/backface-quad_m0.esmaterial@@ -0,0 +1,13 @@+{+ "version": "1.0",+ "type": "material",+ "shader": "builtin:model",+ "blendMode": 9,+ "depthTest": true,+ "depthWrite": true,+ "cull": 0,+ "properties": {+ "u_metallic": 0,+ "u_roughness": 1+ }+}diff --git a/fixtures/scenes/backface-quad_m0.esmaterial.meta b/fixtures/scenes/backface-quad_m0.esmaterial.metanew file mode 100644index 000000000..12f391675--- /dev/null+++ b/fixtures/scenes/backface-quad_m0.esmaterial.meta@@ -0,0 +1,6 @@+{+ "uuid": "7f39e004-ccd2-4e4c-a950-918803dfd63e",+ "version": "2.0",+ "type": "material",+ "importer": {}+}diff --git a/fixtures/scenes/mesh-backface.esscene b/fixtures/scenes/mesh-backface.esscenenew file mode 100644index 000000000..e6a790cf7--- /dev/null+++ b/fixtures/scenes/mesh-backface.esscene@@ -0,0 +1,66 @@+{+ "version": 4,+ "name": "MeshBackface",+ "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": "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+ }+ }+ ]+ }+ ]+}diff --git a/fixtures/scenes/mesh-backface.textures.json b/fixtures/scenes/mesh-backface.textures.jsonnew file mode 100644index 000000000..0967ef424--- /dev/null+++ b/fixtures/scenes/mesh-backface.textures.json@@ -0,0 +1 @@+{}diff --git a/sdk/src/render/builtinShaders.ts b/sdk/src/render/builtinShaders.tsindex f555f8bcd..ffab3adf5 100644--- a/sdk/src/render/builtinShaders.ts+++ b/sdk/src/render/builtinShaders.ts@@ -104,6 +104,8 @@ void main() {#ifdef MESH_NORMALShighp vec3 N = perturbNormal(normalize(v_worldNormal), v_worldXYZ, v_texCoord,sampleNormal(u_normalMap, v_texCoord));+ // A double-sided surface is lit from the side it is seen from (glTF's rule).+ if (!gl_FrontFacing) N = -N;#elsevec3 N = sampleNormal(u_normalMap, v_texCoord);#endif@@ -112,11 +114,12 @@ void main() {#pragma end#pragma fragment wgsl-@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {+@fragment fn fs_main(v : VSOut, @builtin(front_facing) front : bool) -> @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,+ var N = perturbNormal(normalize(v.v_worldNormal), v.v_worldXYZ, v.v_texCoord,sampleNormal(u_normalMap, u_normalMap_s, v.v_texCoord));+ if (!front) { N = -N; }#elselet N = sampleNormal(u_normalMap, u_normalMap_s, v.v_texCoord);#endif@@ -366,6 +369,8 @@ void main() {#ifdef MESH_NORMALShighp vec3 N = perturbNormal(normalize(v_worldNormal), v_worldXYZ, v_texCoord,sampleNormal(u_normalMap, v_texCoord));+ // A double-sided surface is lit from the side it is seen from (glTF's rule).+ if (!gl_FrontFacing) N = -N;#elsevec3 N = sampleNormal(u_normalMap, v_texCoord);#endif@@ -386,7 +391,7 @@ void main() {#pragma end#pragma fragment wgsl-@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {+@fragment fn fs_main(v : VSOut, @builtin(front_facing) front : bool) -> @location(0) vec4f {// WGSL has no module varying: the entry point hands the injected header the// run its GLSL twin reads straight off one. Without this every material-shaded// draw read instance 0's indirect light — one probe volume for the lot.@@ -394,8 +399,9 @@ void main() {let base = textureSampleLevel(t0, s0, v.v_texCoord, 0.0) * v.v_color * mc.u_tint;if (base.a < mc.u_alphaCutoff) { discard; }#ifdef MESH_NORMALS- let N = perturbNormal(normalize(v.v_worldNormal), v.v_worldXYZ, v.v_texCoord,+ var N = perturbNormal(normalize(v.v_worldNormal), v.v_worldXYZ, v.v_texCoord,sampleNormal(u_normalMap, u_normalMap_s, v.v_texCoord));+ if (!front) { N = -N; }#elselet N = sampleNormal(u_normalMap, u_normalMap_s, v.v_texCoord);#endifdiff --git a/src/esengine/data/shaders/mesh.esshader b/src/esengine/data/shaders/mesh.esshaderindex ab0c38fc4..cf18f9a7b 100644--- a/src/esengine/data/shaders/mesh.esshader+++ b/src/esengine/data/shaders/mesh.esshader@@ -222,6 +222,8 @@ void main() {#ifdef NORMAL_MAPN = perturbNormal(N, v_worldPos, v_texCoord, sampleNormal(u_normalMap, v_texCoord));#endif+ // A double-sided surface is lit from the side it is seen from (glTF's rule).+ if (!gl_FrontFacing) N = -N;// A rough dielectric, and the position it really has: a shadow is cast on a// point in space, and xy is not one. Specular stays ON — switching it off is// not a cheaper PBR, it is a different surface, and a mesh whose material@@ -416,7 +418,7 @@ struct VSOut {@location(6) @interpolate(flat) v_probeSlot : f32,};-@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {+@fragment fn fs_main(v : VSOut, @builtin(front_facing) front : bool) -> @location(0) vec4f {// WGSL has no module varying: the entry point hands the injected header the// slot its GLSL twin reads straight off one.g_probeSlot = v.v_probeSlot;@@ -436,6 +438,7 @@ struct VSOut {#ifdef NORMAL_MAPN = perturbNormal(N, v.v_worldPos, v.v_texCoord, sampleNormal(t1, s1, v.v_texCoord));#endif+ if (!front) { N = -N; }// A rough dielectric, and the position it really has: a shadow is cast on a// point in space, and xy is not one. Specular stays ON — switching it off is// not a cheaper PBR, it is a different surface, and a mesh whose materialdiff --git a/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp b/src/esengine/renderer/rhi/ShaderEmbeds.generated.hppindex 086591aca..45520ac47 100644--- a/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp+++ b/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp@@ -574,6 +574,8 @@ void main() {N = perturbNormal(N, v_worldPos, v_texCoord, sampleNormal(u_normalMap, v_texCoord));#endif+ if (!gl_FrontFacing) N = -N;+@@ -767,7 +769,7 @@ struct VSOut {@location(6) @interpolate(flat) v_probeSlot : f32,};-@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {+@fragment fn fs_main(v : VSOut, @builtin(front_facing) front : bool) -> @location(0) vec4f {g_probeSlot = v.v_probeSlot;@@ -787,6 +789,7 @@ struct VSOut {#ifdef NORMAL_MAPN = perturbNormal(N, v.v_worldPos, v.v_texCoord, sampleNormal(t1, s1, v.v_texCoord));#endif+ if (!front) { N = -N; }diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 14ac0d4fb..24d39864f 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -1236,6 +1236,8 @@ export const NOTED = {{ note: 'An FBX model\'s specular map can be read as packed occlusion' },'feat(cook): textures encode on every core':{ note: 'Exporting compresses textures on every core' },+ 'fix(render): a double-sided mesh is lit on the side the camera sees':+ { note: 'A double-sided mesh is lit on the side the camera sees' },'fix(eht): the generator writes LF on every platform':{ internal: 'The engine header tool is a repository generator; on Windows it rewrote every generated file with CRLF, which only a contributor there ever saw.' },};diff --git a/tools/renderScenes.mjs b/tools/renderScenes.mjsindex 36dd44b55..3930dab2f 100644--- a/tools/renderScenes.mjs+++ b/tools/renderScenes.mjs@@ -692,6 +692,11 @@ export const SCENES = [// 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.{ id: "mesh-normalmap-xy", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_PREFAB: "/scenes/normalmap-xy.esprefab", ESTELLA_VERIFY_SCENE: "/scenes/mesh-normalmap-xy.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/mesh-normalmap-xy.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "4", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.375,\"y\":0.5,\"rgb\":[241,241,241],\"tol\":30},{\"x\":0.625,\"y\":0.5,\"rgb\":[241,241,241],\"tol\":30}]" } },+ // A double-sided quad turned away from the camera, lit from the camera's side:+ // its back is what is seen and what the light reaches, so it must be lit. Once+ // through a material (the path an imported model takes) and once without one.+ { id: "mesh-backface-material", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_PREFAB: "/scenes/backface-quad.esprefab", ESTELLA_VERIFY_SCENE: "/scenes/mesh-backface.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/mesh-backface.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "4", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.5,\"y\":0.5,\"rgb\":[255,255,255],\"tol\":40}]" } },+ { id: "mesh-backface-plain", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_PREFAB: "/scenes/backface-quad-plain.esprefab", ESTELLA_VERIFY_SCENE: "/scenes/mesh-backface.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/mesh-backface.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "4", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.5,\"y\":0.5,\"rgb\":[255,255,255],\"tol\":40}]" } },// The channels only a material can carry, one quad each, through the products a// real import wrote. Ambient light only: the black quad's lit half can only be// emission, the white quad's right half is occluded away, the cutout discards.