A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
feat(sky): clouds that keep time with the game, follow the weather's wind, and have a second layer
yhhab79fa1687ba
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex c5d53f07b..d71d8b0bd 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -16,6 +16,7 @@ published separately; it ships inside the editor.### Added+- **Clouds that keep time with the game.** Where a Sky Atmosphere's clouds are is now what the wind has done so far, so a wind that changes carries them on instead of moving the whole sky; a Weather's wind reaches them, their shapes change as they go, and a paused game has a still sky. They can have a second layer with its own height, grain and wind, and `skyClockState()` keeps their place in a save. Self-shadowing reaches through a deep cloud, edges wear into billows, and the lit edge near the sun is narrower.- **Ground that is a volume.** The optional `esengine/voxel` module adds `VoxelTerrain`, beside the height-field Terrain: it holds caves and overhangs, and can be carved, filled and painted with a sphere, a box or a capsule while the game runs. Only the chunks an edit reached are made again, with their collision, and a save holds only what was changed, chunk by chunk. A sleeping body is now woken when the collision under it changes, on a Terrain as well, where before it stayed in the air.- **What stands on the ground can blend into it.** The `builtin:ground-blend` material fades a rock or a foundation into whatever is behind it, on both backends, with nothing baked, and can bring a second layer up from the ground or paint one with vertex colours. `RoadDecal` lays a mesh along a Spline over a terrain with soft edges. The `snowCover` post effect and a Snow weather lay snow on whatever faces the sky, terrain and meshes alike.- **GPU particles can hit things, tell each other, and drag trails.** `collisionMode: Depth` collides with what the camera sees, `Sdf` with `ParticleCollider` spheres, boxes and capsules that need no physics (and run on the CPU too). A GPU emitter's births, deaths and collisions go to a buffer: a `gpuSimulation` child spawns from it in its own kernel, with no trip to the CPU, and `collisionEvents` read back to the game a frame or two late. `trailEnabled` now works on the GPU.diff --git a/desktop b/desktopindex 4dd611bd1..5d08f5a40 160000--- a/desktop+++ b/desktop@@ -1 +1 @@-Subproject commit 4dd611bd18ea5d3218c56077314d27b409176fe4+Subproject commit 5d08f5a4011d8aa11e54f80898585ade7421fabadiff --git a/docs/astro/src/content/docs/graphics/post-processing.mdx b/docs/astro/src/content/docs/graphics/post-processing.mdxindex 4a0b1cffc..9a27cc552 100644--- a/docs/astro/src/content/docs/graphics/post-processing.mdx+++ b/docs/astro/src/content/docs/graphics/post-processing.mdx@@ -86,6 +86,29 @@ scene is cleared to nothing while the effect is in the chain and its alpha sayssky is laid under an edge by that share instead of replacing the pixel. Nothing downstream sees it: the effect hands back alpha 1.Turn **Keep Background** up and the scene is cleared to the camera's colour again, which is what keeping the background means.+### Clouds++`skyAtmosphere` marches its clouds through the air, and the sun lights them from inside: each step asks how much of the cloud lies between it and the sun, so the far side of a deep cloud is dark and its thin edge is bright. **Silver Lining** is how bright that edge is near the sun.++| Setting | What it does |+|---|---|+| **Cloud Coverage**, **Cloud Base**, **Cloud Thickness** | How much of the sky the main layer covers, and where it stands, in kilometres. |+| **Cloud Scale**, **Cloud Softness** | How large its clouds are (2 is half as wide), and how soft their edges are. |+| **Cloud Change** | How fast their shapes change as they go. 0 keeps each cloud as it is. |+| **Wind Speed**, **Wind Direction** | How fast they drift, in kilometres a second, and which way. |+| **Upper Layer Coverage** | A second layer. 0 (the default) costs nothing. |+| **Upper Layer Base**, **Thickness**, **Scale**, **Wind** | Its own height, grain and share of the wind. It is usually high, thin and faster; set below the main layer it stands in front of it. It casts a shadow too. |++**Clouds keep to the game's clock.** Where they are is what the wind has done so far, added up frame by frame, so a stronger wind or one that swings round carries them on from where they are, and a [Weather](/docs/graphics/weather/) can speed them up in the middle of a transition with nothing jumping. A paused game has a still sky, and the same steps make the same one. The fog's shafts read the same place. To bring a sky back from a save, store `skyClockState()` and hand it to `setSkyClockState()`.++### Limits of the clouds++- **Clouds are seen from below.** A camera above the cloud layer sees none of it, so a flight over the clouds has no cloud tops to show.+- **Their grain.** The march's jitter is filtered on the way back to full size, and a faint pattern can still show near the horizon or at a large **Cloud Scale**.+- **The fog's shafts stand under the main layer only.** The upper layer does not make beams.+- **Where a cloud is does not change the key light.** Its shadow is laid on surfaces in the sky's pass; the light's own strength follows the weather, not the cloud in front of the sun.+- **Not measured on a device.** The upper layer adds 12 samples of its density to each half-size pixel it covers against 64 for the main layer, and nothing at 0.+### Cloud shadowsTurn **Cloud Shadows** above 0 and the sun's ray from each surface is marched up through the cloud layer the sky draws, against thediff --git a/docs/astro/src/content/docs/graphics/weather.mdx b/docs/astro/src/content/docs/graphics/weather.mdxindex b416a68c6..86c2038a1 100644--- a/docs/astro/src/content/docs/graphics/weather.mdx+++ b/docs/astro/src/content/docs/graphics/weather.mdx@@ -61,7 +61,7 @@ Post Process Volume, which the panel says.The ambient light carries `reflectionScale` and `reflectionSaturation` for exactly this: what baked probes show ismultiplied by the first and drained of colour by the second, and the weather turns both down.- **Sky.** A Sky Atmosphere effect takes the preset's `overcast` as cloud coverage and haze: the clouds thicken and join up, and the sun, moon and stars behind them are hidden by the same transmittance.-- **Wind.** The preset's `wind` scales how far plants sway (a Foliage material's `u_windStrength`, a Sway sprite's `u_swayWind`) and `waves` scales the height of the waves on every body of water, in what is drawn and in what the surface answers.+- **Wind.** The preset's `wind` scales how fast a Sky Atmosphere's clouds drift and how far plants sway (a Foliage material's `u_windStrength`, a Sway sprite's `u_swayWind`) and `waves` scales the height of the waves on every body of water, in what is drawn and in what the surface answers.- **Grade and exposure.** Colour Grade's saturation and contrast are scaled, and Auto Exposure's compensation raised.The effects are applied after the scene's volumes have blended, so a scene that fogs itself thinly stays thinner thandiff --git a/docs/astro/src/content/docs/zh-cn/graphics/post-processing.mdx b/docs/astro/src/content/docs/zh-cn/graphics/post-processing.mdxindex ea4402a11..4f9225021 100644--- a/docs/astro/src/content/docs/zh-cn/graphics/post-processing.mdx+++ b/docs/astro/src/content/docs/zh-cn/graphics/post-processing.mdx@@ -74,6 +74,29 @@ Estella 通过 ping-pong 帧缓冲运行全屏后处理。加一个 `PostProcess`skyAtmosphere` 在场景之后、在场景下面绘制天空。为了让比一个像素还细的电线或栏杆保持完整,只要这个效果在链里,场景就被清为全透明,它的 alpha 表示几何体覆盖了每个像素的多少,于是天空按这个比例垫在边缘下面,而不是整个替换这个像素。下游看不到这一点:效果交回的 alpha 是 1。把**保留背景**调高,场景就恢复成清为相机颜色,这正是保留背景的含义。+### 云++`skyAtmosphere` 在空气中步进绘制云,太阳从云的内部照亮它们:每一步都会询问这一点和太阳之间隔着多少云,所以厚云的背光面是暗的,薄边是亮的。**Silver Lining** 控制靠近太阳时云边有多亮。++| 设置 | 作用 |+|---|---|+| **Cloud Coverage**、**Cloud Base**、**Cloud Thickness** | 主云层的覆盖率和所在高度,单位是千米。 |+| **Cloud Scale**、**Cloud Softness** | 云的大小(2 表示宽度减半)和边缘的柔和程度。 |+| **Cloud Change** | 云形随时间变化的速度。0 表示每朵云保持原样。 |+| **Wind Speed**、**Wind Direction** | 漂移速度(千米每秒)和方向。 |+| **Upper Layer Coverage** | 第二层云。0(默认)没有任何开销。 |+| **Upper Layer Base**、**Thickness**、**Scale**、**Wind** | 第二层自己的高度、颗粒和受风比例。通常又高又薄、移动更快;设在主云层之下时,它会挡在主云层前面。它也会投下云影。 |++**云遵循游戏时钟。** 云的位置是风到目前为止累积的结果,逐帧相加,所以风变强或转向时,云从当前位置继续走;[天气](/docs/zh-cn/graphics/weather/)在过渡途中给云加速也不会跳变。游戏暂停时天空静止,相同的步进得到相同的天空。雾的光柱读取同一个位置。要从存档恢复天空,保存 `skyClockState()`,再传给 `setSkyClockState()`。++### 云的限制++- **云只能从下方看。** 相机在云层之上时看不到云,所以飞越云海时没有云顶可看。+- **颗粒。** 步进抖动在放大回全分辨率时已经过滤,但地平线附近或 **Cloud Scale** 很大时仍可能有淡淡的纹理。+- **雾的光柱只跟随主云层。** 第二层云不产生光束。+- **云的位置不改变主光。** 云影是在天空通道里叠到表面上的;光源本身的强度跟随天气,不跟随挡在太阳前面的云。+- **没有在真机上测过。** 第二层云对它覆盖的每个半分辨率像素增加 12 次密度采样,主云层是 64 次;覆盖率为 0 时没有开销。+### 云影把**云影**调到 0 以上,从每个表面朝太阳发出的光线会穿过天空绘制的云层逐步前进,用的是同一份密度,所以云下的地面更暗、更斑驳,天空开阔处则被照亮。被挡掉的是太阳直射光;环境光不受影响。开启云影也会把云固定在世界里而不是跟着相机走,所以人可以走进云影再走出来。天空以千米为单位,场景用自己的单位:**每千米世界单位**说明场景里多少个单位是一千米。为 0(默认值)时效果与之前完全一样。阴影只落在表面上;云仍然只画在有天空的地方。diff --git a/docs/astro/src/content/docs/zh-cn/graphics/weather.mdx b/docs/astro/src/content/docs/zh-cn/graphics/weather.mdxindex e4cc168d5..32f7fb3b1 100644--- a/docs/astro/src/content/docs/zh-cn/graphics/weather.mdx+++ b/docs/astro/src/content/docs/zh-cn/graphics/weather.mdx@@ -50,7 +50,7 @@ Details 面板把内建天气显示为按钮(项目自己的预设 id 仍然有- **反射。** 烘焙的反射探针显示的是它烘焙时的天空,所以下雨的街道会映出晴天的街道。环境光带有 `reflectionScale` 和`reflectionSaturation` 就是为此:探针显示的内容乘以前者、按后者褪色,天气会把两者调低。- **天空。** Sky Atmosphere 特效把预设的 `overcast` 当作云量和雾霾:云变厚并连成一片,后面的太阳、月亮和星星被同一份透射率遮住。-- **风。** 预设的 `wind` 缩放植物摇摆的幅度(Foliage 材质的 `u_windStrength`、Sway 精灵的 `u_swayWind`),`waves` 缩放每片水体的浪高——画出来的和水面查询回答的都一起变。+- **风。** 预设的 `wind` 缩放 Sky Atmosphere 的云漂移速度和植物摇摆的幅度(Foliage 材质的 `u_windStrength`、Sway 精灵的 `u_swayWind`),`waves` 缩放每片水体的浪高——画出来的和水面查询回答的都一起变。- **调色与曝光。** Colour Grade 的饱和度和对比度被缩放,Auto Exposure 的补偿被提高。这些效果在场景的体积混合之后才施加,所以在同一场雨里,雾设得薄的场景仍然比雾设得厚的场景薄。diff --git a/docs/astro/src/data/apiStability.generated.json b/docs/astro/src/data/apiStability.generated.jsonindex 03917785e..301ec372b 100644--- a/docs/astro/src/data/apiStability.generated.json+++ b/docs/astro/src/data/apiStability.generated.json@@ -3,10 +3,10 @@"counts": {"public": 115,"beta": 178,- "experimental": 2965,+ "experimental": 2968,"deprecated": 3},- "total": 3258,+ "total": 3261,"subsystems": [{"id": "mobile-controls",diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.mdindex 759b30002..27f63ac00 100644--- a/sdk/etc/index.api.md+++ b/sdk/etc/index.api.md@@ -1,7 +1,7 @@# API surface — esengine/index<!-- Generated by tools/api-surface.mjs — do not edit. Run --update to accept changes. -->-Symbols: 115 public · 166 beta · 2096 experimental · 3 deprecated+Symbols: 115 public · 166 beta · 2099 experimental · 3 deprecated## ABI_LAYOUT_HASH — const @experimental```@@ -9967,6 +9967,15 @@ vertexCount: numbervertices: Uint8Array<ArrayBufferLike>```+## SkyClockState — interface @experimental+```+driftX: number+driftZ: number+evolve: number+upperDriftX: number+upperDriftZ: number+```+## SkyRadiance — type @experimental```(dx: number, dy: number, dz: number,@@ -17348,6 +17357,11 @@ ServicesPlugin(bytes: number): void```+## setSkyClockState — function @experimental+```+(next: Partial<SkyClockState> | null): void+```+## setTextureBudget — function @experimental```(bytes: number): void@@ -17513,6 +17527,11 @@ ServicesPlugin<T>(initial: T): Signal<T>```+## skyClockState — function @experimental+```+(): SkyClockState+```+## skyRadiance — function @experimental```(spec: SkySpec): SkyRadiancediff --git a/sdk/etc/index.native.api.md b/sdk/etc/index.native.api.mdindex 281c0118e..41a76da29 100644--- a/sdk/etc/index.native.api.md+++ b/sdk/etc/index.native.api.md@@ -1,7 +1,7 @@# API surface — esengine/index.native<!-- Generated by tools/api-surface.mjs — do not edit. Run --update to accept changes. -->-Symbols: 115 public · 171 beta · 2115 experimental · 3 deprecated+Symbols: 115 public · 171 beta · 2118 experimental · 3 deprecated## ABI_LAYOUT_HASH — const @experimental```@@ -10183,6 +10183,15 @@ vertexCount: numbervertices: Uint8Array<ArrayBufferLike>```+## SkyClockState — interface @experimental+```+driftX: number+driftZ: number+evolve: number+upperDriftX: number+upperDriftZ: number+```+## SkyRadiance — type @experimental```(dx: number, dy: number, dz: number,@@ -17614,6 +17623,11 @@ ServicesPlugin(bytes: number): void```+## setSkyClockState — function @experimental+```+(next: Partial<SkyClockState> | null): void+```+## setTextureBudget — function @experimental```(bytes: number): void@@ -17779,6 +17793,11 @@ ServicesPlugin<T>(initial: T): Signal<T>```+## skyClockState — function @experimental+```+(): SkyClockState+```+## skyRadiance — function @experimental```(spec: SkySpec): SkyRadiancediff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.mdindex 577a9bb7a..2bd9a4687 100644--- a/sdk/etc/index.node.api.md+++ b/sdk/etc/index.node.api.md@@ -1,7 +1,7 @@# API surface — esengine/index.node<!-- Generated by tools/api-surface.mjs — do not edit. Run --update to accept changes. -->-Symbols: 115 public · 168 beta · 2095 experimental · 3 deprecated+Symbols: 115 public · 168 beta · 2098 experimental · 3 deprecated## ANCHOR_AXES — const @experimental```@@ -9962,6 +9962,15 @@ vertexCount: numbervertices: Uint8Array<ArrayBufferLike>```+## SkyClockState — interface @experimental+```+driftX: number+driftZ: number+evolve: number+upperDriftX: number+upperDriftZ: number+```+## SkyRadiance — type @experimental```(dx: number, dy: number, dz: number,@@ -17353,6 +17362,11 @@ ServicesPlugin(bytes: number): void```+## setSkyClockState — function @experimental+```+(next: Partial<SkyClockState> | null): void+```+## setTextureBudget — function @experimental```(bytes: number): void@@ -17518,6 +17532,11 @@ ServicesPlugin<T>(initial: T): Signal<T>```+## skyClockState — function @experimental+```+(): SkyClockState+```+## skyRadiance — function @experimental```(spec: SkySpec): SkyRadiancediff --git a/sdk/etc/minigame.api.md b/sdk/etc/minigame.api.mdindex 571a273c8..4e7774220 100644--- a/sdk/etc/minigame.api.md+++ b/sdk/etc/minigame.api.md@@ -1,7 +1,7 @@# API surface — esengine/minigame<!-- Generated by tools/api-surface.mjs — do not edit. Run --update to accept changes. -->-Symbols: 115 public · 166 beta · 2117 experimental · 3 deprecated+Symbols: 115 public · 166 beta · 2120 experimental · 3 deprecated## ANCHOR_AXES — const @experimental```@@ -10258,6 +10258,15 @@ vertexCount: numbervertices: Uint8Array<ArrayBufferLike>```+## SkyClockState — interface @experimental+```+driftX: number+driftZ: number+evolve: number+upperDriftX: number+upperDriftZ: number+```+## SkyRadiance — type @experimental```(dx: number, dy: number, dz: number,@@ -17660,6 +17669,11 @@ ServicesPlugin(bytes: number): void```+## setSkyClockState — function @experimental+```+(next: Partial<SkyClockState> | null): void+```+## setTextureBudget — function @experimental```(bytes: number): void@@ -17825,6 +17839,11 @@ ServicesPlugin<T>(initial: T): Signal<T>```+## skyClockState — function @experimental+```+(): SkyClockState+```+## skyRadiance — function @experimental```(spec: SkySpec): SkyRadiancediff --git a/sdk/etc/wechat.api.md b/sdk/etc/wechat.api.mdindex 516520b9c..aeb2e70ae 100644--- a/sdk/etc/wechat.api.md+++ b/sdk/etc/wechat.api.md@@ -1,7 +1,7 @@# API surface — esengine/wechat<!-- Generated by tools/api-surface.mjs — do not edit. Run --update to accept changes. -->-Symbols: 115 public · 166 beta · 2108 experimental · 3 deprecated+Symbols: 115 public · 166 beta · 2111 experimental · 3 deprecated## ANCHOR_AXES — const @experimental```@@ -9969,6 +9969,15 @@ vertexCount: numbervertices: Uint8Array<ArrayBufferLike>```+## SkyClockState — interface @experimental+```+driftX: number+driftZ: number+evolve: number+upperDriftX: number+upperDriftZ: number+```+## SkyRadiance — type @experimental```(dx: number, dy: number, dz: number,@@ -17386,6 +17395,11 @@ ServicesPlugin(bytes: number): void```+## setSkyClockState — function @experimental+```+(next: Partial<SkyClockState> | null): void+```+## setTextureBudget — function @experimental```(bytes: number): void@@ -17551,6 +17565,11 @@ ServicesPlugin<T>(initial: T): Signal<T>```+## skyClockState — function @experimental+```+(): SkyClockState+```+## skyRadiance — function @experimental```(spec: SkySpec): SkyRadiancediff --git a/sdk/src/core-render.ts b/sdk/src/core-render.tsindex 9230abacc..19b7544c4 100644--- a/sdk/src/core-render.ts+++ b/sdk/src/core-render.ts@@ -188,6 +188,9 @@ export {WEATHER_RAIN,WEATHER_STORM,WEATHER_SNOW,+ skyClockState,+ setSkyClockState,+ type SkyClockState,WEATHER_NONE,type WeatherState,type WeatherPreset,diff --git a/sdk/src/postprocess.ts b/sdk/src/postprocess.tsindex 2d9b1ec47..473ae2093 100644--- a/sdk/src/postprocess.ts+++ b/sdk/src/postprocess.ts@@ -34,6 +34,9 @@ export {WEATHER_RAIN,WEATHER_STORM,WEATHER_SNOW,+ skyClockState,+ setSkyClockState,+ type SkyClockState,WEATHER_NONE,type WeatherState,type WeatherPreset,diff --git a/sdk/src/postprocess/cloudShafts.ts b/sdk/src/postprocess/cloudShafts.tsindex eb70e17aa..2f4f8e474 100644--- a/sdk/src/postprocess/cloudShafts.ts+++ b/sdk/src/postprocess/cloudShafts.ts@@ -30,8 +30,6 @@ export function applyCloudsToFog(effects: Map<string, BlendedEffect>): void {return;}const overcast = Math.min(Math.max(value(sky, 'skyAtmosphere', 'u_overcast'), 0), 1);- const angle = value(sky, 'skyAtmosphere', 'u_windDirection') * Math.PI / 180;- const speed = value(sky, 'skyAtmosphere', 'u_windSpeed');const set = (name: string, v: number): void => { fog.uniforms.set(name, v); };set('u_skyCover', Math.max(value(sky, 'skyAtmosphere', 'u_clouds'), overcast * 0.95));set('u_skyBase', value(sky, 'skyAtmosphere', 'u_cloudBase'));@@ -39,8 +37,11 @@ export function applyCloudsToFog(effects: Map<string, BlendedEffect>): void {set('u_skyAltitude', value(sky, 'skyAtmosphere', 'u_altitude'));set('u_skyUnits', value(sky, 'skyAtmosphere', 'u_unitsPerKm'));set('u_skySigma', 1.3 * (1 + overcast * 1.5));- set('u_skyWindX', Math.sin(angle) * speed);- set('u_skyWindZ', Math.cos(angle) * speed);+ set('u_skyDriftX', value(sky, 'skyAtmosphere', 'u_cloudDriftX'));+ set('u_skyDriftZ', value(sky, 'skyAtmosphere', 'u_cloudDriftZ'));+ set('u_skyEvolve', value(sky, 'skyAtmosphere', 'u_cloudEvolve'));+ set('u_skyScale', value(sky, 'skyAtmosphere', 'u_cloudScale'));+ set('u_skySoftness', value(sky, 'skyAtmosphere', 'u_cloudSoftness'));// The masks that shape those clouds, as the sky has them: the same drawn clouds and the same lit air.for (const [mask, tile] of [['u_cloudMask', 'u_cloudMaskTile'], ['u_cloudMask2', 'u_cloudMask2Tile']] as const) {const ref = sky.textures.get(mask);diff --git a/sdk/src/postprocess/effects.ts b/sdk/src/postprocess/effects.tsindex 703a1a0ee..ee8d113d2 100644--- a/sdk/src/postprocess/effects.ts+++ b/sdk/src/postprocess/effects.ts@@ -439,6 +439,26 @@ registerEffect({// their shape, the noise only erodes it. The second mask is a softer, higher layer folded in at a third.{ name: 'u_cloudMaskTile', label: 'Cloud Mask Tile (km)', min: 0, max: 100, step: 0.1, defaultValue: 0 },{ name: 'u_cloudMask2Tile', label: 'Cloud Mask 2 Tile (km)', min: 0, max: 100, step: 0.1, defaultValue: 0 },+ // The size of the clouds, 2 being half as wide, and how soft their edges are.+ { name: 'u_cloudScale', label: 'Cloud Scale', min: 0.1, max: 8, step: 0.05, defaultValue: 1 },+ { name: 'u_cloudSoftness', label: 'Cloud Softness', min: 0.02, max: 1, step: 0.01, defaultValue: 0.22 },+ // How fast the clouds' shapes change as they drift. 0 keeps each cloud as it is.+ { name: 'u_cloudChange', label: 'Cloud Change', min: 0, max: 1, step: 0.001, defaultValue: 0.004 },+ // How bright a cloud's edge is near the sun.+ { name: 'u_cloudSilver', label: 'Silver Lining', min: 0, max: 2, step: 0.01, defaultValue: 0.6 },+ // A second layer with a height, a grain and a wind of its own: high thin cloud over the main layer, or a low one under it. 0 costs nothing.+ { name: 'u_cloud2Coverage', label: 'Upper Layer Coverage', min: 0, max: 1, step: 0.01, defaultValue: 0, neutralValue: 0 },+ { name: 'u_cloud2Base', label: 'Upper Layer Base', min: 0.2, max: 20, step: 0.1, defaultValue: 6 },+ { name: 'u_cloud2Thickness', label: 'Upper Layer Thickness', min: 0.05, max: 8, step: 0.05, defaultValue: 0.6 },+ { name: 'u_cloud2Scale', label: 'Upper Layer Scale', min: 0.1, max: 8, step: 0.05, defaultValue: 1.8 },+ // × the wind up there: it usually blows harder.+ { name: 'u_cloud2Wind', label: 'Upper Layer Wind', min: 0, max: 8, step: 0.05, defaultValue: 1.6 },+ // Where the clouds have got to, added up by the sky's clock (skyClock.ts).+ { name: 'u_cloudDriftX', label: 'Cloud Drift X', min: -1e8, max: 1e8, step: 0.01, defaultValue: 0, internal: true },+ { name: 'u_cloudDriftZ', label: 'Cloud Drift Z', min: -1e8, max: 1e8, step: 0.01, defaultValue: 0, internal: true },+ { name: 'u_cloud2DriftX', label: 'Upper Layer Drift X', min: -1e8, max: 1e8, step: 0.01, defaultValue: 0, internal: true },+ { name: 'u_cloud2DriftZ', label: 'Upper Layer Drift Z', min: -1e8, max: 1e8, step: 0.01, defaultValue: 0, internal: true },+ { name: 'u_cloudEvolve', label: 'Cloud Evolve', min: 0, max: 1e8, step: 0.01, defaultValue: 0, internal: true },],textures: [{ name: 'u_cloudMask', label: 'Cloud Mask (tileable coverage, linear, repeat)' },@@ -552,8 +572,11 @@ registerEffect({{ name: 'u_skyAltitude', label: 'Sky Altitude', min: 0, max: 20, step: 0.1, defaultValue: 0.1, internal: true },{ name: 'u_skyUnits', label: 'Sky Units per km', min: 1, max: 100000, step: 1, defaultValue: 1000, internal: true },{ name: 'u_skySigma', label: 'Sky Cloud Density', min: 0, max: 10, step: 0.1, defaultValue: 1.3, internal: true },- { name: 'u_skyWindX', label: 'Sky Wind X', min: -2, max: 2, step: 0.01, defaultValue: 0, internal: true },- { name: 'u_skyWindZ', label: 'Sky Wind Z', min: -2, max: 2, step: 0.01, defaultValue: 0, internal: true },+ { name: 'u_skyDriftX', label: 'Sky Drift X', min: -1e8, max: 1e8, step: 0.01, defaultValue: 0, internal: true },+ { name: 'u_skyEvolve', label: 'Sky Evolve', min: 0, max: 1e8, step: 0.01, defaultValue: 0, internal: true },+ { name: 'u_skyScale', label: 'Sky Cloud Scale', min: 0.1, max: 8, step: 0.05, defaultValue: 1, internal: true },+ { name: 'u_skySoftness', label: 'Sky Cloud Softness', min: 0.02, max: 1, step: 0.01, defaultValue: 0.22, internal: true },+ { name: 'u_skyDriftZ', label: 'Sky Drift Z', min: -1e8, max: 1e8, step: 0.01, defaultValue: 0, internal: true },{ name: 'u_cloudMaskTile', label: 'Sky Cloud Mask Tile', min: 0, max: 100, step: 0.1, defaultValue: 0, internal: true },{ name: 'u_cloudMask2Tile', label: 'Sky Cloud Mask 2 Tile', min: 0, max: 100, step: 0.1, defaultValue: 0, internal: true },],diff --git a/sdk/src/postprocess/index.ts b/sdk/src/postprocess/index.tsindex 94a2d315b..3637806c8 100644--- a/sdk/src/postprocess/index.ts+++ b/sdk/src/postprocess/index.ts@@ -49,4 +49,5 @@ export {type WeatherState,type WeatherPreset,} from './weather';+export { skyClockState, setSkyClockState, type SkyClockState } from './skyClock';export { timeOfDayAt, sunPosition, currentTimeOfDay, TimeDrive, type TimeOfDayState } from './timeOfDay';diff --git a/sdk/src/postprocess/skyClock.ts b/sdk/src/postprocess/skyClock.tsnew file mode 100644index 000000000..32d3c3b69--- /dev/null+++ b/sdk/src/postprocess/skyClock.ts@@ -0,0 +1,82 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file skyClock.ts+ * @brief How far the clouds have drifted and changed: what the wind has done so far, added up frame by frame.+ *+ * @details A place worked out as speed times the time since the start moves the whole sky at once when the speed changes, by as much as the+ * game has been running. So this keeps the sum instead: a storm blowing up speeds the clouds from where they are, the wind can+ * swing round, a volume can bring a stronger one, and none of it moves a cloud. It follows the game's clock, so a paused game has a+ * still sky and the same steps make the same clouds. Sky, its ground shadow and the fog's shafts all read this one sum.+ */+import { getEffectDef } from './effects';+import type { BlendedEffect } from './volumeBlending';++/** Where the clouds are in their drift. Plain numbers: what a save holds to bring the same sky back. @experimental */+export interface SkyClockState {+ /** How far the main layer has drifted, in kilometres. */+ driftX: number;+ driftZ: number;+ /** How far the upper layer has drifted, in kilometres. */+ upperDriftX: number;+ upperDriftZ: number;+ /** How far the clouds' shapes have changed, in the noise's own units. */+ evolve: number;+}++const state: SkyClockState = { driftX: 0, driftZ: 0, upperDriftX: 0, upperDriftZ: 0, evolve: 0 };+/** Kilometres a second each way, and the change a second, as the sky last said. */+const velocity = { x: 0, z: 0, upperX: 0, upperZ: 0, evolve: 0 };++function value(effect: BlendedEffect, name: string): number {+ const set = effect.uniforms.get(name);+ if (set !== undefined) return set;+ return getEffectDef('skyAtmosphere')?.uniforms.find((u) => u.name === name)?.defaultValue ?? 0;+}++/** Moves the clouds on by a frame of the game's clock, by the wind the sky last said. */+export function advanceSkyClock(delta: number): void {+ if (!(delta > 0)) return;+ state.driftX += velocity.x * delta;+ state.driftZ += velocity.z * delta;+ state.upperDriftX += velocity.upperX * delta;+ state.upperDriftZ += velocity.upperZ * delta;+ state.evolve += velocity.evolve * delta;+}++/** Tells the sky where its clouds are, and takes from it how they are moving. `wind` is the weather's × on the wind, 1 with none. */+export function applySkyClock(effects: Map<string, BlendedEffect>, wind: number): void {+ const sky = effects.get('skyAtmosphere');+ if (!sky || !sky.enabled) {+ // Nothing to carry: a sky that comes back takes up from where its clouds were, not from a wind that blew while it was gone.+ velocity.x = velocity.z = velocity.upperX = velocity.upperZ = velocity.evolve = 0;+ return;+ }+ const angle = value(sky, 'u_windDirection') * Math.PI / 180;+ const speed = value(sky, 'u_windSpeed') * wind;+ velocity.x = Math.sin(angle) * speed;+ velocity.z = Math.cos(angle) * speed;+ const upper = speed * value(sky, 'u_cloud2Wind');+ velocity.upperX = Math.sin(angle) * upper;+ velocity.upperZ = Math.cos(angle) * upper;+ velocity.evolve = value(sky, 'u_cloudChange');+ sky.uniforms.set('u_cloudDriftX', state.driftX);+ sky.uniforms.set('u_cloudDriftZ', state.driftZ);+ sky.uniforms.set('u_cloud2DriftX', state.upperDriftX);+ sky.uniforms.set('u_cloud2DriftZ', state.upperDriftZ);+ sky.uniforms.set('u_cloudEvolve', state.evolve);+}++/** Where the clouds are now. @experimental */+export function skyClockState(): SkyClockState {+ return { ...state };+}++/** Puts the clouds where a save had them, or back to the start. @experimental */+export function setSkyClockState(next: Partial<SkyClockState> | null): void {+ state.driftX = next?.driftX ?? 0;+ state.driftZ = next?.driftZ ?? 0;+ state.upperDriftX = next?.upperDriftX ?? 0;+ state.upperDriftZ = next?.upperDriftZ ?? 0;+ state.evolve = next?.evolve ?? 0;+}diff --git a/sdk/src/postprocess/sources/atmosphere.ts b/sdk/src/postprocess/sources/atmosphere.tsindex c86382588..cf60e8abb 100644--- a/sdk/src/postprocess/sources/atmosphere.ts+++ b/sdk/src/postprocess/sources/atmosphere.ts@@ -11,6 +11,306 @@ import { rain2dEffects } from './rain2d';import { fog2dEffects } from './fog2d';import { SSAO_GLSL, SSAO_WGSL, PUDDLE_GLSL, PUDDLE_WGSL, BASIN_GLSL, BASIN_WGSL, GBUFFER_GLSL, GBUFFER_WGSL } from './shared';+/** The sky's clouds, as both of its passes draw them: the half-size pass that works them out, and the composite for the pixels it has no answer for. */+const CLOUD_GLSL = `/// 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 + u_cloudEvolve, p.z) * 0.32 * u_cloudScale;+ 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);+ float d = smoothstep(1.0 - coverage, 1.0 - coverage + u_cloudSoftness, n) * shape;+ // Finer noise bites only where the cloud is thin, so its middle stays whole and its edge is worn into billows. Near the eye only the sky's own march asks.+ if (octaves > 2 && d > 0.0 && d < 0.9) d = max(d - (1.0 - d) * vnoise(q * 11.0) * 0.55, 0.0);+ return d;+}++/// The upper layer: its own height and grain, and thinner, so a few steps see it. No mask: that is the main layer's.+float cloud2Density(vec3 p) {+ float h = clamp((p.y - u_cloud2Base) / u_cloud2Thickness, 0.0, 1.0);+ float shape = smoothstep(0.0, 0.3, h) * smoothstep(1.0, 0.5, h);+ vec3 q = vec3(p.x, p.y * 3.0 + u_cloudEvolve * 1.7, p.z) * 0.32 * u_cloud2Scale;+ float n = vnoise(q) * 0.62 + vnoise(q * 2.31) * 0.38;+ return smoothstep(1.0 - u_cloud2Coverage, 1.0 - u_cloud2Coverage + 0.3, n) * shape;+}++/// How much of the sun's light goes on toward the eye: mostly the same whichever way, and a bright lobe near the sun that gives a cloud its silver edge.+float cloudPhase(float cosS) {+ const float G = 0.86;+ return 0.42 + 0.018 * u_cloudSilver * (1.0 - G * G) / pow(1.0 + G * G - 2.0 * G * cosS, 1.5);+}++/// 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 && u_cloud2Coverage <= 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;+ }+ if (u_cloud2Coverage > 0.01) {+ float t = (u_cloud2Base + u_cloud2Thickness * 0.5 - h) / sun.y;+ vec3 upper = vec3(u_cloud2DriftX - u_cloudDriftX, 0.0, u_cloud2DriftZ - u_cloudDriftZ);+ if (t > 0.0) tau += cloud2Density(vec3(world.x * k + sun.x * t, h + sun.y * t, world.z * k + sun.z * t) + drift + upper) * sigma * u_cloud2Thickness / sun.y;+ }+ 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 = min((base + thick - u_altitude) / dir.y, tb + thick * 6.0);+ 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 = cloudPhase(cosS);+ 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;+ // Short, then longer: the light that matters is lost near the point, and the far side of a deep cloud still has to count.+ float reach = max(thick, 0.5) * 0.14;+ for (int k = 1; k <= 3; ++k) {+ float along = reach * float(k * k);+ vec3 q = p + sun * along;+ tau += cloudDensity(q, coverage, base, thick, 2) * reach * float(2 * k - 1) * sigma;+ }+ // Beer's law, with the light that scatters on through a cloud many times; and a little less at the very skin, where little has scattered yet.+ float energy = (exp(-tau * 1.1) + 0.25 * exp(-tau * 0.25)) * mix(1.0, 1.0 - exp(-(d * sigma * ds + tau) * 3.0), 0.5);+ 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;+ }+ if (u_cloud2Coverage > 0.01 && dir.y > 0.01) {+ float tb = max(u_cloud2Base - u_altitude, 0.0) / dir.y;+ float tt = max(u_cloud2Base + u_cloud2Thickness - u_altitude, 0.0) / dir.y;+ const int STEPS = 6;+ float ds = (tt - tb) / float(STEPS);+ float jitter = ign(gl_FragCoord.yx);+ float sigma = 1.3 * (1.0 + overcast);+ vec3 drift = windDrift + vec3(u_cloud2DriftX - u_cloudDriftX, 0.0, u_cloud2DriftZ - u_cloudDriftZ);+ vec3 ambient = dome * 0.7 + lin(vec3(0.55, 0.65, 0.9)) * 0.015 * night;+ vec3 sunLight = sunTrans * 1.2 * u_cloudBrightness;+ float phase = cloudPhase(dot(dir, sun));+ float trans = 1.0;+ vec3 lit = vec3(0.0);+ for (int i = 0; i < STEPS; ++i) {+ float t = tb + (float(i) + jitter) * ds;+ vec3 p = vec3(dir.x * t, u_altitude + dir.y * t, dir.z * t) + drift;+ float d = cloud2Density(p);+ if (d < 0.002) continue;+ float tau = cloud2Density(p + sun * u_cloud2Thickness * 0.5) * u_cloud2Thickness * 0.5 * sigma;+ vec3 S = sunLight * exp(-tau) * phase * 2.0 + ambient;+ float tr = exp(-d * sigma * ds);+ lit += trans * (1.0 - tr) * S;+ trans *= tr;+ }+ float fade = smoothstep(0.01, 0.14, dir.y);+ trans = mix(1.0, trans, fade);+ lit *= fade;+ // Colour * T + L for the nearer of the two, over the farther: seen from below, the lower is the nearer.+ if (u_cloud2Base >= u_cloudBase) L += T * lit; else L = lit + trans * L;+ T *= trans;+ }+}+`;++const CLOUD_WGSL = `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 + mc.u_cloudEvolve, p.z) * 0.32 * mc.u_cloudScale;+ 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);+ var d = smoothstep(1.0 - coverage, 1.0 - coverage + mc.u_cloudSoftness, n) * shape;+ if (octaves > 2 && d > 0.0 && d < 0.9) { d = max(d - (1.0 - d) * vnoise(q * 11.0) * 0.55, 0.0); }+ return d;+}++fn cloud2Density(p : vec3f) -> f32 {+ let h = clamp((p.y - mc.u_cloud2Base) / mc.u_cloud2Thickness, 0.0, 1.0);+ let shape = smoothstep(0.0, 0.3, h) * smoothstep(1.0, 0.5, h);+ let q = vec3f(p.x, p.y * 3.0 + mc.u_cloudEvolve * 1.7, p.z) * 0.32 * mc.u_cloud2Scale;+ let n = vnoise(q) * 0.62 + vnoise(q * 2.31) * 0.38;+ return smoothstep(1.0 - mc.u_cloud2Coverage, 1.0 - mc.u_cloud2Coverage + 0.3, n) * shape;+}++fn cloudPhase(cosS : f32) -> f32 {+ let G = 0.86;+ return 0.42 + 0.018 * mc.u_cloudSilver * (1.0 - G * G) / pow(1.0 + G * G - 2.0 * G * cosS, 1.5);+}++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 && mc.u_cloud2Coverage <= 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;+ }+ if (mc.u_cloud2Coverage > 0.01) {+ let upT = (mc.u_cloud2Base + mc.u_cloud2Thickness * 0.5 - h) / sun.y;+ let upper = vec3f(mc.u_cloud2DriftX - mc.u_cloudDriftX, 0.0, mc.u_cloud2DriftZ - mc.u_cloudDriftZ);+ if (upT > 0.0) { tau = tau + cloud2Density(vec3f(world.x * k + sun.x * upT, h + sun.y * upT, world.z * k + sun.z * upT) + drift + upper) * sigma * mc.u_cloud2Thickness / sun.y; }+ }+ 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 = min((base + thick - mc.u_altitude) / dir.y, tb + thick * 6.0);+ 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 = cloudPhase(cosS);+ 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;+ let reach = max(thick, 0.5) * 0.14;+ for (var k : i32 = 1; k <= 3; k = k + 1) {+ let along = reach * f32(k * k);+ let q = p + sun * along;+ tau = tau + cloudDensity(q, coverage, base, thick, 2) * reach * f32(2 * k - 1) * sigma;+ }+ let energy = (exp(-tau * 1.1) + 0.25 * exp(-tau * 0.25)) * mix(1.0, 1.0 - exp(-(d * sigma * ds + tau) * 3.0), 0.5);+ 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;+ }+ if (mc.u_cloud2Coverage > 0.01 && dir.y > 0.01) {+ let tb = max(mc.u_cloud2Base - mc.u_altitude, 0.0) / dir.y;+ let tt = max(mc.u_cloud2Base + mc.u_cloud2Thickness - mc.u_altitude, 0.0) / dir.y;+ let STEPS = 6;+ let ds = (tt - tb) / f32(STEPS);+ let jitter = ign(px.yx);+ let sigma = 1.3 * (1.0 + overcast);+ let drift = windDrift + vec3f(mc.u_cloud2DriftX - mc.u_cloudDriftX, 0.0, mc.u_cloud2DriftZ - mc.u_cloudDriftZ);+ let ambient = dome * 0.7 + lin(vec3f(0.55, 0.65, 0.9)) * 0.015 * night;+ let sunLight = sunTrans * 1.2 * mc.u_cloudBrightness;+ let phase = cloudPhase(dot(dir, sun));+ var trans = 1.0;+ var lit = vec3f(0.0);+ for (var i : i32 = 0; i < STEPS; i = i + 1) {+ 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 = cloud2Density(p);+ if (d < 0.002) { continue; }+ let tau = cloud2Density(p + sun * mc.u_cloud2Thickness * 0.5) * mc.u_cloud2Thickness * 0.5 * sigma;+ let S = sunLight * exp(-tau) * phase * 2.0 + ambient;+ let tr = exp(-d * sigma * ds);+ lit = lit + trans * (1.0 - tr) * S;+ trans = trans * tr;+ }+ let fade = smoothstep(0.01, 0.14, dir.y);+ trans = mix(1.0, trans, fade);+ lit = lit * fade;+ if (mc.u_cloud2Base >= mc.u_cloudBase) { out.L = out.L + out.T * lit; } else { out.L = lit + trans * out.L; }+ out.T = out.T * trans;+ }++ return out;+}+`;+export const atmosphereEffects = {...rain2dEffects,...fog2dEffects,@@ -57,6 +357,20 @@ export const atmosphereEffects = {#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 param u_cloudScale float default(1) range(0.1,8)+#pragma param u_cloudSoftness float default(0.22) range(0.02,1)+#pragma param u_cloudChange float default(0.004) range(0,1)+#pragma param u_cloudSilver float default(0.6) range(0,2)+#pragma param u_cloud2Coverage float default(0) range(0,1)+#pragma param u_cloud2Base float default(6) range(0.2,20)+#pragma param u_cloud2Thickness float default(0.6) range(0.05,8)+#pragma param u_cloud2Scale float default(1.8) range(0.1,8)+#pragma param u_cloud2Wind float default(1.6) range(0,8)+#pragma param u_cloudDriftX float default(0) range(-100000000,100000000)+#pragma param u_cloudDriftZ float default(0) range(-100000000,100000000)+#pragma param u_cloud2DriftX float default(0) range(-100000000,100000000)+#pragma param u_cloud2DriftZ float default(0) range(-100000000,100000000)+#pragma param u_cloudEvolve float default(0) range(0,100000000)#pragma lights#pragma fragment@@ -163,101 +477,17 @@ vec3 scatter(vec3 dir, vec3 sun, float alt, float haze, out vec3 sunTrans) {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;- }--}-${SSAO_GLSL}+${CLOUD_GLSL}${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.+vec4 splineWeights(float f) {+ float f2 = f * f, f3 = f2 * f;+ return vec4(1.0 - 3.0 * f + 3.0 * f2 - f3, 4.0 - 6.0 * f2 + 3.0 * f3, 1.0 + 3.0 * f + 3.0 * f2 - 3.0 * f3, f3) / 6.0;+}+bool upsampledClouds(out vec3 L, out float T) {ivec2 hsize = textureSize(u_texture, 0);vec2 hf = v_texCoord * vec2(hsize) - 0.5;@@ -265,13 +495,16 @@ bool upsampledClouds(out vec3 L, out float T) {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;+ // Four texels a side and not two: the march's jitter is a pattern a couple of texels across, which a plain bilinear read shows as a halftone, and a cloud has no edge this softens.+ vec4 wx = splineWeights(fr.x), wy = splineWeights(fr.y);+ for (int y = 0; y < 4; ++y) {+ for (int x = 0; x < 4; ++x) {+ vec4 h = texelFetch(u_texture, clamp(base + ivec2(x - 1, y - 1), ivec2(0), hsize - ivec2(1)), 0);+ if (h.a < 0.0) continue;+ float bw = wx[x] * wy[y];+ sum += h * bw;+ wsum += bw;+ }}if (wsum < UP_MIN_WEIGHT) return false;sum /= wsum;@@ -343,8 +576,7 @@ void main() {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;+ vec3 windDrift = vec3(u_cloudDriftX, 0.0, u_cloudDriftZ) + anchor;if (geometry) {float lit;if (!upsampledShadow(worldFromDepth(v_texCoord, depth), entry, lit)) {@@ -401,7 +633,7 @@ void main() {}// 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) {+ if ((max(u_clouds, overcast * 0.95) > 0.01 || u_cloud2Coverage > 0.01) && dir.y > 0.01) {vec3 L;float T;if (!upsampledClouds(L, T)) cloudLayer(dir, sun, dome, sunTrans, night, overcast, windDrift, L, T);@@ -470,146 +702,49 @@ fn lightDepth(p : vec3f, d : vec3f) -> vec3f {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;+ 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 1.0 - strength * 0.8 * (1.0 - exp(-tau));+ return vec3f(od, 1.0);}-struct CloudLayer { L : vec3f, T : f32 };+struct Scatter { radiance : vec3f, sunTrans : vec3f };-/// 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;+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 fade = smoothstep(0.01, 0.14, dir.y);- out.T = mix(1.0, trans, fade);- out.L = lit * fade;}-+ 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;}-${SSAO_WGSL}++${CLOUD_WGSL}${SSAO_WGSL}const UP_MIN_WEIGHT : f32 = 0.15;@@ -617,6 +752,12 @@ 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 splineWeights(f : f32) -> vec4f {+ let f2 = f * f;+ let f3 = f2 * f;+ return vec4f(1.0 - 3.0 * f + 3.0 * f2 - f3, 4.0 - 6.0 * f2 + 3.0 * f3, 1.0 + 3.0 * f + 3.0 * f2 - 3.0 * f3, f3) / 6.0;+}+fn upsampledClouds(uv : vec2f) -> CloudRead {var out : CloudRead;out.ok = false;@@ -626,17 +767,16 @@ fn upsampledClouds(uv : vec2f) -> CloudRead {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;+ let wx = splineWeights(fr.x);+ let wy = splineWeights(fr.y);+ for (var y : i32 = 0; y < 4; y = y + 1) {+ for (var x : i32 = 0; x < 4; x = x + 1) {+ let h = textureLoad(t0, clamp(base + vec2i(x - 1, y - 1), vec2i(0), hsize - vec2i(1)), 0);+ if (h.a < 0.0) { continue; }+ let bw = wx[x] * wy[y];+ sum = sum + h * bw;+ wsum = wsum + bw;+ }}if (wsum < UP_MIN_WEIGHT) { return out; }sum = sum / wsum;@@ -712,8 +852,7 @@ fn upsampledShadow(uv : vec2f, P : vec3f, entry : vec3f) -> ShadowRead {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;+ let windDrift = vec3f(mc.u_cloudDriftX, 0.0, mc.u_cloudDriftZ) + anchor;if (geometry) {var lit = 1.0;let up = upsampledShadow(v.v_texCoord, worldFromDepth(v.v_texCoord, depth), entry);@@ -763,7 +902,7 @@ fn upsampledShadow(uv : vec2f, P : vec3f, entry : vec3f) -> ShadowRead {}}- if (max(mc.u_clouds, overcast * 0.95) > 0.01 && dir.y > 0.01) {+ if ((max(mc.u_clouds, overcast * 0.95) > 0.01 || mc.u_cloud2Coverage > 0.01) && dir.y > 0.01) {var cl : CloudLayer;let up = upsampledClouds(v.v_texCoord);if (up.ok) {@@ -827,6 +966,20 @@ fn upsampledShadow(uv : vec2f, P : vec3f, entry : vec3f) -> ShadowRead {#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 param u_cloudScale float default(1) range(0.1,8)+#pragma param u_cloudSoftness float default(0.22) range(0.02,1)+#pragma param u_cloudChange float default(0.004) range(0,1)+#pragma param u_cloudSilver float default(0.6) range(0,2)+#pragma param u_cloud2Coverage float default(0) range(0,1)+#pragma param u_cloud2Base float default(6) range(0.2,20)+#pragma param u_cloud2Thickness float default(0.6) range(0.05,8)+#pragma param u_cloud2Scale float default(1.8) range(0.1,8)+#pragma param u_cloud2Wind float default(1.6) range(0,8)+#pragma param u_cloudDriftX float default(0) range(-100000000,100000000)+#pragma param u_cloudDriftZ float default(0) range(-100000000,100000000)+#pragma param u_cloud2DriftX float default(0) range(-100000000,100000000)+#pragma param u_cloud2DriftZ float default(0) range(-100000000,100000000)+#pragma param u_cloudEvolve float default(0) range(0,100000000)#pragma lights#pragma highprecision@@ -933,96 +1086,7 @@ vec3 scatter(vec3 dir, vec3 sun, float alt, float haze, out vec3 sunTrans) {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;- }--}-+${CLOUD_GLSL}void main() {vec4 src = texture(u_texture, v_texCoord);ivec2 size = textureSize(u_sceneDepth, 0);@@ -1060,8 +1124,7 @@ void main() {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;+ vec3 windDrift = vec3(u_cloudDriftX, 0.0, u_cloudDriftZ) + 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);@@ -1176,104 +1239,7 @@ fn scatter(dir : vec3f, sun : vec3f, alt : f32, haze : f32) -> Scatter {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;-}-+${CLOUD_WGSL}@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));@@ -1304,8 +1270,7 @@ fn cloudLayer(dir : vec3f, sun : vec3f, dome : vec3f, sunTrans : vec3f, night :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;+ let windDrift = vec3f(mc.u_cloudDriftX, 0.0, mc.u_cloudDriftZ) + 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);@@ -1364,8 +1329,11 @@ fn cloudLayer(dir : vec3f, sun : vec3f, dome : vec3f, sunTrans : vec3f, night :#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_skyDriftX float default(0) range(-100000000,100000000)+#pragma param u_skyDriftZ float default(0) range(-100000000,100000000)+#pragma param u_skyEvolve float default(0) range(0,100000000)+#pragma param u_skyScale float default(1) range(0.1,8)+#pragma param u_skySoftness float default(0.22) range(0.02,1)#pragma param u_cloudMask texture default(black)#pragma param u_cloudMaskTile float default(0) range(0,100)#pragma param u_cloudMask2 texture default(black)@@ -1425,9 +1393,9 @@ float cloudField(vec2 xz, float noise, float h) {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;+ vec3 q = vec3(p.x, p.y * 1.8 + u_skyEvolve, p.z) * 0.32 * u_skyScale;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;+ return smoothstep(1.0 - coverage, 1.0 - coverage + u_skySoftness, 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) {@@ -1474,7 +1442,7 @@ vec3 volumeLight(vec3 entry, vec3 dir, float len, bool sky, vec3 sun, vec4 lightfloat 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;+ vec3 drift = vec3(u_skyDriftX, 0.0, u_skyDriftZ);float shade = 1.0;for (int i = 0; i < VOLUME_STEPS; ++i) {float t = u_startDistance + (float(i) + jitter) * ds;@@ -1610,9 +1578,9 @@ fn cloudField(xz : vec2f, noise : f32, h : f32) -> f32 {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 q = vec3f(p.x, p.y * 1.8 + mc.u_skyEvolve, p.z) * 0.32 * mc.u_skyScale;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;+ return smoothstep(1.0 - coverage, 1.0 - coverage + mc.u_skySoftness, 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; }@@ -1654,7 +1622,7 @@ fn volumeLight(entry : vec3f, dir : vec3f, len : f32, sky : bool, sun : vec3f, flet 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;+ let drift = vec3f(mc.u_skyDriftX, 0.0, mc.u_skyDriftZ);var shade = 1.0;for (var i : i32 = 0; i < VOLUME_STEPS; i = i + 1) {let t = mc.u_startDistance + (f32(i) + jitter) * ds;@@ -1796,8 +1764,11 @@ fn upsampledVolume(uv : vec2f, len : f32) -> VolumeRead {#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_skyDriftX float default(0) range(-100000000,100000000)+#pragma param u_skyDriftZ float default(0) range(-100000000,100000000)+#pragma param u_skyEvolve float default(0) range(0,100000000)+#pragma param u_skyScale float default(1) range(0.1,8)+#pragma param u_skySoftness float default(0.22) range(0.02,1)#pragma param u_cloudMask texture default(black)#pragma param u_cloudMaskTile float default(0) range(0,100)#pragma param u_cloudMask2 texture default(black)@@ -1855,9 +1826,9 @@ float cloudField(vec2 xz, float noise, float h) {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;+ vec3 q = vec3(p.x, p.y * 1.8 + u_skyEvolve, p.z) * 0.32 * u_skyScale;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;+ return smoothstep(1.0 - coverage, 1.0 - coverage + u_skySoftness, 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) {@@ -1904,7 +1875,7 @@ vec3 volumeLight(vec3 entry, vec3 dir, float len, bool sky, vec3 sun, vec4 lightfloat 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;+ vec3 drift = vec3(u_skyDriftX, 0.0, u_skyDriftZ);float shade = 1.0;for (int i = 0; i < VOLUME_STEPS; ++i) {float t = u_startDistance + (float(i) + jitter) * ds;@@ -1992,9 +1963,9 @@ fn cloudField(xz : vec2f, noise : f32, h : f32) -> f32 {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 q = vec3f(p.x, p.y * 1.8 + mc.u_skyEvolve, p.z) * 0.32 * mc.u_skyScale;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;+ return smoothstep(1.0 - coverage, 1.0 - coverage + mc.u_skySoftness, 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; }@@ -2036,7 +2007,7 @@ fn volumeLight(entry : vec3f, dir : vec3f, len : f32, sky : bool, sun : vec3f, flet 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;+ let drift = vec3f(mc.u_skyDriftX, 0.0, mc.u_skyDriftZ);var shade = 1.0;for (var i : i32 = 0; i < VOLUME_STEPS; i = i + 1) {let t = mc.u_startDistance + (f32(i) + jitter) * ds;diff --git a/sdk/src/postprocess/volumeSystem.ts b/sdk/src/postprocess/volumeSystem.tsindex 8430df1c5..4221fef89 100644--- a/sdk/src/postprocess/volumeSystem.ts+++ b/sdk/src/postprocess/volumeSystem.ts@@ -13,8 +13,9 @@ import { getEffectDef } from './effects';import { blendVolumeEffects, computeVolumeFactor, type ActiveVolume, type BlendedEffect, type VolumeTransform } from './volumeBlending';import type { ShaderHandle } from '../render/material';import { Material } from '../render/material';-import { applyWeatherToEffects, currentWeather, currentWeatherDrive } from './weather';+import { applyWeatherToEffects, currentWeather, currentWeatherDrive, WeatherDrive } from './weather';import { applyCloudsToFog } from './cloudShafts';+import { applySkyClock } from './skyClock';import { applyRainSheltersToRain } from '../render/rainShelter';import { applyFogOfWarToFog } from '../render/fogOfWar';import { runtimeTextureHandle } from './runtimeTextures';@@ -307,6 +308,7 @@ export function effectsAt(volumes: { data: PostProcessVolumeData; tx: VolumeTranif (weather) applyWeatherToEffects(blended, weather, currentWeatherDrive());const clock = currentTimeOfDay();if (clock) applyTimeToEffects(blended, clock, currentTimeDrive(), timeLightsApplied());+ applySkyClock(blended, weather && (currentWeatherDrive() & WeatherDrive.Wind) ? weather.wind : 1);applyCloudsToFog(blended);// A 2D rain is told where the ground under a roof is dry, by a mask drawn this frame.applyRainSheltersToRain(blended);diff --git a/sdk/src/postprocess/weatherSystem.ts b/sdk/src/postprocess/weatherSystem.tsindex 180b0c51a..c0a32943a 100644--- a/sdk/src/postprocess/weatherSystem.ts+++ b/sdk/src/postprocess/weatherSystem.ts@@ -26,6 +26,7 @@ import {blendWeather, setCurrentWeather, weatherPresetId, WEATHER_NONE, WeatherDrive, type WeatherState,} from './weather';import { Material } from '../render/material';+import { advanceSkyClock, setSkyClockState } from './skyClock';import { timeOfDayAt, setCurrentTimeOfDay, TimeDrive, type TimeOfDayState } from './timeOfDay';/** What a light was authored with, kept so a weather scales the author's number and never its own last answer. */@@ -278,6 +279,7 @@ function advance(entity: Entity, data: WeatherData, dt: number): void {export const weatherSystem = defineSystem([Query(Mut(Weather)), Query(Mut(TimeOfDay)), Res(Time), GetWorld()],(weathers: Iterable<[Entity, WeatherData]>, times: Iterable<[Entity, TimeOfDayData]>, time: TimeData, world: World) => {+ advanceSkyClock(time.delta);let found: [Entity, WeatherData] | null = null;for (const entry of weathers) { found = entry; break; }let clock: [Entity, TimeOfDayData] | null = null;@@ -338,4 +340,5 @@ export function resetWeatherSystem(): void {cycles.clear();setCurrentWeather(null);setCurrentTimeOfDay(null);+ setSkyClockState(null);}diff --git a/sdk/tests/cloud-shafts.test.ts b/sdk/tests/cloud-shafts.test.tsindex 81b09f7fe..2b61a69ab 100644--- a/sdk/tests/cloud-shafts.test.ts+++ b/sdk/tests/cloud-shafts.test.ts@@ -15,7 +15,7 @@ const effect = (uniforms: Record<string, number> = {}, enabled = true, textures:describe('applyCloudsToFog', () => {it('copies the sky\'s cloud layer onto a fog that asks for cloud shafts, with the wind as a velocity in kilometres a second', () => {const fog = effect({ u_cloudShade: 0.8 });- const sky = effect({ u_cloudShadow: 0.6, u_clouds: 0.4, u_overcast: 0.5, u_cloudBase: 2, u_cloudThickness: 3, u_unitsPerKm: 100000, u_windSpeed: 0.1, u_windDirection: 90 });+ const sky = effect({ u_cloudDriftX: 7, u_cloudDriftZ: -3, u_cloudEvolve: 0.5, u_cloudScale: 2, u_cloudShadow: 0.6, u_clouds: 0.4, u_overcast: 0.5, u_cloudBase: 2, u_cloudThickness: 3, u_unitsPerKm: 100000, u_windSpeed: 0.1, u_windDirection: 90 });applyCloudsToFog(new Map([['heightFog', fog], ['skyAtmosphere', sky]]));// An overcast of half is a cover of 0.475, above the sky's own 0.4.expect(fog.uniforms.get('u_skyCover')).toBeCloseTo(0.475, 6);@@ -23,8 +23,10 @@ describe('applyCloudsToFog', () => {expect(fog.uniforms.get('u_skyThickness')).toBe(3);expect(fog.uniforms.get('u_skyUnits')).toBe(100000);expect(fog.uniforms.get('u_skySigma')).toBeCloseTo(1.3 * 1.75, 6);- expect(fog.uniforms.get('u_skyWindX')).toBeCloseTo(0.1, 6);- expect(fog.uniforms.get('u_skyWindZ')).toBeCloseTo(0, 6);+ expect(fog.uniforms.get('u_skyDriftX')).toBe(7);+ expect(fog.uniforms.get('u_skyDriftZ')).toBe(-3);+ expect(fog.uniforms.get('u_skyEvolve')).toBe(0.5);+ expect(fog.uniforms.get('u_skyScale')).toBe(2);});it('hands the fog the sky\'s cloud masks and their tiles, and none where the sky has none', () => {diff --git a/sdk/tests/sky-clock.test.ts b/sdk/tests/sky-clock.test.tsnew file mode 100644index 000000000..7d9f40f82--- /dev/null+++ b/sdk/tests/sky-clock.test.ts@@ -0,0 +1,114 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/** The sky's clouds are where their wind has taken them so far: a wind that changes moves them from there, and never moves the sky. */+import { beforeEach, describe, expect, it } from 'vitest';+import { advanceSkyClock, applySkyClock, setSkyClockState, skyClockState } from '../src/postprocess/skyClock';+import { applyCloudsToFog } from '../src/postprocess/cloudShafts';+import { resetWeatherSystem } from '../src/postprocess/weatherSystem';+import type { BlendedEffect } from '../src/postprocess/volumeBlending';++const sky = (uniforms: Record<string, number> = {}): BlendedEffect =>+ ({ enabled: true, uniforms: new Map(Object.entries({ u_windSpeed: 1, u_windDirection: 90, u_cloud2Wind: 2, u_cloudChange: 0.5, ...uniforms })), colors: new Map(), textures: new Map() }) as unknown as BlendedEffect;+const effects = (e: BlendedEffect): Map<string, BlendedEffect> => new Map([['skyAtmosphere', e]]);++beforeEach(() => setSkyClockState(null));++describe('the sky clock', () => {+ it('adds up what the wind does, the way it blows', () => {+ applySkyClock(effects(sky()), 1);+ advanceSkyClock(2);+ expect(skyClockState().driftX).toBeCloseTo(2, 6);+ expect(skyClockState().driftZ).toBeCloseTo(0, 6);+ expect(skyClockState().evolve).toBeCloseTo(1, 6);+ });++ it('tells the sky where its clouds are', () => {+ const e = sky();+ applySkyClock(effects(e), 1);+ advanceSkyClock(3);+ applySkyClock(effects(e), 1);+ expect(e.uniforms.get('u_cloudDriftX')).toBeCloseTo(3, 6);+ expect(e.uniforms.get('u_cloudEvolve')).toBeCloseTo(1.5, 6);+ });++ it('does not move the clouds when the wind changes, however long it has run', () => {+ applySkyClock(effects(sky()), 1);+ advanceSkyClock(10000);+ const before = skyClockState().driftX;+ const e = sky({ u_windSpeed: 2 });+ applySkyClock(effects(e), 1);+ expect(skyClockState().driftX).toBe(before);+ expect(e.uniforms.get('u_cloudDriftX')).toBe(before);+ advanceSkyClock(1);+ expect(skyClockState().driftX).toBeCloseTo(before + 2, 3);+ });++ it('turns with the wind from where the clouds are', () => {+ applySkyClock(effects(sky()), 1);+ advanceSkyClock(5);+ applySkyClock(effects(sky({ u_windDirection: 0 })), 1);+ advanceSkyClock(5);+ expect(skyClockState().driftX).toBeCloseTo(5, 6);+ expect(skyClockState().driftZ).toBeCloseTo(5, 6);+ });++ it('takes the weather\'s wind', () => {+ applySkyClock(effects(sky()), 3);+ advanceSkyClock(1);+ expect(skyClockState().driftX).toBeCloseTo(3, 6);+ expect(skyClockState().upperDriftX).toBeCloseTo(6, 6);+ });++ it('moves the upper layer by its own share of the wind', () => {+ applySkyClock(effects(sky({ u_cloud2Wind: 0.5 })), 1);+ advanceSkyClock(4);+ expect(skyClockState().upperDriftX).toBeCloseTo(2, 6);+ expect(skyClockState().driftX).toBeCloseTo(4, 6);+ });++ it('stands still while the game does, and the same steps come to the same place', () => {+ applySkyClock(effects(sky()), 1);+ advanceSkyClock(1 / 60);+ const at = skyClockState();+ advanceSkyClock(0);+ expect(skyClockState()).toEqual(at);+ setSkyClockState(null);+ applySkyClock(effects(sky()), 1);+ advanceSkyClock(1 / 60);+ expect(skyClockState()).toEqual(at);+ });++ it('keeps a saved place, and starts over with none', () => {+ setSkyClockState({ driftX: 9, evolve: 2 });+ expect(skyClockState()).toEqual({ driftX: 9, driftZ: 0, upperDriftX: 0, upperDriftZ: 0, evolve: 2 });+ setSkyClockState(null);+ expect(skyClockState().driftX).toBe(0);+ });++ it('starts over when the weather system does, so another run in one page is the same sky', () => {+ applySkyClock(effects(sky()), 1);+ advanceSkyClock(5);+ resetWeatherSystem();+ expect(skyClockState().driftX).toBe(0);+ });++ it('leaves a scene with no sky alone', () => {+ const none = new Map<string, BlendedEffect>();+ applySkyClock(none, 1);+ advanceSkyClock(5);+ expect(none.size).toBe(0);+ expect(skyClockState().driftX).toBe(0);+ });++ it('gives the fog the same place the sky has, so its shafts stand under the clouds', () => {+ const e = sky({ u_cloudShadow: 0.5, u_clouds: 0.5 });+ const fog = { enabled: true, uniforms: new Map([['u_cloudShade', 1]]), colors: new Map(), textures: new Map() } as unknown as BlendedEffect;+ const all = new Map([['skyAtmosphere', e], ['heightFog', fog]]);+ applySkyClock(all, 1);+ advanceSkyClock(7);+ applySkyClock(all, 1);+ applyCloudsToFog(all);+ expect(fog.uniforms.get('u_skyDriftX')).toBeCloseTo(7, 6);+ expect(fog.uniforms.get('u_skyEvolve')).toBeCloseTo(3.5, 6);+ });+});diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 7e4765f4a..6fb6810d8 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -4232,6 +4232,8 @@ export const NOTED = {{"note": "Particles and plain sprites in a Canvas paint by their place in its tree."},"fix(text): stopping lets go of the labels the engine holds, so the next Play cannot draw them on other entities":{"note": "Labels no longer come back as black boxes after a change between Plays."},+ "feat(sky): clouds that keep time with the game, follow the weather's wind, and have a second layer":+ {"note": "Clouds that keep time with the game."},"feat(voxel): ground that is a volume, carved while the game runs, with collision that follows and saves by chunk":{"note": "Ground that is a volume."},"feat(terrain): a material that fades into the ground it meets, a road laid over a terrain, and snow on whatever faces the sky":