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
perf(water): the sea reads the sun's shadow as one hard edge, not as a penumbra
yhhef343a20ad8b
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 2a3c150fa..e42ffa6ef 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -30,6 +30,7 @@ published separately; it ships inside the editor.### Changed+- **The sea takes the sun's shadow as one hard edge.** The water lit itself with the sun's full soft shadow (a search for blockers and a filter of up to sixty-four taps), which a surface the sun seldom leaves and whose edge the waves hide does not need; a shader can now set `g_shadowHard` before it lights to read the shadow as the edge a source with no size throws, nine taps, and the water does. The picture is the same to the eye (a mean difference of a quarter of a level on the Terrain & Water example); the water is about 0.5 ms shorter on an RTX 3060.- **A lit surface no longer works out the light slots nothing shines from.** The light block holds sixteen lights and a scene has one or two, but the lighting loop shaded all sixteen: every empty slot paid for its distance, its direction and the whole specular lobe, to add zero. A slot with no intensity returns at once now; the picture is the same to the pixel gates on WebGL2 and WebGPU. A frame of the Terrain & Water example is 4 to 6 ms shorter on an RTX 3060 (the trees' shading alone is more than a third faster).- **The sky's clouds and the fog's light shafts cost a third of what they did.** The cloud layer, the shadow it casts on the ground and the sun's light through the haze are smooth and were worked out for every pixel of the frame; they are worked out at half size now, and each pixel reads them back from the four texels around it, weighted by how near their depth is to its own, so nothing is carried across the edge of a hill or a leaf. The dome, the sun, the moon and the stars, which have an edge, stay full size. The picture is the same to the eye (a mean difference of a third of a level of 255 on the Terrain & Water example); a frame of it is 5 ms shorter on an RTX 3060 (25.6 to 20.1). The sky and the height fog are two passes each in a chain now, `sky_clouds` and `sky_composite`, `fog_volume` and `fog_composite`.- **The water surface leaves early where it is not water.** A shader that may discard late is not let test depth early, so the water behind a hill ran all of it; the test that decides whether a pixel is water now comes before the waves and the light are worked out. The picture is the same, a frame of the Terrain & Water example about a millisecond shorter or more on an RTX 3060. The WebGL2 shader only.diff --git a/docs/TERRAIN_WATER_GPU_COST.md b/docs/TERRAIN_WATER_GPU_COST.mdindex 1ea69ef71..232aca9c2 100644--- a/docs/TERRAIN_WATER_GPU_COST.md+++ b/docs/TERRAIN_WATER_GPU_COST.md@@ -440,3 +440,7 @@| 0 | 5.64 | 3.21 | 18.9 → 14.8 |比“少算 3.3 ms 的数学”省得更多(机位 1 少了 5.2 ms),因为着色器变短后占用率也上来了。画面与之前逐像素比,平均差 0.09/255。这条对每个有光照的网格都成立,不只这个示例。++### 水面的阴影(2026-10-26,更正)++上一节说“水的主要成本是它自己的阴影查找”,**这个说法错了**。水着色器里自己的那一次阴影采样(焦散处)和焦散本身都量了:剪掉后水段不变(1.18 对 1.18、2.15 对 2.13)。关掉阴影后水变便宜,是因为水面用 `applyLightingSurface` 打光,太阳的软阴影(16 个找遮挡点的采样加最多 64 个滤波采样)是在那里面。海面几乎不在阴影里、边缘又被波浪盖住,所以现在水着色器在打光前设 `g_shadowHard = 1`,阴影按“没有大小的光源”读成 9 个采样的硬边:水段 2.16 → 1.68(机位 0)、2.28 → 1.71(机位 3),画面平均差 0.24/255 和 0.54/255,放大看海面上的云影还在。`g_shadowHard` 和 `g_hairShadow` 一样是着色器在打光前设的一个全局,别的着色器也可以用。diff --git a/sdk/src/water/waterShader.ts b/sdk/src/water/waterShader.tsindex 2a90d7680..de0aeb47a 100644--- a/sdk/src/water/waterShader.ts+++ b/sdk/src/water/waterShader.ts@@ -748,6 +748,8 @@ void main() {vec3 albedo = mix(scatterColor * murk, u_foamColor.rgb, foam);// Rain stipples the surface finer than any ring: it reflects a little less sharply.float roughness = mix(u_roughness + 0.05 * clamp(u_rain, 0.0, 1.0), 0.7, foam);+ // The sea is seldom in shadow and its edge is lost in the waves: the sun's shadow is read as one hard edge here, not as a penumbra.+ g_shadowHard = 1.0;vec3 lit = applyLightingSurface(albedo, N, P, viewDir, 0.0, roughness, 1.0, 1.0, ext);float cosV = clamp(dot(N, viewDir), 0.0, 1.0);float fresnel = 0.0204 + (1.0 - 0.0204) * pow(1.0 - cosV, 5.0);@@ -1205,6 +1207,7 @@ fn waterSurface(p : vec2f, time : f32, dist : f32, amp : f32, calm : f32) -> vecext.misc.x = 0.0204;let albedo = mix(scatterColor * murk, mc.u_foamColor.rgb, vec3f(foam));let roughness = mix(mc.u_roughness + 0.05 * clamp(mc.u_rain, 0.0, 1.0), 0.7, foam);+ g_shadowHard = 1.0;let lit = applyLightingSurface(albedo, N, P, viewDir, 0.0, roughness, 1.0, 1.0, ext);let cosV = clamp(dot(N, viewDir), 0.0, 1.0);var fresnel = 0.0204 + (1.0 - 0.0204) * pow(1.0 - cosV, 5.0);diff --git a/src/esengine/resource/ShaderParser.cpp b/src/esengine/resource/ShaderParser.cppindex c815c606e..9a50db6af 100644--- a/src/esengine/resource/ShaderParser.cpp+++ b/src/esengine/resource/ShaderParser.cpp@@ -1271,6 +1271,9 @@ struct PlanarConstants { planarProjection : mat4x4f, planarParams : vec4f };var<private> g_probeSlot : f32 = 0.0;// Set by a hair shader before it lights: how much of the light a layer of strands takes, per world unit along it (0 = a surface, a hard compare).var<private> g_hairShadow : f32 = 0.0;+// Set by a shader that lights a surface the sun seldom shadows (the sea) before it lights: the shadow is read as an edge a source with no size+// throws, nine taps, and not as the penumbra of the sun's size, a search and a filter of up to sixty-four.+var<private> g_shadowHard : f32 = 0.0;// The GLSL twin's v_lightmap, handed over by the entry point like the slot above.var<private> g_lightmap : vec3f = vec3f(0.0);// The mesh's own tangent frame, handed over by the entry point like the slot above; zero where the shader never sets it, which@@ -1468,7 +1471,7 @@ fn shadowFactor3D(worldPos : vec3f, N : vec3f, L : vec3f, source : vec2f,// A source with no size throws one edge: a tent over the 3x3 texels around the// point, weighted by where it falls among them. Equal weights step at every// texel and the map's grid shows along the edge; a 2x2 still stair-steps it.- if (src <= 0.0) {+ if (src <= 0.0 || g_shadowHard > 0.5) {let texelUv = lc.u_shadowParams.y;let f = at0 / texelUv - 1.0;let c = (floor(f) + 0.5) * texelUv;@@ -2569,6 +2572,8 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p"const highp float kShadowDisc = 24.0;\n"// Set by a hair shader before it lights: how much of the light a layer of strands takes, per world unit along it."highp float g_hairShadow = 0.0;\n"+ // See the WGSL twin: a shader sets it before it lights to take the shadow as one hard edge.+ "highp float g_shadowHard = 0.0;\n"// A spiral, not a table: the golden angle spreads any number of taps evenly// over a disc, so the pattern follows the count instead of sitting beside it."highp vec2 diskTap(in int i, in int n, in highp float radius) {\n"@@ -2718,7 +2723,7 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p// A source with no size throws one edge: a tent over the 3x3 texels around the// point, weighted by where it falls among them. Equal weights step at every// texel and the map's grid shows along the edge; a 2x2 still stair-steps it.- " if (src <= 0.0) {\n"+ " if (src <= 0.0 || g_shadowHard > 0.5) {\n"" highp float texelUv = u_shadowParams.y;\n"" highp vec2 f = at0 / texelUv - 1.0;\n"" highp vec2 c = (floor(f) + 0.5) * texelUv;\n"diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex dc48726cd..d008069a1 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -2990,4 +2990,6 @@ export const NOTED = {{"note":"The sky's clouds and the fog's light shafts cost a third of what they did."},"perf(render): a lit surface skips the light slots nothing shines from":{"note":"A lit surface no longer works out the light slots nothing shines from."},+ "perf(water): the sea reads the sun's shadow as one hard edge, not as a penumbra":+ {"note":"The sea takes the sun's shadow as one hard edge."},};