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(render): a terrain layer and a field of plants each say how much of the sky reaches them - a .esterrainlayer's occlusion scales the occlusion its mask holds, and the Foliage material's u_rootOcclusion dims a plant's root and nothing at its tip; both reach the lighting model's ambient occlusion, which dims the environment's light and not the sun's, as a material's Ambient Occlusion does in Unreal: grass shaded by its own blades and the ground under it take less of the sky than bare rock

yhha8f392453b29
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 64dcd7dec..7b6dddc80 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,7 @@ published separately; it ships inside the editor.
### Added
+- **A terrain layer and a field of plants each say how much of the sky reaches them.** A `.esterrainlayer`'s `occlusion` (0 to 1) scales the occlusion its mask holds, and the Foliage material's `u_rootOcclusion` is the sky's light at a plant's root, rising to all of it at the tip. Both dim only the environment's light, as a material's ambient occlusion does; the sun and other lights are as they were. Left out (1), nothing changes. The terrain layer editor has the field.
- **An ambient light's diffuse has its own scale and tint.** `diffuseScale` and `diffuseTint` on an Ambient light multiply the light its environment gives surfaces, and leave the reflections and the sky drawn behind them as they were: a sky whose irradiance is too dark or too blue for a look is lifted without brightening every reflection. At 1 and white (the defaults) nothing changes.
- **Things that break.** A model can end with a `fracture` step, `{ op: "fracture", pieces: 12, at, focus }`, which cuts it beforehand into pieces that fit together with no gap, each a closed solid whose cut faces are tagged apart from its skin, smaller round `at` where it will be struck. It is still drawn whole; the editor also writes a mesh for each piece and a prefab of them, each with a dynamic body and a convex hull, and the reply hands over a `Breakable` to put beside the MeshRenderer. Raising `Breakable.broken` hides the whole (its renderer, body and colliders are switched off, not removed), puts the pieces where it stood and throws them out from `origin` at `burst` world units a second; `lifetime` takes them away later, and lowering the flag makes it whole again. Its pieces are loaded while it stands whole, so they appear on the frame it breaks. In the editor a clay pot written by `model_write` and placed on a floor broke into the 12 pieces asked for, then the 5 asked for after an edit, each of which moved, spread apart and came to rest on the floor, and was whole again when the flag was lowered. Nothing breaks it of itself yet: a script, a graph or a collision handler raises the flag. `Physics3D.setLinearVelocity(entity, velocity)` is what throws the pieces and is there for anything else that sends a body off. `Prefabs.prepare(path)` loads a prefab and everything its entities use and holds it, so a later `instantiate` does not wait on files.
- **A thing breaks when it is hit hard enough.** `Breakable.breakImpulse` breaks it on a contact at least that hard, the speed at which the two came together times the mass they met with, and the pieces leave from where it was hit. It needs a collider of its own, which the editor's reply to a fractured model now hands over as `hull` (a `ConvexCollider3D` of its shape); with a dynamic body as well it can be knocked over and break where it lands. Every `Physics3DEvents.contactEnters` entry now carries `speed` (how fast the first body was closing on the second along the normal, read before the solver answered), `mass` (the kilograms they met with, 1 / (1/a + 1/b), a body that does not move counting as none) and `impulse`, their product, from both the Jolt and PhysX worlds. A sphere of 0.5 m dropped 1.5 m onto a pad reported 5.56 m/s from Jolt and 5.40 from PhysX against the 5.42 the fall gives, and met it with 523.60 kg, its own mass, from both. In the editor a 2 kg stone dropped six metres onto a pot broke it from its rim and took its collider away, and a 50 g pebble dropped from as high onto another left it whole. Native builds carry the new buffers but were only compiled on Windows, not run.
diff --git a/desktop b/desktop
index e3cdd20f0..afb7766ae 160000
--- a/desktop
+++ b/desktop
@@ -1 +1 @@
-Subproject commit e3cdd20f0001b39892ef75b8930c2aa02ea027b7
+Subproject commit afb7766ae47ffc0b706b201b310ae07e8746d5b5
diff --git a/docs/astro/src/content/docs/graphics/foliage.mdx b/docs/astro/src/content/docs/graphics/foliage.mdx
index 9026760fd..d3f5076ae 100644
--- a/docs/astro/src/content/docs/graphics/foliage.mdx
+++ b/docs/astro/src/content/docs/graphics/foliage.mdx
@@ -65,7 +65,10 @@ a flutter on top. Its parameters are in the material: `u_windStrength`, `u_windS
longer a gust) and `u_flutter`. Plants are two-sided and let light through them (`u_translucency`, and `u_translucencyColor` for what colour comes through: a leaf glows yellower
from behind than it looks from the front); `u_specular` is how much they mirror the sun and the sky, and `u_bladeFaceEye` how far a blade
of grass turns its flat to the eye, so a blade is not a line when seen edge on. A leaf texture with an alpha channel
-(`u_albedo`, `u_useTexture`, `u_alphaCutoff`) cuts the outline of a leaf out of a card.
+(`u_albedo`, `u_useTexture`, `u_alphaCutoff`) cuts the outline of a leaf out of a card. `u_rootOcclusion` is how much of the sky's
+light reaches a plant's root, rising to all of it at the tip: the blades round a root hide the sky from it, as a material's ambient
+occlusion does in Unreal. It dims only the environment's light (the ambient light and its sky); the sun and other lights reach the root
+as before. At 1 (the default) nothing is dimmed.
A shader of your own can read the same weights: a vertex hook (`#pragma vertexoffset`) sees the vertex colour as `es_vertexColor` and the mesh's second uv as `es_vertexUv1` (zero on a mesh with none): where a head's vertex sits from its plant's axis, when `faceEye` asks for it.
diff --git a/docs/astro/src/content/docs/graphics/terrain.mdx b/docs/astro/src/content/docs/graphics/terrain.mdx
index 65c79465f..579a532b4 100644
--- a/docs/astro/src/content/docs/graphics/terrain.mdx
+++ b/docs/astro/src/content/docs/graphics/terrain.mdx
@@ -83,6 +83,7 @@ paint all eight.
| `roughness` | The roughness where there is no mask. |
| `normalStrength` | How strongly the normal map bends the light: 1 as drawn, 0 flat. |
| `heightBlend` | Optional. How much this layer's own height decides where it meets another: 0 blends by the paint alone (grass into sand), 1 by the layers' heights (stones standing through grass). Where several layers meet, each counts by how much of it is there. Absent, it is the material's `u_heightBlend`. |
+| `occlusion` | Optional, 0 to 1. How much of the sky's light reaches this layer, times its mask's occlusion (red): a ground the plants on it shade from the sky takes less of it than the bare rock beside it, as a material's ambient occlusion does. The sun and other lights are not dimmed. Absent, it is 1: the mask alone. |
The ground is drawn by the **Terrain Layers** shader (`builtin:terrain-layers`). Every layer is read by world position without repeat
near the eye, with a wider tile mixed in far off; where layers meet, the one whose mask stands higher shows (stones through grass, sand
diff --git a/docs/astro/src/content/docs/zh-cn/graphics/foliage.mdx b/docs/astro/src/content/docs/zh-cn/graphics/foliage.mdx
index 09a2cf86c..c37976300 100644
--- a/docs/astro/src/content/docs/zh-cn/graphics/foliage.mdx
+++ b/docs/astro/src/content/docs/zh-cn/graphics/foliage.mdx
@@ -51,7 +51,7 @@ MCP 的 `audit_vegetation` 会给出配置上限和当前绘制统计。先用 `
## 风
-**Foliage** 着色器(`builtin:foliage`)按顶点颜色里画好的权重弯曲每个顶点(红色是它在植物上多高,所以根不动、尖端动;绿色是植物自己的相位,所以一片草地不会整体一起摆),再加一阵在世界上传播的阵风和叠在上面的颤动。参数在材质里:`u_windStrength`、`u_windSpeed`、`u_windDirection`(度)、`u_gustScale`(越小阵风越长)和 `u_flutter`。植物是双面的,并且让光透过(`u_translucency`,透过来的光是什么颜色用 `u_translucencyColor`:叶子从背后看比从正面看更黄);`u_specular` 是它们反射太阳和天空的多少,`u_bladeFaceEye` 是草叶把宽面转向视点多少,这样侧对着看时它不会只剩一条线;带 alpha 通道的叶子纹理(`u_albedo`、`u_useTexture`、`u_alphaCutoff`)能从一张卡片里抠出叶子的轮廓。
+**Foliage** 着色器(`builtin:foliage`)按顶点颜色里画好的权重弯曲每个顶点(红色是它在植物上多高,所以根不动、尖端动;绿色是植物自己的相位,所以一片草地不会整体一起摆),再加一阵在世界上传播的阵风和叠在上面的颤动。参数在材质里:`u_windStrength`、`u_windSpeed`、`u_windDirection`(度)、`u_gustScale`(越小阵风越长)和 `u_flutter`。植物是双面的,并且让光透过(`u_translucency`,透过来的光是什么颜色用 `u_translucencyColor`:叶子从背后看比从正面看更黄);`u_specular` 是它们反射太阳和天空的多少,`u_bladeFaceEye` 是草叶把宽面转向视点多少,这样侧对着看时它不会只剩一条线;带 alpha 通道的叶子纹理(`u_albedo`、`u_useTexture`、`u_alphaCutoff`)能从一张卡片里抠出叶子的轮廓。`u_rootOcclusion` 是植物根部能接收多少天空光,沿植物向上增加,到尖端为全部:根周围的草叶挡住了天空,和 Unreal 里材质的环境遮蔽一样。它只压暗环境光(环境光灯和它的天空),太阳和其他灯光照到根部时不变。为 1(默认值)时不压暗。
你自己的着色器也能读到同样的权重:顶点钩子(`#pragma vertexoffset`)里,顶点颜色是 `es_vertexColor`,网格的第二套 UV 是 `es_vertexUv1`(网格没有时为零):设了 `faceEye` 时,它是头部顶点相对植物轴线的位置。
diff --git a/docs/astro/src/content/docs/zh-cn/graphics/terrain.mdx b/docs/astro/src/content/docs/zh-cn/graphics/terrain.mdx
index 30a9a1a21..759335674 100644
--- a/docs/astro/src/content/docs/zh-cn/graphics/terrain.mdx
+++ b/docs/astro/src/content/docs/zh-cn/graphics/terrain.mdx
@@ -65,6 +65,7 @@ description: 一个 Terrain 组件,用高度图或噪声生成地面,可以是
| `roughness` | 没有遮罩贴图时的粗糙度。 |
| `normalStrength` | 法线贴图的强度:1 为原样,0 为平坦。 |
| `heightBlend` | 可选。这一层自己的高度在与别的层相交处起多大作用:0 只按涂色混合(草到沙),1 按各层高度混合(石头从草里露出)。多层相遇时,每层按它在此处所占的比例计入。缺省则用材质的 `u_heightBlend`。 |
+| `occlusion` | 可选,0 到 1。这一层能接收多少天空光,再乘上遮罩里的遮蔽(红色):长着植物的地面被植物挡住天空,比旁边裸露的岩石接收得少,和材质的环境遮蔽一样。太阳和其他灯光不受影响。缺省为 1:只按遮罩。 |
地面由 **Terrain Layers** 着色器(`builtin:terrain-layers`)绘制。每一层按世界位置铺设,近处去除重复,远处混入更大的平铺;层与层交界处遮罩高度更高的一层显露(草间露出石头、石缝里填着沙);坡面上同时从侧面投影,悬崖不会被拉伸。法线贴图和遮罩的遮蔽、粗糙度参与光照。材质控制效果:`u_sharpness`、`u_heightBlend`、`u_blendNoise`、`u_antiTile`、`u_farScale`、`u_macroVariation`、`u_triplanarFrom`(多陡开始读侧面)和 `u_wetLevel`/`u_wetBand`,含义同下文 Terrain 着色器。从侧面读的层按高度取样,所以崖壁贴图的岩层是水平的,而且沿整面墙笔直:`u_strataWarp` 是岩层上下起伏的幅度,由宽度为 `u_strataSpan` 的缓慢噪声决定(默认 0,保持笔直)。`u_antiTileTurn` 是去重复采样时每个格子最多旋转多少:有方向的层(带沙纹的沙)逐格旋转后会逐格受光、露出六边形,设为 0 则只平移不旋转。
diff --git a/fixtures/scenes/terrain-layer-occlusion.esscene b/fixtures/scenes/terrain-layer-occlusion.esscene
new file mode 100644
index 000000000..28976bb19
--- /dev/null
+++ b/fixtures/scenes/terrain-layer-occlusion.esscene
@@ -0,0 +1,156 @@
+{
+ "version": 4,
+ "name": "terrain-layer-occlusion",
+ "entities": [
+ {
+ "id": 0,
+ "name": "Camera",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": 40,
+ "z": 0
+ },
+ "rotation": {
+ "x": -0.7071068,
+ "y": 0,
+ "z": 0,
+ "w": 0.7071068
+ }
+ }
+ },
+ {
+ "type": "Camera",
+ "data": {
+ "projectionType": 1,
+ "orthoSize": 12,
+ "nearPlane": 0.1,
+ "farPlane": 200,
+ "isActive": true,
+ "clearFlags": 3
+ }
+ }
+ ]
+ },
+ {
+ "id": 1,
+ "name": "Sky",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {}
+ },
+ {
+ "type": "Light",
+ "data": {
+ "type": 2,
+ "color": {
+ "r": 1,
+ "g": 1,
+ "b": 1,
+ "a": 1
+ },
+ "intensity": 1,
+ "enabled": true
+ }
+ }
+ ]
+ },
+ {
+ "id": 2,
+ "name": "Ground",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {}
+ },
+ {
+ "type": "Terrain",
+ "data": {
+ "sizeX": 32,
+ "sizeZ": 32,
+ "height": 1,
+ "cellSize": 1,
+ "detail": 1,
+ "heightmap": "/scenes/terrain-paint/height.png",
+ "splatmap": "/scenes/terrain-paint/splat.png",
+ "layerColor0": {
+ "r": 1,
+ "g": 0,
+ "b": 0,
+ "a": 1
+ },
+ "layerColor1": {
+ "r": 0,
+ "g": 0,
+ "b": 1,
+ "a": 1
+ },
+ "layerColor2": {
+ "r": 0,
+ "g": 1,
+ "b": 0,
+ "a": 1
+ },
+ "layerColor3": {
+ "r": 1,
+ "g": 1,
+ "b": 1,
+ "a": 1
+ },
+ "layers": [
+ "/scenes/terrain-layer-occlusion/open.esterrainlayer",
+ "/scenes/terrain-layer-occlusion/shaded.esterrainlayer"
+ ]
+ }
+ }
+ ]
+ },
+ {
+ "id": 3,
+ "name": "Sun",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "rotation": {
+ "x": -0.7071068,
+ "y": 0,
+ "z": 0,
+ "w": 0.7071068
+ }
+ }
+ },
+ {
+ "type": "Light",
+ "data": {
+ "type": 1,
+ "color": {
+ "r": 1,
+ "g": 1,
+ "b": 1,
+ "a": 1
+ },
+ "intensity": 1,
+ "enabled": false
+ }
+ }
+ ]
+ }
+ ]
+}
\ No newline at end of file
diff --git a/fixtures/scenes/terrain-layer-occlusion/open.esterrainlayer b/fixtures/scenes/terrain-layer-occlusion/open.esterrainlayer
new file mode 100644
index 000000000..f730f4dad
--- /dev/null
+++ b/fixtures/scenes/terrain-layer-occlusion/open.esterrainlayer
@@ -0,0 +1,10 @@
+{
+ "tile": 4,
+ "tint": [
+ 1,
+ 1,
+ 1
+ ],
+ "roughness": 1,
+ "normalStrength": 0
+}
\ No newline at end of file
diff --git a/fixtures/scenes/terrain-layer-occlusion/shaded.esterrainlayer b/fixtures/scenes/terrain-layer-occlusion/shaded.esterrainlayer
new file mode 100644
index 000000000..ef870e8e0
--- /dev/null
+++ b/fixtures/scenes/terrain-layer-occlusion/shaded.esterrainlayer
@@ -0,0 +1,11 @@
+{
+ "tile": 4,
+ "tint": [
+ 1,
+ 1,
+ 1
+ ],
+ "roughness": 1,
+ "normalStrength": 0,
+ "occlusion": 0.5
+}
\ No newline at end of file
diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.md
index 5bade0d0c..db3339302 100644
--- a/sdk/etc/index.api.md
+++ b/sdk/etc/index.api.md
@@ -11652,6 +11652,7 @@ heightBlend: number | undefined
mask: string | undefined
normal: string | undefined
normalStrength: number
+occlusion: number | undefined
roughness: number
tile: number
tint: [number, number, number]
diff --git a/sdk/etc/index.native.api.md b/sdk/etc/index.native.api.md
index 1161bcd32..7499a69e4 100644
--- a/sdk/etc/index.native.api.md
+++ b/sdk/etc/index.native.api.md
@@ -11881,6 +11881,7 @@ heightBlend: number | undefined
mask: string | undefined
normal: string | undefined
normalStrength: number
+occlusion: number | undefined
roughness: number
tile: number
tint: [number, number, number]
diff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.md
index f701fa3e9..754fa7bb7 100644
--- a/sdk/etc/index.node.api.md
+++ b/sdk/etc/index.node.api.md
@@ -11647,6 +11647,7 @@ heightBlend: number | undefined
mask: string | undefined
normal: string | undefined
normalStrength: number
+occlusion: number | undefined
roughness: number
tile: number
tint: [number, number, number]
diff --git a/sdk/etc/minigame.api.md b/sdk/etc/minigame.api.md
index 48418580a..7cfb8bbb6 100644
--- a/sdk/etc/minigame.api.md
+++ b/sdk/etc/minigame.api.md
@@ -11944,6 +11944,7 @@ heightBlend: number | undefined
mask: string | undefined
normal: string | undefined
normalStrength: number
+occlusion: number | undefined
roughness: number
tile: number
tint: [number, number, number]
diff --git a/sdk/etc/terrain.api.md b/sdk/etc/terrain.api.md
index c920fe56c..3d8978903 100644
--- a/sdk/etc/terrain.api.md
+++ b/sdk/etc/terrain.api.md
@@ -531,6 +531,7 @@ heightBlend: number | undefined
mask: string | undefined
normal: string | undefined
normalStrength: number
+occlusion: number | undefined
roughness: number
tile: number
tint: [number, number, number]
diff --git a/sdk/etc/wechat.api.md b/sdk/etc/wechat.api.md
index 3831dfc66..33db9a710 100644
--- a/sdk/etc/wechat.api.md
+++ b/sdk/etc/wechat.api.md
@@ -11654,6 +11654,7 @@ heightBlend: number | undefined
mask: string | undefined
normal: string | undefined
normalStrength: number
+occlusion: number | undefined
roughness: number
tile: number
tint: [number, number, number]
diff --git a/sdk/src/foliage/foliageShader.ts b/sdk/src/foliage/foliageShader.ts
index a894200cc..e472973c1 100644
--- a/sdk/src/foliage/foliageShader.ts
+++ b/sdk/src/foliage/foliageShader.ts
@@ -42,6 +42,9 @@ const FOLIAGE = `#pragma shader "Foliage"
#pragma param u_translucencyColor color default(1,1,1,1)
// How much it mirrors, 0 to 1: the sun's glint and the sky. A field of blades each mirroring a blue sky is greyer than its green.
#pragma param u_specular float default(0.5) range(0,1) ui(slider)
+// How much of the sky's light reaches a plant's root, rising to all of it at the tip: the blades round it hide the sky from below, as a
+// material's ambient occlusion does. Only the environment's light: the sun and other lights are not dimmed. 1 dims nothing.
+#pragma param u_rootOcclusion float default(1) range(0,1) ui(slider)
#pragma param u_thickness float default(0.3) range(0,1) ui(slider)
// The wind: how hard it blows, how fast it moves, where it comes from (degrees), how long a gust is (the lower, the longer), and how much flutter rides on it.
#pragma param u_windStrength float default(2) range(0,20) ui(slider)
@@ -324,7 +327,7 @@ void main() {
ext.trans = vec4(u_translucency, u_thickness, 0.0, 0.0);
}
vec3 viewDir = viewDirection(P);
- vec3 lit = applyLightingSurface(albedo, N, P, viewDir, 0.0, u_roughness, u_specular, 1.0, ext);
+ vec3 lit = applyLightingSurface(albedo, N, P, viewDir, 0.0, u_roughness, u_specular, mix(u_rootOcclusion, 1.0, height), ext);
fragColor = vec4(lit, alpha);
#ifdef ES_GBUFFER
fragSurface = vec4(packNormal(N), u_roughness, 0.75);
@@ -401,7 +404,7 @@ fn packNormal(n : vec3f) -> vec2f {
ext.trans = vec4f(mc.u_translucency, mc.u_thickness, 0.0, 0.0);
}
let viewDir = viewDirection(P);
- let lit = applyLightingSurface(albedo, N, P, viewDir, 0.0, mc.u_roughness, mc.u_specular, 1.0, ext);
+ let lit = applyLightingSurface(albedo, N, P, viewDir, 0.0, mc.u_roughness, mc.u_specular, mix(mc.u_rootOcclusion, 1.0, height), ext);
#ifdef ES_GBUFFER
var frag : FragOut;
frag.color = vec4f(lit, alpha);
diff --git a/sdk/src/terrain/layers.ts b/sdk/src/terrain/layers.ts
index 6b5fee282..304d4d65f 100644
--- a/sdk/src/terrain/layers.ts
+++ b/sdk/src/terrain/layers.ts
@@ -36,6 +36,12 @@ export interface TerrainLayerDef {
* 1 by the layers' heights (stones standing through grass). Absent, it is the material's `u_heightBlend`.
*/
heightBlend?: number;
+ /**
+ * How much of the sky's light reaches the layer, times its mask's occlusion (red), 0 to 1: a ground that the plants on it shade from
+ * the sky takes less of it than the bare rock beside it, as a material's ambient occlusion does. The sun and other lights are not
+ * dimmed. Absent, it is 1: the mask alone.
+ */
+ occlusion?: number;
}
/** A layer file that names no tile repeats every 4 m: 400 units at the engine's reference, in the project's own (ecs/worldUnits.ts). */
@@ -68,6 +74,7 @@ export function parseTerrainLayer(json: unknown, file = 'terrain layer'): Terrai
roughness: num('roughness', DEFAULTS.roughness, 0, 1),
normalStrength: num('normalStrength', DEFAULTS.normalStrength, 0, 4),
...(o.heightBlend === undefined ? {} : { heightBlend: num('heightBlend', 0, 0, 1) }),
+ ...(o.occlusion === undefined ? {} : { occlusion: num('occlusion', 1, 0, 1) }),
};
}
@@ -145,14 +152,17 @@ export function packLayers(layers: readonly LayerSource[], maxSize = 1024): Pack
bend[k * 4 + 3] = 255;
}
normal.push(bend);
+ const open = def.occlusion ?? 1;
if (m) {
- mask.push(new Uint8Array(resample(m, size)));
+ const read = new Uint8Array(resample(m, size));
+ if (open !== 1) for (let k = 0; k < n; k++) read[k * 4] = Math.round(read[k * 4]! * open);
+ mask.push(read);
} else {
// Without a mask the layer stands as high as its colour is bright, as a ground of stones between sand does.
const made = new Uint8Array(n * 4);
const rough = Math.round(def.roughness * 255);
for (let k = 0; k < n; k++) {
- made[k * 4] = 255;
+ made[k * 4] = Math.round(255 * open);
made[k * 4 + 1] = rough;
made[k * 4 + 2] = a ? Math.round(colour[k * 4]! * 0.299 + colour[k * 4 + 1]! * 0.587 + colour[k * 4 + 2]! * 0.114) : 128;
made[k * 4 + 3] = 255;
diff --git a/sdk/tests/foliage-far-versions.test.ts b/sdk/tests/foliage-far-versions.test.ts
index 7d3a0b603..28fcba555 100644
--- a/sdk/tests/foliage-far-versions.test.ts
+++ b/sdk/tests/foliage-far-versions.test.ts
@@ -231,4 +231,14 @@ describe('what a blade of grass and a leaf are given to be shaped and lit by', (
expect(source).toContain('mix(flat2, dot(across, flat2) < 0.0 ? -across : across, u_bladeFaceEye)');
expect(source).toContain('mix(flat2, across, vec2f(mc.u_bladeFaceEye))');
});
+
+ it('hides the sky from a root by its root occlusion, up to none at the tip, and dims nothing by default', async () => {
+ const { FOLIAGE_SHADER_TEMPLATE } = await import('../src/foliage/foliageShader');
+ const source = FOLIAGE_SHADER_TEMPLATE.source;
+ expect(source).toContain('#pragma param u_rootOcclusion float default(1) range(0,1)');
+ // As the occlusion the lighting model takes, which dims the environment's light and not the lights' (terrain-layer-occlusion and
+ // terrain-layer-occlusion-sun in tools/renderScenes.mjs draw that), in both languages.
+ expect(source).toContain('u_specular, mix(u_rootOcclusion, 1.0, height), ext)');
+ expect(source).toContain('mc.u_specular, mix(mc.u_rootOcclusion, 1.0, height), ext)');
+ });
});
diff --git a/sdk/tests/terrain-layers.test.ts b/sdk/tests/terrain-layers.test.ts
index 35e4e72cc..3ac34d157 100644
--- a/sdk/tests/terrain-layers.test.ts
+++ b/sdk/tests/terrain-layers.test.ts
@@ -34,6 +34,12 @@ describe('a terrain layer file', () => {
expect(() => parseTerrainLayer({ heightBlend: 2 }, 'g.esterrainlayer')).toThrow(/g\.esterrainlayer\.heightBlend/);
});
+ it('keeps a sky occlusion of its own only when it names one', () => {
+ expect(parseTerrainLayer({}).occlusion).toBeUndefined();
+ expect(parseTerrainLayer({ occlusion: 0.6 }).occlusion).toBe(0.6);
+ expect(() => parseTerrainLayer({ occlusion: 1.5 }, 'g.esterrainlayer')).toThrow(/g\.esterrainlayer\.occlusion/);
+ });
+
it('says which field it cannot take', () => {
expect(() => parseTerrainLayer({ tile: -1 }, 'g.esterrainlayer')).toThrow(/g\.esterrainlayer\.tile/);
expect(() => parseTerrainLayer({ tint: [1, 1] })).toThrow(/tint/);
@@ -76,6 +82,20 @@ describe('packing layers', () => {
expect(packed.mask[1]![1]).toBe(Math.round(0.4 * 255));
expect(packed.mask[1]![2]).toBe(128);
});
+
+ it('scales the occlusion its mask holds by its sky occlusion, and leaves roughness and height alone', () => {
+ const shaded = parseTerrainLayer({ occlusion: 0.5 });
+ const packed = packLayers([
+ { def: shaded, albedo: null, normal: null, mask: solid(16, [200, 90, 60, 255]) },
+ { def: shaded, albedo: null, normal: null, mask: null },
+ { def: grass, albedo: null, normal: null, mask: solid(16, [200, 90, 60, 255]) },
+ ]);
+ expect(Array.from(packed.mask[0]!.slice(0, 3))).toEqual([100, 90, 60]);
+ // No mask: no occlusion of its own, so the sky occlusion alone.
+ expect(packed.mask[1]![0]).toBe(128);
+ // Without one, the mask as it is.
+ expect(Array.from(packed.mask[2]!.slice(0, 3))).toEqual([200, 90, 60]);
+ });
});
describe('eight layers of paint', () => {
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 480d7862c..57b1067ec 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -4720,6 +4720,8 @@ export const NOTED = {
{"note": "A cloud's shadow dims the sun, not the sky."},
"feat(render): an ambient light's diffuse has its own scale and tint - Diffuse Scale and Diffuse Tint multiply the light an environment gives surfaces and leave what they reflect of it and the sky drawn behind them alone, as Unreal's Sky Light keeps Intensity apart from its Indirect Lighting and Specular scales: a sky whose irradiance is too dark or too blue for a look is lifted without brightening every reflection":
{"note": "An ambient light's diffuse has its own scale and tint."},
+ "feat(render): a terrain layer and a field of plants each say how much of the sky reaches them - a .esterrainlayer's occlusion scales the occlusion its mask holds, and the Foliage material's u_rootOcclusion dims a plant's root and nothing at its tip; both reach the lighting model's ambient occlusion, which dims the environment's light and not the sun's, as a material's Ambient Occlusion does in Unreal: grass shaded by its own blades and the ground under it take less of the sky than bare rock":
+ {"note": "A terrain layer and a field of plants each say how much of the sky reaches them."},
"chore(gates): what the checks asked of this week's work - a disposition for esengine/image in a native script and its namespace there, Waterfall's width and its place in the corpus, the recipe worker's bundle as not a contract, shadeLight2D's inout against its return, and a test that named App as a type":
{"internal": "tables the gates read, brought level; a natively packaged script can now import esengine/image, which no native game has run yet"},
"feat(image): a picture's source goes where a texture goes, and its maps are recipes too - a texture field takes an .esimage, the image tools write a picture's normal and surface maps as recipes beside it and answer their refs, text is set by the drawer a game sets it with, and the viewport draws a recipe as a game does":
diff --git a/tools/renderScenes.mjs b/tools/renderScenes.mjs
index b312793a0..6cbbaea54 100644
--- a/tools/renderScenes.mjs
+++ b/tools/renderScenes.mjs
@@ -93,6 +93,21 @@ export const SCENES = [
]),
},
},
+ // A terrain layer's sky occlusion dims the sky's light on it and nothing else: two white layers, the right one at 0.5, are half as
+ // bright under the ambient light alone and the same under a sun alone.
+ ...[
+ { id: 'terrain-layer-occlusion', set: [], expect: [[0.3, [242, 242, 242]], [0.7, [121, 121, 121]]] },
+ { id: 'terrain-layer-occlusion-sun', set: [{ entity: 1, component: 'Light', key: 'enabled', value: false }, { entity: 3, component: 'Light', key: 'enabled', value: true }], expect: [[0.3, [242, 242, 242]], [0.7, [242, 242, 242]]] },
+ ].map((test) => ({
+ id: test.id, tier: 'pr', webgpu: true,
+ env: {
+ ESTELLA_VERIFY_SCENE: '/scenes/terrain-layer-occlusion.esscene',
+ // Layers are packed and their shader compiled before the ground draws at all: a cold WebGL2 start took more than 2.5 s.
+ ESTELLA_VERIFY_W: '256', ESTELLA_VERIFY_H: '256', ESTELLA_VERIFY_STEPS: '30', ESTELLA_VERIFY_SETTLE_MS: '8000',
+ ...(test.set.length ? { ESTELLA_VERIFY_SET_FIELD: JSON.stringify(test.set) } : {}),
+ ESTELLA_VERIFY_EXPECT: JSON.stringify(test.expect.map(([x, rgb]) => ({ x, y: 0.5, rgb, tol: 14 }))),
+ },
+ })),
{
id: 'shadow-vertex-displacement', tier: 'pr', webgpu: true,
env: {