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(planar): a reflection softens with roughness and keeps a hot spot's hue
yhh52e26011e8b1
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex f733e0425..569a49069 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -26,6 +26,7 @@ published separately; it ships inside the editor.- **The sky lights the scene.** Sky Atmosphere's **Light the Scene** makes the sky the scene's environment: every Ambient light that names no environment of its own lights with it, its diffuse light from the dome and the clouds and its reflection on metal and wet ground, scaled by the light's colour and intensity as an imported one is. The sky is worked out again as it changes (the sun moving, a weather coming in, clouds drifting), at most once a second; with clouds that takes about 20 ms on a desktop. The sun's disc is left out, since the directional light the sky follows is its direct light. Below the horizon it is the ground, lit by the sky's sun and dome and giving back **Ground Albedo** of it (0.2 by default, between asphalt and grass; Unreal's atmosphere uses 0.4), so a wall in shadow takes the sunlit ground's bounce as well as the sky's: under a high clear sun that doubles its fill, under a low hazy one it adds little. Off by default, so a scene already lit by hand looks as it did.- **The sky's lower half is the sunlit ground.** When Sky Atmosphere lights the scene, the panorama below the horizon is the ground lit by the sky's own sun and by the dome above, giving back **Ground Albedo** of it (0.2) and fading into the horizon colour; it was the horizon's haze at a quarter, a ground that took no sun. A wall in shadow takes the sunlit ground's bounce. Lighting only: the sky drawn below the horizon is unchanged.- **Rooms stay dark under a sky that changes (sky occlusion).** With **Sky occlusion** on in the Lighting settings (`BakedLighting.skyOcclusion`), a probe bake keeps how much of the sky each probe sees instead of the sky itself: the probes hold the lights and their bounces, and the sky the frame has now (a live Sky Atmosphere, a weather, an environment) is added through what each probe sees of it, to the diffuse light and to the sky's reflection. A ball inside a roofed room measured 119 where the baked sky gave it 150, and a ball outside kept its light. A `.esprobes` baked this way carries `skyVisibility`; one without it lights as before. Probes are read per object, as Unity's light probes are: one large mesh spanning indoors and out takes one value.+- **A planar reflection softens with roughness and keeps a hot spot's hue.** The reflection is filtered over nine taps, a pixel and a half at the least and wider as the surface gets rougher (it was five taps of a few texels, so a damp road mirrored as sharply as a puddle and its silhouettes stepped), and what it shows is brought under a soft ceiling of 1.5 by its brightest channel, with a knee from 0.75, so a reflected sun or a sky's glow stays orange instead of clipping toward white. planar-reflection-hot checks a (6, 0.6, 0) cube reads 255,203,0 directly and 255,108,0 in the mirror, on both backends.- **Wet ground: a map says where it is damp and where water stands.** The Model material takes a wet map over the world's X and Z (`u_wetMask`, with `u_wetOrigin` and `u_wetSize`) and a master `u_wetness`: red is standing water, green dampness; damp ground darkens (`u_wetDarken`) and grows smoother (`u_wetRoughness`), a puddle goes flat and near-mirror, and a Planar Reflection on the same mesh then shows the scene in the pools alone. Wetness 0 (the default) costs one comparison. wet-ground checks a puddle, a damp patch and dry ground under one sun on both backends.- **Color Grade has split toning, an S-curve and lift.** **Shadow Tint** and **Highlight Tint** multiply the dark and the bright tones (blended by luma between **Tone Low** and **Tone High**), **S-Curve** blends in a smoothstep about the picture's range and **Lift** adds a tinted black level, all on the picture as the display shows it (put the grade after Tonemap). Defaults are white, white, 0 and black, so no existing grade changes. A reference game's neutral-grey shed turned out to be a scene as orange as ours, graded teal in the shadows and lifted toward teal in the blacks; color-grade-split checks the three tones on both backends.- **A captured probe volume drives reflections too.** What a shiny surface inside a capturing `LightProbeVolume` reflects is the capture, box-projected, rather than the outdoor sky: the SDK prefilters each capture into a column of one reflection atlas beside the sky and the frame uses it wherever no baked `ReflectionProbe` is. A mesh now picks its reflection box by its world bounds (its centre, else the smallest box it overlaps), as probes do, so an imported level's walls whose origin sits outside the room still reflect it. A capture face now counts as done only once it is drawn. probe-capture-reflection checks a chrome ball shows the green wall on its right and the magenta one on its left, on both backends.diff --git a/docs/astro/src/content/docs/graphics/lighting.mdx b/docs/astro/src/content/docs/graphics/lighting.mdxindex fb7baf47d..7534483d9 100644--- a/docs/astro/src/content/docs/graphics/lighting.mdx+++ b/docs/astro/src/content/docs/graphics/lighting.mdx@@ -650,6 +650,10 @@ refreshPlanarReflection(app, surface);const capture = planarReflectionStatus(app, surface, cameraEntity);```+The reflection is filtered by the surface's roughness (nine taps, a pixel and a half at the least and wider as the surface gets rougher), so a damp road+shows a soft reflection where a puddle's stays crisp, and a reflected sun or sky hot spot is brought under a soft ceiling of 1.5 by its brightest channel+(a knee from 0.75, hue kept) instead of clipping toward white.+The existing material controls tint, metallic response, Fresnel and roughness.The captured radiance replaces the environment's specular input for this draw;it is not added over an already reflected sky. The roughness filter is a smalldiff --git a/fixtures/scenes/planar-reflection-hot.esscene b/fixtures/scenes/planar-reflection-hot.esscenenew file mode 100644index 000000000..a84c2ec73--- /dev/null+++ b/fixtures/scenes/planar-reflection-hot.esscene@@ -0,0 +1,248 @@+{+ "version": 4,+ "name": "Planar Reflection Hot",+ "entities": [+ {+ "id": 1,+ "name": "Camera",+ "parent": null,+ "children": [],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": 0,+ "y": 3,+ "z": 6+ },+ "rotation": {+ "x": -0.22975292,+ "y": 0,+ "z": 0,+ "w": 0.97324899+ }+ }+ },+ {+ "type": "Camera",+ "data": {+ "projectionType": 1,+ "orthoSize": 6,+ "isActive": true,+ "clearFlags": 3,+ "nearPlane": 0.1,+ "farPlane": 100+ }+ }+ ]+ },+ {+ "id": 2,+ "name": "Mirror",+ "parent": null,+ "children": [],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {+ "rotation": {+ "x": -0.70710678,+ "y": 0,+ "z": 0,+ "w": 0.70710678+ },+ "scale": {+ "x": 7,+ "y": 7,+ "z": 1+ }+ }+ },+ {+ "type": "MeshRenderer",+ "data": {+ "mesh": "/scenes/shadow-quad.esmesh",+ "material": "/scenes/mat-planar/mirror.esmaterial",+ "opaque": true,+ "lit": true,+ "enabled": true+ }+ },+ {+ "type": "PlanarReflection",+ "data": {+ "axis": 2,+ "quality": 1,+ "resolutionScale": 1,+ "maxSize": 512,+ "enabled": true+ }+ }+ ]+ },+ {+ "id": 3,+ "name": "Red",+ "parent": null,+ "children": [],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": -1.3,+ "y": 1,+ "z": 0+ },+ "scale": {+ "x": 0.01,+ "y": 0.01,+ "z": 0.01+ }+ }+ },+ {+ "type": "MeshRenderer",+ "data": {+ "mesh": "builtin:cube",+ "opaque": true,+ "cullBackfaces": true,+ "lit": false,+ "layer": 1,+ "color": {+ "r": 6,+ "g": 0.6,+ "b": 0,+ "a": 1+ }+ }+ }+ ]+ },+ {+ "id": 4,+ "name": "Green",+ "parent": null,+ "children": [],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": 1.3,+ "y": 0.7,+ "z": -0.8+ },+ "scale": {+ "x": 0.007,+ "y": 0.007,+ "z": 0.007+ }+ }+ },+ {+ "type": "MeshRenderer",+ "data": {+ "mesh": "builtin:cube",+ "opaque": true,+ "cullBackfaces": true,+ "lit": false,+ "layer": 2,+ "color": {+ "r": 0,+ "g": 1,+ "b": 0,+ "a": 1+ }+ }+ }+ ]+ },+ {+ "id": 5,+ "name": "Below plane",+ "parent": null,+ "children": [],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": 0,+ "y": -1,+ "z": 0+ },+ "scale": {+ "x": 0.007,+ "y": 0.007,+ "z": 0.007+ }+ }+ },+ {+ "type": "MeshRenderer",+ "data": {+ "mesh": "builtin:cube",+ "opaque": true,+ "lit": false,+ "color": {+ "r": 0,+ "g": 0,+ "b": 1,+ "a": 1+ }+ }+ }+ ]+ },+ {+ "id": 6,+ "name": "Second camera",+ "parent": null,+ "children": [],+ "visible": true,+ "components": [+ {+ "type": "Transform",+ "data": {+ "position": {+ "x": 1.5,+ "y": 3,+ "z": 6+ },+ "rotation": {+ "x": -0.22975292,+ "y": 0,+ "z": 0,+ "w": 0.97324899+ }+ }+ },+ {+ "type": "Camera",+ "data": {+ "projectionType": 1,+ "orthoSize": 6,+ "isActive": false,+ "clearFlags": 3,+ "nearPlane": 0.1,+ "farPlane": 100,+ "priority": 1,+ "viewport": {+ "x": 0.5,+ "y": 0,+ "z": 0.5,+ "w": 1+ }+ }+ }+ ]+ }+ ]+}\ No newline at end of filediff --git a/src/esengine/resource/ShaderParser.cpp b/src/esengine/resource/ShaderParser.cppindex 72bf7b32a..7fd674317 100644--- a/src/esengine/resource/ShaderParser.cpp+++ b/src/esengine/resource/ShaderParser.cpp@@ -2087,12 +2087,26 @@ fn envRadiance(R : vec3f, roughness : f32, column : f32, P : vec3f) -> vec3f {if (any(uv < vec2f(0.0)) || any(uv > vec2f(1.0))) { return vec3f(0.0); }// A small spatial filter, not prefiltered GGX: preserves material roughness// without allocating a second blur chain or claiming an exact BRDF convolution.- let r = max(planar.planarParams.zw, vec2f(roughness * roughness * 0.015));- return (textureSampleLevel(t3, s3, uv, 0.0).rgb * 4.0- + textureSampleLevel(t3, s3, clamp(uv + vec2f(r.x, 0.0), vec2f(0.0), vec2f(1.0)), 0.0).rgb+ // Blurred by how rough the surface is, a pixel and a half at the least (a mirror is never razor sharp at this size) and+ // wider as the roughness grows, so a wet road's reflection softens where a puddle's stays crisp: nine taps, the middle one+ // weighted most. The sideways reach follows the target's shape, so the blur is round on screen.+ let reach = max(planar.planarParams.z * 1.5, roughness * roughness * 0.04);+ let r = vec2f(reach, reach * planar.planarParams.w / planar.planarParams.z);+ let d = r * 0.7071;+ let seen = textureSampleLevel(t3, s3, uv, 0.0).rgb * 0.2+ + (textureSampleLevel(t3, s3, clamp(uv + vec2f(r.x, 0.0), vec2f(0.0), vec2f(1.0)), 0.0).rgb+ textureSampleLevel(t3, s3, clamp(uv - vec2f(r.x, 0.0), vec2f(0.0), vec2f(1.0)), 0.0).rgb+ textureSampleLevel(t3, s3, clamp(uv + vec2f(0.0, r.y), vec2f(0.0), vec2f(1.0)), 0.0).rgb- + textureSampleLevel(t3, s3, clamp(uv - vec2f(0.0, r.y), vec2f(0.0), vec2f(1.0)), 0.0).rgb) * 0.125;+ + textureSampleLevel(t3, s3, clamp(uv - vec2f(0.0, r.y), vec2f(0.0), vec2f(1.0)), 0.0).rgb+ + textureSampleLevel(t3, s3, clamp(uv + d, vec2f(0.0), vec2f(1.0)), 0.0).rgb+ + textureSampleLevel(t3, s3, clamp(uv - d, vec2f(0.0), vec2f(1.0)), 0.0).rgb+ + textureSampleLevel(t3, s3, clamp(uv + vec2f(d.x, -d.y), vec2f(0.0), vec2f(1.0)), 0.0).rgb+ + textureSampleLevel(t3, s3, clamp(uv + vec2f(-d.x, d.y), vec2f(0.0), vec2f(1.0)), 0.0).rgb) * 0.1;+ // A reflected sun or a sky's hot spot is far brighter than any picture holds, and a mirror shows all of it: the colour is+ // brought under a soft ceiling of 1.5 by its brightest channel (a knee from 0.75, so no edge shows where it starts), so a burning orange stays orange instead of clipping toward white.+ let peak = max(seen.r, max(seen.g, seen.b));+ let held = select(0.75 + 0.75 * (1.0 - exp(-(peak - 0.75) / 0.75)), peak, peak <= 0.75);+ return seen * (held / max(peak, 1e-4));}#endifif (lc.u_envParams.x < 0.5) { return ambientFacing(R); }@@ -3579,12 +3593,23 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p" if (clip.w <= 0.0) return vec3(0.0);\n"" highp vec2 uv = clip.xy / clip.w * vec2(0.5, 0.5 * u_planarParams.y) + 0.5;\n"" if (any(lessThan(uv, vec2(0.0))) || any(greaterThan(uv, vec2(1.0)))) return vec3(0.0);\n"- " highp vec2 r = max(u_planarParams.zw, vec2(roughness * roughness * 0.015));\n"- " return (textureLod(u_envMap, uv, 0.0).rgb * 4.0\n"- " + textureLod(u_envMap, clamp(uv + vec2(r.x, 0.0), 0.0, 1.0), 0.0).rgb\n"+ // Blurred by how rough the surface is (see the WGSL twin): a pixel and a half at the least, wider as the roughness grows.+ " highp float reach = max(u_planarParams.z * 1.5, roughness * roughness * 0.04);\n"+ " highp vec2 r = vec2(reach, reach * u_planarParams.w / u_planarParams.z);\n"+ " highp vec2 d = r * 0.7071;\n"+ " highp vec3 seen = textureLod(u_envMap, uv, 0.0).rgb * 0.2\n"+ " + (textureLod(u_envMap, clamp(uv + vec2(r.x, 0.0), 0.0, 1.0), 0.0).rgb\n"" + textureLod(u_envMap, clamp(uv - vec2(r.x, 0.0), 0.0, 1.0), 0.0).rgb\n"" + textureLod(u_envMap, clamp(uv + vec2(0.0, r.y), 0.0, 1.0), 0.0).rgb\n"- " + textureLod(u_envMap, clamp(uv - vec2(0.0, r.y), 0.0, 1.0), 0.0).rgb) * 0.125;\n"+ " + textureLod(u_envMap, clamp(uv - vec2(0.0, r.y), 0.0, 1.0), 0.0).rgb\n"+ " + textureLod(u_envMap, clamp(uv + d, 0.0, 1.0), 0.0).rgb\n"+ " + textureLod(u_envMap, clamp(uv - d, 0.0, 1.0), 0.0).rgb\n"+ " + textureLod(u_envMap, clamp(uv + vec2(d.x, -d.y), 0.0, 1.0), 0.0).rgb\n"+ " + textureLod(u_envMap, clamp(uv + vec2(-d.x, d.y), 0.0, 1.0), 0.0).rgb) * 0.1;\n"+ // See the WGSL twin: a soft ceiling on the brightest channel, so the hue holds and no edge shows.+ " highp float peak = max(seen.r, max(seen.g, seen.b));\n"+ " highp float held = peak <= 0.75 ? peak : 0.75 + 0.75 * (1.0 - exp(-(peak - 0.75) / 0.75));\n"+ " return seen * (held / max(peak, 1e-4));\n"" }\n""#endif\n"" if (u_envParams.x < 0.5) return ambientFacing(R);\n"diff --git a/tools/renderScenes.mjs b/tools/renderScenes.mjsindex 0db30b895..7315d875d 100644--- a/tools/renderScenes.mjs+++ b/tools/renderScenes.mjs@@ -486,6 +486,9 @@ export const SCENES = [// Dynamic captures keep source geometry and the clipped reflection visible together.{"id": "planar-reflection", "tier": "pr", "webgpu": true, "env": {"ESTELLA_VERIFY_SCENE": "/scenes/planar-reflection.esscene", "ESTELLA_VERIFY_W": "512", "ESTELLA_VERIFY_H": "512", "ESTELLA_VERIFY_STEPS": "3", "ESTELLA_VERIFY_EXPECT": "[{\"x\": 0.390625, \"y\": 0.41015625, \"rgb\": [255, 0, 0], \"tol\": 25}, {\"x\": 0.390625, \"y\": 0.56640625, \"rgb\": [255, 0, 0], \"tol\": 25}, {\"x\": 0.60546875, \"y\": 0.5234375, \"rgb\": [0, 255, 0], \"tol\": 25}]", "ESTELLA_VERIFY_COUNTERS": "{\"render.planar.draws\": 1}"}},+ // A mirror shows all of what it sees, and a sun or a hot spot is far past 1: the reflected colour is brought down to a cap by its+ // brightest channel, hue kept. A red cube at (6, 0.6, 0), in a linear frame, is seen directly as 255,203,0 and in the mirror as (1.5, 0.15, 0): 255,108,0 (unclamped it would be 255,203,0 again).+ {"id": "planar-reflection-hot", "tier": "pr", "webgpu": true, "env": {"ESTELLA_VERIFY_SCENE": "/scenes/planar-reflection-hot.esscene", "ESTELLA_VERIFY_W": "512", "ESTELLA_VERIFY_H": "512", "ESTELLA_VERIFY_COLORSPACE": "linear", "ESTELLA_VERIFY_STEPS": "3", "ESTELLA_VERIFY_EXPECT": "[{\"x\": 0.390625, \"y\": 0.41015625, \"rgb\": [255, 203, 0], \"tol\": 25}, {\"x\": 0.390625, \"y\": 0.56640625, \"rgb\": [255, 108, 0], \"tol\": 25}]", "ESTELLA_VERIFY_COUNTERS": "{\"render.planar.draws\": 1}"}},{"id": "planar-reflection-off", "tier": "pr", "webgpu": true, "env": {"ESTELLA_VERIFY_SCENE": "/scenes/planar-reflection.esscene", "ESTELLA_VERIFY_W": "512", "ESTELLA_VERIFY_H": "512", "ESTELLA_VERIFY_STEPS": "3", "ESTELLA_VERIFY_EXPECT": "[{\"x\": 0.390625, \"y\": 0.41015625, \"rgb\": [255, 0, 0], \"tol\": 25}, {\"x\": 0.390625, \"y\": 0.56640625, \"rgb\": [0, 0, 0], \"tol\": 15}]", "ESTELLA_VERIFY_SET_FIELD": "{\"entity\": 2, \"component\": \"PlanarReflection\", \"key\": \"enabled\", \"value\": false, \"steps\": 3}"}},// TWO mirrors under one sky, one inside a ReflectionProbe: the baked column is// red and the sky green, so the picture says which answer each surface took.