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.

fix(sky): a cloud mask's clouds are domes, and no longer smear into streaks seen low

yhh9ca1f2a548cf
diff --git a/docs/astro/src/content/docs/graphics/post-processing.mdx b/docs/astro/src/content/docs/graphics/post-processing.mdx
index ddd414087..93c372bbc 100644
--- a/docs/astro/src/content/docs/graphics/post-processing.mdx
+++ b/docs/astro/src/content/docs/graphics/post-processing.mdx
@@ -297,7 +297,7 @@ 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.
+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. It says nothing of height, so give the layer a modest `cloudThickness` (under a kilometre): seen low, a thick layer of it smears into streaks along the line of sight.
`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
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 c2aa8cedc..121b35257 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,7 +223,7 @@ registerEffect({
高的云需要同样高的空气:`heightFalloff` 取小,`volumeRange` 取几公里。天空的覆盖、高度、厚度和风每帧
复制到雾上,不是雾自己的设置。
-云的形状可以由一张图决定。`skyAtmosphere` 带上**云遮罩**(`u_cloudMask`,一张可平铺的覆盖率纹理:线性数据、单通道、重复寻址,0 通透、1 浓密)和它的 `cloudMaskTile`(一次重复覆盖云层多少公里),云就画在遮罩说有的地方,噪声只负责侵蚀边缘;第二张更柔、更高的遮罩(`u_cloudMask2` 与 `cloudMask2Tile`)按三分之一并入。云投在场景上的影子、雾里被照亮的光束都是同一份云,雾从天空读取遮罩。遮罩是数据:不要 sRGB,不要压缩。
+云的形状可以由一张图决定。`skyAtmosphere` 带上**云遮罩**(`u_cloudMask`,一张可平铺的覆盖率纹理:线性数据、单通道、重复寻址,0 通透、1 浓密)和它的 `cloudMaskTile`(一次重复覆盖云层多少公里),云就画在遮罩说有的地方,噪声只负责侵蚀边缘;第二张更柔、更高的遮罩(`u_cloudMask2` 与 `cloudMask2Tile`)按三分之一并入。云投在场景上的影子、雾里被照亮的光束都是同一份云,雾从天空读取遮罩。遮罩是数据:不要 sRGB,不要压缩。它不含高度信息,所以云层的 `cloudThickness` 要薄(不到一公里):低角度看过去,太厚的云层会沿视线被涂抹成条纹。
`lightShafts` 在屏幕空间里找出太阳光穿过前方遮挡物的样子:每个像素朝太阳在屏幕上的位置步进,数这一路有
多少是开阔天空。太阳需要在屏幕上或刚好在屏幕外,而且只有深度缓冲里有的东西才能投出光束。两者配合出
diff --git a/sdk/src/postprocess/sources/atmosphere.ts b/sdk/src/postprocess/sources/atmosphere.ts
index 7d4a1cc06..7fa5bfd09 100644
--- a/sdk/src/postprocess/sources/atmosphere.ts
+++ b/sdk/src/postprocess/sources/atmosphere.ts
@@ -142,13 +142,14 @@ 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) {
+/// 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);
- return mix(m, noise, 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) {
@@ -158,7 +159,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);
+ n = cloudField(p.xz, n, h);
return smoothstep(1.0 - coverage, 1.0 - coverage + 0.22, n) * shape;
}
@@ -411,11 +412,11 @@ fn scatter(dir : vec3f, sun : vec3f, alt : f32, haze : f32) -> Scatter {
return out;
}
-fn cloudField(xz : vec2f, noise : f32) -> f32 {
+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.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 {
@@ -424,7 +425,7 @@ fn cloudDensity(p : vec3f, coverage : f32, base : f32, thick : f32, octaves : i3
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);
+ n = cloudField(p.xz, n, h);
return smoothstep(1.0 - coverage, 1.0 - coverage + 0.22, n) * shape;
}
@@ -647,13 +648,14 @@ 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) {
+/// 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);
- return mix(m, noise, 0.3);
+ // A mask knows nothing of height, and a column of it seen along the slab is a streak: the cloud's top stands only over the mask's core, a dome.
+ return mix(m, noise, 0.5) - 0.35 * (h - 0.35);
}
/// The sky's cloud density, the same one the dome draws (see skyAtmosphere), at a point in kilometres.
@@ -661,7 +663,7 @@ float cloudDensity(vec3 p, float coverage, float base, float thick) {
float h = clamp((p.y - base) / thick, 0.0, 1.0);
float shape = smoothstep(0.0, 0.14, h) * smoothstep(1.0, 0.55, h);
vec3 q = vec3(p.x, p.y * 1.8, p.z) * 0.32;
- float n = cloudField(p.xz, vnoise(q) * 0.58 + vnoise(q * 2.13) * 0.28 + 0.07);
+ float n = cloudField(p.xz, vnoise(q) * 0.58 + vnoise(q * 2.13) * 0.28 + 0.07, h);
return smoothstep(1.0 - coverage, 1.0 - coverage + 0.22, n) * shape;
}
/// How much of the sun reaches the air at @p world through the cloud layer, 1 being all of it: the sun's ray marched up through the slab.
@@ -788,18 +790,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 {
+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.3);
+ return mix(m, noise, 0.5) - 0.35 * (h - 0.35);
}
fn cloudDensity(p : vec3f, coverage : f32, base : f32, thick : f32) -> f32 {
let h = clamp((p.y - base) / thick, 0.0, 1.0);
let shape = smoothstep(0.0, 0.14, h) * smoothstep(1.0, 0.55, h);
let q = vec3f(p.x, p.y * 1.8, p.z) * 0.32;
- let n = cloudField(p.xz, vnoise(q) * 0.58 + vnoise(q * 2.13) * 0.28 + 0.07);
+ let n = cloudField(p.xz, vnoise(q) * 0.58 + vnoise(q * 2.13) * 0.28 + 0.07, h);
return smoothstep(1.0 - coverage, 1.0 - coverage + 0.22, n) * shape;
}
fn cloudLight(world : vec3f, camY : f32, sun : vec3f, drift : vec3f) -> f32 {
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 0614bbece..46bdd6865 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -2860,6 +2860,10 @@ export const NOTED = {
{"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."},
+ "fix(sky): a cloud mask's clouds are domes, and no longer smear into streaks seen low":
+ {"note":"Clouds drawn from a coverage picture."},
+ "fix(examples): Terrain & Water has a thin cloud layer, so its masked clouds do not streak":
+ {"note":"Clouds drawn from a coverage picture."},
"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":