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 3D shadow reaches the scene a perspective camera is looking at

esenginea2ac3ec764c4
diff --git a/desktop/public/scenes/mesh-shadow-perspective.esscene b/desktop/public/scenes/mesh-shadow-perspective.esscene
new file mode 100644
index 000000000..fdd63e152
--- /dev/null
+++ b/desktop/public/scenes/mesh-shadow-perspective.esscene
@@ -0,0 +1,93 @@
+{
+ "version": 1,
+ "name": "MeshShadowPerspective",
+ "entities": [
+ {
+ "id": 0,
+ "name": "Camera",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ { "type": "Transform", "data": { "position": { "x": 0, "y": 0, "z": 300 } } },
+ {
+ "type": "Camera",
+ "data": {
+ "projectionType": 0,
+ "fov": 45,
+ "nearPlane": 10,
+ "farPlane": 2000,
+ "isActive": true,
+ "priority": 0
+ }
+ }
+ ]
+ },
+ {
+ "id": 1,
+ "name": "Ground",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": { "position": { "x": 0, "y": 0, "z": -200 }, "scale": { "x": 600, "y": 600, "z": 1 } }
+ },
+ {
+ "type": "Mesh2D",
+ "data": {
+ "mesh": "/scenes/shadow-quad.esmesh",
+ "color": { "r": 1, "g": 1, "b": 1, "a": 1 },
+ "lit": true,
+ "opaque": true,
+ "enabled": true
+ }
+ }
+ ]
+ },
+ {
+ "id": 2,
+ "name": "Blocker",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": { "position": { "x": 0, "y": 0, "z": 0 }, "scale": { "x": 150, "y": 150, "z": 1 } }
+ },
+ {
+ "type": "Mesh2D",
+ "data": {
+ "mesh": "/scenes/shadow-quad.esmesh",
+ "color": { "r": 1, "g": 1, "b": 1, "a": 1 },
+ "lit": true,
+ "opaque": true,
+ "enabled": true
+ }
+ }
+ ]
+ },
+ {
+ "id": 3,
+ "name": "Sun",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ { "type": "Transform", "data": { "position": { "x": 0, "y": 0, "z": 0 } } },
+ {
+ "type": "Light2D",
+ "data": {
+ "type": 1,
+ "color": { "r": 1, "g": 1, "b": 1, "a": 1 },
+ "intensity": 1,
+ "direction": { "x": 0.5, "y": 0 },
+ "meshShadows": true
+ }
+ }
+ ]
+ }
+ ]
+}
diff --git a/src/esengine/renderer/frame/RenderFrame.cpp b/src/esengine/renderer/frame/RenderFrame.cpp
index b08c9f113..3f96c54b4 100644
--- a/src/esengine/renderer/frame/RenderFrame.cpp
+++ b/src/esengine/renderer/frame/RenderFrame.cpp
@@ -7,6 +7,7 @@
#include "../../ecs/components/Transform.hpp"
#include "../../ecs/components/Light2D.hpp"
#include "../../ecs/components/ShadowCaster2D.hpp"
+#include "../../ecs/components/Mesh2D.hpp"
#include "../../resource/ShaderParser.hpp"
#include "../../core/Log.hpp"
#include "../../core/FrameProfiler.hpp"
@@ -766,6 +767,30 @@ static constexpr u32 kShadowMapSize = 1024;
/// camera-sized area to stop shadowing itself, small enough not to detach contact.
static constexpr f32 kShadowBias = 0.0015f;
+/// The world box every GPU-resident mesh occupies, or false when none does — what
+/// a shadow map has to cover, casters and receivers alike. Rotation is ignored,
+/// the same approximation MeshPlugin's own cull uses.
+bool RenderFrame::meshWorldBounds(ecs::Registry& registry, glm::vec3& outMin, glm::vec3& outMax) {
+ auto meshes = registry.view<ecs::Transform, ecs::Mesh2D>();
+ bool any = false;
+ for (auto entity : meshes) {
+ const auto& mesh = meshes.get<ecs::Mesh2D>(entity);
+ if (!mesh.enabled || !mesh.mesh.isValid()) continue;
+ const Mesh* resident = resource_manager_.getMesh(mesh.mesh);
+ if (!resident) continue;
+ auto& transform = meshes.get<ecs::Transform>(entity);
+ transform.ensureDecomposed();
+ const glm::vec3 scale = transform.worldScale;
+ const glm::vec3 centre = transform.worldPosition
+ + (resident->localMin + resident->localMax) * 0.5f * scale;
+ const glm::vec3 half = glm::abs((resident->localMax - resident->localMin) * 0.5f * scale);
+ outMin = any ? glm::min(outMin, centre - half) : centre - half;
+ outMax = any ? glm::max(outMax, centre + half) : centre + half;
+ any = true;
+ }
+ return any;
+}
+
void RenderFrame::renderShadowMap(ecs::Registry& registry) {
shadow_texture_id_ = 0;
// Zeroed params first: a matrix that outlived its map would shadow the scene
@@ -780,15 +805,19 @@ void RenderFrame::renderShadowMap(ecs::Registry& registry) {
auto* rt = target_manager_.get(shadow_rt_);
if (!rt) return;
- // What the map has to cover: the camera's own view, unless the light asked for a
- // fixed reach. A radius rather than the rect, so the coverage does not change as
- // the light turns — a shadow that resized when the sun moved would crawl.
+ // What the map covers: the 3D geometry, unless the light asked for a fixed reach.
+ // A radius, so coverage does not change as the light turns. NOT the camera's
+ // world rect — that reprojects NDC z=0, the whole view only when orthographic.
+ glm::vec3 boundsMin(0.0f), boundsMax(0.0f);
+ const bool haveBounds = meshWorldBounds(registry, boundsMin, boundsMax);
const CameraWorldRect view = computeCameraWorldRect(view_projection_);
- const f32 fitted = 0.5f * glm::length(glm::vec2(view.right - view.left,
- view.top - view.bottom));
+ const f32 fitted = haveBounds
+ ? 0.5f * glm::length(boundsMax - boundsMin)
+ : 0.5f * glm::length(glm::vec2(view.right - view.left, view.top - view.bottom));
const f32 radius = shadow_light_extent_ > 0.0f ? shadow_light_extent_
: std::max(fitted, 1.0f);
- const glm::vec3 centre(view.center.x, view.center.y, 0.0f);
+ const glm::vec3 centre = haveBounds ? (boundsMin + boundsMax) * 0.5f
+ : glm::vec3(view.center.x, view.center.y, 0.0f);
// The light's own direction, in the three dimensions the 2D field implies: the
// shader lights a surface from normalize(-dir.xy, 1), so rays travel the other way.
diff --git a/src/esengine/renderer/frame/RenderFrame.hpp b/src/esengine/renderer/frame/RenderFrame.hpp
index 38cdfed94..d54c16da8 100644
--- a/src/esengine/renderer/frame/RenderFrame.hpp
+++ b/src/esengine/renderer/frame/RenderFrame.hpp
@@ -308,6 +308,10 @@ private:
* one: the map is one per frame, the receiving shader having one of each.
*/
void renderShadowMap(ecs::Registry& registry);
+
+ /// The world box the resident meshes occupy — what a shadow map must cover.
+ /// False when nothing 3D is in the scene.
+ bool meshWorldBounds(ecs::Registry& registry, glm::vec3& outMin, glm::vec3& outMax);
/// Opens the frame's own target with @p clear's load-op. Shared by begin() and the
/// shadow pass, which re-opens it after drawing through a target of its own.
void openPass(const PassClear& clear, RenderTargetManager::Handle target);
diff --git a/tools/renderScenes.mjs b/tools/renderScenes.mjs
index 98b57f02e..a1afa93b4 100644
--- a/tools/renderScenes.mjs
+++ b/tools/renderScenes.mjs
@@ -56,6 +56,11 @@ export const SCENES = [
// x, so the shadow lands 100 along +x and x 75..175 is floor the light cannot see.
// The blocker itself stays lit — shadowing one's own caster is the classic bug.
{ id: "mesh-shadow", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/mesh-shadow.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.708,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":20},{\"x\":0.167,\"y\":0.5,\"rgb\":[228,228,228],\"tol\":20},{\"x\":0.5,\"y\":0.5,\"rgb\":[228,228,228],\"tol\":20}]" } },
+
+ // The same question through a PERSPECTIVE camera, which is what a 3D scene uses:
+ // coverage derived from the camera's NDC z=0 slice is the whole view for the
+ // orthographic one above and a sliver near the near plane for this one.
+ { id: "mesh-shadow-perspective", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/mesh-shadow-perspective.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.86,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":20},{\"x\":0.10,\"y\":0.5,\"rgb\":[228,228,228],\"tol\":20},{\"x\":0.5,\"y\":0.5,\"rgb\":[228,228,228],\"tol\":20}]" } },
// A baked panorama lighting a surface BY DIRECTION: two coplanar triangles, one
// white colour, normals up and down under a sky blue above and red below. A flat
// ambient term draws the halves identically, which is why the probes invert.