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(shadows): the lookup steps toward the light as far as a slope of 1.2 asks and takes the rest along the normal - a floor under a sun reads as it did, and a wall the light grazes still does not reach past a ceiling

yhhd2730f0524fe
diff --git a/src/esengine/resource/ShaderParser.cpp b/src/esengine/resource/ShaderParser.cpp
index b08df98ab..3d55f2ec9 100644
--- a/src/esengine/resource/ShaderParser.cpp
+++ b/src/esengine/resource/ShaderParser.cpp
@@ -1629,14 +1629,16 @@ fn shadowFactor3D(worldPos : vec3f, N : vec3f, L : vec3f, source : vec2f,
// Ask a step toward the light rather than shift the answer: a step in world units
// is the same step whether a map's depth runs linearly with distance or
// hyperbolically, which is what lets one expression serve a cascade and a cone.
- // The point asked about is moved off the surface along its normal, as far as a texel of the surface is wide at the angle it
- // stands to the light, and then a texel and a half toward the light. All of it toward the light, which is what this did, is
- // 1.5 texels over the cosine: eleven texels for a wall the sun grazes, and where a texel is a few units (a coarse map, a
- // large scene) that is further than a ceiling is thick - the top of every wall under a roof came out lit.
- let aside = worldPos + N * (texel * 1.0 * sqrt(1.0 - NoL * NoL));
+ // The point asked about is stepped toward the light, a texel and a half of doubt and what that is worth at the angle the surface
+ // stands - but only as far as a slope of 1.2 asks (a floor under a sun 40 degrees up or more: all of it, as it always was). Past
+ // that the rest of the clearance is taken along the normal instead. All of it toward the light is 1.5 texels over the cosine,
+ // eleven texels for a wall the sun grazes, and where a texel is a few units (a coarse map, a large scene) that is further than
+ // a ceiling is thick: the top of every wall under a roof came out lit.
+ let lean = min(slope, 1.2);
+ let aside = worldPos + N * (texel * 1.5 * max(sqrt(1.0 - NoL * NoL) - lean * NoL, 0.0));
let lcq = m * vec4f(aside, 1.0);
let uvq = lcq.xy / max(lcq.w, 1e-6) * 0.5 + 0.5;
- let here = shadowDepth(m, aside + L * (texel * 1.5));
+ let here = shadowDepth(m, aside + L * (texel * 1.5 * (1.0 + lean)));
// The ONE place the two backends must not agree: v = 0 samples a texture's
// bottom in GL and its top here, and the rect arrives with its low corner in
// GL's terms — so the tile and the square inside it are both turned over.
@@ -1702,8 +1704,8 @@ fn shadowFactor3D(worldPos : vec3f, N : vec3f, L : vec3f, source : vec2f,
let turn = vec2f(cos(spin), sin(spin));
// See the GLSL twin: the matrix is applied to the direction once, and each tap moves the clip position along it.
let toLight = m * vec4f(L, 0.0);
- // The taps are round the moved point, so what is left to forgive is the texel and a half and what each tap's own distance is worth.
- let stepBase = texel * 1.5;
+ // The taps are round the moved point: the same step as it took, and what each tap's own distance is worth.
+ let stepBase = texel * 1.5 * (1.0 + lean);
let stepPer = texel * slope;
var sum = 0.0;
var hits = 0.0;
@@ -1744,7 +1746,7 @@ fn shadowFactor3D(worldPos : vec3f, N : vec3f, L : vec3f, source : vec2f,
// span beyond it — which is why a shadow hardens as its caster nears what it
// falls on, and why a sun's angle and a lamp's size are the same expression.
let radius = clamp(src * span / max(dbw * texel, 1e-4), 0.5, kShadowDisc);
- let wide = shadowDepth(m, aside + L * (texel * (1.5 + radius * slope)));
+ let wide = shadowDepth(m, aside + L * (texel * (1.5 * (1.0 + lean) + radius * slope)));
// Thirty-two, where the hard edge needs four: the disc is as wide as the source
// makes it, and a penumbra averaged over too few taps bands instead of blurring.
var soft = 0.0;
@@ -3113,11 +3115,12 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p
// Ask a step toward the light rather than shift the answer: a step in world
// units is the same step whether a map's depth runs linearly with distance or
// hyperbolically, which is what lets one expression serve a cascade and a cone.
- // See the WGSL twin: the point is moved off the surface along its normal, and only a texel and a half toward the light.
- " highp vec3 aside = worldPos + N * (texel * 1.0 * sqrt(1.0 - NoL * NoL));\n"
+ // See the WGSL twin: toward the light as far as a slope of 1.2 asks, and along the normal for the rest.
+ " highp float lean = min(slope, 1.2);\n"
+ " highp vec3 aside = worldPos + N * (texel * 1.5 * max(sqrt(1.0 - NoL * NoL) - lean * NoL, 0.0));\n"
" highp vec4 lcq = m * vec4(aside, 1.0);\n"
" highp vec2 uvq = lcq.xy / max(lcq.w, 1e-6) * 0.5 + 0.5;\n"
- " highp float here = shadowDepth(m, aside + L * (texel * 1.5));\n"
+ " highp float here = shadowDepth(m, aside + L * (texel * 1.5 * (1.0 + lean)));\n"
" highp vec2 at0 = rect.xy + uvq * rect.z;\n"
" highp vec2 lo = rect.xy + u_shadowParams.y * 0.5;\n"
" highp vec2 hi = rect.xy + rect.z - u_shadowParams.y * 0.5;\n"
@@ -3179,7 +3182,7 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p
" // The depth a tap forgives to is that of the point a step toward the light, and the step is linear in how far the tap stands: so the\n"
" // matrix is applied to the direction once, and a tap moves the clip position along it instead of going through the matrix again.\n"
" highp vec4 toLight = m * vec4(L, 0.0);\n"
- " highp float stepBase = texel * 1.5;\n"
+ " highp float stepBase = texel * 1.5 * (1.0 + lean);\n"
" highp float stepPer = texel * slope;\n"
" highp float sum = 0.0;\n"
" highp float hits = 0.0;\n"
@@ -3225,7 +3228,7 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p
// it falls on, and why a sun's angle and a lamp's size are one expression.
" highp float radius = clamp(src * span / max(dbw * texel, 1e-4),\n"
" 0.5, kShadowDisc);\n"
- " highp float wide = shadowDepth(m, aside + L * (texel * (1.5 + radius * slope)));\n"
+ " highp float wide = shadowDepth(m, aside + L * (texel * (1.5 * (1.0 + lean) + radius * slope)));\n"
// Thirty-two, where the hard edge needs four: the disc is as wide as the
// source makes it, and a penumbra averaged over too few taps bands rather
// than blurs.