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): a Rain effect with wet ground, puddles, ripples and splashes, and reflections that treat puddles as mirrors
yhh9b9114446c8b
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 57672089e..51c9532b8 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -16,6 +16,7 @@ published separately; it ships inside the editor.### Added+- **Rain, wet ground and puddles.** A Rain post-process effect adds falling rain, wet ground, puddles and ripples. `rain` draws three layers of slanted streaks, each at its own distance so a nearer surface hides them and the sky does not; darkens the ground that faces up by `wetness`; lays ripples and splashes where drops land on a grid in the world, so they stay on the ground as the camera moves (ripples need `intensity` above 0: wet ground under no rain is still); and, by `puddles` and `puddleScale`, covers part of the flat ground with standing water that is darker and takes denser, clearer ripples. `ssr` takes the same two numbers and treats those pools as mirrors — flat, far more reflective than dry ground, and sharp — so the pools the rain darkens are the pools that reflect. The clock is `u_time * timeScale + phase`, so a capture can pin it. The rain is a screen-space layer, not particles that collide with the world, and a puddle is flat ground whatever it lies on. Verified in the editor on both backends: streaks light a dark sky and move between two moments, wet ground reads darker, standing water darker still, and puddles change the reflection.- **Auto Exposure and Depth of Field.** Auto Exposure and Depth of Field post-process effects are available. `autoExposure` measures the average log luminance of the picture (leaving out pixels darker than 2^-8, so a black backdrop does not open the exposure to its limit), turns it into the exposure that puts it at middle grey, and moves toward that over time from where the last frame left it — brighter faster than darker, per second, within `minEV` and `maxEV`, with a `bias` in stops. To keep the settled value between frames a post-process shader can now declare a `u_feedback` texture: the engine keeps that pass's last output, at the pass's own size, and binds it there, without shifting the picture the way `u_history` does. `depthOfField` blurs by distance from `focusDistance`, with a sharp slab of `focusRange`, a blur radius that grows with how far a surface is from focus up to `maxBlur`, and a gather that counts a neighbour only where its own blur reaches back, so a sharp object does not bleed into a blurred background. Verified in the editor on both backends: a dim scene brightens (mean 6.9 to 16.5) and a bright one darkens (30.7 to 20.4), a drop in light is first dark and then recovered over real time (5.2 then 15.8), and focusing close takes the squared edge energy down to 21% of the sharp frame.- **Height Fog and Light Shafts.** Height Fog and Light Shafts post-process effects add atmosphere. `heightFog` is exponential height fog with a closed-form integral — no marching — so a valley fills and a hilltop clears, the horizon turns hazy while the zenith stays clear, and looking toward the sun the haze takes the sun's colour; sky pixels run through the same formula with a very long ray, and `skyFog` sets how much haze the sky keeps. `lightShafts` marches each pixel toward the sun's place on screen and counts how much of the way is open sky, leaving rays through whatever stands in front of the sun. The sun's direction is set on the effects (azimuth and elevation), not read from a light. Neither is a shadowed volume: fog is lit evenly. Both run on WebGL2 and WebGPU; with an output transform of None, a floor fully taken by the fog reads exactly the authored colour on both.- **Screen Space Reflections.** A Screen Space Reflections post-process effect mirrors props in floors. Add `ssr` to a `PostProcessVolume`: with no G-buffer, the surface normal is rebuilt from the depth buffer, the reflected ray is marched through the world and projected back to the screen at every step, and a step landing just behind the visible surface is a hit, refined by bisection. The reflection is blended over the pixel by a Schlick Fresnel term — faint face-on, strong at a grazing angle — and blurred by how far the ray travelled, which stands in for roughness. `intensity`, `maxDistance`, `thickness` and `floorBias` are the settings; `floorBias` at 1 lets only up-facing surfaces reflect, since the chain does not know a material's roughness. What is off screen or in the sky is not reflected; the pixel is left as it was. It reads the same depth buffer as ambient occlusion, runs on WebGL2 and WebGPU alike, and its per-pixel start offset is meant to be averaged away by `taa`. Verified in the editor on a lit scene with both backends: the props appear in the floor they stand on and nothing else on screen changes.diff --git a/desktop b/desktopindex c36c46e52..7cf0ce90a 160000--- a/desktop+++ b/desktop@@ -1 +1 @@-Subproject commit c36c46e522035f1c13dd69b5c518c8829c352092+Subproject commit 7cf0ce90aa5d0491b834124ab89b11351bb07d36diff --git a/docs/astro/src/content/docs/graphics/post-processing.mdx b/docs/astro/src/content/docs/graphics/post-processing.mdxindex 1434cb253..a66d3ded7 100644--- a/docs/astro/src/content/docs/graphics/post-processing.mdx+++ b/docs/astro/src/content/docs/graphics/post-processing.mdx@@ -27,6 +27,7 @@ several can stack in one frame at no extra target cost beyond the passes.| `lightShafts` | Sun rays through whatever stands in front of it | `intensity`, `sunAzimuth`, `sunElevation`, `decay`, `reach` || `autoExposure` | Eye adaptation: exposure follows how bright the picture is | `bias`, `speedUp`, `speedDown`, `minEV`, `maxEV` || `depthOfField` | Blur by distance from a focus | `focusDistance`, `focusRange`, `aperture`, `maxBlur` |+| `rain` | Falling rain, wet ground, puddles, ripples and splashes | `intensity`, `wetness`, `wind`, `speed`, `puddles`, `puddleScale` || `ssr` | Screen-space reflections: props mirrored in the floor they stand on | `intensity`, `maxDistance`, `thickness`, `floorBias` || `taa` | Temporal anti-aliasing: edges smoothed over several frames | `blend` || `lensDistortion` | Barrel / pincushion warp | `strength`, `zoom` |@@ -43,6 +44,28 @@ Android, desktop, Steam) runs on. `createWebApp({ msaaSamples })` sizes it, and`fxaa` is the cheaper, blurrier alternative for devices where even one sampleextra is too much.+### Rain, wet ground and puddles++`rain` is a storm in two parts. **Falling rain** is three layers of thin slanted streaks scrolling down the+screen, each standing at its own distance: a streak is hidden by a surface nearer than its layer and drawn+over one farther away, and the sky counts as infinitely far. **Wet ground** is found from the depth buffer —+a surface that faces up, flat enough to hold water — and darkened by `wetness`, because water in the pores takes+the light a dry surface scatters. Drops that land on it leave **ripples** (rings that spread and fade) and+**splashes** (the small quick crown of a drop on ground that is not water), laid out on a grid in the world so they+stay on the ground when the camera moves. Ripples need drops: with `intensity` at 0 the ground is wet and still.++**Puddles** are standing water: `puddles` is how much of the flat ground is under it and `puddleScale` how big the+pools are, a world length. A pool is darker still, takes denser and clearer ripples, and no splashes. Where a puddle+lies is a function of the world position alone, so it does not move with the camera. A puddle is also a *mirror*, and+that is the reflections effect's part: give `ssr` the same `puddles` and `puddleScale` and the pools the rain+darkens are the pools that reflect — flat, whatever the ground under them, and far more reflective than a dry surface,+with a sharp reflection where a dry one is blurred. Wet ground in general is shinier than dry: raise `ssr`'s `intensity`+when it rains.++The clock is the game's. A capture that has to be the same on every run sets `timeScale` to 0 and `phase` to the time it+wants. What this is not: it does not know a material's roughness or the real shape of the ground, so a puddle is+a patch of flat ground whatever it lies on, and the rain is a screen-space layer, not particles that collide with the world.+### Auto exposure`autoExposure` is the eye: it measures how bright the picture is and settles the exposure that putsdiff --git a/docs/astro/src/content/docs/zh-cn/graphics/post-processing.mdx b/docs/astro/src/content/docs/zh-cn/graphics/post-processing.mdxindex 832c96515..b719e169a 100644--- a/docs/astro/src/content/docs/zh-cn/graphics/post-processing.mdx+++ b/docs/astro/src/content/docs/zh-cn/graphics/post-processing.mdx@@ -26,6 +26,7 @@ Estella 通过 ping-pong 帧缓冲运行全屏后处理。加一个 `PostProcess| `lightShafts` | 太阳光穿过前方遮挡物形成的光束 | `intensity`、`sunAzimuth`、`sunElevation`、`decay`、`reach` || `autoExposure` | 眼睛适应:曝光跟随画面亮度 | `bias`、`speedUp`、`speedDown`、`minEV`、`maxEV` || `depthOfField` | 按到焦点的距离模糊 | `focusDistance`、`focusRange`、`aperture`、`maxBlur` |+| `rain` | 下雨、潮湿的地面、水坑、涟漪与水花 | `intensity`、`wetness`、`wind`、`speed`、`puddles`、`puddleScale` || `ssr` | 屏幕空间反射:道具映在它们所站的地面上 | `intensity`、`maxDistance`、`thickness`、`floorBias` || `taa` | 时间性抗锯齿:边缘跨多帧平滑 | `blend` || `lensDistortion` | 桶形 / 枕形畸变 | `strength`、`zoom` |@@ -40,6 +41,21 @@ Estella 通过 ping-pong 帧缓冲运行全屏后处理。加一个 `PostProcess`createWebApp({ msaaSamples })` 调它的大小,`msaaSamples: 1` 在填充率比边缘更要紧的设备上关掉它。`fxaa` 是更便宜也更糊的替代品,留给连多一个采样都吃不下的设备。+### 雨、潮湿的地面与水坑++`rain` 是由两部分组成的暴雨。**落下的雨**是三层在屏幕上向下滚动的细斜线,每层站在自己的距离上:比该层更近的表面会挡住+雨丝,更远的表面则被雨丝盖住,天空算作无限远。**潮湿的地面**由深度缓冲找出——朝上、平得能存水的表面——并按 `wetness`+压暗,因为孔隙里的水吞掉了干燥表面会散射的光。落在上面的雨滴留下**涟漪**(扩散并消退的圆环)和**水花**(落在非水面上的+小而快的冠状溅起),它们排布在世界里的网格上,所以相机移动时仍留在地面上。涟漪需要雨滴:`intensity` 为 0 时地面潮湿但静止。++**水坑**是积水:`puddles` 是多少平地被水覆盖,`puddleScale` 是水坑多大(世界长度)。水坑更暗,涟漪更密更清晰,没有水花。+水坑在哪里只取决于世界位置,所以不会随相机移动。水坑同时是一面**镜子**,这是反射效果的部分:给 `ssr` 同样的 `puddles` 与+`puddleScale`,雨压暗的水坑就是会反射的水坑——不管下面是什么地面都是平的,反射比干燥表面强得多,且清晰,而干燥表面的反射是模糊的。+潮湿的地面整体比干燥的更亮泽:下雨时提高 `ssr` 的 `intensity`。++时钟是游戏的。要在每次运行都相同的截图,把 `timeScale` 设为 0,`phase` 设为想要的时间。它不是什么:它不知道材质的粗糙度或地面真实的形状,+所以水坑是一片平地,不管它落在什么上;雨是屏幕空间的一层,不是会与世界碰撞的粒子。+### 自动曝光`autoExposure` 就是眼睛:它测量画面有多亮,并稳定到把画面放在中灰的曝光上。测量的是亮度**对数**的平均,diff --git a/sdk/src/postprocess/effects.ts b/sdk/src/postprocess/effects.tsindex 2d8ef9187..557407b53 100644--- a/sdk/src/postprocess/effects.ts+++ b/sdk/src/postprocess/effects.ts@@ -302,6 +302,31 @@ registerEffect({],});+registerEffect({+ type: 'rain',+ label: 'Rain',+ factory: () => postProcessEffects.createRain(),+ uniforms: [+ // How much rain falls: how many of the streaks that a layer could hold are there.+ { name: 'u_intensity', label: 'Intensity', min: 0, max: 2, step: 0.01, defaultValue: 1, neutralValue: 0 },+ // How wet the ground is: how far it darkens and how clearly the rings read.+ { name: 'u_wetness', label: 'Wetness', min: 0, max: 1, step: 0.01, defaultValue: 1, neutralValue: 0 },+ // The slant of the fall, and how fast it falls.+ { name: 'u_wind', label: 'Wind', min: -1, max: 1, step: 0.01, defaultValue: 0.15 },+ { name: 'u_speed', label: 'Speed', min: 0.2, max: 6, step: 0.1, defaultValue: 1.6 },+ { name: 'u_streakLength', label: 'Streak Length', min: 0.05, max: 0.9, step: 0.01, defaultValue: 0.3 },+ // A WORLD length: the size of the grid cell each ring lives in.+ { name: 'u_rippleScale', label: 'Ripple Scale', min: 5, max: 400, step: 1, defaultValue: 60 },+ // How much of the flat ground is standing water, and how big the pools are (a WORLD length).+ // The reflections effect draws the same pools when it is given the same two numbers.+ { name: 'u_puddles', label: 'Puddles', min: 0, max: 1, step: 0.01, defaultValue: 0.3, neutralValue: 0 },+ { name: 'u_puddleScale', label: 'Puddle Size', min: 40, max: 2000, step: 10, defaultValue: 350 },+ ],+ colors: [+ { name: 'u_streakColor', label: 'Streak Colour', defaultValue: { r: 0.78, g: 0.82, b: 0.9, a: 1 } },+ ],+});+registerEffect({type: 'ssao',label: 'Ambient Occlusion',@@ -346,6 +371,10 @@ registerEffect({// 1 = only surfaces that face up reflect (wet ground, polished floors); 0 = every surface// reflects by its Fresnel term alone.{ name: 'u_floorBias', label: 'Floor Bias', min: 0, max: 1, step: 0.01, defaultValue: 1 },+ // Standing water: flat, and a mirror. Give it the same two numbers as the Rain effect's puddles+ // and the pools it darkens are the pools that reflect.+ { name: 'u_puddles', label: 'Puddles', min: 0, max: 1, step: 0.01, defaultValue: 0, neutralValue: 0 },+ { name: 'u_puddleScale', label: 'Puddle Size', min: 40, max: 2000, step: 10, defaultValue: 350 },],});diff --git a/sdk/src/postprocess/postProcessEffects.ts b/sdk/src/postprocess/postProcessEffects.tsindex 413466be2..2c539de83 100644--- a/sdk/src/postprocess/postProcessEffects.ts+++ b/sdk/src/postprocess/postProcessEffects.ts@@ -71,6 +71,46 @@ fn worldPerDepth(uv : vec2f, d : f32, P : vec3f) -> f32 {return length(worldFromDepth(uv, d + e2) - P) / abs(e2);}`;+/**+ * Where puddles lie, in both languages: a value-noise field over the ground's world xz, cut at a level the+ * coverage sets. A function of the WORLD position alone, so the rain effect (which darkens a puddle and+ * rings it) and the reflections effect (which makes it a mirror) draw the same puddles by being given the+ * same two numbers, and neither has to tell the other where they are.+ */+const PUDDLE_GLSL = `+float puddleHash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }+float puddleNoise(vec2 p) {+ vec2 i = floor(p);+ vec2 f = fract(p);+ f = f * f * (3.0 - 2.0 * f);+ return mix(mix(puddleHash(i), puddleHash(i + vec2(1.0, 0.0)), f.x),+ mix(puddleHash(i + vec2(0.0, 1.0)), puddleHash(i + vec2(1.0, 1.0)), f.x), f.y);+}+float puddleMask(vec2 xz, float coverage, float scale) {+ if (coverage <= 0.0) return 0.0;+ vec2 p = xz / max(scale, 1.0);+ float n = 0.55 * puddleNoise(p) + 0.3 * puddleNoise(p * 2.3 + 17.0) + 0.15 * puddleNoise(p * 5.1 + 41.0);+ float t = 0.78 - coverage * 0.42;+ return smoothstep(t - 0.03, t + 0.03, n);+}`;++const PUDDLE_WGSL = `+fn puddleHash(p : vec2f) -> f32 { return fract(sin(dot(p, vec2f(127.1, 311.7))) * 43758.5453); }+fn puddleNoise(p : vec2f) -> f32 {+ let i = floor(p);+ var f = fract(p);+ f = f * f * (3.0 - 2.0 * f);+ return mix(mix(puddleHash(i), puddleHash(i + vec2f(1.0, 0.0)), f.x),+ mix(puddleHash(i + vec2f(0.0, 1.0)), puddleHash(i + vec2f(1.0, 1.0)), f.x), f.y);+}+fn puddleMask(xz : vec2f, coverage : f32, scale : f32) -> f32 {+ if (coverage <= 0.0) { return 0.0; }+ let p = xz / max(scale, 1.0);+ let n = 0.55 * puddleNoise(p) + 0.3 * puddleNoise(p * 2.3 + 17.0) + 0.15 * puddleNoise(p * 5.1 + 41.0);+ let t = 0.78 - coverage * 0.42;+ return smoothstep(t - 0.03, t + 0.03, n);+}`;+export const postProcessEffects = {createLutGrade(): ShaderHandle {// True LUT color grading: a 2D-packed 32^3 LUT (1024x32 — 32 slices of@@ -328,6 +368,8 @@ fn project(P : vec3f) -> vec3f {#pragma param u_maxDistance float default(400) range(10,3000)#pragma param u_thickness float default(12) range(1,200)#pragma param u_floorBias float default(1) range(0,1)+#pragma param u_puddles float default(0) range(0,1)+#pragma param u_puddleScale float default(350) range(40,2000)#pragma fragmentprecision highp float;@@ -338,6 +380,7 @@ uniform highp sampler2D u_sceneDepth;out vec4 fragColor;${SSAO_GLSL}+${PUDDLE_GLSL}${GLSL}void main() {@@ -364,10 +407,19 @@ void main() {vec3 V = viewDirection(P);if (dot(N, V) < 0.0) N = -N;+ // Standing water is flat and mirror-like whatever the ground under it is: its normal is+ // straight up, and a still surface reflects far more than the 4% a dry one does.+ float puddle = 0.0;+ if (u_puddles > 0.0) {+ puddle = puddleMask(P.xz, u_puddles, u_puddleScale) * smoothstep(0.86, 0.95, N.y);+ N = normalize(mix(N, vec3(0.0, 1.0, 0.0), puddle));+ }+// Fresnel at this angle, Schlick with the dielectric 4% a surface without a metal in// it reflects face-on; and the floor mask that keeps walls from shining.float cosT = clamp(dot(N, V), 0.0, 1.0);float fresnel = 0.04 + 0.96 * pow(1.0 - cosT, 5.0);+ fresnel = mix(fresnel, max(fresnel, 0.55), puddle);float up = mix(1.0, smoothstep(0.55, 0.9, N.y), u_floorBias);float weight = fresnel * up * u_intensity;if (weight < 0.002) { fragColor = scene; return; }@@ -411,7 +463,7 @@ void main() {vec2 e = min(sh.xy, 1.0 - sh.xy);float edge = smoothstep(0.0, 0.1, min(e.x, e.y));// A farther hit is a rougher reflection: blur it by how far it is.- float blur = (length(b - P) / max(u_maxDistance, 1.0)) * 6.0;+ float blur = (length(b - P) / max(u_maxDistance, 1.0)) * 6.0 * (1.0 - 0.85 * puddle);vec2 texel = 1.0 / vec2(textureSize(u_texture, 0));vec3 col = texture(u_texture, sh.xy).rgb * 0.4;col += texture(u_texture, sh.xy + vec2( blur, 0.0) * texel).rgb * 0.15;@@ -431,6 +483,7 @@ void main() {#pragma fragment wgsl${SSAO_WGSL}+${PUDDLE_WGSL}${WGSL}@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {@@ -455,8 +508,15 @@ ${WGSL}let V = viewDirection(P);if (dot(N, V) < 0.0) { N = -N; }+ var puddle = 0.0;+ if (mc.u_puddles > 0.0) {+ puddle = puddleMask(P.xz, mc.u_puddles, mc.u_puddleScale) * smoothstep(0.86, 0.95, N.y);+ N = normalize(mix(N, vec3f(0.0, 1.0, 0.0), puddle));+ }+let cosT = clamp(dot(N, V), 0.0, 1.0);- let fresnel = 0.04 + 0.96 * pow(1.0 - cosT, 5.0);+ var fresnel = 0.04 + 0.96 * pow(1.0 - cosT, 5.0);+ fresnel = mix(fresnel, max(fresnel, 0.55), puddle);let up = mix(1.0, smoothstep(0.55, 0.9, N.y), mc.u_floorBias);let weight = fresnel * up * mc.u_intensity;if (weight < 0.002) { return scene; }@@ -496,7 +556,7 @@ ${WGSL}let e = min(sh.xy, vec2f(1.0) - sh.xy);let edge = smoothstep(0.0, 0.1, min(e.x, e.y));let travelled = length(b - P);- let blur = (travelled / max(mc.u_maxDistance, 1.0)) * 6.0;+ let blur = (travelled / max(mc.u_maxDistance, 1.0)) * 6.0 * (1.0 - 0.85 * puddle);let texel = 1.0 / vec2f(vec2i(textureDimensions(t0, 0)));var col = textureSampleLevel(t0, s0, sh.xy, 0.0).rgb * 0.4;col = col + textureSampleLevel(t0, s0, sh.xy + vec2f( blur, 0.0) * texel, 0.0).rgb * 0.15;@@ -1722,6 +1782,235 @@ ${WGSL}return Material.compileShader(source);},+ /**+ * Rain, in two parts that a real storm has. Falling streaks: three layers of thin slanted lines+ * scrolling down the screen, each standing at a different distance, so a streak is hidden by a+ * surface nearer than the layer it belongs to and drawn over one farther away — the sky counts as+ * infinitely far. And a wet ground: a surface that faces up is darkened, and rings spread over it+ * where drops land, laid out on a grid in the WORLD so they stay on the ground when the camera moves.+ *+ * The clock is `u_time.x * u_timeScale + u_phase`: left alone it runs with the game, and a capture that+ * must be the same on every run sets the scale to 0 and the phase to the time it wants.+ *+ * Wet surfaces are also shinier than dry ones, which is not done here: raise \`ssr\` beside it.+ */+ createRain(): ShaderHandle {+ const GLSL = `+float hash11(float n) { return fract(sin(n * 127.1) * 43758.5453); }+float hash21(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }++/// One layer of streaks: columns across the screen, one streak per cell at most, scrolling down.+float streaks(vec2 uv, float aspect, float cols, float speed, float seed, float T) {+ vec2 q = vec2(uv.x * aspect - uv.y * u_wind, uv.y);+ float cx = floor(q.x * cols);+ float fx = fract(q.x * cols);+ float h1 = hash11(cx + seed);+ float h2 = hash11(cx * 1.7 + seed * 3.1);+ // GL rows run up the screen, so a pattern that falls moves toward lower rows.+ float y = q.y * 4.0 + T * speed * (0.8 + 0.4 * h1) + h2 * 17.0;+ float cell = floor(y);+ float fy = fract(y);+ float here = hash11(cx * 31.0 + cell + seed);+ if (here > u_intensity * 0.5) return 0.0;+ float dx = abs(fx - 0.5 - (hash11(cell + cx) - 0.5) * 0.6);+ float across = 1.0 - smoothstep(0.0, 0.06, dx);+ float along = (1.0 - fy / u_streakLength) * step(fy, u_streakLength);+ return across * along;+}`;+ const WGSL = `+fn hash11(n : f32) -> f32 { return fract(sin(n * 127.1) * 43758.5453); }+fn hash21(p : vec2f) -> f32 { return fract(sin(dot(p, vec2f(127.1, 311.7))) * 43758.5453); }++fn streaks(uv : vec2f, aspect : f32, cols : f32, speed : f32, seed : f32, T : f32) -> f32 {+ let q = vec2f(uv.x * aspect - (1.0 - uv.y) * mc.u_wind, 1.0 - uv.y);+ let cx = floor(q.x * cols);+ let fx = fract(q.x * cols);+ let h1 = hash11(cx + seed);+ let h2 = hash11(cx * 1.7 + seed * 3.1);+ // Here uv runs DOWN the screen and q is flipped back to run up, as in GL, so the same+ // falling motion is the same sign.+ let y = q.y * 4.0 + T * speed * (0.8 + 0.4 * h1) + h2 * 17.0;+ let cell = floor(y);+ let fy = fract(y);+ let here = hash11(cx * 31.0 + cell + seed);+ if (here > mc.u_intensity * 0.5) { return 0.0; }+ let dx = abs(fx - 0.5 - (hash11(cell + cx) - 0.5) * 0.6);+ let across = 1.0 - smoothstep(0.0, 0.06, dx);+ var along = 0.0;+ if (fy <= mc.u_streakLength) { along = 1.0 - fy / mc.u_streakLength; }+ return across * along;+}`;+ const source = `#pragma shader "PP Rain"+#pragma version 300 es+#pragma domain PostProcess+#pragma param u_intensity float default(1) range(0,2)+#pragma param u_wetness float default(1) range(0,1)+#pragma param u_wind float default(0.15) range(-1,1)+#pragma param u_speed float default(1.6) range(0.2,6)+#pragma param u_streakLength float default(0.3) range(0.05,0.9)+#pragma param u_rippleScale float default(60) range(5,400)+#pragma param u_puddles float default(0.3) range(0,1)+#pragma param u_puddleScale float default(350) range(40,2000)+#pragma param u_streakColor color default(0.78,0.82,0.9,1)+#pragma param u_timeScale float default(1) range(0,1)+#pragma param u_phase float default(0) range(0,100000)++#pragma fragment+precision highp float;++in vec2 v_texCoord;+uniform sampler2D u_texture;+uniform highp sampler2D u_sceneDepth;+out vec4 fragColor;++${SSAO_GLSL}+${PUDDLE_GLSL}+${GLSL}++void main() {+ vec4 src = texture(u_texture, v_texCoord);+ vec2 sz = vec2(textureSize(u_texture, 0));+ float aspect = sz.x / sz.y;+ float T = u_time.x * u_timeScale + u_phase;+ ivec2 size = textureSize(u_sceneDepth, 0);+ ivec2 c = screenTexel(v_texCoord, size);+ float d = depthAt(c, size);+ vec3 entry = worldFromDepth(v_texCoord, 0.0);+ // How far the surface here is, measured along the same ray as the streak layers are.+ float dist = d >= 1.0 ? 1e9 : length(worldFromDepth(uvOfTexel(c, size), d) - entry);++ vec3 tint = u_streakColor.rgb;+#ifdef ES_LINEAR+ tint = srgbToLinear(tint);+#endif+ vec3 col = src.rgb;++ if (d < 1.0 && u_wetness > 0.0) {+ vec3 P = worldFromDepth(uvOfTexel(c, size), d);+ float dl = depthAt(c + ivec2(-1, 0), size);+ float dr = depthAt(c + ivec2( 1, 0), size);+ float dd = depthAt(c + ivec2(0, -1), size);+ float du = depthAt(c + ivec2(0, 1), size);+ vec3 ax = abs(dr - d) < abs(dl - d) ? worldAt(c + ivec2(1, 0), size) - P+ : P - worldAt(c + ivec2(-1, 0), size);+ vec3 ay = abs(du - d) < abs(dd - d) ? worldAt(c + ivec2(0, 1), size) - P+ : P - worldAt(c + ivec2(0, -1), size);+ vec3 N = normalize(cross(ax, ay));+ if (dot(N, viewDirection(P)) < 0.0) N = -N;+ float up = smoothstep(0.86, 0.95, N.y);+ // A wet surface is darker, because water in the pores takes the light a dry one scatters;+ // standing water is darker still.+ float puddle = puddleMask(P.xz, u_puddles, u_puddleScale) * up * u_wetness;+ col *= 1.0 - 0.32 * u_wetness * up;+ col *= 1.0 - 0.35 * puddle;+ // Drops landing need drops falling: wet ground under no rain is still.+ float falling = clamp(u_intensity, 0.0, 1.0);+ float near = 1.0 - smoothstep(1200.0, 3500.0, dist);+ // Rings where drops land, on a grid in the world; they are denser and clearer on standing water.+ vec2 g = P.xz / u_rippleScale;+ vec2 id = floor(g);+ vec2 f = fract(g) - 0.5;+ float h = hash21(id);+ vec2 centre = (vec2(hash21(id + 7.0), hash21(id + 13.0)) - 0.5) * 0.5;+ float phase = fract(T * 0.9 + h);+ float ring = (1.0 - smoothstep(0.0, 0.035, abs(length(f - centre) - phase * 0.5)))+ * (1.0 - phase) * (1.0 - phase) * step(hash21(id + 3.0), mix(0.55, 0.9, puddle));+ col += tint * ring * mix(0.4, 0.9, puddle) * u_wetness * up * near * falling;+ // Splashes: the small, quick crown of a drop that lands on ground that is not water.+ vec2 g2 = P.xz / (u_rippleScale * 0.3);+ vec2 id2 = floor(g2);+ vec2 f2 = fract(g2) - 0.5;+ float ph2 = fract(T * 2.4 + hash21(id2 + 29.0));+ vec2 c2 = (vec2(hash21(id2 + 5.0), hash21(id2 + 11.0)) - 0.5) * 0.4;+ float splash = (1.0 - smoothstep(0.0, 0.06, abs(length(f2 - c2) - ph2 * 0.3)))+ * (1.0 - ph2) * (1.0 - ph2) * (1.0 - ph2) * step(hash21(id2 + 2.0), 0.5);+ col += tint * splash * 0.55 * u_wetness * up * near * falling * (1.0 - puddle);+ }++ // Three layers, nearest last: a layer is drawn only over what is farther than it.+ float rain = 0.0;+ if (dist > 120.0) rain += streaks(v_texCoord, aspect, 70.0, u_speed * 1.0, 1.0, T) * 0.35;+ if (dist > 450.0) rain += streaks(v_texCoord, aspect, 130.0, u_speed * 1.4, 2.0, T) * 0.28;+ if (dist > 1400.0) rain += streaks(v_texCoord, aspect, 230.0, u_speed * 1.9, 3.0, T) * 0.22;+ col += tint * rain * u_intensity;+ fragColor = vec4(col, src.a);+}+#pragma end++#pragma fragment wgsl+${SSAO_WGSL}+${PUDDLE_WGSL}+${WGSL}++@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {+ let src = textureSampleLevel(t0, s0, v.v_texCoord, 0.0);+ let sz = vec2f(vec2i(textureDimensions(t0, 0)));+ let aspect = sz.x / sz.y;+ let T = tc.u_time.x * mc.u_timeScale + mc.u_phase;+ let size = vec2i(textureDimensions(t7, 0));+ let c = screenTexel(v.v_texCoord, size);+ let d = depthAt(c, size);+ let entry = worldFromDepth(v.v_texCoord, 0.0);+ var dist = 1e9;+ if (d < 1.0) { dist = length(worldFromDepth(uvOfTexel(c, size), d) - entry); }++ var tint = mc.u_streakColor.rgb;+#ifdef ES_LINEAR+ tint = srgbToLinear(tint);+#endif+ var col = src.rgb;++ if (d < 1.0 && mc.u_wetness > 0.0) {+ let P = worldFromDepth(uvOfTexel(c, size), d);+ let dl = depthAt(c + vec2i(-1, 0), size);+ let dr = depthAt(c + vec2i( 1, 0), size);+ let dd = depthAt(c + vec2i(0, -1), size);+ let du = depthAt(c + vec2i(0, 1), size);+ var ax = P - worldAt(c + vec2i(-1, 0), size);+ if (abs(dr - d) < abs(dl - d)) { ax = worldAt(c + vec2i(1, 0), size) - P; }+ var ay = P - worldAt(c + vec2i(0, -1), size);+ if (abs(du - d) < abs(dd - d)) { ay = worldAt(c + vec2i(0, 1), size) - P; }+ var N = normalize(cross(ax, ay));+ if (dot(N, viewDirection(P)) < 0.0) { N = -N; }+ let up = smoothstep(0.86, 0.95, N.y);+ let puddle = puddleMask(P.xz, mc.u_puddles, mc.u_puddleScale) * up * mc.u_wetness;+ col = col * (1.0 - 0.32 * mc.u_wetness * up);+ col = col * (1.0 - 0.35 * puddle);+ let falling = clamp(mc.u_intensity, 0.0, 1.0);+ let near = 1.0 - smoothstep(1200.0, 3500.0, dist);+ let g = P.xz / mc.u_rippleScale;+ let id = floor(g);+ let f = fract(g) - 0.5;+ let h = hash21(id);+ let centre = (vec2f(hash21(id + 7.0), hash21(id + 13.0)) - 0.5) * 0.5;+ let phase = fract(T * 0.9 + h);+ var ring = (1.0 - smoothstep(0.0, 0.035, abs(length(f - centre) - phase * 0.5)))+ * (1.0 - phase) * (1.0 - phase);+ if (hash21(id + 3.0) > mix(0.55, 0.9, puddle)) { ring = 0.0; }+ col = col + tint * ring * mix(0.4, 0.9, puddle) * mc.u_wetness * up * near * falling;+ let g2 = P.xz / (mc.u_rippleScale * 0.3);+ let id2 = floor(g2);+ let f2 = fract(g2) - 0.5;+ let ph2 = fract(T * 2.4 + hash21(id2 + 29.0));+ let c2 = (vec2f(hash21(id2 + 5.0), hash21(id2 + 11.0)) - 0.5) * 0.4;+ var splash = (1.0 - smoothstep(0.0, 0.06, abs(length(f2 - c2) - ph2 * 0.3)))+ * (1.0 - ph2) * (1.0 - ph2) * (1.0 - ph2);+ if (hash21(id2 + 2.0) > 0.5) { splash = 0.0; }+ col = col + tint * splash * 0.55 * mc.u_wetness * up * near * falling * (1.0 - puddle);+ }++ var rain = 0.0;+ if (dist > 120.0) { rain = rain + streaks(v.v_texCoord, aspect, 70.0, mc.u_speed * 1.0, 1.0, T) * 0.35; }+ if (dist > 450.0) { rain = rain + streaks(v.v_texCoord, aspect, 130.0, mc.u_speed * 1.4, 2.0, T) * 0.28; }+ if (dist > 1400.0) { rain = rain + streaks(v.v_texCoord, aspect, 230.0, mc.u_speed * 1.9, 3.0, T) * 0.22; }+ col = col + tint * rain * mc.u_intensity;+ return vec4f(col, src.a);+}+#pragma end+`;+ return Material.compileShader(source);+ },+createDistanceFog(): ShaderHandle {// Fog is a DISTANCE and a depth sample is not one: the same 0.9 is a// metre away up close and a hundred out. `worldFromDepth` (injected withdiff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 6ef2d73e0..c5bd2748b 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -2479,4 +2479,6 @@ export const NOTED = {{"note":"Height Fog and Light Shafts post-process effects add atmosphere."},"feat(render): Auto Exposure and Depth of Field effects, and a pass that remembers its last output without shifting the picture":{"note":"Auto Exposure and Depth of Field post-process effects are available."},+ "feat(render): a Rain effect with wet ground, puddles, ripples and splashes, and reflections that treat puddles as mirrors":+ {"note":"A Rain post-process effect adds falling rain, wet ground, puddles and ripples."},};