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.

perf(postprocess): the sky's clouds and the fog's shafts are worked out at half size and read back by depth

yhh7b715c39ea6f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 346cf2276..2499fe575 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -30,6 +30,7 @@ published separately; it ships inside the editor.
### Changed
+- **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.
- **A lit surface takes its shadow in fewer operations.** The shadow lookup of a light with a size (the sun's penumbra) worked a `sin`, a `cos` and a `sqrt` for each of its forty-eight taps and a whole matrix for each of the sixteen that look for a blocker; the taps are a table now, turned once a pixel, and the matrix is applied to the light's direction once. Inside a shadow, where every blocker tap hits, it returns at once, and a penumbra a texel or two wide is averaged over 8 or 16 taps, not 32. The picture is the same to the pixel gates on WebGL2 and WebGPU; a frame of the Terrain & Water example is 3 to 10 ms shorter on an RTX 3060.
diff --git a/docs/GPU_PERFORMANCE_OPTIONS.md b/docs/GPU_PERFORMANCE_OPTIONS.md
index 924d11980..ef55ff67f 100644
--- a/docs/GPU_PERFORMANCE_OPTIONS.md
+++ b/docs/GPU_PERFORMANCE_OPTIONS.md
@@ -10,7 +10,10 @@
## 按证据排序
-### 1. 天空大气和高度雾:LUT 化、低分辨率、跨帧复用 —— 现在最大的一块
+### 1. 天空大气和高度雾:LUT 化、低分辨率、跨帧复用 —— 半分辨率这一步已落地(2026-10-26)
+
+**已落地:**逐 pass 读数说明这两块的成本几乎全是云的步进、云影和云光柱,不是大气散射本身;它们现在在半分辨率算、按深度读回,默认机位整帧 25.6 → 20.1 ms,见 `docs/TERRAIN_WATER_GPU_COST.md` 的“天空与雾的半分辨率”。下面的 LUT 化只剩天空大气的散射(0.86 ms)和雾的解析部分(1.25 ms),**不再值得做**;真正还大的是 `scene` 本身。
+
**证据:**所有优化之后,默认机位 24.8 ms 的帧里,后处理链占 12.4 ms,其中天空大气 7.9 ms、高度雾 5.1 ms(沿河机位:7.2 与 5.1);两者不是相加的关系,但合起来是帧的一半以上。它们各是一个全分辨率的单 pass。
diff --git a/docs/ROADMAP_BLOCKED_2026-10-16.md b/docs/ROADMAP_BLOCKED_2026-10-16.md
index 7e3d6fc9a..ab91d7208 100644
--- a/docs/ROADMAP_BLOCKED_2026-10-16.md
+++ b/docs/ROADMAP_BLOCKED_2026-10-16.md
@@ -82,3 +82,93 @@ Project read still fails before authentication: GitHub CLI/config.yml access den
Project read remains blocked before authentication: access denied to GitHub CLI/config.yml (exit 1). Latest drafts, releases and acceptance evidence were not obtained. No new pending requirements; no changes to remote cards, snapshots or engine code. Same reported sandbox blocker; no repeated authorization request or bypass. Local log remains uncommitted under the read-only .git restriction.
+
+## 2026-10-21 12:08 UTC recheck
+
+Project read still fails before authentication: GitHub CLI/config.yml access denied (exit 1). No latest drafts, releases or acceptance evidence obtained. No new pending requirements; remote cards, snapshots and engine code remain untouched. Same reported sandbox blocker; no repeated authorization request or bypass. Local log remains uncommitted under the read-only .git restriction.
+
+
+## 2026-10-21 19:06 UTC recheck
+
+Project read still fails before authentication: GitHub CLI/config.yml access denied (exit 1). Latest drafts, releases and acceptance evidence were not obtained. No new pending requirements; remote cards, snapshots and engine code remain untouched. Same reported sandbox blocker; no repeated authorization request or bypass. Local log remains uncommitted under the read-only .git restriction.
+
+
+## 2026-10-22 02:07 UTC recheck
+
+Project read remains blocked before authentication: access denied to GitHub CLI/config.yml (exit 1). No latest drafts, releases or acceptance evidence obtained. No new pending requirements; remote cards, snapshots and engine code remain untouched. Same reported sandbox blocker; no repeated authorization request or bypass. Local log remains uncommitted under the read-only .git restriction.
+
+
+## 2026-10-22 09:05 UTC recheck
+
+Project read still fails before authentication: GitHub CLI/config.yml access denied (exit 1). No latest drafts, releases or acceptance evidence obtained. No new pending requirements; remote cards, snapshots and engine code remain untouched. Same reported sandbox blocker; no repeated authorization request or bypass. Local log remains uncommitted under the read-only .git restriction.
+
+
+## 2026-10-22 16:05 UTC recheck
+
+Project read remains blocked before authentication: GitHub CLI/config.yml access denied (exit 1). No latest drafts, releases or acceptance evidence obtained. No new pending requirements; remote cards, snapshots and engine code remain untouched. Same reported sandbox blocker; no repeated authorization request or bypass. Local log remains uncommitted under the read-only .git restriction.
+
+
+## 2026-10-22 23:02 UTC recheck
+
+Project read still fails before authentication: GitHub CLI/config.yml access denied (exit 1). No latest drafts, releases or acceptance evidence obtained. No new pending requirements; remote cards, snapshots and engine code remain untouched. Same reported sandbox blocker; no repeated authorization request or bypass. Local log remains uncommitted under the read-only .git restriction.
+
+
+## 2026-10-23 06:05 UTC recheck
+
+Project read remains blocked before authentication: GitHub CLI/config.yml access denied (exit 1). No latest drafts, releases or acceptance evidence obtained. No new pending requirements; remote cards, snapshots and engine code remain untouched. Same reported sandbox blocker; no repeated authorization request or bypass. Local log remains uncommitted under the read-only .git restriction.
+
+
+## 2026-10-23 13:02 UTC recheck
+
+Project read remains blocked before authentication: GitHub CLI/config.yml access denied (exit 1). No latest drafts, releases or acceptance evidence obtained. No new pending requirements; remote cards, snapshots and engine code remain untouched. Same reported sandbox blocker; no repeated authorization request or bypass. Local log remains uncommitted under the read-only .git restriction.
+
+
+## 2026-10-23 20:05 UTC recheck
+
+Project read remains blocked before authentication: GitHub CLI/config.yml access denied (exit 1). Latest drafts and user changes could not be read; no fresh release or acceptance verification performed. No new pending requirements or remote changes. Same sandbox blocker; no authorization retry or bypass. Engine code untouched.
+
+
+## 2026-10-24 03:02 UTC recheck
+
+Project read remains blocked before authentication: GitHub CLI/config.yml access denied (exit 1). Latest drafts and user changes could not be read; no fresh release or acceptance verification performed. No new pending requirements or remote changes. Same sandbox blocker; no authorization retry or bypass. Engine code untouched.
+
+
+## 2026-10-24 10:02 UTC recheck
+
+Project read remains blocked before authentication: GitHub CLI/config.yml access denied (exit 1). Latest drafts and user changes could not be read; no fresh release or acceptance verification performed. No new pending requirements or remote changes. Same sandbox blocker; no authorization retry or bypass. Engine code untouched.
+
+
+## 2026-10-24 17:01 UTC recheck
+
+Project read remains blocked before authentication: GitHub CLI/config.yml access denied (exit 1). Latest drafts and user changes could not be read; no fresh release or acceptance verification performed. No new pending requirements or remote changes. Same sandbox blocker; no authorization retry or bypass. Engine code untouched.
+
+
+## 2026-10-25 00:02 UTC recheck
+
+Project read remains blocked before authentication: GitHub CLI/config.yml access denied (exit 1). Latest drafts and user changes could not be read; no fresh release or acceptance verification performed. No new pending requirements or remote changes. Same sandbox blocker; no authorization retry or bypass. Engine code untouched.
+
+
+## 2026-10-25 07:02 UTC recheck
+
+Project read remains blocked before authentication: GitHub CLI/config.yml access denied (exit 1). Latest drafts and user changes could not be read; no fresh release or acceptance verification performed. No new pending requirements or remote changes. Same sandbox blocker; no authorization retry or bypass. Engine code untouched.
+
+
+## 2026-10-25 13:57 UTC recheck
+
+Project read remains blocked before authentication: GitHub CLI/config.yml access denied (exit 1). Latest drafts and user changes could not be read; no fresh release or acceptance verification performed. No new pending requirements or remote changes. Same sandbox blocker; no authorization retry or bypass. Engine code untouched.
+
+
+## 2026-10-25 20:57 UTC recheck
+
+Project read remains blocked before authentication: GitHub CLI/config.yml access denied (exit 1). Latest drafts and user changes could not be read; no fresh release or acceptance verification performed. No new pending requirements or remote changes. Same sandbox blocker; no authorization retry or bypass. Engine code untouched.
+
+
+## 2026-10-26 04:02 UTC recheck
+
+Project read remains blocked before authentication: GitHub CLI/config.yml access denied (exit 1). Latest drafts and user changes could not be read; no fresh release or acceptance verification performed. No new pending requirements or remote changes. Same sandbox blocker; no authorization retry or bypass. Engine code untouched.
+
+
+## 2026-10-26 11:06 UTC recheck
+
+Project read remains blocked before authentication: GitHub CLI/config.yml access denied (exit 1). Latest drafts and user changes could not be read; no fresh release or acceptance verification performed. No new pending requirements or remote changes. Same sandbox blocker; no authorization retry or bypass. Engine code untouched.
+
diff --git a/docs/TERRAIN_WATER_GPU_COST.md b/docs/TERRAIN_WATER_GPU_COST.md
index 34ecc303f..9028a2833 100644
--- a/docs/TERRAIN_WATER_GPU_COST.md
+++ b/docs/TERRAIN_WATER_GPU_COST.md
@@ -369,3 +369,17 @@
- 不透明场景加预通道是帧的 40% 到 50%,随机位涨到 16 到 18 ms;阴影图在机位 1/3 上是 4.2 到 4.3 ms,机位 0 上只有 0.4。
- 镜头光晕(`lensFlare`)0.75 ms 是固定的,没有人想到过它。
- 其余全屏 pass(bloom、分级、FXAA、曝光、gtao 的模糊)加起来不到 1.5 ms:合并它们(选项第 7 项)的上限很低。
+
+### 天空与雾的半分辨率(2026-10-26)
+
+先量清成本落在哪(`--passes`,机位 0):天空的 4.1 ms 里,云的步进加地面上的云影占了 3.2 ms(关掉云,天空只剩 0.86 ms);雾的 5.0 ms 里,云的光柱(`u_cloudShade`)占 3.7 ms,其余的体积光加解析雾只有 1.25 ms。也就是说“天空和雾”的成本几乎全是云和云的光柱,而且它们是平滑的、只随方向或世界位置变。(之前的消融把雾的 3.7 ms 记在了天空头上,因为关云会同时关掉云光柱。)
+
+做法:云层(`colour * T + L` 的映射)、地面上的云影、雾里太阳穿过雾的光(体积光)各自在半分辨率的 pass 里算,全分辨率的合成 pass 用双线性权重乘以深度近度(云影与雾按世界点/射线长度的近度加权,天空按有无天空纹素)读回;读不到的像素(天际线缝隙)天空与云影回到逐像素的原算法,雾不回退(平滑权重总有答案——一个叶缘像素回退会让整个 warp 都走一遍循环,第一版就是这样没省到)。
+
+| 机位 | 之前整图 | 之后整图 | 天空 | 雾 |
+|---|---|---|---|---|
+| 0 | 25.6 | 20.1 | 4.1 → 0.8 + 1.5 | 4.9 → 1.25 + 1.3 |
+| 1 | 35.3 | 29.9 | 天空 1.1 + 1.5 | 雾 1.3 + 1.3 |
+| 3 | 38.7 | 33.4 | 天空 1.0 + 1.5 | 雾 1.3 + 1.3 |
+
+画面:机位 0 与之前的截图逐像素比(排除两次运行本来就不同的部分),平均差 0.34/255,超过 24 的像素 0.05%,天际线和树缘放大看不出光晕。两个后端:GLSL 与 WGSL 都有,`sky-fog` 渲染场景在 WebGL2 与 WebGPU 上读出同样的颜色。**没有量的:**手机(半分辨率的 pass 在分块 GPU 上多一次读写),夜晚与阴天的云。
diff --git a/fixtures/scenes/sky-fog.esscene b/fixtures/scenes/sky-fog.esscene
new file mode 100644
index 000000000..1ebe786bf
--- /dev/null
+++ b/fixtures/scenes/sky-fog.esscene
@@ -0,0 +1,188 @@
+{
+ "version": 4,
+ "name": "SkyFog",
+ "entities": [
+ {
+ "id": 0,
+ "name": "Camera",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": 0,
+ "z": 300
+ },
+ "rotation": {
+ "x": -0.17,
+ "y": 0,
+ "z": 0,
+ "w": 0.985
+ }
+ }
+ },
+ {
+ "type": "Camera",
+ "data": {
+ "projectionType": 0,
+ "fov": 90,
+ "nearPlane": 0.1,
+ "farPlane": 1000,
+ "isActive": true,
+ "priority": 0
+ }
+ },
+ {
+ "type": "PostProcessVolume",
+ "data": {
+ "isGlobal": true,
+ "priority": 0,
+ "weight": 1,
+ "effects": [
+ {
+ "type": "skyAtmosphere",
+ "enabled": true,
+ "uniforms": {
+ "u_clouds": 0.6,
+ "u_cloudShadow": 0.5,
+ "u_cloudBase": 1.0,
+ "u_cloudThickness": 1.0
+ }
+ },
+ {
+ "type": "heightFog",
+ "enabled": true,
+ "uniforms": {
+ "u_density": 0.004,
+ "u_heightFalloff": 0.0,
+ "u_skyFog": 0.3
+ }
+ }
+ ]
+ }
+ }
+ ]
+ },
+ {
+ "id": 1,
+ "name": "Far",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": -60,
+ "y": 0,
+ "z": 0
+ },
+ "scale": {
+ "x": 0.15,
+ "y": 0.15,
+ "z": 1
+ }
+ }
+ },
+ {
+ "type": "MeshRenderer",
+ "data": {
+ "color": {
+ "r": 0.8,
+ "g": 0.2,
+ "b": 0.2,
+ "a": 1
+ },
+ "layer": 0,
+ "enabled": true,
+ "opaque": true,
+ "mesh": "/scenes/textured-quad.esmesh"
+ }
+ }
+ ]
+ },
+ {
+ "id": 2,
+ "name": "Near",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 60,
+ "y": 0,
+ "z": 120
+ },
+ "scale": {
+ "x": 0.15,
+ "y": 0.15,
+ "z": 1
+ }
+ }
+ },
+ {
+ "type": "MeshRenderer",
+ "data": {
+ "color": {
+ "r": 0.8,
+ "g": 0.2,
+ "b": 0.2,
+ "a": 1
+ },
+ "layer": 0,
+ "enabled": true,
+ "opaque": true,
+ "mesh": "/scenes/textured-quad.esmesh"
+ }
+ }
+ ]
+ },
+ {
+ "id": 3,
+ "name": "Wall",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": 0,
+ "z": -150
+ },
+ "scale": {
+ "x": 5,
+ "y": 5,
+ "z": 1
+ }
+ }
+ },
+ {
+ "type": "MeshRenderer",
+ "data": {
+ "color": {
+ "r": 0.2,
+ "g": 0.7,
+ "b": 0.3,
+ "a": 1
+ },
+ "layer": 0,
+ "enabled": true,
+ "opaque": true,
+ "mesh": "/scenes/textured-quad.esmesh"
+ }
+ }
+ ]
+ }
+ ]
+}
\ No newline at end of file
diff --git a/sdk/src/postprocess/effects.ts b/sdk/src/postprocess/effects.ts
index c54b87bb5..2cf4bfd01 100644
--- a/sdk/src/postprocess/effects.ts
+++ b/sdk/src/postprocess/effects.ts
@@ -298,7 +298,13 @@ registerEffect({
registerEffect({
type: 'skyAtmosphere',
label: 'Sky Atmosphere',
- factory: () => postProcessEffects.createSkyAtmosphere(),
+ factory: () => postProcessEffects.createSkyClouds(),
+ // The clouds (and the shadow they cast on the ground) are smooth and dear, so they are worked out at half size and the composite
+ // reads them back by the way a pixel's neighbours agree with it; the dome, sun, moon and stars, which have an edge, stay full size.
+ multiPass: [
+ { name: 'sky_clouds', factory: () => postProcessEffects.createSkyClouds(), scale: 0.5 },
+ { name: 'sky_composite', factory: () => postProcessEffects.createSkyAtmosphere() },
+ ],
uniforms: [
// On a planet the sky is of the ground the camera stands on: the radius, above 0, turns that on, and the centre is where the planet stands.
{ name: 'u_planetRadius', label: 'Planet Radius', min: 0, max: 100000000, step: 100, defaultValue: 0, neutralValue: 0 },
@@ -400,7 +406,13 @@ registerEffect({
registerEffect({
type: 'heightFog',
label: 'Height Fog',
- factory: () => postProcessEffects.createHeightFog(),
+ factory: () => postProcessEffects.createHeightFogVolume(),
+ // The sun's light through the haze (the shadow map, the clouds' shafts) is the dear part and smooth: it is walked at half size and
+ // read back by how near each texel's ray is to the pixel's. The haze itself is analytic and stays full size.
+ multiPass: [
+ { name: 'fog_volume', factory: () => postProcessEffects.createHeightFogVolume(), scale: 0.5 },
+ { name: 'fog_composite', factory: () => postProcessEffects.createHeightFog() },
+ ],
uniforms: [
// Per WORLD unit at the base height: the share of light a unit of fog takes out.
{ name: 'u_density', label: 'Density', min: 0, max: 0.05, step: 0.0001, defaultValue: 0.0015, neutralValue: 0 },
diff --git a/sdk/src/postprocess/sources/atmosphere.ts b/sdk/src/postprocess/sources/atmosphere.ts
index 04649ead5..efc9118c7 100644
--- a/sdk/src/postprocess/sources/atmosphere.ts
+++ b/sdk/src/postprocess/sources/atmosphere.ts
@@ -13,6 +13,10 @@ export const atmosphereEffects = {
/**
* A sky that has a time of day and weather in it: the scattering of the atmosphere, a sun that goes red as it sets, a
* moon, stars, and volumetric clouds that the sun lights.
+ *
+ * Pass 2 of 2, the composite. The clouds, and the shadow they cast on the ground, are the dear part and smooth, so the
+ * first pass works them out at half size and this one reads them back by the way a pixel's neighbours agree with it
+ * (see createSkyClouds); the dome, the sun, the moon and the stars, which have an edge, are drawn here at full size.
*/
createSkyAtmosphere(): ShaderHandle {
const source = `#pragma shader "PP Sky Atmosphere"
@@ -56,6 +60,7 @@ precision highp float;
in vec2 v_texCoord;
uniform sampler2D u_texture;
+uniform sampler2D u_sceneTexture;
uniform highp sampler2D u_sceneDepth;
out vec4 fragColor;
@@ -197,9 +202,110 @@ float cloudShadowAt(vec3 world, vec3 cam, vec3 sun, float coverage, float overca
}
return 1.0 - strength * 0.8 * (1.0 - exp(-tau));
}
+/// The cloud slab along @p dir as a map of what is behind it, colour * T + L: how much of it the clouds let through, and the light they add.
+/// A function of the direction alone, so it can be worked out small and read back big.
+void cloudLayer(vec3 dir, vec3 sun, vec3 dome, vec3 sunTrans, float night, float overcast, vec3 windDrift, out vec3 L, out float T) {
+ L = vec3(0.0);
+ T = 1.0;
+ float coverage = max(u_clouds, overcast * 0.95);
+ if (coverage > 0.01 && dir.y > 0.01) {
+ float base = u_cloudBase;
+ float thick = u_cloudThickness;
+ float tb = max(base - u_altitude, 0.0) / dir.y;
+ float tt = (base + thick - u_altitude) / dir.y;
+ const int STEPS = 16;
+ float ds = (tt - tb) / float(STEPS);
+ float jitter = ign(gl_FragCoord.xy);
+ float sigma = 1.3 * (1.0 + overcast * 1.5);
+ vec3 drift = windDrift;
+ float cosS = dot(dir, sun);
+ float phase = mix(0.35 + 0.65 * pow(max(cosS, 0.0), 6.0), 0.5, 0.4);
+ vec3 ambient = dome * 0.7 + lin(vec3(0.55, 0.65, 0.9)) * 0.015 * night;
+ vec3 sunLight = sunTrans * 1.2 * u_cloudBrightness;
+ float trans = 1.0;
+ vec3 lit = vec3(0.0);
+ for (int i = 0; i < STEPS; ++i) {
+ if (trans < 0.02) break;
+ float t = tb + (float(i) + jitter) * ds;
+ vec3 p = vec3(dir.x * t, u_altitude + dir.y * t, dir.z * t) + drift;
+ float d = cloudDensity(p, coverage, base, thick, 3);
+ if (d < 0.002) continue;
+ float tau = 0.0;
+ for (int k = 1; k <= 3; ++k) {
+ vec3 q = p + sun * (0.35 * float(k));
+ tau += cloudDensity(q, coverage, base, thick, 2) * 0.35 * sigma;
+ }
+ float energy = exp(-tau * 1.1) + 0.25 * exp(-tau * 0.25);
+ vec3 S = sunLight * energy * phase * 2.0 + ambient;
+ float ext = d * sigma * ds;
+ float tr = exp(-ext);
+ lit += trans * (1.0 - tr) * S;
+ trans *= tr;
+ }
+ float fade = smoothstep(0.01, 0.14, dir.y);
+ T = mix(1.0, trans, fade);
+ L = lit * fade;
+ }
+
+}
+${SSAO_GLSL}
+
+const float UP_MIN_WEIGHT = 0.15;
+
+/// The cloud layer for a sky pixel, read from the half-size pass: the sky texels among the four it falls between (a surface's
+/// texel has a negative alpha), by bilinear weight. False when none of them is sky, and the pixel works it out itself.
+bool upsampledClouds(out vec3 L, out float T) {
+ ivec2 hsize = textureSize(u_texture, 0);
+ vec2 hf = v_texCoord * vec2(hsize) - 0.5;
+ ivec2 base = ivec2(floor(hf));
+ vec2 fr = hf - vec2(base);
+ vec4 sum = vec4(0.0);
+ float wsum = 0.0;
+ for (int j = 0; j < 4; ++j) {
+ ivec2 o = ivec2(j & 1, j >> 1);
+ vec4 h = texelFetch(u_texture, clamp(base + o, ivec2(0), hsize - ivec2(1)), 0);
+ if (h.a < 0.0) continue;
+ float bw = (o.x == 1 ? fr.x : 1.0 - fr.x) * (o.y == 1 ? fr.y : 1.0 - fr.y);
+ sum += h * bw;
+ wsum += bw;
+ }
+ if (wsum < UP_MIN_WEIGHT) return false;
+ sum /= wsum;
+ L = sum.rgb;
+ T = sum.a;
+ return true;
+}
+
+/// The cloud shadow on a surface, read from the half-size pass: the surface texels among the four the pixel falls between, each weighted
+/// by how near its point in the world is to this pixel's, so a texel across a silhouette does not lend its shadow. False when none is near.
+bool upsampledShadow(vec3 P, vec3 entry, out float lit) {
+ ivec2 dsize = textureSize(u_sceneDepth, 0);
+ ivec2 hsize = textureSize(u_texture, 0);
+ vec2 hf = v_texCoord * vec2(hsize) - 0.5;
+ ivec2 base = ivec2(floor(hf));
+ vec2 fr = hf - vec2(base);
+ float tol = max(0.08 * length(P - entry), 1e-4);
+ float sum = 0.0;
+ float wsum = 0.0;
+ for (int j = 0; j < 4; ++j) {
+ ivec2 o = ivec2(j & 1, j >> 1);
+ ivec2 t = clamp(base + o, ivec2(0), hsize - ivec2(1));
+ vec4 h = texelFetch(u_texture, t, 0);
+ if (h.a >= 0.0) continue;
+ vec2 uv = uvOfTexel(t, hsize);
+ vec3 Pj = worldFromDepth(uv, depthAt(screenTexel(uv, dsize), dsize));
+ float bw = (o.x == 1 ? fr.x : 1.0 - fr.x) * (o.y == 1 ? fr.y : 1.0 - fr.y);
+ float w = bw * max(1.0 - length(Pj - P) / tol, 0.0);
+ sum += h.r * w;
+ wsum += w;
+ }
+ if (wsum < UP_MIN_WEIGHT) return false;
+ lit = sum / wsum;
+ return true;
+}
void main() {
- vec4 src = texture(u_texture, v_texCoord);
+ vec4 src = texture(u_sceneTexture, v_texCoord);
ivec2 size = textureSize(u_sceneDepth, 0);
float depth = texelFetch(u_sceneDepth, clamp(ivec2(v_texCoord * vec2(size)), ivec2(0), size - ivec2(1)), 0).r;
// The scene was cleared to nothing when the background is not kept (the engine's rule, same threshold): what is left of a pixel's
@@ -236,7 +342,10 @@ void main() {
float windAngle = radians(u_windDirection);
vec3 windDrift = vec3(sin(windAngle), 0.0, cos(windAngle)) * (u_windSpeed * u_time.x) + anchor;
if (geometry) {
- float lit = cloudShadowAt(worldFromDepth(v_texCoord, depth) + u_renderOrigin.xyz, entryWorld, sun, max(u_clouds, overcast * 0.95), overcast, windDrift - anchor);
+ float lit;
+ if (!upsampledShadow(worldFromDepth(v_texCoord, depth), entry, lit)) {
+ lit = cloudShadowAt(worldFromDepth(v_texCoord, depth) + u_renderOrigin.xyz, entryWorld, sun, max(u_clouds, overcast * 0.95), overcast, windDrift - anchor);
+ }
fragColor = vec4(src.rgb * lit, src.a);
return;
}
@@ -287,44 +396,12 @@ void main() {
}
}
- // Clouds: a slab marched against a noise density, lit by the sun through what is between.
- float coverage = max(u_clouds, overcast * 0.95);
- if (coverage > 0.01 && dir.y > 0.01) {
- float base = u_cloudBase;
- float thick = u_cloudThickness;
- float tb = max(base - u_altitude, 0.0) / dir.y;
- float tt = (base + thick - u_altitude) / dir.y;
- const int STEPS = 16;
- float ds = (tt - tb) / float(STEPS);
- float jitter = ign(gl_FragCoord.xy);
- float sigma = 1.3 * (1.0 + overcast * 1.5);
- vec3 drift = windDrift;
- float cosS = dot(dir, sun);
- float phase = mix(0.35 + 0.65 * pow(max(cosS, 0.0), 6.0), 0.5, 0.4);
- vec3 ambient = dome * 0.7 + lin(vec3(0.55, 0.65, 0.9)) * 0.015 * night;
- vec3 sunLight = sunTrans * 1.2 * u_cloudBrightness;
- float trans = 1.0;
- vec3 lit = vec3(0.0);
- for (int i = 0; i < STEPS; ++i) {
- if (trans < 0.02) break;
- float t = tb + (float(i) + jitter) * ds;
- vec3 p = vec3(dir.x * t, u_altitude + dir.y * t, dir.z * t) + drift;
- float d = cloudDensity(p, coverage, base, thick, 3);
- if (d < 0.002) continue;
- float tau = 0.0;
- for (int k = 1; k <= 3; ++k) {
- vec3 q = p + sun * (0.35 * float(k));
- tau += cloudDensity(q, coverage, base, thick, 2) * 0.35 * sigma;
- }
- float energy = exp(-tau * 1.1) + 0.25 * exp(-tau * 0.25);
- vec3 S = sunLight * energy * phase * 2.0 + ambient;
- float ext = d * sigma * ds;
- float tr = exp(-ext);
- lit += trans * (1.0 - tr) * S;
- trans *= tr;
- }
- float fade = smoothstep(0.01, 0.14, dir.y);
- col = mix(col, col * trans + lit, fade);
+ // Clouds: read from the half-size pass, worked out here only where it has nothing for this pixel (a gap in a skyline).
+ if (max(u_clouds, overcast * 0.95) > 0.01 && dir.y > 0.01) {
+ vec3 L;
+ float T;
+ if (!upsampledClouds(L, T)) cloudLayer(dir, sun, dome, sunTrans, night, overcast, windDrift, L, T);
+ col = col * T + L;
}
// An overcast sky is also a little greyer than the cloud-free one under it.
@@ -479,8 +556,130 @@ fn cloudShadowAt(world : vec3f, cam : vec3f, sun : vec3f, coverage : f32, overca
return 1.0 - strength * 0.8 * (1.0 - exp(-tau));
}
+struct CloudLayer { L : vec3f, T : f32 };
+
+/// The cloud slab along dir as a map of what is behind it, colour * T + L. A function of the direction alone, so it can be worked out small and read back big.
+fn cloudLayer(dir : vec3f, sun : vec3f, dome : vec3f, sunTrans : vec3f, night : f32, overcast : f32, windDrift : vec3f, px : vec2f) -> CloudLayer {
+ var out : CloudLayer;
+ out.L = vec3f(0.0);
+ out.T = 1.0;
+ let coverage = max(mc.u_clouds, overcast * 0.95);
+ if (coverage > 0.01 && dir.y > 0.01) {
+ let base = mc.u_cloudBase;
+ let thick = mc.u_cloudThickness;
+ let tb = max(base - mc.u_altitude, 0.0) / dir.y;
+ let tt = (base + thick - mc.u_altitude) / dir.y;
+ let STEPS = 16;
+ let ds = (tt - tb) / f32(STEPS);
+ let jitter = ign(px);
+ let sigma = 1.3 * (1.0 + overcast * 1.5);
+ let drift = windDrift;
+ let cosS = dot(dir, sun);
+ let phase = mix(0.35 + 0.65 * pow(max(cosS, 0.0), 6.0), 0.5, 0.4);
+ let ambient = dome * 0.7 + lin(vec3f(0.55, 0.65, 0.9)) * 0.015 * night;
+ let sunLight = sunTrans * 1.2 * mc.u_cloudBrightness;
+ var trans = 1.0;
+ var lit = vec3f(0.0);
+ for (var i : i32 = 0; i < STEPS; i = i + 1) {
+ if (trans < 0.02) { break; }
+ let t = tb + (f32(i) + jitter) * ds;
+ let p = vec3f(dir.x * t, mc.u_altitude + dir.y * t, dir.z * t) + drift;
+ let d = cloudDensity(p, coverage, base, thick, 3);
+ if (d < 0.002) { continue; }
+ var tau = 0.0;
+ for (var k : i32 = 1; k <= 3; k = k + 1) {
+ let q = p + sun * (0.35 * f32(k));
+ tau = tau + cloudDensity(q, coverage, base, thick, 2) * 0.35 * sigma;
+ }
+ let energy = exp(-tau * 1.1) + 0.25 * exp(-tau * 0.25);
+ let S = sunLight * energy * phase * 2.0 + ambient;
+ let ext = d * sigma * ds;
+ let tr = exp(-ext);
+ lit = lit + trans * (1.0 - tr) * S;
+ trans = trans * tr;
+ }
+ let fade = smoothstep(0.01, 0.14, dir.y);
+ out.T = mix(1.0, trans, fade);
+ out.L = lit * fade;
+ }
+
+ return out;
+}
+${SSAO_WGSL}
+
+const UP_MIN_WEIGHT : f32 = 0.15;
+
+struct CloudRead { ok : bool, L : vec3f, T : f32 };
+
+/// The cloud layer for a sky pixel, read from the half-size pass: the sky texels among the four it falls between (a surface's texel has a
+/// negative alpha), by bilinear weight. Not ok when none of them is sky, and the pixel works it out itself.
+fn upsampledClouds(uv : vec2f) -> CloudRead {
+ var out : CloudRead;
+ out.ok = false;
+ let hsize = vec2i(textureDimensions(t0, 0));
+ let hf = uv * vec2f(hsize) - 0.5;
+ let base = vec2i(floor(hf));
+ let fr = hf - vec2f(base);
+ var sum = vec4f(0.0);
+ var wsum = 0.0;
+ for (var j : i32 = 0; j < 4; j = j + 1) {
+ let o = vec2i(j & 1, j >> 1);
+ let h = textureLoad(t0, clamp(base + o, vec2i(0), hsize - vec2i(1)), 0);
+ if (h.a < 0.0) { continue; }
+ var bx = 1.0 - fr.x;
+ if (o.x == 1) { bx = fr.x; }
+ var by = 1.0 - fr.y;
+ if (o.y == 1) { by = fr.y; }
+ let bw = bx * by;
+ sum = sum + h * bw;
+ wsum = wsum + bw;
+ }
+ if (wsum < UP_MIN_WEIGHT) { return out; }
+ sum = sum / wsum;
+ out.ok = true;
+ out.L = sum.rgb;
+ out.T = sum.a;
+ return out;
+}
+
+struct ShadowRead { ok : bool, lit : f32 };
+
+/// The cloud shadow on a surface, read from the half-size pass: the surface texels among the four the pixel falls between, each weighted by how
+/// near its point in the world is to this pixel's, so a texel across a silhouette does not lend its shadow. Not ok when none is near.
+fn upsampledShadow(uv : vec2f, P : vec3f, entry : vec3f) -> ShadowRead {
+ var out : ShadowRead;
+ out.ok = false;
+ let dsize = vec2i(textureDimensions(t7, 0));
+ let hsize = vec2i(textureDimensions(t0, 0));
+ let hf = uv * vec2f(hsize) - 0.5;
+ let base = vec2i(floor(hf));
+ let fr = hf - vec2f(base);
+ let tol = max(0.08 * length(P - entry), 1e-4);
+ var sum = 0.0;
+ var wsum = 0.0;
+ for (var j : i32 = 0; j < 4; j = j + 1) {
+ let o = vec2i(j & 1, j >> 1);
+ let t = clamp(base + o, vec2i(0), hsize - vec2i(1));
+ let h = textureLoad(t0, t, 0);
+ if (h.a >= 0.0) { continue; }
+ let tuv = uvOfTexel(t, hsize);
+ let Pj = worldFromDepth(tuv, depthAt(screenTexel(tuv, dsize), dsize));
+ var bx = 1.0 - fr.x;
+ if (o.x == 1) { bx = fr.x; }
+ var by = 1.0 - fr.y;
+ if (o.y == 1) { by = fr.y; }
+ let w = bx * by * max(1.0 - length(Pj - P) / tol, 0.0);
+ sum = sum + h.r * w;
+ wsum = wsum + w;
+ }
+ if (wsum < UP_MIN_WEIGHT) { return out; }
+ out.ok = true;
+ out.lit = sum / wsum;
+ return out;
+}
+
@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
- let src = textureSampleLevel(t0, s0, v.v_texCoord, 0.0);
+ let src = textureSampleLevel(t1, s1, v.v_texCoord, 0.0);
let size = vec2i(textureDimensions(t7, 0));
let depth = textureLoad(t7, clamp(vec2i(v.v_texCoord * vec2f(size)), vec2i(0), size - vec2i(1)), 0);
let covered = mc.u_panorama <= 0.001;
@@ -512,7 +711,9 @@ fn cloudShadowAt(world : vec3f, cam : vec3f, sun : vec3f, coverage : f32, overca
let windAngle = radians(mc.u_windDirection);
let windDrift = vec3f(sin(windAngle), 0.0, cos(windAngle)) * (mc.u_windSpeed * tc.u_time.x) + anchor;
if (geometry) {
- let lit = cloudShadowAt(worldFromDepth(v.v_texCoord, depth) + frame.renderOrigin.xyz, entryWorld, sun, max(mc.u_clouds, overcast * 0.95), overcast, windDrift - anchor, v.pos.xy);
+ var lit = 1.0;
+ let up = upsampledShadow(v.v_texCoord, worldFromDepth(v.v_texCoord, depth), entry);
+ if (up.ok) { lit = up.lit; } else { lit = cloudShadowAt(worldFromDepth(v.v_texCoord, depth) + frame.renderOrigin.xyz, entryWorld, sun, max(mc.u_clouds, overcast * 0.95), overcast, windDrift - anchor, v.pos.xy); }
return vec4f(src.rgb * lit, src.a);
}
@@ -558,43 +759,16 @@ fn cloudShadowAt(world : vec3f, cam : vec3f, sun : vec3f, coverage : f32, overca
}
}
- let coverage = max(mc.u_clouds, overcast * 0.95);
- if (coverage > 0.01 && dir.y > 0.01) {
- let base = mc.u_cloudBase;
- let thick = mc.u_cloudThickness;
- let tb = max(base - mc.u_altitude, 0.0) / dir.y;
- let tt = (base + thick - mc.u_altitude) / dir.y;
- let STEPS = 16;
- let ds = (tt - tb) / f32(STEPS);
- let jitter = ign(v.pos.xy);
- let sigma = 1.3 * (1.0 + overcast * 1.5);
- let drift = windDrift;
- let cosS = dot(dir, sun);
- let phase = mix(0.35 + 0.65 * pow(max(cosS, 0.0), 6.0), 0.5, 0.4);
- let ambient = dome * 0.7 + lin(vec3f(0.55, 0.65, 0.9)) * 0.015 * night;
- let sunLight = sunTrans * 1.2 * mc.u_cloudBrightness;
- var trans = 1.0;
- var lit = vec3f(0.0);
- for (var i : i32 = 0; i < STEPS; i = i + 1) {
- if (trans < 0.02) { break; }
- let t = tb + (f32(i) + jitter) * ds;
- let p = vec3f(dir.x * t, mc.u_altitude + dir.y * t, dir.z * t) + drift;
- let d = cloudDensity(p, coverage, base, thick, 3);
- if (d < 0.002) { continue; }
- var tau = 0.0;
- for (var k : i32 = 1; k <= 3; k = k + 1) {
- let q = p + sun * (0.35 * f32(k));
- tau = tau + cloudDensity(q, coverage, base, thick, 2) * 0.35 * sigma;
- }
- let energy = exp(-tau * 1.1) + 0.25 * exp(-tau * 0.25);
- let S = sunLight * energy * phase * 2.0 + ambient;
- let ext = d * sigma * ds;
- let tr = exp(-ext);
- lit = lit + trans * (1.0 - tr) * S;
- trans = trans * tr;
+ if (max(mc.u_clouds, overcast * 0.95) > 0.01 && dir.y > 0.01) {
+ var cl : CloudLayer;
+ let up = upsampledClouds(v.v_texCoord);
+ if (up.ok) {
+ cl.L = up.L;
+ cl.T = up.T;
+ } else {
+ cl = cloudLayer(dir, sun, dome, sunTrans, night, overcast, windDrift, v.pos.xy);
}
- let fade = smoothstep(0.01, 0.14, dir.y);
- col = mix(col, col * trans + lit, fade);
+ col = col * cl.T + cl.L;
}
let luma = dot(col, vec3f(0.2126, 0.7152, 0.0722));
@@ -610,46 +784,47 @@ fn cloudShadowAt(world : vec3f, cam : vec3f, sun : vec3f, coverage : f32, overca
return Material.compileShader(source);
},
/**
- * Exponential height fog with the atmosphere's two tells: the sun's glow in the haze and a
- * sky that thickens toward the horizon.
+ * The sky's clouds and their shadow on the ground, at half size: pass 1 of 2. Each texel stands for the pixels under it and
+ * keeps either the cloud layer in front of a sky pixel (what it lets through, and the light it adds) or the light that
+ * reaches a surface, which of the two the depth says. The composite reads these back.
*/
- createHeightFog(): ShaderHandle {
- const source = `#pragma shader "PP Height Fog"
+ createSkyClouds(): ShaderHandle {
+ const source = `#pragma shader "PP Sky Clouds"
#pragma version 300 es
#pragma domain PostProcess
-#pragma param u_fogColor color default(0.62,0.7,0.8,1)
-#pragma param u_sunColor color default(1,0.82,0.55,1)
-#pragma param u_density float default(0.0015) range(0,0.05)
-#pragma param u_heightFalloff float default(0.003) range(0,0.1)
-#pragma param u_baseHeight float default(0) range(-1000,5000)
+#pragma param u_intensity float default(1) range(0,8)
+#pragma param u_panorama float default(0) range(0,1)
+// A marker the engine reads, not a setting: with it declared (and the background not kept) the scene is cleared to nothing, so its alpha
+// is how much of each pixel geometry covers and the sky is laid under it. See PostProcessPipeline::sceneCoverageWanted.
+#pragma param u_sceneCoverage float default(1) range(0,1)
+#pragma param u_overcast float default(0) range(0,1)
+#pragma param u_haze float default(1) range(0,8)
+#pragma param u_altitude float default(0.1) range(0,20)
+#pragma param u_sunSize float default(1) range(0.2,8)
+#pragma param u_sunBrightness float default(40) range(0,400)
+#pragma param u_moon float default(1) range(0,4)
+#pragma param u_stars float default(1) range(0,4)
+#pragma param u_clouds float default(0.45) range(0,1)
+#pragma param u_cloudBase float default(1.5) range(0.2,10)
+#pragma param u_cloudThickness float default(2.5) range(0.2,8)
+#pragma param u_cloudBrightness float default(1) range(0,4)
+#pragma param u_windSpeed float default(0.05) range(0,2)
+#pragma param u_windDirection float default(60) range(0,360)
+#pragma param u_followSun float default(1) range(0,1)
#pragma param u_planetRadius float default(0) range(0,100000000)
#pragma param u_planetX float default(0) range(-100000000,100000000)
#pragma param u_planetY float default(0) range(-100000000,100000000)
#pragma param u_planetZ float default(0) range(-100000000,100000000)
-#pragma param u_startDistance float default(0) range(0,3000)
-#pragma param u_maxOpacity float default(1) range(0,1)
#pragma param u_sunAzimuth float default(40) range(0,360)
-#pragma param u_sunElevation float default(25) range(-90,90)
-#pragma param u_inscatter float default(0.6) range(0,2)
-#pragma param u_sunExponent float default(8) range(1,64)
-#pragma param u_skyFog float default(0.8) range(0,1)
-#pragma param u_followSun float default(1) range(0,1)
-#pragma param u_volumetric float default(0) range(0,1)
-#pragma param u_volumeRange float default(3000) range(100,1000000)
-#pragma param u_cloudShade float default(0) range(0,1)
-#pragma param u_skyCover float default(0) range(0,1)
-#pragma param u_skyBase float default(1.5) range(0.2,10)
-#pragma param u_skyThickness float default(2.5) range(0.2,8)
-#pragma param u_skyAltitude float default(0.1) range(0,20)
-#pragma param u_skyUnits float default(1000) range(1,100000)
-#pragma param u_skySigma float default(1.3) range(0,10)
-#pragma param u_skyWindX float default(0) range(-2,2)
-#pragma param u_skyWindZ float default(0) range(-2,2)
+#pragma param u_sunElevation float default(35) range(-90,90)
+#pragma param u_cloudShadow float default(0) range(0,1)
+#pragma param u_unitsPerKm float default(1000) range(1,100000)
#pragma param u_cloudMask texture default(black)
#pragma param u_cloudMaskTile float default(0) range(0,100)
#pragma param u_cloudMask2 texture default(black)
#pragma param u_cloudMask2Tile float default(0) range(0,100)
#pragma lights
+#pragma highprecision
#pragma fragment
precision highp float;
@@ -659,24 +834,22 @@ uniform sampler2D u_texture;
uniform highp sampler2D u_sceneDepth;
out vec4 fragColor;
-const float FAR_RAY = 100000.0;
-const int VOLUME_STEPS = 24;
-
-float ign(vec2 p) {
- return fract(52.9829189 * fract(dot(p, vec2(0.06711056, 0.00583715))));
-}
-/// How much of a ray's light a particle scatters toward the viewer: forward, the way fog glows around the sun.
-float phaseHG(float cosine) {
- const float g = 0.55;
- return (1.0 - g * g) / pow(max(1.0 + g * g - 2.0 * g * cosine, 1e-3), 1.5) * 0.25;
-}
-
+const float R_E = 6360.0;
+const float R_A = 6420.0;
+const vec3 BETA_R = vec3(0.0058, 0.0135, 0.0331);
+const float BETA_M = 0.021;
+const float H_R = 8.0;
+const float H_M = 1.2;
+const float SUN_I = 22.0;
float hash13(vec3 p) {
p = fract(p * 0.1031);
p += dot(p, p.zyx + 31.32);
return fract((p.x + p.y) * p.z);
}
+float ign(vec2 p) {
+ return fract(52.9829189 * fract(dot(p, vec2(0.06711056, 0.00583715))));
+}
float vnoise(vec3 p) {
vec3 i = floor(p);
vec3 f = fract(p);
@@ -687,17 +860,994 @@ float vnoise(vec3 p) {
float d = mix(hash13(i + vec3(0, 1, 1)), hash13(i + vec3(1, 1, 1)), f.x);
return mix(mix(a, b, f.y), mix(c, d, f.y), f.z);
}
-/// Where a coverage texture is given, the shape of the clouds on the ground is its, and the noise erodes it. The layers are tiled in kilometres
-/// of the cloud plane, the second a softer, higher one folded in.
-float cloudField(vec2 xz, float noise, float h) {
- if (u_cloudMaskTile <= 0.0) return noise;
- float m = textureLod(u_cloudMask, xz / u_cloudMaskTile, 0.0).r;
- if (u_cloudMask2Tile > 0.0) m = mix(m, textureLod(u_cloudMask2, xz / u_cloudMask2Tile, 0.0).r, 0.3);
- // A mask knows nothing of height, and a column of it seen along the slab is a streak: the cloud's top stands only over the mask's core, a dome.
- return mix(m, noise, 0.5) - 0.35 * (h - 0.35);
-}
-/// The sky's cloud density, the same one the dome draws (see skyAtmosphere), at a point in kilometres.
+vec3 lin(vec3 c) {
+#ifdef ES_LINEAR
+ return srgbToLinear(c);
+#else
+ return c;
+#endif
+}
+
+/// Where a ray from o along d meets the sphere of radius r about the planet's centre: the two distances, or -1 for a miss.
+vec2 sphereHit(vec3 o, vec3 d, float r) {
+ float b = dot(o, d);
+ float disc = b * b - (dot(o, o) - r * r);
+ if (disc < 0.0) return vec2(-1.0);
+ float s = sqrt(disc);
+ return vec2(-b - s, -b + s);
+}
+
+/// The turn that takes up to +Y by the shortest way: the sky is worked out where up is +Y, so on a round world it is worked out in the frame of the ground the camera stands on.
+mat3 upFrame(vec3 up) {
+ vec3 v = vec3(-up.z, 0.0, up.x);
+ float k = 1.0 / max(1.0 + up.y, 1e-4);
+ mat3 cross = mat3(0.0, v.z, -v.y, -v.z, 0.0, v.x, v.y, -v.x, 0.0);
+ return mat3(1.0) + cross + cross * cross * k;
+}
+
+/// The air between p and the top of the atmosphere along d, as (Rayleigh, Mie) optical depth; z is 0 where the planet is in the way.
+vec3 lightDepth(vec3 p, vec3 d) {
+ vec2 ground = sphereHit(p, d, R_E);
+ if (ground.y > 0.0 && ground.x > 0.0) return vec3(0.0);
+ float len = sphereHit(p, d, R_A).y;
+ float ds = len / 4.0;
+ vec2 od = vec2(0.0);
+ for (int j = 0; j < 4; ++j) {
+ vec3 q = p + d * (ds * (float(j) + 0.5));
+ od += exp(-(length(q) - R_E) / vec2(H_R, H_M)) * ds;
+ }
+ return vec3(od, 1.0);
+}
+
+/// The sky's radiance along dir, by single scattering; @p sunTrans is the sun's light by the time it reaches the viewer.
+vec3 scatter(vec3 dir, vec3 sun, float alt, float haze, out vec3 sunTrans) {
+ vec3 o = vec3(0.0, R_E + alt, 0.0);
+ float len = sphereHit(o, dir, R_A).y;
+ const int N = 16;
+ float ds = len / float(N);
+ float mu = dot(dir, sun);
+ float g = 0.76;
+ float phaseR = 3.0 / (16.0 * 3.14159265) * (1.0 + mu * mu);
+ float phaseM = 3.0 / (8.0 * 3.14159265) * ((1.0 - g * g) * (1.0 + mu * mu)) / ((2.0 + g * g) * pow(1.0 + g * g - 2.0 * g * mu, 1.5));
+ vec2 od = vec2(0.0);
+ vec3 sumR = vec3(0.0);
+ vec3 sumM = vec3(0.0);
+ for (int i = 0; i < N; ++i) {
+ vec3 p = o + dir * (ds * (float(i) + 0.5));
+ vec2 dens = exp(-(length(p) - R_E) / vec2(H_R, H_M)) * ds;
+ od += dens;
+ vec3 ol = lightDepth(p, sun);
+ if (ol.z > 0.5) {
+ vec3 attn = exp(-(BETA_R * (od.x + ol.x) + BETA_M * haze * 1.1 * (od.y + ol.y)));
+ sumR += attn * dens.x;
+ sumM += attn * dens.y;
+ }
+ }
+ vec3 st = lightDepth(o, sun);
+ sunTrans = st.z > 0.5 ? exp(-(BETA_R * st.x + BETA_M * haze * 1.1 * st.y)) : vec3(0.0);
+ return (sumR * BETA_R * phaseR + sumM * BETA_M * haze * phaseM) * SUN_I;
+}
+
+/// Where a coverage texture is given, the shape of the clouds on the ground is its, and the noise erodes it. The layers are tiled in kilometres
+/// of the cloud plane, the second a softer, higher one folded in.
+float cloudField(vec2 xz, float noise, float h) {
+ if (u_cloudMaskTile <= 0.0) return noise;
+ float m = textureLod(u_cloudMask, xz / u_cloudMaskTile, 0.0).r;
+ if (u_cloudMask2Tile > 0.0) m = mix(m, textureLod(u_cloudMask2, xz / u_cloudMask2Tile, 0.0).r, 0.3);
+ // A mask knows nothing of height, and a column of it seen along the slab is a streak: the cloud's top stands only over the mask's core, a dome.
+ return mix(m, noise, 0.5) - 0.35 * (h - 0.35);
+}
+
+float cloudDensity(vec3 p, float coverage, float base, float thick, int octaves) {
+ float h = clamp((p.y - base) / thick, 0.0, 1.0);
+ float shape = smoothstep(0.0, 0.14, h) * smoothstep(1.0, 0.55, h);
+ vec3 q = vec3(p.x, p.y * 1.8, p.z) * 0.32;
+ float n = vnoise(q) * 0.58 + vnoise(q * 2.13) * 0.28;
+ if (octaves > 2) n += vnoise(q * 4.7) * 0.14;
+ else n += 0.07;
+ n = cloudField(p.xz, n, h);
+ return smoothstep(1.0 - coverage, 1.0 - coverage + 0.22, n) * shape;
+}
+
+/// How much of the sun reaches a surface at @p world through the cloud layer: 1 is all of it. The sun's ray is marched up through
+/// the slab against the same density the sky draws, so a shadow falls where the cloud above it is. Ambient light is not shadowed.
+float cloudShadowAt(vec3 world, vec3 cam, vec3 sun, float coverage, float overcast, vec3 drift) {
+ float strength = u_cloudShadow * smoothstep(0.02, 0.14, sun.y);
+ if (strength <= 0.001 || coverage <= 0.01) return 1.0;
+ float k = 1.0 / max(u_unitsPerKm, 1e-3);
+ float h = u_altitude + (world.y - cam.y) * k;
+ float tt = (u_cloudBase + u_cloudThickness - h) / sun.y;
+ if (tt <= 0.0) return 1.0;
+ float tb = max(u_cloudBase - h, 0.0) / sun.y;
+ const int N = 6;
+ float ds = (tt - tb) / float(N);
+ float sigma = 1.3 * (1.0 + overcast * 1.5);
+ float tau = 0.0;
+ float jitter = ign(gl_FragCoord.xy);
+ for (int i = 0; i < N; ++i) {
+ float t = tb + (float(i) + jitter) * ds;
+ vec3 p = vec3(world.x * k + sun.x * t, h + sun.y * t, world.z * k + sun.z * t) + drift;
+ tau += cloudDensity(p, coverage, u_cloudBase, u_cloudThickness, 2) * sigma * ds;
+ }
+ return 1.0 - strength * 0.8 * (1.0 - exp(-tau));
+}
+/// The cloud slab along @p dir as a map of what is behind it, colour * T + L: how much of it the clouds let through, and the light they add.
+/// A function of the direction alone, so it can be worked out small and read back big.
+void cloudLayer(vec3 dir, vec3 sun, vec3 dome, vec3 sunTrans, float night, float overcast, vec3 windDrift, out vec3 L, out float T) {
+ L = vec3(0.0);
+ T = 1.0;
+ float coverage = max(u_clouds, overcast * 0.95);
+ if (coverage > 0.01 && dir.y > 0.01) {
+ float base = u_cloudBase;
+ float thick = u_cloudThickness;
+ float tb = max(base - u_altitude, 0.0) / dir.y;
+ float tt = (base + thick - u_altitude) / dir.y;
+ const int STEPS = 16;
+ float ds = (tt - tb) / float(STEPS);
+ float jitter = ign(gl_FragCoord.xy);
+ float sigma = 1.3 * (1.0 + overcast * 1.5);
+ vec3 drift = windDrift;
+ float cosS = dot(dir, sun);
+ float phase = mix(0.35 + 0.65 * pow(max(cosS, 0.0), 6.0), 0.5, 0.4);
+ vec3 ambient = dome * 0.7 + lin(vec3(0.55, 0.65, 0.9)) * 0.015 * night;
+ vec3 sunLight = sunTrans * 1.2 * u_cloudBrightness;
+ float trans = 1.0;
+ vec3 lit = vec3(0.0);
+ for (int i = 0; i < STEPS; ++i) {
+ if (trans < 0.02) break;
+ float t = tb + (float(i) + jitter) * ds;
+ vec3 p = vec3(dir.x * t, u_altitude + dir.y * t, dir.z * t) + drift;
+ float d = cloudDensity(p, coverage, base, thick, 3);
+ if (d < 0.002) continue;
+ float tau = 0.0;
+ for (int k = 1; k <= 3; ++k) {
+ vec3 q = p + sun * (0.35 * float(k));
+ tau += cloudDensity(q, coverage, base, thick, 2) * 0.35 * sigma;
+ }
+ float energy = exp(-tau * 1.1) + 0.25 * exp(-tau * 0.25);
+ vec3 S = sunLight * energy * phase * 2.0 + ambient;
+ float ext = d * sigma * ds;
+ float tr = exp(-ext);
+ lit += trans * (1.0 - tr) * S;
+ trans *= tr;
+ }
+ float fade = smoothstep(0.01, 0.14, dir.y);
+ T = mix(1.0, trans, fade);
+ L = lit * fade;
+ }
+
+}
+
+void main() {
+ vec4 src = texture(u_texture, v_texCoord);
+ ivec2 size = textureSize(u_sceneDepth, 0);
+ float depth = texelFetch(u_sceneDepth, clamp(ivec2(v_texCoord * vec2(size)), ivec2(0), size - ivec2(1)), 0).r;
+ // The scene was cleared to nothing when the background is not kept (the engine's rule, same threshold): what is left of a pixel's
+ // alpha is how much the sky shows through it, and that includes a pixel whose depth says surface because one sample of it is.
+ bool covered = u_panorama <= 0.001;
+ bool geometry = covered ? (depth < 1.0 && src.a > 0.999) : depth < 1.0;
+ // A texel stands for the pixels under it. A sky texel holds the cloud layer in front of it, colour * T + L, with T (never negative) in
+ // the alpha; a surface texel holds the cloud shadow it lies in, its light in the red and minus that in the alpha.
+ if (geometry && u_cloudShadow <= 0.001) { fragColor = vec4(1.0, 0.0, 0.0, -1.0); return; }
+
+ vec3 entry = worldFromDepth(v_texCoord, 0.0);
+ vec3 dir = normalize(worldFromDepth(v_texCoord, 1.0) - entry);
+
+ vec4 lightDir, lightColor;
+ ivec2 tiles;
+ findSun(lightDir, lightColor, tiles);
+ bool follow = u_followSun > 0.5 && lightDir.w > 0.5;
+ float az = radians(u_sunAzimuth);
+ float el = radians(u_sunElevation);
+ vec3 sun = follow ? lightDir.xyz : vec3(cos(el) * sin(az), sin(el), cos(el) * cos(az));
+ if (u_planetRadius > 0.0) {
+ // Up is away from the planet's centre where the camera is: the direction and the sun are turned into the frame where that is +Y.
+ mat3 frame = upFrame(normalize(entry + u_renderOrigin.xyz - vec3(u_planetX, u_planetY, u_planetZ)));
+ dir = frame * dir;
+ sun = frame * sun;
+ }
+ float se = sun.y;
+
+ float overcast = clamp(u_overcast, 0.0, 1.0);
+ float haze = u_haze * (1.0 + overcast * 2.0);
+
+ // Clouds that cast shadows are in the world, not on the camera: the pattern is anchored to where the camera stands.
+ bool shadowed = u_cloudShadow > 0.001;
+ vec3 entryWorld = entry + u_renderOrigin.xyz;
+ vec3 anchor = shadowed ? vec3(entryWorld.x, 0.0, entryWorld.z) / max(u_unitsPerKm, 1e-3) : vec3(0.0);
+ float windAngle = radians(u_windDirection);
+ vec3 windDrift = vec3(sin(windAngle), 0.0, cos(windAngle)) * (u_windSpeed * u_time.x) + anchor;
+ if (geometry) {
+ float lit = cloudShadowAt(worldFromDepth(v_texCoord, depth) + u_renderOrigin.xyz, entryWorld, sun, max(u_clouds, overcast * 0.95), overcast, windDrift - anchor);
+ fragColor = vec4(lit, 0.0, 0.0, -lit);
+ return;
+ }
+
+ // The dome. Below the horizon there is no sky: the ray is flattened onto it so a camera that looks down sees the haze there.
+ vec3 sdir = normalize(vec3(dir.x, max(dir.y, 0.015), dir.z));
+ vec3 sunTrans;
+ vec3 col = scatter(sdir, sun, u_altitude, haze, sunTrans) * 0.55 * u_intensity;
+ // Below the horizon there is no sky, only the dimmer light the ground sends up.
+ col *= 1.0 - 0.75 * smoothstep(0.0, -0.3, dir.y);
+ // What the air keeps of the night: a faint glow so the dome is not black where there is no moon.
+ float night = 1.0 - smoothstep(-0.10, 0.12, se);
+ col += mix(lin(vec3(0.003, 0.006, 0.02)), lin(vec3(0.016, 0.024, 0.06)), pow(1.0 - max(dir.y, 0.0), 3.0)) * night;
+ vec3 dome = col;
+ vec3 L;
+ float T;
+ cloudLayer(dir, sun, dome, sunTrans, night, overcast, windDrift, L, T);
+ fragColor = vec4(L, T);
+}
+#pragma end
+
+#pragma fragment wgsl
+const R_E : f32 = 6360.0;
+const R_A : f32 = 6420.0;
+const BETA_R : vec3f = vec3f(0.0058, 0.0135, 0.0331);
+const BETA_M : f32 = 0.021;
+const H_R : f32 = 8.0;
+const H_M : f32 = 1.2;
+const SUN_I : f32 = 22.0;
+
+fn hash13(p0 : vec3f) -> f32 {
+ var p = fract(p0 * 0.1031);
+ p = p + dot(p, p.zyx + 31.32);
+ return fract((p.x + p.y) * p.z);
+}
+fn ign(p : vec2f) -> f32 {
+ return fract(52.9829189 * fract(dot(p, vec2f(0.06711056, 0.00583715))));
+}
+fn vnoise(p : vec3f) -> f32 {
+ let i = floor(p);
+ var f = fract(p);
+ f = f * f * (3.0 - 2.0 * f);
+ let a = mix(hash13(i), hash13(i + vec3f(1, 0, 0)), f.x);
+ let b = mix(hash13(i + vec3f(0, 1, 0)), hash13(i + vec3f(1, 1, 0)), f.x);
+ let c = mix(hash13(i + vec3f(0, 0, 1)), hash13(i + vec3f(1, 0, 1)), f.x);
+ let d = mix(hash13(i + vec3f(0, 1, 1)), hash13(i + vec3f(1, 1, 1)), f.x);
+ return mix(mix(a, b, f.y), mix(c, d, f.y), f.z);
+}
+
+fn lin(c : vec3f) -> vec3f {
+#ifdef ES_LINEAR
+ return srgbToLinear(c);
+#else
+ return c;
+#endif
+}
+
+fn sphereHit(o : vec3f, d : vec3f, r : f32) -> vec2f {
+ let b = dot(o, d);
+ let disc = b * b - (dot(o, o) - r * r);
+ if (disc < 0.0) { return vec2f(-1.0); }
+ let s = sqrt(disc);
+ return vec2f(-b - s, -b + s);
+}
+
+fn lightDepth(p : vec3f, d : vec3f) -> vec3f {
+ let ground = sphereHit(p, d, R_E);
+ if (ground.y > 0.0 && ground.x > 0.0) { return vec3f(0.0); }
+ let len = sphereHit(p, d, R_A).y;
+ let ds = len / 4.0;
+ var od = vec2f(0.0);
+ for (var j : i32 = 0; j < 4; j = j + 1) {
+ let q = p + d * (ds * (f32(j) + 0.5));
+ od = od + exp(-(length(q) - R_E) / vec2f(H_R, H_M)) * ds;
+ }
+ return vec3f(od, 1.0);
+}
+
+struct Scatter { radiance : vec3f, sunTrans : vec3f };
+
+fn scatter(dir : vec3f, sun : vec3f, alt : f32, haze : f32) -> Scatter {
+ let o = vec3f(0.0, R_E + alt, 0.0);
+ let len = sphereHit(o, dir, R_A).y;
+ let N = 16;
+ let ds = len / f32(N);
+ let mu = dot(dir, sun);
+ let g = 0.76;
+ let phaseR = 3.0 / (16.0 * 3.14159265) * (1.0 + mu * mu);
+ let phaseM = 3.0 / (8.0 * 3.14159265) * ((1.0 - g * g) * (1.0 + mu * mu)) / ((2.0 + g * g) * pow(1.0 + g * g - 2.0 * g * mu, 1.5));
+ var od = vec2f(0.0);
+ var sumR = vec3f(0.0);
+ var sumM = vec3f(0.0);
+ for (var i : i32 = 0; i < N; i = i + 1) {
+ let p = o + dir * (ds * (f32(i) + 0.5));
+ let dens = exp(-(length(p) - R_E) / vec2f(H_R, H_M)) * ds;
+ od = od + dens;
+ let ol = lightDepth(p, sun);
+ if (ol.z > 0.5) {
+ let attn = exp(-(BETA_R * (od.x + ol.x) + BETA_M * haze * 1.1 * (od.y + ol.y)));
+ sumR = sumR + attn * dens.x;
+ sumM = sumM + attn * dens.y;
+ }
+ }
+ let st = lightDepth(o, sun);
+ var trans = vec3f(0.0);
+ if (st.z > 0.5) { trans = exp(-(BETA_R * st.x + BETA_M * haze * 1.1 * st.y)); }
+ var out : Scatter;
+ out.radiance = (sumR * BETA_R * phaseR + sumM * BETA_M * haze * phaseM) * SUN_I;
+ out.sunTrans = trans;
+ return out;
+}
+
+fn cloudField(xz : vec2f, noise : f32, h : f32) -> f32 {
+ if (mc.u_cloudMaskTile <= 0.0) { return noise; }
+ var m = textureSampleLevel(u_cloudMask, u_cloudMask_s, xz / mc.u_cloudMaskTile, 0.0).r;
+ if (mc.u_cloudMask2Tile > 0.0) { m = mix(m, textureSampleLevel(u_cloudMask2, u_cloudMask2_s, xz / mc.u_cloudMask2Tile, 0.0).r, 0.3); }
+ return mix(m, noise, 0.5) - 0.35 * (h - 0.35);
+}
+
+fn cloudDensity(p : vec3f, coverage : f32, base : f32, thick : f32, octaves : i32) -> f32 {
+ let h = clamp((p.y - base) / thick, 0.0, 1.0);
+ let shape = smoothstep(0.0, 0.14, h) * smoothstep(1.0, 0.55, h);
+ let q = vec3f(p.x, p.y * 1.8, p.z) * 0.32;
+ var n = vnoise(q) * 0.58 + vnoise(q * 2.13) * 0.28;
+ if (octaves > 2) { n = n + vnoise(q * 4.7) * 0.14; } else { n = n + 0.07; }
+ n = cloudField(p.xz, n, h);
+ return smoothstep(1.0 - coverage, 1.0 - coverage + 0.22, n) * shape;
+}
+
+fn frame_origin() -> vec3f { return frame.renderOrigin.xyz; }
+
+/// The turn that takes up to +Y by the shortest way: the sky is worked out where up is +Y, so on a round world in the frame of the ground the camera stands on.
+fn upFrame(up : vec3f) -> mat3x3f {
+ let v = vec3f(-up.z, 0.0, up.x);
+ let k = 1.0 / max(1.0 + up.y, 1e-4);
+ let cross = mat3x3f(vec3f(0.0, v.z, -v.y), vec3f(-v.z, 0.0, v.x), vec3f(v.y, -v.x, 0.0));
+ return mat3x3f(vec3f(1.0, 0.0, 0.0), vec3f(0.0, 1.0, 0.0), vec3f(0.0, 0.0, 1.0)) + cross + (cross * cross) * k;
+}
+
+fn cloudShadowAt(world : vec3f, cam : vec3f, sun : vec3f, coverage : f32, overcast : f32, drift : vec3f, px : vec2f) -> f32 {
+ let strength = mc.u_cloudShadow * smoothstep(0.02, 0.14, sun.y);
+ if (strength <= 0.001 || coverage <= 0.01) { return 1.0; }
+ let k = 1.0 / max(mc.u_unitsPerKm, 1e-3);
+ let h = mc.u_altitude + (world.y - cam.y) * k;
+ let tt = (mc.u_cloudBase + mc.u_cloudThickness - h) / sun.y;
+ if (tt <= 0.0) { return 1.0; }
+ let tb = max(mc.u_cloudBase - h, 0.0) / sun.y;
+ let N = 6;
+ let ds = (tt - tb) / f32(N);
+ let sigma = 1.3 * (1.0 + overcast * 1.5);
+ var tau = 0.0;
+ let jitter = ign(px);
+ for (var i : i32 = 0; i < N; i = i + 1) {
+ let t = tb + (f32(i) + jitter) * ds;
+ let p = vec3f(world.x * k + sun.x * t, h + sun.y * t, world.z * k + sun.z * t) + drift;
+ tau = tau + cloudDensity(p, coverage, mc.u_cloudBase, mc.u_cloudThickness, 2) * sigma * ds;
+ }
+ return 1.0 - strength * 0.8 * (1.0 - exp(-tau));
+}
+
+struct CloudLayer { L : vec3f, T : f32 };
+
+/// The cloud slab along dir as a map of what is behind it, colour * T + L. A function of the direction alone, so it can be worked out small and read back big.
+fn cloudLayer(dir : vec3f, sun : vec3f, dome : vec3f, sunTrans : vec3f, night : f32, overcast : f32, windDrift : vec3f, px : vec2f) -> CloudLayer {
+ var out : CloudLayer;
+ out.L = vec3f(0.0);
+ out.T = 1.0;
+ let coverage = max(mc.u_clouds, overcast * 0.95);
+ if (coverage > 0.01 && dir.y > 0.01) {
+ let base = mc.u_cloudBase;
+ let thick = mc.u_cloudThickness;
+ let tb = max(base - mc.u_altitude, 0.0) / dir.y;
+ let tt = (base + thick - mc.u_altitude) / dir.y;
+ let STEPS = 16;
+ let ds = (tt - tb) / f32(STEPS);
+ let jitter = ign(px);
+ let sigma = 1.3 * (1.0 + overcast * 1.5);
+ let drift = windDrift;
+ let cosS = dot(dir, sun);
+ let phase = mix(0.35 + 0.65 * pow(max(cosS, 0.0), 6.0), 0.5, 0.4);
+ let ambient = dome * 0.7 + lin(vec3f(0.55, 0.65, 0.9)) * 0.015 * night;
+ let sunLight = sunTrans * 1.2 * mc.u_cloudBrightness;
+ var trans = 1.0;
+ var lit = vec3f(0.0);
+ for (var i : i32 = 0; i < STEPS; i = i + 1) {
+ if (trans < 0.02) { break; }
+ let t = tb + (f32(i) + jitter) * ds;
+ let p = vec3f(dir.x * t, mc.u_altitude + dir.y * t, dir.z * t) + drift;
+ let d = cloudDensity(p, coverage, base, thick, 3);
+ if (d < 0.002) { continue; }
+ var tau = 0.0;
+ for (var k : i32 = 1; k <= 3; k = k + 1) {
+ let q = p + sun * (0.35 * f32(k));
+ tau = tau + cloudDensity(q, coverage, base, thick, 2) * 0.35 * sigma;
+ }
+ let energy = exp(-tau * 1.1) + 0.25 * exp(-tau * 0.25);
+ let S = sunLight * energy * phase * 2.0 + ambient;
+ let ext = d * sigma * ds;
+ let tr = exp(-ext);
+ lit = lit + trans * (1.0 - tr) * S;
+ trans = trans * tr;
+ }
+ let fade = smoothstep(0.01, 0.14, dir.y);
+ out.T = mix(1.0, trans, fade);
+ out.L = lit * fade;
+ }
+
+ return out;
+}
+
+@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
+ let src = textureSampleLevel(t0, s0, v.v_texCoord, 0.0);
+ let size = vec2i(textureDimensions(t7, 0));
+ let depth = textureLoad(t7, clamp(vec2i(v.v_texCoord * vec2f(size)), vec2i(0), size - vec2i(1)), 0);
+ let covered = mc.u_panorama <= 0.001;
+ let geometry = (covered && depth < 1.0 && src.a > 0.999) || (!covered && depth < 1.0);
+ if (geometry && mc.u_cloudShadow <= 0.001) { return vec4f(1.0, 0.0, 0.0, -1.0); }
+
+ let entry = worldFromDepth(v.v_texCoord, 0.0);
+ var dir = normalize(worldFromDepth(v.v_texCoord, 1.0) - entry);
+
+ let found = findSun();
+ let follow = mc.u_followSun > 0.5 && found.dir.w > 0.5;
+ let az = radians(mc.u_sunAzimuth);
+ let el = radians(mc.u_sunElevation);
+ var sun = vec3f(cos(el) * sin(az), sin(el), cos(el) * cos(az));
+ if (follow) { sun = found.dir.xyz; }
+ if (mc.u_planetRadius > 0.0) {
+ let frame = upFrame(normalize(entry + frame_origin() - vec3f(mc.u_planetX, mc.u_planetY, mc.u_planetZ)));
+ dir = frame * dir;
+ sun = frame * sun;
+ }
+ let se = sun.y;
+
+ let overcast = clamp(mc.u_overcast, 0.0, 1.0);
+ let haze = mc.u_haze * (1.0 + overcast * 2.0);
+
+ let entryWorld = entry + frame.renderOrigin.xyz;
+ var anchor = vec3f(0.0);
+ if (mc.u_cloudShadow > 0.001) { anchor = vec3f(entryWorld.x, 0.0, entryWorld.z) / max(mc.u_unitsPerKm, 1e-3); }
+ let windAngle = radians(mc.u_windDirection);
+ let windDrift = vec3f(sin(windAngle), 0.0, cos(windAngle)) * (mc.u_windSpeed * tc.u_time.x) + anchor;
+ if (geometry) {
+ let lit = cloudShadowAt(worldFromDepth(v.v_texCoord, depth) + frame.renderOrigin.xyz, entryWorld, sun, max(mc.u_clouds, overcast * 0.95), overcast, windDrift - anchor, v.pos.xy);
+ return vec4f(lit, 0.0, 0.0, -lit);
+ }
+
+ let sdir = normalize(vec3f(dir.x, max(dir.y, 0.015), dir.z));
+ let sc = scatter(sdir, sun, mc.u_altitude, haze);
+ let sunTrans = sc.sunTrans;
+ var col = sc.radiance * 0.55 * mc.u_intensity;
+ col = col * (1.0 - 0.75 * smoothstep(0.0, -0.3, dir.y));
+ let night = 1.0 - smoothstep(-0.10, 0.12, se);
+ col = col + mix(lin(vec3f(0.003, 0.006, 0.02)), lin(vec3f(0.016, 0.024, 0.06)), pow(1.0 - max(dir.y, 0.0), 3.0)) * night;
+ let dome = col;
+ let cl = cloudLayer(dir, sun, dome, sunTrans, night, overcast, windDrift, v.pos.xy);
+ return vec4f(cl.L, cl.T);
+}
+#pragma end
+`;
+ return Material.compileShader(source);
+ },
+ /**
+ * Exponential height fog with the atmosphere's two tells: the sun's glow in the haze and a
+ * sky that thickens toward the horizon.
+ *
+ * Pass 2 of 2, the composite. The light the sun sends through the haze (through the shadow map, and the clouds' shafts) is the dear part
+ * and smooth, so the first pass walks it at half size and this one reads it back by how near each texel's ray is to the pixel's (see
+ * createHeightFogVolume); the haze itself, which is analytic, is drawn here at full size.
+ */
+ createHeightFog(): ShaderHandle {
+ const source = `#pragma shader "PP Height Fog"
+#pragma version 300 es
+#pragma domain PostProcess
+#pragma param u_fogColor color default(0.62,0.7,0.8,1)
+#pragma param u_sunColor color default(1,0.82,0.55,1)
+#pragma param u_density float default(0.0015) range(0,0.05)
+#pragma param u_heightFalloff float default(0.003) range(0,0.1)
+#pragma param u_baseHeight float default(0) range(-1000,5000)
+#pragma param u_planetRadius float default(0) range(0,100000000)
+#pragma param u_planetX float default(0) range(-100000000,100000000)
+#pragma param u_planetY float default(0) range(-100000000,100000000)
+#pragma param u_planetZ float default(0) range(-100000000,100000000)
+#pragma param u_startDistance float default(0) range(0,3000)
+#pragma param u_maxOpacity float default(1) range(0,1)
+#pragma param u_sunAzimuth float default(40) range(0,360)
+#pragma param u_sunElevation float default(25) range(-90,90)
+#pragma param u_inscatter float default(0.6) range(0,2)
+#pragma param u_sunExponent float default(8) range(1,64)
+#pragma param u_skyFog float default(0.8) range(0,1)
+#pragma param u_followSun float default(1) range(0,1)
+#pragma param u_volumetric float default(0) range(0,1)
+#pragma param u_volumeRange float default(3000) range(100,1000000)
+#pragma param u_cloudShade float default(0) range(0,1)
+#pragma param u_skyCover float default(0) range(0,1)
+#pragma param u_skyBase float default(1.5) range(0.2,10)
+#pragma param u_skyThickness float default(2.5) range(0.2,8)
+#pragma param u_skyAltitude float default(0.1) range(0,20)
+#pragma param u_skyUnits float default(1000) range(1,100000)
+#pragma param u_skySigma float default(1.3) range(0,10)
+#pragma param u_skyWindX float default(0) range(-2,2)
+#pragma param u_skyWindZ float default(0) range(-2,2)
+#pragma param u_cloudMask texture default(black)
+#pragma param u_cloudMaskTile float default(0) range(0,100)
+#pragma param u_cloudMask2 texture default(black)
+#pragma param u_cloudMask2Tile float default(0) range(0,100)
+#pragma lights
+
+#pragma fragment
+precision highp float;
+
+in vec2 v_texCoord;
+uniform sampler2D u_texture;
+uniform sampler2D u_sceneTexture;
+uniform highp sampler2D u_sceneDepth;
+out vec4 fragColor;
+
+${SSAO_GLSL}
+
+const float FAR_RAY = 100000.0;
+const int VOLUME_STEPS = 24;
+
+float ign(vec2 p) {
+ return fract(52.9829189 * fract(dot(p, vec2(0.06711056, 0.00583715))));
+}
+/// How much of a ray's light a particle scatters toward the viewer: forward, the way fog glows around the sun.
+float phaseHG(float cosine) {
+ const float g = 0.55;
+ return (1.0 - g * g) / pow(max(1.0 + g * g - 2.0 * g * cosine, 1e-3), 1.5) * 0.25;
+}
+
+
+float hash13(vec3 p) {
+ p = fract(p * 0.1031);
+ p += dot(p, p.zyx + 31.32);
+ return fract((p.x + p.y) * p.z);
+}
+float vnoise(vec3 p) {
+ vec3 i = floor(p);
+ vec3 f = fract(p);
+ f = f * f * (3.0 - 2.0 * f);
+ float a = mix(hash13(i), hash13(i + vec3(1, 0, 0)), f.x);
+ float b = mix(hash13(i + vec3(0, 1, 0)), hash13(i + vec3(1, 1, 0)), f.x);
+ float c = mix(hash13(i + vec3(0, 0, 1)), hash13(i + vec3(1, 0, 1)), f.x);
+ float d = mix(hash13(i + vec3(0, 1, 1)), hash13(i + vec3(1, 1, 1)), f.x);
+ return mix(mix(a, b, f.y), mix(c, d, f.y), f.z);
+}
+/// Where a coverage texture is given, the shape of the clouds on the ground is its, and the noise erodes it. The layers are tiled in kilometres
+/// of the cloud plane, the second a softer, higher one folded in.
+float cloudField(vec2 xz, float noise, float h) {
+ if (u_cloudMaskTile <= 0.0) return noise;
+ float m = textureLod(u_cloudMask, xz / u_cloudMaskTile, 0.0).r;
+ if (u_cloudMask2Tile > 0.0) m = mix(m, textureLod(u_cloudMask2, xz / u_cloudMask2Tile, 0.0).r, 0.3);
+ // A mask knows nothing of height, and a column of it seen along the slab is a streak: the cloud's top stands only over the mask's core, a dome.
+ return mix(m, noise, 0.5) - 0.35 * (h - 0.35);
+}
+
+/// The sky's cloud density, the same one the dome draws (see skyAtmosphere), at a point in kilometres.
+float cloudDensity(vec3 p, float coverage, float base, float thick) {
+ float h = clamp((p.y - base) / thick, 0.0, 1.0);
+ float shape = smoothstep(0.0, 0.14, h) * smoothstep(1.0, 0.55, h);
+ vec3 q = vec3(p.x, p.y * 1.8, p.z) * 0.32;
+ float n = cloudField(p.xz, vnoise(q) * 0.58 + vnoise(q * 2.13) * 0.28 + 0.07, h);
+ return smoothstep(1.0 - coverage, 1.0 - coverage + 0.22, n) * shape;
+}
+/// How much of the sun reaches the air at @p world through the cloud layer, 1 being all of it: the sun's ray marched up through the slab.
+float cloudLight(vec3 world, float camY, vec3 sun, vec3 drift) {
+ if (u_cloudShade <= 0.001 || u_skyCover <= 0.01 || sun.y <= 0.02) return 1.0;
+ float k = 1.0 / max(u_skyUnits, 1e-3);
+ float h = u_skyAltitude + (world.y - camY) * k;
+ float tt = (u_skyBase + u_skyThickness - h) / sun.y;
+ if (tt <= 0.0) return 1.0;
+ float tb = max(u_skyBase - h, 0.0) / sun.y;
+ const int N = 4;
+ float ds = (tt - tb) / float(N);
+ float tau = 0.0;
+ for (int i = 0; i < N; ++i) {
+ float t = tb + (float(i) + 0.5) * ds;
+ vec3 q = vec3(world.x * k + sun.x * t, h + sun.y * t, world.z * k + sun.z * t) + drift;
+ tau += cloudDensity(q, u_skyCover, u_skyBase, u_skyThickness) * u_skySigma * ds;
+ }
+ return 1.0 - u_cloudShade * (1.0 - exp(-tau));
+}
+
+/// The height of a point above the ground the fog lies on: world Y on a flat world, the distance above the sphere's surface on a planet (never below it: inside the planet the fog is the ground's, not an exponential of a depth).
+float fogAltitude(vec3 p) {
+ vec3 a = p + u_renderOrigin.xyz;
+ return u_planetRadius > 0.0 ? max(length(a - vec3(u_planetX, u_planetY, u_planetZ)) - u_planetRadius, 0.0) : a.y;
+}
+/// What the exponential density integrates to along a ray: the eye's density, the length,
+/// and the term for how fast the ray leaves the fog. Written so a level ray (no climb)
+/// needs no division, and so a long ray downward cannot overflow.
+float opticalDepth(float eyeY, float dirY, float len) {
+ float k = u_heightFalloff;
+ float dens = u_density * exp(clamp(-k * (eyeY - u_baseHeight), -60.0, 60.0));
+ float kd = k * dirY * len;
+ float span = abs(kd) < 1e-4 ? 1.0 : (1.0 - exp(clamp(-kd, -60.0, 60.0))) / kd;
+ return dens * len * span;
+}
+
+/// The sun's light through the haze, where the shadow map says it gets through: the air between the camera and the surface is walked in steps,
+/// and each one that stands in light adds what it scatters toward the eye. What the sky's clouds let through of the sun is in it too.
+vec3 volumeLight(vec3 entry, vec3 dir, float len, bool sky, vec3 sun, vec4 lightColor, ivec2 tiles, vec3 sunTint) {
+ float range = min(len, u_volumeRange);
+ float span = max(range - u_startDistance, 0.0);
+ float ds = span / float(VOLUME_STEPS);
+ float jitter = ign(gl_FragCoord.xy);
+ float transmit = 1.0;
+ float lit = 0.0;
+ // The clouds are as the sky draws them, fixed in the world and drifting; their light changes slowly along the ray, so every third step asks.
+ vec3 drift = vec3(u_skyWindX, 0.0, u_skyWindZ) * u_time.x;
+ float shade = 1.0;
+ for (int i = 0; i < VOLUME_STEPS; ++i) {
+ float t = u_startDistance + (float(i) + jitter) * ds;
+ vec3 p = entry + dir * t;
+ if (i % 3 == 0) shade = cloudLight(p + u_renderOrigin.xyz, entry.y + u_renderOrigin.y, sun, drift);
+ float dens = u_density * exp(clamp(-u_heightFalloff * (fogAltitude(p) - u_baseHeight), -60.0, 60.0));
+ lit += transmit * dens * ds * sunVisibility(p, tiles.x, tiles.y) * shade;
+ transmit *= exp(-dens * ds);
+ }
+ float strength = clamp(lightColor.a, 0.0, 8.0) * 0.5;
+ return sunTint * lit * phaseHG(dot(dir, sun)) * u_inscatter * u_volumetric * strength * (sky ? u_skyFog : 1.0);
+}
+
+/// The volumetric light for a pixel, read from the half-size pass: the four texels it falls between, each weighted by how near its ray's length
+/// is to this pixel's, so a texel across a silhouette lends little of its light. Always an answer: a pixel that walked the air itself would
+/// hold up every pixel beside it in its warp, and a leaf's edge is such a pixel.
+bool upsampledVolume(float len, out vec3 add) {
+ ivec2 dsize = textureSize(u_sceneDepth, 0);
+ ivec2 hsize = textureSize(u_texture, 0);
+ vec2 hf = v_texCoord * vec2(hsize) - 0.5;
+ ivec2 base = ivec2(floor(hf));
+ vec2 fr = hf - vec2(base);
+ vec3 sum = vec3(0.0);
+ float wsum = 0.0;
+ for (int j = 0; j < 4; ++j) {
+ ivec2 o = ivec2(j & 1, j >> 1);
+ ivec2 t = clamp(base + o, ivec2(0), hsize - ivec2(1));
+ vec2 uv = uvOfTexel(t, hsize);
+ float dj = depthAt(screenTexel(uv, dsize), dsize);
+ float lj = dj >= 1.0 ? FAR_RAY : length(worldFromDepth(uv, dj) - worldFromDepth(uv, 0.0));
+ float bw = (o.x == 1 ? fr.x : 1.0 - fr.x) * (o.y == 1 ? fr.y : 1.0 - fr.y);
+ float rel = abs(lj - len) / max(len, 1.0);
+ float w = bw / (1.0 + 400.0 * rel * rel);
+ sum += texelFetch(u_texture, t, 0).rgb * w;
+ wsum += w;
+ }
+ add = sum / wsum;
+ return true;
+}
+
+void main() {
+ vec4 src = texture(u_sceneTexture, v_texCoord);
+ ivec2 size = textureSize(u_sceneDepth, 0);
+ float depth = texelFetch(u_sceneDepth, clamp(ivec2(v_texCoord * vec2(size)), ivec2(0), size - ivec2(1)), 0).r;
+ bool sky = depth >= 1.0;
+
+ // The ray starts on the near plane, which is the one place that is a point under a
+ // perspective camera and under an orthographic one alike.
+ vec3 entry = worldFromDepth(v_texCoord, 0.0);
+ vec3 target = worldFromDepth(v_texCoord, sky ? 1.0 : depth);
+ vec3 dir = normalize(target - entry);
+ float len = sky ? FAR_RAY : length(target - entry);
+ float effective = max(len - u_startDistance, 0.0);
+ if (effective <= 0.0) { fragColor = src; return; }
+
+ float eyeAlt = fogAltitude(entry + dir * u_startDistance);
+ float climb = dir.y;
+ if (u_planetRadius > 0.0) {
+ // The ground curves away: the climb is the rise between the ray's ends, and a sky ray climbs as it points.
+ vec3 up = normalize(entry + dir * u_startDistance + u_renderOrigin.xyz - vec3(u_planetX, u_planetY, u_planetZ));
+ climb = sky ? dot(dir, up) : (fogAltitude(target) - eyeAlt) / effective;
+ }
+ float tau = opticalDepth(eyeAlt, climb, effective);
+ float amount = (1.0 - exp(-tau)) * u_maxOpacity * (sky ? u_skyFog : 1.0);
+
+ // The sun is the scene's own directional light unless the effect is told to ignore it: where it stands and
+ // what colour it is come from the light block, so a sun that moves takes its haze with it.
+ vec4 lightDir, lightColor;
+ ivec2 tiles;
+ findSun(lightDir, lightColor, tiles);
+ bool follow = u_followSun > 0.5 && lightDir.w > 0.5;
+ float az = radians(u_sunAzimuth);
+ float el = radians(u_sunElevation);
+ vec3 sun = follow ? lightDir.xyz : vec3(cos(el) * sin(az), sin(el), cos(el) * cos(az));
+ float glow = pow(max(dot(dir, sun), 0.0), u_sunExponent);
+ vec3 fog = u_fogColor.rgb;
+ vec3 sunTint = u_sunColor.rgb;
+#ifdef ES_LINEAR
+ fog = srgbToLinear(fog);
+ sunTint = srgbToLinear(sunTint);
+#endif
+ // The light's own colour is already in the pipeline's space; only its hue is wanted, not its strength.
+ if (follow) sunTint = lightColor.rgb / max(max(lightColor.r, lightColor.g), max(lightColor.b, 1e-3));
+ float shaped = u_volumetric > 0.0 ? glow * (1.0 - u_volumetric) : glow;
+ vec3 colour = mix(fog, sunTint, clamp(shaped * u_inscatter, 0.0, 1.0));
+ vec3 result = mix(src.rgb, colour, clamp(amount, 0.0, 1.0));
+
+ // The sun's light through the haze: read from the half-size pass, walked here only where it has nothing for this pixel.
+ if (u_volumetric > 0.0 && follow) {
+ vec3 add;
+ if (!upsampledVolume(len, add)) add = volumeLight(entry, dir, len, sky, sun, lightColor, tiles, sunTint);
+ result += add;
+ }
+ fragColor = vec4(result, src.a);
+}
+#pragma end
+
+#pragma fragment wgsl
+${SSAO_WGSL}
+const FAR_RAY : f32 = 100000.0;
+const VOLUME_STEPS : i32 = 24;
+
+fn ign(p : vec2f) -> f32 {
+ return fract(52.9829189 * fract(dot(p, vec2f(0.06711056, 0.00583715))));
+}
+fn phaseHG(cosine : f32) -> f32 {
+ let g = 0.55;
+ return (1.0 - g * g) / pow(max(1.0 + g * g - 2.0 * g * cosine, 1e-3), 1.5) * 0.25;
+}
+
+fn hash13(p0 : vec3f) -> f32 {
+ var p = fract(p0 * 0.1031);
+ p = p + dot(p, p.zyx + 31.32);
+ return fract((p.x + p.y) * p.z);
+}
+fn vnoise(p : vec3f) -> f32 {
+ let i = floor(p);
+ var f = fract(p);
+ f = f * f * (3.0 - 2.0 * f);
+ let a = mix(hash13(i), hash13(i + vec3f(1, 0, 0)), f.x);
+ let b = mix(hash13(i + vec3f(0, 1, 0)), hash13(i + vec3f(1, 1, 0)), f.x);
+ let c = mix(hash13(i + vec3f(0, 0, 1)), hash13(i + vec3f(1, 0, 1)), f.x);
+ let d = mix(hash13(i + vec3f(0, 1, 1)), hash13(i + vec3f(1, 1, 1)), f.x);
+ return mix(mix(a, b, f.y), mix(c, d, f.y), f.z);
+}
+fn cloudField(xz : vec2f, noise : f32, h : f32) -> f32 {
+ if (mc.u_cloudMaskTile <= 0.0) { return noise; }
+ var m = textureSampleLevel(u_cloudMask, u_cloudMask_s, xz / mc.u_cloudMaskTile, 0.0).r;
+ if (mc.u_cloudMask2Tile > 0.0) { m = mix(m, textureSampleLevel(u_cloudMask2, u_cloudMask2_s, xz / mc.u_cloudMask2Tile, 0.0).r, 0.3); }
+ return mix(m, noise, 0.5) - 0.35 * (h - 0.35);
+}
+
+fn cloudDensity(p : vec3f, coverage : f32, base : f32, thick : f32) -> f32 {
+ let h = clamp((p.y - base) / thick, 0.0, 1.0);
+ let shape = smoothstep(0.0, 0.14, h) * smoothstep(1.0, 0.55, h);
+ let q = vec3f(p.x, p.y * 1.8, p.z) * 0.32;
+ let n = cloudField(p.xz, vnoise(q) * 0.58 + vnoise(q * 2.13) * 0.28 + 0.07, h);
+ return smoothstep(1.0 - coverage, 1.0 - coverage + 0.22, n) * shape;
+}
+fn cloudLight(world : vec3f, camY : f32, sun : vec3f, drift : vec3f) -> f32 {
+ if (mc.u_cloudShade <= 0.001 || mc.u_skyCover <= 0.01 || sun.y <= 0.02) { return 1.0; }
+ let k = 1.0 / max(mc.u_skyUnits, 1e-3);
+ let h = mc.u_skyAltitude + (world.y - camY) * k;
+ let tt = (mc.u_skyBase + mc.u_skyThickness - h) / sun.y;
+ if (tt <= 0.0) { return 1.0; }
+ let tb = max(mc.u_skyBase - h, 0.0) / sun.y;
+ let N = 4;
+ let ds = (tt - tb) / f32(N);
+ var tau = 0.0;
+ for (var i : i32 = 0; i < N; i = i + 1) {
+ let t = tb + (f32(i) + 0.5) * ds;
+ let q = vec3f(world.x * k + sun.x * t, h + sun.y * t, world.z * k + sun.z * t) + drift;
+ tau = tau + cloudDensity(q, mc.u_skyCover, mc.u_skyBase, mc.u_skyThickness) * mc.u_skySigma * ds;
+ }
+ return 1.0 - mc.u_cloudShade * (1.0 - exp(-tau));
+}
+
+fn fogAltitude(p : vec3f) -> f32 {
+ let a = p + frame.renderOrigin.xyz;
+ if (mc.u_planetRadius > 0.0) { return max(length(a - vec3f(mc.u_planetX, mc.u_planetY, mc.u_planetZ)) - mc.u_planetRadius, 0.0); }
+ return a.y;
+}
+fn opticalDepth(eyeY : f32, dirY : f32, len : f32) -> f32 {
+ let k = mc.u_heightFalloff;
+ let dens = mc.u_density * exp(clamp(-k * (eyeY - mc.u_baseHeight), -60.0, 60.0));
+ let kd = k * dirY * len;
+ var span = 1.0;
+ if (abs(kd) >= 1e-4) { span = (1.0 - exp(clamp(-kd, -60.0, 60.0))) / kd; }
+ return dens * len * span;
+}
+
+/// The sun's light through the haze, where the shadow map says it gets through. See the GLSL twin.
+fn volumeLight(entry : vec3f, dir : vec3f, len : f32, sky : bool, sun : vec3f, found : Sun, sunTint : vec3f, px : vec2f) -> vec3f {
+ let range = min(len, mc.u_volumeRange);
+ let span = max(range - mc.u_startDistance, 0.0);
+ let ds = span / f32(VOLUME_STEPS);
+ let jitter = ign(px);
+ var transmit = 1.0;
+ var lit = 0.0;
+ let drift = vec3f(mc.u_skyWindX, 0.0, mc.u_skyWindZ) * tc.u_time.x;
+ var shade = 1.0;
+ for (var i : i32 = 0; i < VOLUME_STEPS; i = i + 1) {
+ let t = mc.u_startDistance + (f32(i) + jitter) * ds;
+ let p = entry + dir * t;
+ if (i % 3 == 0) { shade = cloudLight(p + frame.renderOrigin.xyz, entry.y + frame.renderOrigin.y, sun, drift); }
+ let dens = mc.u_density * exp(clamp(-mc.u_heightFalloff * (fogAltitude(p) - mc.u_baseHeight), -60.0, 60.0));
+ lit = lit + transmit * dens * ds * sunVisibility(p, found.tiles.x, found.tiles.y) * shade;
+ transmit = transmit * exp(-dens * ds);
+ }
+ let strength = clamp(found.color.a, 0.0, 8.0) * 0.5;
+ var skyShare = 1.0;
+ if (sky) { skyShare = mc.u_skyFog; }
+ return sunTint * lit * phaseHG(dot(dir, sun)) * mc.u_inscatter * mc.u_volumetric * strength * skyShare;
+}
+
+struct VolumeRead { ok : bool, add : vec3f };
+
+/// The volumetric light for a pixel, read from the half-size pass. See the GLSL twin.
+fn upsampledVolume(uv : vec2f, len : f32) -> VolumeRead {
+ var out : VolumeRead;
+ out.ok = false;
+ let dsize = vec2i(textureDimensions(t7, 0));
+ let hsize = vec2i(textureDimensions(t0, 0));
+ let hf = uv * vec2f(hsize) - 0.5;
+ let base = vec2i(floor(hf));
+ let fr = hf - vec2f(base);
+ var sum = vec3f(0.0);
+ var wsum = 0.0;
+ for (var j : i32 = 0; j < 4; j = j + 1) {
+ let o = vec2i(j & 1, j >> 1);
+ let t = clamp(base + o, vec2i(0), hsize - vec2i(1));
+ let tuv = uvOfTexel(t, hsize);
+ let dj = depthAt(screenTexel(tuv, dsize), dsize);
+ var lj = FAR_RAY;
+ if (dj < 1.0) { lj = length(worldFromDepth(tuv, dj) - worldFromDepth(tuv, 0.0)); }
+ var bx = 1.0 - fr.x;
+ if (o.x == 1) { bx = fr.x; }
+ var by = 1.0 - fr.y;
+ if (o.y == 1) { by = fr.y; }
+ let rel = abs(lj - len) / max(len, 1.0);
+ let w = bx * by / (1.0 + 400.0 * rel * rel);
+ sum = sum + textureLoad(t0, t, 0).rgb * w;
+ wsum = wsum + w;
+ }
+ out.ok = true;
+ out.add = sum / wsum;
+ return out;
+}
+
+@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
+ let src = textureSampleLevel(t1, s1, v.v_texCoord, 0.0);
+ let size = vec2i(textureDimensions(t7, 0));
+ let depth = textureLoad(t7, clamp(vec2i(v.v_texCoord * vec2f(size)), vec2i(0), size - vec2i(1)), 0);
+ let sky = depth >= 1.0;
+
+ let entry = worldFromDepth(v.v_texCoord, 0.0);
+ var d = depth;
+ if (sky) { d = 1.0; }
+ let tgt = worldFromDepth(v.v_texCoord, d);
+ let dir = normalize(tgt - entry);
+ var len = length(tgt - entry);
+ if (sky) { len = FAR_RAY; }
+ let effective = max(len - mc.u_startDistance, 0.0);
+ if (effective <= 0.0) { return src; }
+
+ let eyeAlt = fogAltitude(entry + dir * mc.u_startDistance);
+ var climb = dir.y;
+ if (mc.u_planetRadius > 0.0) {
+ let up = normalize(entry + dir * mc.u_startDistance + frame.renderOrigin.xyz - vec3f(mc.u_planetX, mc.u_planetY, mc.u_planetZ));
+ if (sky) { climb = dot(dir, up); } else { climb = (fogAltitude(tgt) - eyeAlt) / effective; }
+ }
+ let tau = opticalDepth(eyeAlt, climb, effective);
+ var amount = (1.0 - exp(-tau)) * mc.u_maxOpacity;
+ if (sky) { amount = amount * mc.u_skyFog; }
+
+ // See the GLSL twin: the scene's own sun unless told not to follow it.
+ let found = findSun();
+ let follow = mc.u_followSun > 0.5 && found.dir.w > 0.5;
+ let az = radians(mc.u_sunAzimuth);
+ let el = radians(mc.u_sunElevation);
+ var sun = vec3f(cos(el) * sin(az), sin(el), cos(el) * cos(az));
+ if (follow) { sun = found.dir.xyz; }
+ var shaped = dot(dir, sun);
+ let glow = pow(max(shaped, 0.0), mc.u_sunExponent);
+ var fog = mc.u_fogColor.rgb;
+ var sunTint = mc.u_sunColor.rgb;
+#ifdef ES_LINEAR
+ fog = srgbToLinear(fog);
+ sunTint = srgbToLinear(sunTint);
+#endif
+ if (follow) { sunTint = found.color.rgb / max(max(found.color.r, found.color.g), max(found.color.b, 1e-3)); }
+ var glowShare = glow;
+ if (mc.u_volumetric > 0.0) { glowShare = glow * (1.0 - mc.u_volumetric); }
+ let colour = mix(fog, sunTint, clamp(glowShare * mc.u_inscatter, 0.0, 1.0));
+ var result = mix(src.rgb, colour, clamp(amount, 0.0, 1.0));
+
+ if (mc.u_volumetric > 0.0 && follow) {
+ let up = upsampledVolume(v.v_texCoord, len);
+ if (up.ok) {
+ result = result + up.add;
+ } else {
+ result = result + volumeLight(entry, dir, len, sky, sun, found, sunTint, v.pos.xy);
+ }
+ }
+ return vec4f(result, src.a);
+}
+#pragma end
+`;
+ return Material.compileShader(source);
+ },
+ /** The volumetric light of the height fog at half size: pass 1 of 2. A texel holds the light the sun sends toward the eye along its ray. */
+ createHeightFogVolume(): ShaderHandle {
+ const source = `#pragma shader "PP Height Fog Volume"
+#pragma version 300 es
+#pragma domain PostProcess
+#pragma param u_fogColor color default(0.62,0.7,0.8,1)
+#pragma param u_sunColor color default(1,0.82,0.55,1)
+#pragma param u_density float default(0.0015) range(0,0.05)
+#pragma param u_heightFalloff float default(0.003) range(0,0.1)
+#pragma param u_baseHeight float default(0) range(-1000,5000)
+#pragma param u_planetRadius float default(0) range(0,100000000)
+#pragma param u_planetX float default(0) range(-100000000,100000000)
+#pragma param u_planetY float default(0) range(-100000000,100000000)
+#pragma param u_planetZ float default(0) range(-100000000,100000000)
+#pragma param u_startDistance float default(0) range(0,3000)
+#pragma param u_maxOpacity float default(1) range(0,1)
+#pragma param u_sunAzimuth float default(40) range(0,360)
+#pragma param u_sunElevation float default(25) range(-90,90)
+#pragma param u_inscatter float default(0.6) range(0,2)
+#pragma param u_sunExponent float default(8) range(1,64)
+#pragma param u_skyFog float default(0.8) range(0,1)
+#pragma param u_followSun float default(1) range(0,1)
+#pragma param u_volumetric float default(0) range(0,1)
+#pragma param u_volumeRange float default(3000) range(100,1000000)
+#pragma param u_cloudShade float default(0) range(0,1)
+#pragma param u_skyCover float default(0) range(0,1)
+#pragma param u_skyBase float default(1.5) range(0.2,10)
+#pragma param u_skyThickness float default(2.5) range(0.2,8)
+#pragma param u_skyAltitude float default(0.1) range(0,20)
+#pragma param u_skyUnits float default(1000) range(1,100000)
+#pragma param u_skySigma float default(1.3) range(0,10)
+#pragma param u_skyWindX float default(0) range(-2,2)
+#pragma param u_skyWindZ float default(0) range(-2,2)
+#pragma param u_cloudMask texture default(black)
+#pragma param u_cloudMaskTile float default(0) range(0,100)
+#pragma param u_cloudMask2 texture default(black)
+#pragma param u_cloudMask2Tile float default(0) range(0,100)
+#pragma lights
+#pragma highprecision
+
+#pragma fragment
+precision highp float;
+
+in vec2 v_texCoord;
+uniform sampler2D u_texture;
+uniform highp sampler2D u_sceneDepth;
+out vec4 fragColor;
+
+const float FAR_RAY = 100000.0;
+const int VOLUME_STEPS = 24;
+
+float ign(vec2 p) {
+ return fract(52.9829189 * fract(dot(p, vec2(0.06711056, 0.00583715))));
+}
+/// How much of a ray's light a particle scatters toward the viewer: forward, the way fog glows around the sun.
+float phaseHG(float cosine) {
+ const float g = 0.55;
+ return (1.0 - g * g) / pow(max(1.0 + g * g - 2.0 * g * cosine, 1e-3), 1.5) * 0.25;
+}
+
+
+float hash13(vec3 p) {
+ p = fract(p * 0.1031);
+ p += dot(p, p.zyx + 31.32);
+ return fract((p.x + p.y) * p.z);
+}
+float vnoise(vec3 p) {
+ vec3 i = floor(p);
+ vec3 f = fract(p);
+ f = f * f * (3.0 - 2.0 * f);
+ float a = mix(hash13(i), hash13(i + vec3(1, 0, 0)), f.x);
+ float b = mix(hash13(i + vec3(0, 1, 0)), hash13(i + vec3(1, 1, 0)), f.x);
+ float c = mix(hash13(i + vec3(0, 0, 1)), hash13(i + vec3(1, 0, 1)), f.x);
+ float d = mix(hash13(i + vec3(0, 1, 1)), hash13(i + vec3(1, 1, 1)), f.x);
+ return mix(mix(a, b, f.y), mix(c, d, f.y), f.z);
+}
+/// Where a coverage texture is given, the shape of the clouds on the ground is its, and the noise erodes it. The layers are tiled in kilometres
+/// of the cloud plane, the second a softer, higher one folded in.
+float cloudField(vec2 xz, float noise, float h) {
+ if (u_cloudMaskTile <= 0.0) return noise;
+ float m = textureLod(u_cloudMask, xz / u_cloudMaskTile, 0.0).r;
+ if (u_cloudMask2Tile > 0.0) m = mix(m, textureLod(u_cloudMask2, xz / u_cloudMask2Tile, 0.0).r, 0.3);
+ // A mask knows nothing of height, and a column of it seen along the slab is a streak: the cloud's top stands only over the mask's core, a dome.
+ return mix(m, noise, 0.5) - 0.35 * (h - 0.35);
+}
+
+/// The sky's cloud density, the same one the dome draws (see skyAtmosphere), at a point in kilometres.
float cloudDensity(vec3 p, float coverage, float base, float thick) {
float h = clamp((p.y - base) / thick, 0.0, 1.0);
float shape = smoothstep(0.0, 0.14, h) * smoothstep(1.0, 0.55, h);
@@ -740,8 +1890,31 @@ float opticalDepth(float eyeY, float dirY, float len) {
return dens * len * span;
}
+/// The sun's light through the haze, where the shadow map says it gets through: the air between the camera and the surface is walked in steps,
+/// and each one that stands in light adds what it scatters toward the eye. What the sky's clouds let through of the sun is in it too.
+vec3 volumeLight(vec3 entry, vec3 dir, float len, bool sky, vec3 sun, vec4 lightColor, ivec2 tiles, vec3 sunTint) {
+ float range = min(len, u_volumeRange);
+ float span = max(range - u_startDistance, 0.0);
+ float ds = span / float(VOLUME_STEPS);
+ float jitter = ign(gl_FragCoord.xy);
+ float transmit = 1.0;
+ float lit = 0.0;
+ // The clouds are as the sky draws them, fixed in the world and drifting; their light changes slowly along the ray, so every third step asks.
+ vec3 drift = vec3(u_skyWindX, 0.0, u_skyWindZ) * u_time.x;
+ float shade = 1.0;
+ for (int i = 0; i < VOLUME_STEPS; ++i) {
+ float t = u_startDistance + (float(i) + jitter) * ds;
+ vec3 p = entry + dir * t;
+ if (i % 3 == 0) shade = cloudLight(p + u_renderOrigin.xyz, entry.y + u_renderOrigin.y, sun, drift);
+ float dens = u_density * exp(clamp(-u_heightFalloff * (fogAltitude(p) - u_baseHeight), -60.0, 60.0));
+ lit += transmit * dens * ds * sunVisibility(p, tiles.x, tiles.y) * shade;
+ transmit *= exp(-dens * ds);
+ }
+ float strength = clamp(lightColor.a, 0.0, 8.0) * 0.5;
+ return sunTint * lit * phaseHG(dot(dir, sun)) * u_inscatter * u_volumetric * strength * (sky ? u_skyFog : 1.0);
+}
+
void main() {
- vec4 src = texture(u_texture, v_texCoord);
ivec2 size = textureSize(u_sceneDepth, 0);
float depth = texelFetch(u_sceneDepth, clamp(ivec2(v_texCoord * vec2(size)), ivec2(0), size - ivec2(1)), 0).r;
bool sky = depth >= 1.0;
@@ -753,20 +1926,11 @@ void main() {
vec3 dir = normalize(target - entry);
float len = sky ? FAR_RAY : length(target - entry);
float effective = max(len - u_startDistance, 0.0);
- if (effective <= 0.0) { fragColor = src; return; }
+
- float eyeAlt = fogAltitude(entry + dir * u_startDistance);
- float climb = dir.y;
- if (u_planetRadius > 0.0) {
- // The ground curves away: the climb is the rise between the ray's ends, and a sky ray climbs as it points.
- vec3 up = normalize(entry + dir * u_startDistance + u_renderOrigin.xyz - vec3(u_planetX, u_planetY, u_planetZ));
- climb = sky ? dot(dir, up) : (fogAltitude(target) - eyeAlt) / effective;
- }
- float tau = opticalDepth(eyeAlt, climb, effective);
- float amount = (1.0 - exp(-tau)) * u_maxOpacity * (sky ? u_skyFog : 1.0);
- // The sun is the scene's own directional light unless the effect is told to ignore it: where it stands and
- // what colour it is come from the light block, so a sun that moves takes its haze with it.
+ if (u_volumetric <= 0.0 || effective <= 0.0) { fragColor = vec4(0.0); return; }
+
vec4 lightDir, lightColor;
ivec2 tiles;
findSun(lightDir, lightColor, tiles);
@@ -774,7 +1938,7 @@ void main() {
float az = radians(u_sunAzimuth);
float el = radians(u_sunElevation);
vec3 sun = follow ? lightDir.xyz : vec3(cos(el) * sin(az), sin(el), cos(el) * cos(az));
- float glow = pow(max(dot(dir, sun), 0.0), u_sunExponent);
+ if (!follow) { fragColor = vec4(0.0); return; }
vec3 fog = u_fogColor.rgb;
vec3 sunTint = u_sunColor.rgb;
#ifdef ES_LINEAR
@@ -783,34 +1947,7 @@ void main() {
#endif
// The light's own colour is already in the pipeline's space; only its hue is wanted, not its strength.
if (follow) sunTint = lightColor.rgb / max(max(lightColor.r, lightColor.g), max(lightColor.b, 1e-3));
- float shaped = u_volumetric > 0.0 ? glow * (1.0 - u_volumetric) : glow;
- vec3 colour = mix(fog, sunTint, clamp(shaped * u_inscatter, 0.0, 1.0));
- vec3 result = mix(src.rgb, colour, clamp(amount, 0.0, 1.0));
-
- // The sun's light through the haze, where the shadow map says it gets through: the air between the camera and
- // the surface is walked in steps, and each one that stands in light adds what it scatters toward the eye.
- if (u_volumetric > 0.0 && follow) {
- float range = min(len, u_volumeRange);
- float span = max(range - u_startDistance, 0.0);
- float ds = span / float(VOLUME_STEPS);
- float jitter = ign(gl_FragCoord.xy);
- float transmit = 1.0;
- float lit = 0.0;
- // The clouds are as the sky draws them, fixed in the world and drifting; their light changes slowly along the ray, so every third step asks.
- vec3 drift = vec3(u_skyWindX, 0.0, u_skyWindZ) * u_time.x;
- float shade = 1.0;
- for (int i = 0; i < VOLUME_STEPS; ++i) {
- float t = u_startDistance + (float(i) + jitter) * ds;
- vec3 p = entry + dir * t;
- if (i % 3 == 0) shade = cloudLight(p + u_renderOrigin.xyz, entry.y + u_renderOrigin.y, sun, drift);
- float dens = u_density * exp(clamp(-u_heightFalloff * (fogAltitude(p) - u_baseHeight), -60.0, 60.0));
- lit += transmit * dens * ds * sunVisibility(p, tiles.x, tiles.y) * shade;
- transmit *= exp(-dens * ds);
- }
- float strength = clamp(lightColor.a, 0.0, 8.0) * 0.5;
- result += sunTint * lit * phaseHG(dot(dir, sun)) * u_inscatter * u_volumetric * strength * (sky ? u_skyFog : 1.0);
- }
- fragColor = vec4(result, src.a);
+ fragColor = vec4(volumeLight(entry, dir, len, sky, sun, lightColor, tiles, sunTint), 1.0);
}
#pragma end
@@ -887,8 +2024,31 @@ fn opticalDepth(eyeY : f32, dirY : f32, len : f32) -> f32 {
return dens * len * span;
}
+/// The sun's light through the haze, where the shadow map says it gets through. See the GLSL twin.
+fn volumeLight(entry : vec3f, dir : vec3f, len : f32, sky : bool, sun : vec3f, found : Sun, sunTint : vec3f, px : vec2f) -> vec3f {
+ let range = min(len, mc.u_volumeRange);
+ let span = max(range - mc.u_startDistance, 0.0);
+ let ds = span / f32(VOLUME_STEPS);
+ let jitter = ign(px);
+ var transmit = 1.0;
+ var lit = 0.0;
+ let drift = vec3f(mc.u_skyWindX, 0.0, mc.u_skyWindZ) * tc.u_time.x;
+ var shade = 1.0;
+ for (var i : i32 = 0; i < VOLUME_STEPS; i = i + 1) {
+ let t = mc.u_startDistance + (f32(i) + jitter) * ds;
+ let p = entry + dir * t;
+ if (i % 3 == 0) { shade = cloudLight(p + frame.renderOrigin.xyz, entry.y + frame.renderOrigin.y, sun, drift); }
+ let dens = mc.u_density * exp(clamp(-mc.u_heightFalloff * (fogAltitude(p) - mc.u_baseHeight), -60.0, 60.0));
+ lit = lit + transmit * dens * ds * sunVisibility(p, found.tiles.x, found.tiles.y) * shade;
+ transmit = transmit * exp(-dens * ds);
+ }
+ let strength = clamp(found.color.a, 0.0, 8.0) * 0.5;
+ var skyShare = 1.0;
+ if (sky) { skyShare = mc.u_skyFog; }
+ return sunTint * lit * phaseHG(dot(dir, sun)) * mc.u_inscatter * mc.u_volumetric * strength * skyShare;
+}
+
@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
- let src = textureSampleLevel(t0, s0, v.v_texCoord, 0.0);
let size = vec2i(textureDimensions(t7, 0));
let depth = textureLoad(t7, clamp(vec2i(v.v_texCoord * vec2f(size)), vec2i(0), size - vec2i(1)), 0);
let sky = depth >= 1.0;
@@ -901,27 +2061,18 @@ fn opticalDepth(eyeY : f32, dirY : f32, len : f32) -> f32 {
var len = length(tgt - entry);
if (sky) { len = FAR_RAY; }
let effective = max(len - mc.u_startDistance, 0.0);
- if (effective <= 0.0) { return src; }
+
- let eyeAlt = fogAltitude(entry + dir * mc.u_startDistance);
- var climb = dir.y;
- if (mc.u_planetRadius > 0.0) {
- let up = normalize(entry + dir * mc.u_startDistance + frame.renderOrigin.xyz - vec3f(mc.u_planetX, mc.u_planetY, mc.u_planetZ));
- if (sky) { climb = dot(dir, up); } else { climb = (fogAltitude(tgt) - eyeAlt) / effective; }
- }
- let tau = opticalDepth(eyeAlt, climb, effective);
- var amount = (1.0 - exp(-tau)) * mc.u_maxOpacity;
- if (sky) { amount = amount * mc.u_skyFog; }
- // See the GLSL twin: the scene's own sun unless told not to follow it.
+ if (mc.u_volumetric <= 0.0 || effective <= 0.0) { return vec4f(0.0); }
+
let found = findSun();
let follow = mc.u_followSun > 0.5 && found.dir.w > 0.5;
let az = radians(mc.u_sunAzimuth);
let el = radians(mc.u_sunElevation);
var sun = vec3f(cos(el) * sin(az), sin(el), cos(el) * cos(az));
if (follow) { sun = found.dir.xyz; }
- var shaped = dot(dir, sun);
- let glow = pow(max(shaped, 0.0), mc.u_sunExponent);
+ if (!follow) { return vec4f(0.0); }
var fog = mc.u_fogColor.rgb;
var sunTint = mc.u_sunColor.rgb;
#ifdef ES_LINEAR
@@ -929,34 +2080,7 @@ fn opticalDepth(eyeY : f32, dirY : f32, len : f32) -> f32 {
sunTint = srgbToLinear(sunTint);
#endif
if (follow) { sunTint = found.color.rgb / max(max(found.color.r, found.color.g), max(found.color.b, 1e-3)); }
- var glowShare = glow;
- if (mc.u_volumetric > 0.0) { glowShare = glow * (1.0 - mc.u_volumetric); }
- let colour = mix(fog, sunTint, clamp(glowShare * mc.u_inscatter, 0.0, 1.0));
- var result = mix(src.rgb, colour, clamp(amount, 0.0, 1.0));
-
- if (mc.u_volumetric > 0.0 && follow) {
- let range = min(len, mc.u_volumeRange);
- let span = max(range - mc.u_startDistance, 0.0);
- let ds = span / f32(VOLUME_STEPS);
- let jitter = ign(v.pos.xy);
- var transmit = 1.0;
- var lit = 0.0;
- let drift = vec3f(mc.u_skyWindX, 0.0, mc.u_skyWindZ) * tc.u_time.x;
- var shade = 1.0;
- for (var i : i32 = 0; i < VOLUME_STEPS; i = i + 1) {
- let t = mc.u_startDistance + (f32(i) + jitter) * ds;
- let p = entry + dir * t;
- if (i % 3 == 0) { shade = cloudLight(p + frame.renderOrigin.xyz, entry.y + frame.renderOrigin.y, sun, drift); }
- let dens = mc.u_density * exp(clamp(-mc.u_heightFalloff * (fogAltitude(p) - mc.u_baseHeight), -60.0, 60.0));
- lit = lit + transmit * dens * ds * sunVisibility(p, found.tiles.x, found.tiles.y) * shade;
- transmit = transmit * exp(-dens * ds);
- }
- let strength = clamp(found.color.a, 0.0, 8.0) * 0.5;
- var skyShare = 1.0;
- if (sky) { skyShare = mc.u_skyFog; }
- result = result + sunTint * lit * phaseHG(dot(dir, sun)) * mc.u_inscatter * mc.u_volumetric * strength * skyShare;
- }
- return vec4f(result, src.a);
+ return vec4f(volumeLight(entry, dir, len, sky, sun, found, sunTint, v.pos.xy), 1.0);
}
#pragma end
`;
diff --git a/sdk/tests/shader-twins.test.ts b/sdk/tests/shader-twins.test.ts
index 96653c574..965851f69 100644
--- a/sdk/tests/shader-twins.test.ts
+++ b/sdk/tests/shader-twins.test.ts
@@ -53,7 +53,7 @@ describe('embedded .esshader structure', () => {
it('found the filter, effect and grid sources', () => {
expect(CASES.filter((c) => c.name.startsWith('filters.ts')).length).toBe(1);
expect(CASES.filter((c) => c.name.startsWith('spriteFilter.ts')).length).toBe(2);
- expect(CASES.filter((c) => c.name.startsWith('postProcessEffects.ts')).length).toBe(35);
+ expect(CASES.filter((c) => c.name.startsWith('postProcessEffects.ts')).length).toBe(37);
expect(CASES.filter((c) => c.name.startsWith('editorGridRenderer.ts')).length).toBe(1);
});
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 76d0ce34d..e12489895 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -2986,4 +2986,6 @@ export const NOTED = {
{"note":"A material's vertex hook reads its own textures when it casts a shadow."},
"perf(water): the surface asks whether it is water before it works out the waves and the light":
{"note":"The water surface leaves early where it is not water."},
+ "perf(postprocess): the sky's clouds and the fog's shafts are worked out at half size and read back by depth":
+ {"note":"The sky's clouds and the fog's light shafts cost a third of what they did."},
};
diff --git a/tools/renderScenes.mjs b/tools/renderScenes.mjs
index df5389f43..d8d9bc6ea 100644
--- a/tools/renderScenes.mjs
+++ b/tools/renderScenes.mjs
@@ -752,6 +752,7 @@ export const SCENES = [
// a 90-degree eye is 1.41x further from the frame's edge than from its centre:
// an effect reading depth paints them alike, and tol 10 does not reach between.
{ id: "distance-fog", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/distance-fog.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/depth-outline.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "4", ESTELLA_VERIFY_EXPECT: "[{\"x\": 0.5, \"y\": 0.5, \"rgb\": [107, 176, 143], \"tol\": 10}, {\"x\": 0.0625, \"y\": 0.5, \"rgb\": [134, 174, 176], \"tol\": 10}, {\"x\": 0.4375, \"y\": 0.5, \"rgb\": [190, 85, 92], \"tol\": 14}, {\"x\": 0.6875, \"y\": 0.5, \"rgb\": [200, 60, 62], \"tol\": 14}]" } },
+ { id: "sky-fog", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/sky-fog.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/depth-outline.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "4", ESTELLA_VERIFY_EXPECT: "[{\"x\": 0.2, \"y\": 0.1, \"rgb\": [144, 175, 187], \"tol\": 12}, {\"x\": 0.5, \"y\": 0.1, \"rgb\": [141, 174, 183], \"tol\": 12}, {\"x\": 0.8, \"y\": 0.2, \"rgb\": [143, 174, 186], \"tol\": 12}, {\"x\": 0.5, \"y\": 0.4, \"rgb\": [139, 171, 180], \"tol\": 12}, {\"x\": 0.2, \"y\": 0.8, \"rgb\": [62, 70, 70], \"tol\": 12}, {\"x\": 0.8, \"y\": 0.8, \"rgb\": [57, 64, 67], \"tol\": 12}]" } },
// SSAO: one unlit grey floor read at seven places. Contact, corner and
// step come back darker than the open floor they are made of, the floating
// box does not, and the wall behind a silhouette keeps its colour.
diff --git a/tools/terrain-water-cost.mjs b/tools/terrain-water-cost.mjs
index 55c267bf5..669c24e31 100644
--- a/tools/terrain-water-cost.mjs
+++ b/tools/terrain-water-cost.mjs
@@ -88,6 +88,12 @@ const VARIANTS = [
{ id: 'sky-off', label: 'sky atmosphere off', scene: dropEffects('skyAtmosphere') },
{ id: 'gtao-off', label: 'ambient occlusion (gtao) off', scene: dropEffects('gtao') },
{ id: 'fog-off', label: 'height fog off', scene: dropEffects('heightFog') },
+ // Where the sky's cost lands: the fog before the sky, the sky without its clouds, the sky without its night.
+ { id: 'fog-first', label: 'height fog before the sky in the chain', scene: (d) => { const v = volume(d); const f = effect(d, 'heightFog'); v.effects = [f, ...v.effects.filter((e) => e !== f)]; } },
+ { id: 'clouds-0', label: 'sky without clouds', scene: (d) => { effect(d, 'skyAtmosphere').uniforms.u_clouds = 0; } },
+ { id: 'stars-0', label: 'sky without stars and moon', scene: (d) => { const u = effect(d, 'skyAtmosphere').uniforms; u.u_stars = 0; u.u_moon = 0; } },
+ { id: 'shade-0', label: 'fog on, cloud shafts 0', scene: (d) => { effect(d, 'heightFog').uniforms.u_cloudShade = 0; } },
+ { id: 'vol-0-shade-0', label: 'fog without volumetric light or cloud shafts', scene: (d) => { const u = effect(d, 'heightFog').uniforms; u.u_volumetric = 0; u.u_cloudShade = 0; } },
{ id: 'volumetric-off', label: 'fog on, volumetric light 0', scene: (d) => { effect(d, 'heightFog').uniforms.u_volumetric = 0; } },
{ id: 'autoexposure-off', label: 'auto exposure off', scene: dropEffects('autoExposure') },
{ id: 'bloom-off', label: 'bloom off', scene: dropEffects('bloom') },