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
feat(render): 2D shadow path on the Lit2D lighting channel (T3, keystone)
yhh190b8e5174d5
diff --git a/src/esengine/renderer/LightConstants.hpp b/src/esengine/renderer/LightConstants.hppindex f888947af..bbd3fe8f8 100644--- a/src/esengine/renderer/LightConstants.hpp+++ b/src/esengine/renderer/LightConstants.hpp@@ -31,6 +31,14 @@ inline constexpr const char* LIGHT_CONSTANTS_BLOCK = "LightConstants";*/inline constexpr u32 MAX_LIGHTS_2D = 16;+/**+ * @brief Max 2D shadow occluders packed into the UBO (axis-aligned boxes in world space).+ * The injected es_applyLighting2D loops up to the active count; 0 occluders = no+ * shadowing (identity), so the feature is inert until the render path feeds boxes.+ * Must match the `u_occluders[..]` array size in ShaderParser's injected GLSL.+ */+inline constexpr u32 MAX_OCCLUDERS_2D = 8;+/*** @brief One 2D light, std140-packed (three vec4s, 48 bytes, 16-aligned).* @details posDir: xy = world position (point/spot) or direction (directional); z = type@@ -55,10 +63,16 @@ struct GpuLight2D {struct LightConstants {glm::vec4 ambient{0.0f};GpuLight2D lights[MAX_LIGHTS_2D];+ /// x = active occluder count; yzw unused. Appended after `lights` so existing std140+ /// offsets (ambient, lights) are unchanged — old Lit2D shaders keep reading the same bytes.+ glm::vec4 occluderCount{0.0f};+ /// World-space AABBs: (minX, minY, maxX, maxY). A point/spot light is shadowed at a fragment+ /// when the fragment→light segment crosses any box.+ glm::vec4 occluders[MAX_OCCLUDERS_2D];};static_assert(sizeof(GpuLight2D) == 48, "GpuLight2D must be std140-tight (three vec4s)");-static_assert(sizeof(LightConstants) == 16 + 48 * MAX_LIGHTS_2D,+static_assert(sizeof(LightConstants) == 16 + 48 * MAX_LIGHTS_2D + 16 + 16 * MAX_OCCLUDERS_2D,"LightConstants must match the std140 GLSL block layout");} // namespace esenginediff --git a/src/esengine/renderer/LightStore.hpp b/src/esengine/renderer/LightStore.hppindex 711a2ef3c..72cba12d6 100644--- a/src/esengine/renderer/LightStore.hpp+++ b/src/esengine/renderer/LightStore.hpp@@ -60,6 +60,16 @@ public:dirty_ = true;}+ /// Appends a world-space AABB occluder (minX, minY, maxX, maxY). Silently drops past+ /// MAX_OCCLUDERS_2D. With no occluders added, the injected shader shadow test is a no-op.+ void addOccluder(const glm::vec4& box) {+ const u32 n = static_cast<u32>(data_.occluderCount.x);+ if (n >= MAX_OCCLUDERS_2D) return;+ data_.occluders[n] = box;+ data_.occluderCount.x = static_cast<f32>(n + 1);+ dirty_ = true;+ }+/// Uploads the mirror (when dirty) and binds it at LIGHT_CONSTANTS_BINDING. Called once per/// frame in flush(); the binding persists, only the contents change. No-op without a device.void uploadAndBind();diff --git a/src/esengine/resource/ShaderParser.cpp b/src/esengine/resource/ShaderParser.cppindex 37534b5ca..1b1983911 100644--- a/src/esengine/resource/ShaderParser.cpp+++ b/src/esengine/resource/ShaderParser.cpp@@ -433,6 +433,8 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p"layout(std140) uniform LightConstants {\n"" highp vec4 u_ambient;\n"" es_Light2D u_lights[16];\n"+ " highp vec4 u_occluderCount;\n" // x = active occluder count+ " highp vec4 u_occluders[8];\n" // world AABBs (minX,minY,maxX,maxY)"};\n"// Engine-owned normal-map convention (RGB[0,1] -> normal[-1,1], normalized), so every// Lit2D shader unpacks tangent-space normals the same way. 2D applies it screen-space@@ -440,6 +442,40 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p"highp vec3 es_sampleNormal(in highp sampler2D map, in highp vec2 uv) {\n"" return normalize(texture(map, uv).xyz * 2.0 - 1.0);\n""}\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.+ "highp float es_segHitsBox(in highp vec2 p0, in highp vec2 p1, in highp vec4 box) {\n"+ " highp vec2 d = p1 - p0;\n"+ " highp float tmin = 0.0;\n"+ " highp float tmax = 1.0;\n"+ " for (int a = 0; a < 2; ++a) {\n"+ " highp float da = (a == 0) ? d.x : d.y;\n"+ " highp float p0a = (a == 0) ? p0.x : p0.y;\n"+ " highp float lo = ((a == 0) ? box.x : box.y) - p0a;\n"+ " highp float hi = ((a == 0) ? box.z : box.w) - p0a;\n"+ " if (abs(da) < 1e-5) {\n"+ " if (lo > 0.0 || hi < 0.0) return 0.0;\n"+ " } else {\n"+ " highp float t1 = lo / da;\n"+ " highp float t2 = hi / da;\n"+ " tmin = max(tmin, min(t1, t2));\n"+ " tmax = min(tmax, max(t1, t2));\n"+ " if (tmin > tmax) return 0.0;\n"+ " }\n"+ " }\n"+ " return 1.0;\n"+ "}\n"+ // 0.0 if any occluder blocks the fragment from the light, else 1.0. No occluders+ // (count 0) -> always 1.0, so the shadow test is inert until the render path feeds boxes.+ "highp float es_shadowFactor2D(in highp vec2 worldPos, in highp vec2 lightPos) {\n"+ " int n = int(u_occluderCount.x);\n"+ " for (int i = 0; i < 8; ++i) {\n"+ " if (i >= n) break;\n"+ " if (es_segHitsBox(worldPos, lightPos, u_occluders[i]) > 0.5) return 0.0;\n"+ " }\n"+ " return 1.0;\n"+ "}\n""highp vec3 es_applyLighting2D(highp vec3 albedo, highp vec3 N, highp vec2 worldPos) {\n"" highp vec3 lit = u_ambient.rgb;\n"" for (int i = 0; i < 16; ++i) {\n"@@ -464,6 +500,9 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p" highp vec2 toFrag = (dist > 0.0001) ? (-d / dist) : sp.xy;\n"" atten *= smoothstep(sp.w, sp.z, dot(sp.xy, toFrag));\n"" }\n"+ " if (pd.z < 0.5 || pd.z >= 1.5) {\n" // point/spot cast shadows; directional skipped+ " atten *= es_shadowFactor2D(worldPos, pd.xy);\n"+ " }\n"" highp float ndotl = max(dot(N, L), 0.0);\n"" lit += col.rgb * (col.a * ndotl * atten);\n"" }\n"