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): a sky of your own shades the ground under its clouds - defineSkyEffect takes groundShadow and a second function, skyShadow(world, sun), what is left of the light at a world position, and the sky pass multiplies every surface it finds by it, as Sky Atmosphere does with its own clouds

yhh1e771044f30e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 22b72ba5c..30fd0e539 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,7 @@ published separately; it ships inside the editor.
### Added
+- **A sky of your own shades the ground under its clouds.** Sky Atmosphere's clouds cast their shadow on the scene; a sky written with `defineSkyEffect` could not, so a stylised sky lit a world with no cloud shadow on it. With `groundShadow: true` the sky's source holds a second function, `skyShadow(world, sun)`, what is left of the light at a world position, and every surface the sky pass finds is multiplied by it.
- **A texture says whether it is a picture or numbers.** Import settings have a **Use**: Colour, or Data for an image read a number a texel (a heightmap, a mask of measured values, a lookup table). A build treats Data as what it is: never compressed, never decoded as sRGB or filled, kept at its depth, and at its own size unless Max Size (which now offers Source size) says otherwise, in which case it is resampled at 16 bits and not through 8. Nobody has to know to set it: a PNG of 16 bits a sample is imported as Data from the editor and the CLI alike, a texture put in a `Terrain`'s heightmap or splatmap is marked Data there and then (a splatmap cooked as a picture had its weights overwritten wherever the fourth was 0, and the ground fell back to grass everywhere), and Project Health lists textures a component reads as numbers that are still set up as pictures, with the fix on the row.
- **An ocean reaches the horizon.** The sea's surface is rings of mesh round the camera, and it ended where they did: from a hill, or with a small `cells` and `rings`, a straight edge with nothing past it. A flat apron now carries it on to 80 km, at the cost of one row of quads.
- **A sea is eight waves spread about the wind.** Water's waves were four peaked sines, the longest far taller than the rest, and from any height the sea read as regular curved bands. The set is now eight: the first heads with the wind, each after it is 0.78 of the length before, nearly as steep, and off the wind by a golden-ratio step within 65 degrees, each at the speed its length gives deep water. Their heights add up to what the four's did, so `waveHeight` raises a crest as high as before and nothing built on the old bound moves. `Waters.sample`, `Floating` and the drawn surface still share one formula: the shader's waves are now written out from the table in `water/waves.ts` (`WAVE_SET`) when its source is assembled, where the same numbers were kept by hand in five places. A quality level's `waterWaves` is 3 / 5 / 8 (was 2 / 3 / 4), longest first. A scene tuned against the old four will look different: the same height spread over more, smaller waves.
diff --git a/docs/astro/src/content/docs/graphics/post-processing.mdx b/docs/astro/src/content/docs/graphics/post-processing.mdx
index 4ab4f93e0..eca2214c1 100644
--- a/docs/astro/src/content/docs/graphics/post-processing.mdx
+++ b/docs/astro/src/content/docs/graphics/post-processing.mdx
@@ -551,6 +551,12 @@ panorama is drawn (leave a sun disc out then: the directional light is the sun's
radiance. Params are uniforms (`mc.` in WGSL), the time is `u_time.x`, and `textures` brings tables the sky needs
(`wrap: 'repeat'` for noise).
+A sky with clouds can shade the ground under them. Set `groundShadow: true` and write a second function beside the
+first, `float skyShadow(vec3 world, vec3 sun)` (WGSL `fn skyShadow(world : vec3f, sun : vec3f) -> f32`): what is left
+of the light at a world position, 1 for all of it. Every surface in the frame is multiplied by it, as Sky
+Atmosphere's **Cloud Shadows** does with its own clouds, so it dims the whole of a surface's light and not the
+sun's alone; keep its floor well above 0.
+
| Method | Description |
|---|---|
| `PostProcess.createStack()` | Create an empty effect stack. |
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 59a6ad078..378a4226e 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
@@ -427,6 +427,11 @@ defineSkyEffect({
方向光就是太阳的直射光)。返回线性辐亮度。参数是 uniform(WGSL 里写 `mc.`),时间是 `u_time.x`,
`textures` 用来带上天空需要的贴图(噪声用 `wrap: 'repeat'`)。
+有云的天空可以给云下的地面投影。设 `groundShadow: true`,并在第一个函数旁边再写一个
+`float skyShadow(vec3 world, vec3 sun)`(WGSL 为 `fn skyShadow(world : vec3f, sun : vec3f) -> f32`):某个世界位置上
+还剩多少光,1 为全部。画面里的每个表面都乘上它,和 Sky Atmosphere 的 **Cloud Shadows** 对自己的云做的一样,
+所以它压暗的是表面的全部光照,不只是太阳的;下限要明显高于 0。
+
| 方法 | 说明 |
|---|---|
| `PostProcess.createStack()` | 创建一个空效果栈。 |
diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.md
index 50c804868..4fd75ec39 100644
--- a/sdk/etc/index.api.md
+++ b/sdk/etc/index.api.md
@@ -10235,6 +10235,7 @@ upperDriftZ: number
```
colors: EffectColorDef[] | undefined
glsl: string
+groundShadow: boolean | undefined
label: string
textures: Record<string, () => BuiltinTexture> | undefined
type: string
diff --git a/sdk/etc/index.native.api.md b/sdk/etc/index.native.api.md
index f8239073f..a7d41300b 100644
--- a/sdk/etc/index.native.api.md
+++ b/sdk/etc/index.native.api.md
@@ -10457,6 +10457,7 @@ upperDriftZ: number
```
colors: EffectColorDef[] | undefined
glsl: string
+groundShadow: boolean | undefined
label: string
textures: Record<string, () => BuiltinTexture> | undefined
type: string
diff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.md
index c639aeb29..f080e4259 100644
--- a/sdk/etc/index.node.api.md
+++ b/sdk/etc/index.node.api.md
@@ -10230,6 +10230,7 @@ upperDriftZ: number
```
colors: EffectColorDef[] | undefined
glsl: string
+groundShadow: boolean | undefined
label: string
textures: Record<string, () => BuiltinTexture> | undefined
type: string
diff --git a/sdk/etc/minigame.api.md b/sdk/etc/minigame.api.md
index 9ab5657b5..5ed310d4a 100644
--- a/sdk/etc/minigame.api.md
+++ b/sdk/etc/minigame.api.md
@@ -10527,6 +10527,7 @@ upperDriftZ: number
```
colors: EffectColorDef[] | undefined
glsl: string
+groundShadow: boolean | undefined
label: string
textures: Record<string, () => BuiltinTexture> | undefined
type: string
diff --git a/sdk/etc/wechat.api.md b/sdk/etc/wechat.api.md
index 9e5cbef28..b66041a81 100644
--- a/sdk/etc/wechat.api.md
+++ b/sdk/etc/wechat.api.md
@@ -10237,6 +10237,7 @@ upperDriftZ: number
```
colors: EffectColorDef[] | undefined
glsl: string
+groundShadow: boolean | undefined
label: string
textures: Record<string, () => BuiltinTexture> | undefined
type: string
diff --git a/sdk/src/postprocess/customSky.ts b/sdk/src/postprocess/customSky.ts
index 06b4b8724..f645409a8 100644
--- a/sdk/src/postprocess/customSky.ts
+++ b/sdk/src/postprocess/customSky.ts
@@ -15,6 +15,10 @@
* 1 while the panorama is drawn — when a sun disc should be left out, since the directional light is the sun's
* direct light there. It returns linear radiance, before exposure. The effect's own params are uniforms like any
* effect's: in GLSL by name, in WGSL as `mc.<name>`; the time is `u_time.x` / `tc.u_time.x`.
+ *
+ * A sky with clouds can shade the ground under them, as Sky Atmosphere's do: with `groundShadow` the author writes a
+ * second function, `skyShadow(world, sun)`, how much of the scene's light is left at a world position (1 is all of
+ * it), and every surface the pass finds is multiplied by it.
*/
import type { ShaderHandle } from '../render/material';
@@ -36,6 +40,11 @@ export interface SkyEffectSource {
glsl: string;
/** The same in WGSL: `fn skyRadiance(dir : vec3f, sun : vec3f, sunColor : vec3f, env : f32) -> vec3f`. */
wgsl: string;
+ /**
+ * The sky shades the ground: the sources also hold `float skyShadow(vec3 world, vec3 sun)` and
+ * `fn skyShadow(world : vec3f, sun : vec3f) -> f32`, what is left of a surface's light at a world position, 0 to 1.
+ */
+ groundShadow?: boolean;
}
/** The sky types defined here: what the volumes look for, beside Sky Atmosphere, to light the scene. */
@@ -115,7 +124,7 @@ void main() {
vec4 src = texture(u_sceneTexture, v_texCoord);
ivec2 size = textureSize(u_sceneDepth, 0);
float depth = texelFetch(u_sceneDepth, clamp(ivec2(v_texCoord * vec2(size)), ivec2(0), size - ivec2(1)), 0).r;
- if (depth < 1.0 && src.a > 0.999) { fragColor = src; return; }
+ if (depth < 1.0 && src.a > 0.999) { fragColor = ${src.groundShadow ? 'vec4(src.rgb * clamp(skyShadow(worldFromDepth(v_texCoord, depth) + u_renderOrigin.xyz, sun), 0.0, 1.0), src.a)' : 'src'}; return; }
vec3 entry = worldFromDepth(v_texCoord, 0.0);
vec3 dir = normalize(worldFromDepth(v_texCoord, 1.0) - entry);
vec3 col = skyRadiance(dir, sun, sunColor, 0.0) * u_intensity;
@@ -147,7 +156,7 @@ fn esSkyPanoramaDir(uv : vec2f) -> vec3f {
let src = textureSampleLevel(t1, s1, v.v_texCoord, 0.0);
let size = vec2i(textureDimensions(t7, 0));
let depth = textureLoad(t7, clamp(vec2i(v.v_texCoord * vec2f(size)), vec2i(0), size - vec2i(1)), 0);
- if (depth < 1.0 && src.a > 0.999) { return src; }
+ if (depth < 1.0 && src.a > 0.999) { return ${src.groundShadow ? 'vec4f(src.rgb * clamp(skyShadow(worldFromDepth(v.v_texCoord, depth) + frame.renderOrigin.xyz, sun), 0.0, 1.0), src.a)' : 'src'}; }
let entry = worldFromDepth(v.v_texCoord, 0.0);
let dir = normalize(worldFromDepth(v.v_texCoord, 1.0) - entry);
let col = skyRadiance(dir, sun, sunColor, 0.0) * mc.u_intensity;
diff --git a/sdk/tests/custom-sky.test.ts b/sdk/tests/custom-sky.test.ts
index bdf822c06..07f318d11 100644
--- a/sdk/tests/custom-sky.test.ts
+++ b/sdk/tests/custom-sky.test.ts
@@ -27,6 +27,17 @@ describe('custom sky', () => {
expect(src.match(/skyRadiance\(/g)!.length).toBeGreaterThanOrEqual(6);
});
+ it('multiplies the surfaces it finds by the shadow the sky gives, at their world place, only when the sky has one', () => {
+ const sky = { type: 't', label: 'T', glsl: '', wgsl: '' };
+ expect(skyEffectSource(sky)).not.toContain('skyShadow');
+ const src = skyEffectSource({ ...sky, groundShadow: true });
+ expect(src).toContain('src.rgb * clamp(skyShadow(worldFromDepth(v_texCoord, depth) + u_renderOrigin.xyz, sun), 0.0, 1.0)');
+ expect(src).toContain('src.rgb * clamp(skyShadow(worldFromDepth(v.v_texCoord, depth) + frame.renderOrigin.xyz, sun), 0.0, 1.0)');
+ // The sun is found before the surface is left, in both languages.
+ expect(src.indexOf('findSun(lightDir')).toBeLessThan(src.indexOf('skyShadow('));
+ expect(src.indexOf('let found = findSun()')).toBeLessThan(src.lastIndexOf('skyShadow('));
+ });
+
it('registers an effect that can light the scene', () => {
defineSkyEffect({ type: 'unitSky', label: 'Unit Sky', glsl: '', wgsl: '' });
expect(isCustomSky('unitSky')).toBe(true);
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 6dd17dc17..7c74feb16 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -4566,4 +4566,6 @@ export const NOTED = {
{"note": "2D lighting works out only the lights that reach a pixel."},
"feat(tilemap): a tilemap layer can be lit - TilemapLayer.lit draws its tiles with the program a lit sprite uses, and which of the loop's lights a flat lit pixel works out is its screen tile's say, so strips stay on the GPU while lights move; four lit layers under sixteen local lamps cost 0.71 ms where working every light out cost 2.58":
{"note": "A tilemap layer can be lit."},
+ "feat(sky): a sky of your own shades the ground under its clouds - defineSkyEffect takes groundShadow and a second function, skyShadow(world, sun), what is left of the light at a world position, and the sky pass multiplies every surface it finds by it, as Sky Atmosphere does with its own clouds":
+ {"note": "A sky of your own shades the ground under its clouds."},
};