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): a 2D shadow caster turns with its entity

esengine1ca79ac63b54
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1572b81bc..ef0da0973 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,13 @@ published separately; it ships inside the editor.
### Fixed
+- **A 2D shadow caster turns with the entity it is on.** Its box was built from the
+ entity's world position and its own size, and from nothing else: a wall laid at an angle
+ cast the shadow of an upright wall, and turning the wall changed nothing on screen. The
+ box now turns the way a collider on the same entity does — the size stays the caster's
+ own, so scaling the entity still does not resize the occluder. The editor's shadow
+ extent turns with it, or it would draw a box that is not the one casting.
+
- **A camera drawing half the screen now keeps to its half.** A camera states the rect it
draws into when it opens its frame; the engine took that rect back off the DEVICE later,
after the collect, where it was whatever the last thing to touch the device had left
diff --git a/desktop b/desktop
index 3555d6c7c..5928efeda 160000
--- a/desktop
+++ b/desktop
@@ -1 +1 @@
-Subproject commit 3555d6c7c5fabe65fe95ed6f54f9c9e064579168
+Subproject commit 5928efedae441af6aaaf36d92d1d9ace6f7a2385
diff --git a/fixtures/scenes/mat-lit-shadow-turned.esscene b/fixtures/scenes/mat-lit-shadow-turned.esscene
new file mode 100644
index 000000000..4c0ecb639
--- /dev/null
+++ b/fixtures/scenes/mat-lit-shadow-turned.esscene
@@ -0,0 +1,72 @@
+{
+ "version": 4,
+ "name": "Main",
+ "entities": [
+ {
+ "id": 0,
+ "name": "Camera",
+ "parent": null,
+ "children": [],
+ "components": [
+ { "type": "Transform", "data": { "position": { "x": 0, "y": 0, "z": 10 } } },
+ { "type": "Camera", "data": { "projectionType": 1, "orthoSize": 300, "isActive": true, "priority": 0 } }
+ ],
+ "visible": true
+ },
+ {
+ "id": 1,
+ "name": "Lit",
+ "parent": null,
+ "children": [],
+ "components": [
+ { "type": "Transform", "data": { "position": { "x": 0, "y": 0, "z": 0 } } },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": { "x": 400, "y": 400 },
+ "color": { "r": 1, "g": 1, "b": 1, "a": 1 },
+ "material": "/scenes/mat-lit/lit.esmaterial"
+ }
+ }
+ ],
+ "visible": true
+ },
+ {
+ "id": 2,
+ "name": "GreenPointLight",
+ "parent": null,
+ "children": [],
+ "components": [
+ { "type": "Transform", "data": { "position": { "x": -150, "y": 0, "z": 0 } } },
+ {
+ "type": "Light",
+ "data": {
+ "type": 0,
+ "color": { "r": 0, "g": 1, "b": 0, "a": 1 },
+ "intensity": 1,
+ "radius": 800,
+ "shadowSoftness": 0
+ }
+ }
+ ],
+ "visible": true
+ },
+ {
+ "id": 3,
+ "name": "TiltedWall",
+ "parent": null,
+ "children": [],
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": { "x": 0, "y": 0, "z": 0 },
+ "rotation": { "x": 0, "y": 0, "z": 0.5, "w": 0.8660254 }
+ }
+ },
+ { "type": "ShadowCaster2D", "data": { "size": { "x": 20, "y": 200 }, "enabled": true } }
+ ],
+ "visible": true
+ }
+ ]
+}
diff --git a/fixtures/scenes/mat-lit-shadow-turned.textures.json b/fixtures/scenes/mat-lit-shadow-turned.textures.json
new file mode 100644
index 000000000..0967ef424
--- /dev/null
+++ b/fixtures/scenes/mat-lit-shadow-turned.textures.json
@@ -0,0 +1 @@
+{}
diff --git a/sdk/src/ecs/component.generated.ts b/sdk/src/ecs/component.generated.ts
index 3e80c4079..58ec24333 100644
--- a/sdk/src/ecs/component.generated.ts
+++ b/sdk/src/ecs/component.generated.ts
@@ -768,7 +768,7 @@ export const COMPONENT_META: Record<string, ComponentMetaEntry> = {
colorFields: [],
animatableFields: ['size.x', 'size.y'],
fields: {
- size: { min: 0, tooltip: "Occluder box size in world units (centered on the entity)." },
+ size: { min: 0, tooltip: "Occluder box size in world units (centred on the entity, turning with it)." },
},
},
ShapeRenderer: {
diff --git a/sdk/tests/__snapshots__/component-fieldmeta-golden.test.ts.snap b/sdk/tests/__snapshots__/component-fieldmeta-golden.test.ts.snap
index cac384822..a79d26fe7 100644
--- a/sdk/tests/__snapshots__/component-fieldmeta-golden.test.ts.snap
+++ b/sdk/tests/__snapshots__/component-fieldmeta-golden.test.ts.snap
@@ -871,7 +871,7 @@ exports[`builtin FieldMeta (RC9-1 equivalence gate) > matches the golden snapsho
"ShadowCaster2D": {
"size": {
"min": 0,
- "tooltip": "Occluder box size in world units (centered on the entity).",
+ "tooltip": "Occluder box size in world units (centred on the entity, turning with it).",
},
},
"ShapeRenderer": {
diff --git a/src/esengine/ecs/components/ShadowCaster2D.hpp b/src/esengine/ecs/components/ShadowCaster2D.hpp
index 81fef1619..8b8855647 100644
--- a/src/esengine/ecs/components/ShadowCaster2D.hpp
+++ b/src/esengine/ecs/components/ShadowCaster2D.hpp
@@ -4,7 +4,7 @@
* @file ShadowCaster2D.hpp
* @brief Marks an entity as a 2D shadow occluder for the Lit lighting path.
* @details The render collect path turns each enabled caster into the four world-space edges
- * of a box centred on its Transform, and the 2D shadow pass DRAWS what those edges
+ * of a box placed by its Transform, and the 2D shadow pass DRAWS what those edges
* hide from each casting light into a screen-space mask. A Lit fragment then reads
* its light's channel of that mask. Drawn rather than solved per pixel, which is what
* lets a scene hold as many occluders as it has walls.
@@ -21,12 +21,13 @@
namespace esengine::ecs {
/**
- * @brief A 2D shadow occluder. The box is centered on the entity's world position.
+ * @brief A 2D shadow occluder. The box is centred on the entity's world position and
+ * turns with it; its size is its own, which is how a collider is placed too.
*/
ES_COMPONENT(renderable=enabled)
struct ShadowCaster2D {
/** @brief Occluder box size in world units (full width/height; centered on the Transform). */
- ES_PROPERTY(animatable, min=0, tooltip="Occluder box size in world units (centered on the entity).")
+ ES_PROPERTY(animatable, min=0, tooltip="Occluder box size in world units (centred on the entity, turning with it).")
glm::vec2 size{32.0f, 32.0f};
/** @brief Disabled casters are skipped during collection. */
diff --git a/src/esengine/renderer/frame/RenderFrameShadow2D.cpp b/src/esengine/renderer/frame/RenderFrameShadow2D.cpp
index 124234809..9d3d5adcb 100644
--- a/src/esengine/renderer/frame/RenderFrameShadow2D.cpp
+++ b/src/esengine/renderer/frame/RenderFrameShadow2D.cpp
@@ -12,6 +12,7 @@
#include "../../core/Log.hpp"
#include "../../ecs/components/Transform.hpp"
#include "../../ecs/components/ShadowCaster2D.hpp"
+#include "../RenderTypePlugin.hpp"
#include "../../resource/ShaderParser.hpp"
#include "../rhi/ShaderEmbeds.generated.hpp"
#include "../../core/FrameProfiler.hpp"
@@ -57,13 +58,19 @@ void RenderFrame::collectShadow2D(ecs::Registry& registry) {
const f32 hx = caster.size.x * 0.5f;
const f32 hy = caster.size.y * 0.5f;
if (hx <= 0.0f || hy <= 0.0f) continue;
- const glm::vec2 lo(p.x - hx, p.y - hy);
- const glm::vec2 hi(p.x + hx, p.y + hy);
+ // The box turns with the entity, the way a collider on it does — a wall laid
+ // at an angle casts the shadow of a wall at that angle. Its SIZE is its own,
+ // also like a collider's: scaling the entity does not resize the occluder.
+ const glm::vec2 turn = flatTurnZ(transform.worldRotation);
+ const auto corner = [&](f32 sx, f32 sy) {
+ return glm::vec2{p.x + sx * hx * turn.x - sy * hy * turn.y,
+ p.y + sx * hx * turn.y + sy * hy * turn.x};
+ };
shadow_rings_2d_.push_back({static_cast<u32>(shadow_points_2d_.size()), 4});
- shadow_points_2d_.push_back({lo.x, lo.y});
- shadow_points_2d_.push_back({hi.x, lo.y});
- shadow_points_2d_.push_back({hi.x, hi.y});
- shadow_points_2d_.push_back({lo.x, hi.y});
+ shadow_points_2d_.push_back(corner(-1.0f, -1.0f));
+ shadow_points_2d_.push_back(corner(1.0f, -1.0f));
+ shadow_points_2d_.push_back(corner(1.0f, 1.0f));
+ shadow_points_2d_.push_back(corner(-1.0f, 1.0f));
}
if (shadow_rings_2d_.empty()) return;
diff --git a/tools/renderScenes.mjs b/tools/renderScenes.mjs
index 9277bc543..191a9accb 100644
--- a/tools/renderScenes.mjs
+++ b/tools/renderScenes.mjs
@@ -397,6 +397,10 @@ export const SCENES = [
// light reaches, dark where the box is in the way.
{ id: "shadow2d-point", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/mat-lit-shadow.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/mat-lit-shadow.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.25,\"y\":0.5,\"rgb\":[0,255,0],\"tol\":60},{\"x\":0.75,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":40}]" } },
{ id: "shadow2d-hard", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/mat-lit-shadow-hard.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/mat-lit-shadow-hard.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.25,\"y\":0.5,\"rgb\":[0,255,0],\"tol\":60},{\"x\":0.75,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":40}]" } },
+ // A wall laid at 60 degrees. Its shadow leans with it: the probe below the
+ // light's line is inside an UNTURNED box's shadow and outside this one's,
+ // and a turn the other way would put it back in.
+ { id: "shadow2d-turned", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/mat-lit-shadow-turned.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/mat-lit-shadow-turned.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.25,\"y\":0.5,\"rgb\":[0,255,0],\"tol\":60},{\"x\":0.75,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":40},{\"x\":0.75,\"y\":0.7332,\"rgb\":[0,149,0],\"tol\":60}]" } },
// Two probes inside the penumbra, where a hard shadow reads 0 and an unshadowed
// surface reads 166: a soft edge is the only thing that lands between them.
{ id: "shadow2d-soft", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/mat-lit-shadow-soft.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/mat-lit-shadow-soft.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.25,\"y\":0.5,\"rgb\":[0,255,0],\"tol\":60},{\"x\":0.75,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":40},{\"x\":0.75,\"y\":0.30,\"rgb\":[0,44,0],\"tol\":22},{\"x\":0.75,\"y\":0.70,\"rgb\":[0,52,0],\"tol\":22}]" } },