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.

feat(sky): clouds may be drawn from a tileable coverage mask, and the shadows and beams read it too

yhh4c204cd52e29
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 18db94728..be70c6414 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,7 @@ published separately; it ships inside the editor.
### Added
+- **Clouds drawn from a coverage picture.** `skyAtmosphere` takes a Cloud Mask (`u_cloudMask`, with `u_cloudMaskTile` in kilometres of the cloud layer) and a second, higher one (`u_cloudMask2`): the clouds stand where the mask says and the noise only erodes them. The shadow the clouds cast and the beams of `heightFog`'s `cloudShade` read the same masks, which the fog takes from the sky. Terrain & Water uses the two the art made.
- **A Display LUT.** The `displayLut` effect reads a look off a 1024x32 lookup table by the picture as the display will show it, in the last pass, after the output transform and the transfer function: the place a look authored against the finished image belongs, and one no effect in the chain can be, since they run on scene values before the curve. It is a variant of the pass that was running, one texture read and no pass of its own, and `PostProcessStack.setDisplayLut(texture, amount)` is the same thing in code. The last pass now reads its parameters off the reflected layout as a material does, and the copies it makes share them.
- **Light beams that follow the clouds.** `heightFog` has a `cloudShade`: its volumetric light marches the sun's ray up through the cloud layer of a `skyAtmosphere` whose clouds cast shadows (the same density the sky draws, fixed to the world and drifting with its wind), so a gap in the clouds is a beam in the air and what they cover is dark, whichever way the camera looks. `volumeRange` may now be as long as a million units, for beams through clouds kilometres up. The sky's clouds are copied onto the fog each frame, and an effect's uniform may be `internal`, which an inspector does not list.
- **Sparkle on the water.** The Water shader reads two tiled normal maps that scroll over the surface (`u_detailMap`, `u_detailTile0/1`, `u_detailStrength0/1`, `u_detailFlow0/1`), packed into the one texture a material has room for, so the sun's glints on small slopes come from lit specular response and not a white texture; a material that sets no strength is unchanged.
diff --git a/desktop b/desktop
index 0337753d4..e106641ae 160000
--- a/desktop
+++ b/desktop
@@ -1 +1 @@
-Subproject commit 0337753d463af2ff79cc40f9f6ad36441ef8306f
+Subproject commit e106641ae421766a26ddcd61f458e018f16c8548
diff --git a/docs/astro/src/content/docs/graphics/post-processing.mdx b/docs/astro/src/content/docs/graphics/post-processing.mdx
index e14a8dfc9..ddd414087 100644
--- a/docs/astro/src/content/docs/graphics/post-processing.mdx
+++ b/docs/astro/src/content/docs/graphics/post-processing.mdx
@@ -297,6 +297,8 @@ fixed to the world and drift with its wind), against the same density the sky dr
`volumeRange`, in world units, and a beam through clouds kilometres up needs air that far up: a fog with a low `heightFalloff` and a `volumeRange` of
a few kilometres. The sky's cover, height, thickness and wind are copied onto the fog each frame; they are not settings of the fog's own.
+The clouds may be given their shape by a picture. A `skyAtmosphere` with a **Cloud Mask** (`u_cloudMask`, a tileable coverage texture: linear data, one channel, repeating, 0 clear and 1 dense) and its `cloudMaskTile` (the kilometres of the cloud layer one repeat covers) draws clouds where the mask says, the noise only eroding the edges; a second, softer and higher mask (`u_cloudMask2` with `cloudMask2Tile`) is folded in at a third. The same clouds shape the shadow they cast on the scene and the beams the fog lights, which read the masks off the sky. A mask is data: no sRGB, no compression.
+
`lightShafts` finds the sun's light through whatever stands in front of it, in screen space: each pixel
marches toward the sun's place on screen and counts how much of the way is open sky. It needs the
sun on screen or just off it, and only what the depth buffer holds can cast a shaft. Together the two
diff --git a/docs/astro/src/content/docs/zh-cn/graphics/post-processing.mdx b/docs/astro/src/content/docs/zh-cn/graphics/post-processing.mdx
index 0936dee2a..c2aa8cedc 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
@@ -223,6 +223,8 @@ registerEffect({
高的云需要同样高的空气:`heightFalloff` 取小,`volumeRange` 取几公里。天空的覆盖、高度、厚度和风每帧
复制到雾上,不是雾自己的设置。
+云的形状可以由一张图决定。`skyAtmosphere` 带上**云遮罩**(`u_cloudMask`,一张可平铺的覆盖率纹理:线性数据、单通道、重复寻址,0 通透、1 浓密)和它的 `cloudMaskTile`(一次重复覆盖云层多少公里),云就画在遮罩说有的地方,噪声只负责侵蚀边缘;第二张更柔、更高的遮罩(`u_cloudMask2` 与 `cloudMask2Tile`)按三分之一并入。云投在场景上的影子、雾里被照亮的光束都是同一份云,雾从天空读取遮罩。遮罩是数据:不要 sRGB,不要压缩。
+
`lightShafts` 在屏幕空间里找出太阳光穿过前方遮挡物的样子:每个像素朝太阳在屏幕上的位置步进,数这一路有
多少是开阔天空。太阳需要在屏幕上或刚好在屏幕外,而且只有深度缓冲里有的东西才能投出光束。两者配合出
低角度太阳的"雾加光束"的样子;`lightShafts` 仍是屏幕空间的,带阴影的体积光是 `heightFog` 的 `volumetric`。
diff --git a/sdk/src/postprocess/cloudShafts.ts b/sdk/src/postprocess/cloudShafts.ts
index 5aefe8da4..eb70e17aa 100644
--- a/sdk/src/postprocess/cloudShafts.ts
+++ b/sdk/src/postprocess/cloudShafts.ts
@@ -25,6 +25,8 @@ export function applyCloudsToFog(effects: Map<string, BlendedEffect>): void {
const sky = effects.get('skyAtmosphere');
if (value(fog, 'heightFog', 'u_cloudShade') <= 0 || !sky || !sky.enabled || value(sky, 'skyAtmosphere', 'u_cloudShadow') <= 0) {
fog.uniforms.set('u_skyCover', 0);
+ fog.textures.delete('u_cloudMask');
+ fog.textures.delete('u_cloudMask2');
return;
}
const overcast = Math.min(Math.max(value(sky, 'skyAtmosphere', 'u_overcast'), 0), 1);
@@ -39,4 +41,10 @@ export function applyCloudsToFog(effects: Map<string, BlendedEffect>): void {
set('u_skySigma', 1.3 * (1 + overcast * 1.5));
set('u_skyWindX', Math.sin(angle) * speed);
set('u_skyWindZ', Math.cos(angle) * speed);
+ // 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);
+ if (ref) fog.textures.set(mask, ref); else fog.textures.delete(mask);
+ set(tile, ref ? value(sky, 'skyAtmosphere', tile) : 0);
+ }
}
diff --git a/sdk/src/postprocess/effects.ts b/sdk/src/postprocess/effects.ts
index cd61727b7..c2f1ef727 100644
--- a/sdk/src/postprocess/effects.ts
+++ b/sdk/src/postprocess/effects.ts
@@ -41,6 +41,8 @@ export interface EffectUniformDef {
export interface EffectTextureDef {
name: string;
label: string;
+ /** Set by the engine from another effect, not by a scene: an inspector does not list it. */
+ internal?: boolean;
}
/** A colour parameter (fog colour): the vec4 uniform it feeds and its default. */
@@ -329,6 +331,14 @@ registerEffect({
{ name: 'u_cloudShadow', label: 'Cloud Shadows', min: 0, max: 1, step: 0.01, defaultValue: 0, neutralValue: 0 },
// How many world units are a kilometre: the sky is in kilometres, the scene in its own units.
{ name: 'u_unitsPerKm', label: 'World Units per km', min: 1, max: 100000, step: 1, defaultValue: 1000 },
+ // With a Cloud Mask, how many kilometres of the cloud layer one repeat of it covers (0 leaves the clouds to the noise): the mask gives the clouds
+ // 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 },
+ ],
+ textures: [
+ { name: 'u_cloudMask', label: 'Cloud Mask (tileable coverage, linear, repeat)' },
+ { name: 'u_cloudMask2', label: 'Cloud Mask 2 (a softer, higher layer)' },
],
});
@@ -422,6 +432,12 @@ registerEffect({
{ 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_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 },
+ ],
+ textures: [
+ { name: 'u_cloudMask', label: 'Sky Cloud Mask', internal: true },
+ { name: 'u_cloudMask2', label: 'Sky Cloud Mask 2', internal: true },
],
colors: [
{ name: 'u_fogColor', label: 'Colour', defaultValue: { r: 0.62, g: 0.7, b: 0.8, a: 1 } },
diff --git a/sdk/src/postprocess/sources/atmosphere.ts b/sdk/src/postprocess/sources/atmosphere.ts
index 51139d361..7d4a1cc06 100644
--- a/sdk/src/postprocess/sources/atmosphere.ts
+++ b/sdk/src/postprocess/sources/atmosphere.ts
@@ -41,6 +41,10 @@ export const atmosphereEffects = {
#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 fragment
@@ -138,6 +142,15 @@ 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 is its: the noise only erodes it (a third of what it says). The layers are tiled
+/// in kilometres of the cloud plane, the second a softer, higher one folded in.
+float cloudField(vec2 xz, float noise) {
+ 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);
+ return mix(m, noise, 0.3);
+}
+
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);
@@ -145,6 +158,7 @@ float cloudDensity(vec3 p, float coverage, float base, float thick, int octaves)
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);
return smoothstep(1.0 - coverage, 1.0 - coverage + 0.22, n) * shape;
}
@@ -397,12 +411,20 @@ fn scatter(dir : vec3f, sun : vec3f, alt : f32, haze : f32) -> Scatter {
return out;
}
+fn cloudField(xz : vec2f, noise : 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.3);
+}
+
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);
return smoothstep(1.0 - coverage, 1.0 - coverage + 0.22, n) * shape;
}
@@ -583,6 +605,10 @@ fn cloudShadowAt(world : vec3f, cam : vec3f, sun : vec3f, coverage : f32, overca
#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
@@ -621,12 +647,21 @@ 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 is its: the noise only erodes it (a third of what it says). The layers are tiled
+/// in kilometres of the cloud plane, the second a softer, higher one folded in.
+float cloudField(vec2 xz, float noise) {
+ 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);
+ return mix(m, noise, 0.3);
+}
+
/// 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 = vnoise(q) * 0.58 + vnoise(q * 2.13) * 0.28 + 0.07;
+ float n = cloudField(p.xz, vnoise(q) * 0.58 + vnoise(q * 2.13) * 0.28 + 0.07);
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.
@@ -753,11 +788,18 @@ fn vnoise(p : vec3f) -> f32 {
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) -> 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.3);
+}
+
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 = vnoise(q) * 0.58 + vnoise(q * 2.13) * 0.28 + 0.07;
+ let n = cloudField(p.xz, vnoise(q) * 0.58 + vnoise(q * 2.13) * 0.28 + 0.07);
return smoothstep(1.0 - coverage, 1.0 - coverage + 0.22, n) * shape;
}
fn cloudLight(world : vec3f, camY : f32, sun : vec3f, drift : vec3f) -> f32 {
diff --git a/sdk/tests/cloud-shafts.test.ts b/sdk/tests/cloud-shafts.test.ts
index 67d413e22..81b09f7fe 100644
--- a/sdk/tests/cloud-shafts.test.ts
+++ b/sdk/tests/cloud-shafts.test.ts
@@ -8,8 +8,8 @@ import { applyCloudsToFog } from '../src/postprocess/cloudShafts';
import type { BlendedEffect } from '../src/postprocess/volumeBlending';
import '../src/postprocess/effects';
-const effect = (uniforms: Record<string, number> = {}, enabled = true): BlendedEffect => ({
- enabled, uniforms: new Map(Object.entries(uniforms)), textures: new Map(), colors: new Map(),
+const effect = (uniforms: Record<string, number> = {}, enabled = true, textures: Record<string, string> = {}): BlendedEffect => ({
+ enabled, uniforms: new Map(Object.entries(uniforms)), textures: new Map(Object.entries(textures)), colors: new Map(),
});
describe('applyCloudsToFog', () => {
@@ -27,6 +27,20 @@ describe('applyCloudsToFog', () => {
expect(fog.uniforms.get('u_skyWindZ')).toBeCloseTo(0, 6);
});
+ it('hands the fog the sky\'s cloud masks and their tiles, and none where the sky has none', () => {
+ const fog = effect({ u_cloudShade: 1 });
+ const sky = effect({ u_cloudShadow: 0.6, u_cloudMaskTile: 5, u_cloudMask2Tile: 9 }, true, { u_cloudMask: 'sky/cumulus.png' });
+ applyCloudsToFog(new Map([['heightFog', fog], ['skyAtmosphere', sky]]));
+ expect(fog.textures.get('u_cloudMask')).toBe('sky/cumulus.png');
+ expect(fog.uniforms.get('u_cloudMaskTile')).toBe(5);
+ // The second tile means nothing without its texture.
+ expect(fog.textures.has('u_cloudMask2')).toBe(false);
+ expect(fog.uniforms.get('u_cloudMask2Tile')).toBe(0);
+ sky.uniforms.set('u_cloudShadow', 0);
+ applyCloudsToFog(new Map([['heightFog', fog], ['skyAtmosphere', sky]]));
+ expect(fog.textures.has('u_cloudMask')).toBe(false);
+ });
+
it('leaves the fog with no clouds without Cloud Shafts, a sky, or clouds that cast shadows', () => {
const cases: [BlendedEffect, BlendedEffect | undefined][] = [
[effect({ u_cloudShade: 0 }), effect({ u_cloudShadow: 0.6 })],
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 0bfde8bd5..0614bbece 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -2854,6 +2854,12 @@ export const NOTED = {
{"note":"A generated world's plants."},
"feat(examples): Terrain & Water takes the compact grass and the groundcover cards":
{"note":"A generated world's plants."},
+ "feat(sky): clouds may be drawn from a tileable coverage mask, and the shadows and beams read it too":
+ {"note":"Clouds drawn from a coverage picture."},
+ "feat(examples): Terrain & Water draws its clouds from the art's coverage masks":
+ {"note":"Clouds drawn from a coverage picture."},
+ "feat(inspector): an effect's internal textures are not listed either":
+ {"internal":"Hides parameters the engine writes itself."},
"feat(water): two tiled, scrolling detail normal maps give the surface its sparkle":
{"note":"Sparkle on the water."},
"fix(lod): a LODGroup with only a cull size is no longer ignored":