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(lighting): occluders no longer shadow their own interior — casters can be lit

yhh171c2d662e33
diff --git a/examples/lighting-2d/README.md b/examples/lighting-2d/README.md
index fb602ce0f..a3d767832 100644
--- a/examples/lighting-2d/README.md
+++ b/examples/lighting-2d/README.md
@@ -25,12 +25,17 @@ Lighting is **multiplicative**: a lit surface renders as
unlit wall is nearly black — so a light does not just brighten the room, it
*reveals* it.
-- **The room** is a grid of tiles plus a few prop sprites, all with `lit: true`
- on their `Sprite` — the one-flag way to receive 2D lights (in the editor it is
- the **Lit** checkbox). A sprite without the flag stays unlit (full-bright) —
- that is why the light markers and obstacle boxes are always visible. When a
- surface needs more than the flag (a tint, a normal map), give it a `Lit2D`
+- **The room** — the tile grid, the props, the pillars, and every placed
+ obstacle — has `lit: true` on its `Sprite`, the one-flag way to receive 2D
+ lights (in the editor it is the **Lit** checkbox). A sprite without the flag
+ stays unlit (full-bright) — the light **markers** are left unlit on purpose so
+ they read as emissive dots. Kill every light and the whole room goes black;
+ only the markers keep glowing. When a surface needs more than the flag (a
+ tint, a normal map), give it a `Lit2D`
material instead — see the [lighting guide](../../docs/astro/src/content/docs/guides/lighting.mdx).
+- **Shadow casters are lit like anything else**: an occluder box never shadows
+ its own interior, so the pillars and obstacles take light on the side facing
+ it while still throwing a shadow cone behind them.
- **The torch** (`src/systems/torch.ts`) is a `Light2D` of type `Point`. Each
frame it is moved to `UICameraInfo.worldMouseX/Y`, the cursor already
projected into world space by the active camera.
diff --git a/examples/lighting-2d/assets/scenes/main.esscene b/examples/lighting-2d/assets/scenes/main.esscene
index 8ddef34b5..743e8706f 100644
--- a/examples/lighting-2d/assets/scenes/main.esscene
+++ b/examples/lighting-2d/assets/scenes/main.esscene
@@ -2441,7 +2441,8 @@
"b": 0.3,
"a": 1
},
- "layer": 2
+ "layer": 2,
+ "lit": true
}
},
{
@@ -2486,7 +2487,8 @@
"b": 0.3,
"a": 1
},
- "layer": 2
+ "layer": 2,
+ "lit": true
}
},
{
diff --git a/examples/lighting-2d/src/systems/place.ts b/examples/lighting-2d/src/systems/place.ts
index 6a4ac4812..4d8d5a324 100644
--- a/examples/lighting-2d/src/systems/place.ts
+++ b/examples/lighting-2d/src/systems/place.ts
@@ -17,8 +17,9 @@ const obstacles: number[] = [];
let nextColor = 0;
// Left-click → colored Point light, right-click → box obstacle, C → clear.
-// Placed markers stay unlit (no `lit` flag) so a light always reads as a
-// bright dot and an obstacle as a solid block, regardless of the lighting.
+// Light markers stay unlit (no `lit` flag) — an emissive dot that reads as the
+// source itself. Obstacles are lit AND cast shadows: an occluder never shadows
+// its own interior, so a caster's sprite takes light like any other surface.
export const placeSystem = defineSystem(
[Res(Input), Res(UICameraInfo), Commands()],
(input, camera: UICameraData, cmds) => {
@@ -54,6 +55,7 @@ export const placeSystem = defineSystem(
size: OBSTACLE_SIZE,
color: { r: 0.22, g: 0.24, b: 0.30, a: 1 },
layer: 4,
+ lit: true,
})
.insert(ShadowCaster2D, { size: OBSTACLE_SIZE, enabled: true })
.id();
diff --git a/src/esengine/resource/ShaderParser.cpp b/src/esengine/resource/ShaderParser.cpp
index 6e063ab7d..b04336cd5 100644
--- a/src/esengine/resource/ShaderParser.cpp
+++ b/src/esengine/resource/ShaderParser.cpp
@@ -495,8 +495,12 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p
"}\n"
// 2D hard shadows: a slab test of the fragment->light segment against a world AABB.
// Returns 1.0 when the segment crosses the box's interior, else 0.0. The [0,1] param
- // clamp means boxes behind the fragment or beyond the light don't occlude.
+ // clamp means boxes behind the fragment or beyond the light don't occlude. A box
+ // occludes the world OUTSIDE it, never a fragment inside itself — so a lit sprite
+ // that is also a ShadowCaster2D casts shadows without blacking out its own pixels
+ // (and geometry overlapping an occluder isn't spuriously darkened by it).
"highp float segHitsBox(in highp vec2 p0, in highp vec2 p1, in highp vec4 box) {\n"
+ " if (p0.x >= box.x && p0.y >= box.y && p0.x <= box.z && p0.y <= box.w) return 0.0;\n"
" highp vec2 d = p1 - p0;\n"
" highp float tmin = 0.0;\n"
" highp float tmax = 1.0;\n"