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.

docs(terrain): the terrain pages describe the CDLOD ground, its pages, infinite extent and erosion

yhh23bbc1dd8c90
diff --git a/docs/astro/src/content/docs/graphics/terrain.mdx b/docs/astro/src/content/docs/graphics/terrain.mdx
index 14ec14cab..b7d3cb6ab 100644
--- a/docs/astro/src/content/docs/graphics/terrain.mdx
+++ b/docs/astro/src/content/docs/graphics/terrain.mdx
@@ -1,45 +1,110 @@
---
title: Terrain
-description: A Terrain component that makes ground from a heightmap or noise, paints it by height and slope, and draws it in chunks that coarsen with distance.
+description: A Terrain component that makes ground from a heightmap or noise, over an area or without end, paints it by height and slope, and draws it as a quadtree of one patch that coarsens with distance.
---
Ground is the largest thing most 3D scenes draw and the thing a mesh is worst at: too many triangles near the camera
-and too few where it matters. The `Terrain` component makes it from a grid of heights and cuts it into chunks, each built at
-several levels of detail, so a hill underfoot is drawn finely and the same hill on the horizon is a few hundred triangles.
+and too few where it matters. The `Terrain` component draws it as a quadtree of squares round the eye, every square the same small
+patch, so a hill underfoot is drawn in fine cells and the same hill on the horizon in cells many times as large.
## The component
-Add a `Terrain` component to an entity. It is centred on that entity; its children are the chunks, made when the scene runs
-(and in the editor's scene view) and rebuilt when a field changes. Nothing is baked and nothing is saved besides the component.
+Add a `Terrain` component to an entity. It is placed at that entity; its children are the quadtree's nodes, made when the scene runs
+(and in the editor's scene view) and moved as the eye moves. Nothing is baked into the scene and nothing is saved besides the component.
+
+The ground is one of two kinds, chosen by `extent`:
+
+- **Area**: ground `sizeX` by `sizeZ`, centred on the entity, from a heightmap or from noise made once. It may be worn by rain and
+ painted by a splatmap.
+- **Infinite**: noise read at any point in the world, so the ground has no edge. The same seed is the same ground everywhere, in the
+ editor, in a game and on a server. It is drawn round the eye out to `drawDistance`.
| Field | What it does |
|---|---|
-| `heightmap` | An image: the red channel is the height. If the green channel is not just grey it is taken as the low byte of a 16-bit height, so a 16-bit map exported into an 8-bit-per-channel PNG keeps its precision. Empty makes the ground from noise. |
-| `sizeX`, `sizeZ`, `height` | World size of the terrain and the world height of its highest ground. |
-| `chunks` | Chunks along each side. Each is culled and drawn on its own, so more chunks cull better and cost more draws. |
-| `chunkCells` | Cells along a chunk's side at its finest level. |
-| `lodLevels`, `detail` | How many levels each chunk has (each half as fine as the one before) and a multiplier on how far the finer levels reach. |
-| `seed`, `frequency`, `octaves`, `persistence`, `ridged`, `resolution` | The noise ground: another seed is another ground; `ridged` goes from rolling hills to sharp ridges. |
-| `splatmap` | An image whose four channels are the weights of the four layers. Empty paints by height and slope. |
-| `rockSlope`, `dirtHeight`, `snowHeight` | How steep ground must be to turn to rock, and the heights where grass gives way to dirt and where snow begins. |
+| `extent` | `Area` or `Infinite`, as above. |
+| `heightmap` | Area only. An image: the red channel is the height. If the green channel is not just grey it is taken as the low byte of a 16-bit height, so a 16-bit map exported into an 8-bit-per-channel PNG keeps its precision. Empty makes the ground from noise. |
+| `sizeX`, `sizeZ`, `height` | An area's world size and the world height of its highest ground. For infinite ground `sizeX` is the span that `frequency` hills are spread over, and `sizeZ` is not used. |
+| `cellSize` | The finest cell of the ground's mesh, in world units: what the ground is drawn at near the eye. Further off the cells double, level by level. |
+| `detail` | A multiplier on how far the finest cells reach before the ground coarsens. Above 1 is finer at a distance and costs more triangles. |
+| `drawDistance` | Infinite only. How far from the eye the ground is drawn. |
+| `seed`, `frequency`, `octaves`, `persistence`, `ridged` | The noise: another seed is another ground; `frequency` is how many hills across; `ridged` goes from rolling hills to sharp ridges. |
+| `resolution` | Area only. Samples along each side of an area's noise ground. |
+| `warp` | 0 to 1: how far the noise is bent by noise of its own, which gives winding valleys and bent ridges instead of round hills. |
+| `erosion` | 0 to 4: how much rain wears the ground (below). 0 leaves it as made. |
+| `splatmap` | Area only. An image whose four channels are the weights of the four layers. Empty paints by height and slope. |
+| `rockSlope`, `dirtHeight`, `snowHeight` | How steep ground must be to turn to rock, and the heights, as a share of `height`, where grass gives way to dirt and where snow begins. |
| `layerColor0`–`3` | Grass, dirt, snow, rock: the colours the ground is drawn in when it has no material. |
| `material` | A material on the **Terrain** shader (below). Empty draws the layer colours straight onto the ground. |
+Changing `detail`, `drawDistance` or a layer colour takes effect on the next frame. Changing any other field builds the ground again.
+
+### Erosion
+
+On an area, `erosion` runs rain over the field drop by drop, one drop for every sample of the field at 1: each drop runs downhill,
+cuts the ground where it can carry more than it does and lays it down where it slows, which leaves gullies down the slopes, sharper
+ridges between them and silt in the valleys. The same seed wears the same ground. It works on a heightmap's field as well as a noise one. Where the water
+ran the grass is taken and dirt is painted in its place, so the channels show. Rain over a large field is seconds of work, so it is
+spread over frames and shown as it runs (see [Building over frames](#building-over-frames)).
+
+Infinite ground has no edge for rain to run off, so its erosion is the noise's own: each octave of the noise is damped where the
+octaves under it are already steep. That leaves smooth valleys between sharp ridges, much as rain would, at no cost beyond the noise.
+It paints no dirt channels.
+
## Layers and the Terrain material
-The four layers are grass, dirt, snow and rock. Each vertex is given its weight of every layer, from the splatmap or from how high
-and how steep the ground is there, and the **Terrain** shader (`builtin:terrain`) draws them: `u_layer0`–`3` are the textures,
-`u_tint0`–`3` their colours and `u_tile0`–`3` how many world units one repeat covers. The textures are tiled by *world position*, so a
-layer runs unbroken from one chunk to the next, and the rock is projected from three sides so a cliff is not a smear. `u_sharpness`
-hardens the seams and `u_macroVariation` lays a slow variation over the grass so a meadow is not one repeated tile.
+The four layers are grass, dirt, snow and rock. Every point of ground has a weight of each layer, from the splatmap or from how high
+and how steep the ground is there, and the **Terrain** shader (`builtin:terrain`) draws them. The textures are tiled by *world position*,
+so a layer runs unbroken across the ground however it is cut, and the rock is projected from three sides so a cliff is not a smear.
+
+| Parameter | What it does |
+|---|---|
+| `u_layer0`–`3` | The layers' textures: grass, dirt, snow, rock. |
+| `u_tint0`–`3` | Each layer's colour, which multiplies its texture. |
+| `u_tile0`–`3` | How many world units one repeat of each layer covers. |
+| `u_sharpness` | How much the layer with more weight takes over at a seam: 1 is a plain blend, higher a harder edge. |
+| `u_heightBlend` | How much the seams follow the layers' own relief (their brightness): rock shows through the gaps in the grass before the grass gives way, and sand fills the low places between stones. 0 is a plain blend of the weights. |
+| `u_antiTile` | How much of each layer is read a second time, turned and on a wider tile, and blended in by slow noise, so no repeat is seen across a field. It fades out with distance, where a repeat is too small to see. |
+| `u_macroVariation` | A wide, slow variation in brightness laid over every layer so a big meadow is not one colour. |
+| `u_relief` | How deep, in world units, the layers' relief is drawn into the light (their brightness taken as height). 0 leaves the ground smooth. |
+| `u_wetLevel`, `u_wetBand` | Ground at the water: below `u_wetLevel + u_wetBand` (world heights) it darkens and shines, most at `u_wetLevel`. A band of 0 draws none. |
+| `u_roughness` | The ground's roughness where it is dry. |
+
+The shader's other parameters (`u_heightPages`, `u_lodRange0` and the like) are written by the `Terrain` component every frame;
+a material leaves them alone.
-## How the levels of detail work
+## How the ground is drawn
-Each chunk is a grid of `chunkCells` cells a side, built again at half that, a quarter, and so on down to two cells. The engine's own
-[level of detail](/docs/performance/level-of-detail/) picks among them by how large the chunk is on screen. Two neighbours at different levels
-have different numbers of vertices along their shared edge, which would leave a crack; each chunk therefore has a **skirt**, a wall let
-down from its edges, so a chunk is built alone and may change level without rebuilding anyone else. Heights and normals are read from the one field
-at every level, so a chunk keeps its lighting as it coarsens and only its silhouette changes.
+The ground is cut by a quadtree. Every node of the tree, at every level, is drawn with the same patch of 32 by 32 cells, scaled to the
+node's square: a level-0 node is `32 * cellSize` across, and each level up is twice as wide, so its cells are twice as large. Each
+level has a range from the eye. Level 0's is `2.5 * 32 * cellSize * detail`, and each level's is twice the one below's. A node nearer
+the eye than the range of the level below it is split into its four children, so the ground is finest round the eye and coarsens level
+by level with distance. An area has enough levels for a top node to be as wide as the area; infinite ground has enough for the top level's range
+to reach `drawDistance`.
+
+The nodes are ordinary `MeshRenderer` children of the terrain entity, all of one mesh and one material, which the renderer draws
+as instances: the whole ground is a handful of draws. The patch itself is flat. The Terrain shader lifts each vertex to the ground's
+height, read from the node's page (below), and gives it the ground's normal from the same page.
+
+Toward the end of its level's range, the shader slides each vertex onto the grid of the level above (geomorphing). By the edge of the
+range every vertex of a fine node lies where the coarser node beside it has one, so two levels meet along the same line: there are no
+cracks and nothing to hide them with, and nothing pops as the eye moves.
+
+### Pages
+
+The heights a node is drawn with are kept in a **page**: the node's square sampled 64 times along each side (two samples to a patch
+cell), with one more past each edge so the slope at the edge is known. A page holds the height in 16 bits over the ground's range
+and the normal, in one atlas texture, and the weights of the four layers in another; each atlas is 2048 by 2048 and holds 900 pages.
+A coarse node's page covers more ground at the same 64 samples, so its detail falls off with its cells.
+
+Pages are made as the tree asks for them, a few each frame within a time budget, coarse and near ones first. Until a node's page is
+made, the node is drawn by its nearest ancestor that has one, so the ground is there at once and sharpens as the pages come in. When
+the atlas is full, the page drawn least recently gives up its place.
+
+### Building over frames
+
+Making pages and wearing an area by rain are spread over frames rather than done in one, so neither holds up a frame. While they run
+they are listed with their progress ("Building the terrain", "Eroding the terrain") in the editor's status bar, and a game can read
+the same list to show a loading screen.
## From code
@@ -55,11 +120,19 @@ const y = terrains.heightAt(x, z); // world height, or null where there is
const n = terrains.normalAt(x, z); // the unit normal there, or null
```
+These read the same source the pages are made from (the area's field, or the infinite noise), so they agree with what is drawn, and
+they answer at once for any point, whether or not its page has been made. Infinite ground has a height everywhere. Until an area's
+images are read and its erosion is done, `heightAt` returns null and `terrains.loading` is true.
+
## Limits
- **No collision.** The ground is drawn, not simulated: `heightAt` is the way to stand something on it, and a physics body falls through it
unless the project adds its own collider.
-- **Heights are one field.** Splat weights are per vertex, at the finest level's resolution, so they are as fine as the grid is.
+- **Paint is as fine as the pages.** Layer weights are sampled with the heights, two to a cell, so a splatmap finer than that near the
+ eye is averaged, and far off it is coarser still.
- **No holes, caves or overhangs.** A heightfield has one height at each place. Caves and bridges are meshes placed with it.
-- **No rotation in `heightAt`.** The terrain's position and uniform scale are taken into account, a rotation about y is not.
-- **Chunks are built a few a frame**, so a large terrain appears over a moment rather than stalling one frame.
+- **Position only.** The terrain follows its entity's position; its rotation and scale are not taken into account, by `heightAt` or by
+ the shader that lifts the ground. Keep the entity unrotated and unscaled, and set the ground's size with `sizeX`, `sizeZ` and `height`.
+- **An infinite ground is noise only.** It takes no heightmap or splatmap, and its erosion paints no dirt.
+- **Pages are made a few a frame**, so on the first frames, or after a jump of the eye, the ground is drawn coarse for a moment and
+ sharpens rather than stalling a frame.
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 6a99bdf6c..dc2eff8c5 100644
--- a/docs/astro/src/content/docs/zh-cn/graphics/terrain.mdx
+++ b/docs/astro/src/content/docs/zh-cn/graphics/terrain.mdx
@@ -1,34 +1,80 @@
---
title: 地形
-description: 一个 Terrain 组件,用高度图或噪声生成地面,按高度和坡度上色,并分块绘制、随距离降低细节。
+description: 一个 Terrain 组件,用高度图或噪声生成地面,可以是一片区域,也可以无边无际;按高度和坡度上色,并用同一块面片组成的四叉树绘制、随距离变粗。
---
-地面是大多数 3D 场景里画得最大的东西,也是网格最不擅长的东西:相机近处三角形太多,而真正需要的地方又太少。`Terrain` 组件从一张高度网格生成它,并切成块,每块按多个细节层级构建,所以脚下的山丘画得很细,而地平线上的同一座山丘只有几百个三角形。
+地面是大多数 3D 场景里画得最大的东西,也是网格最不擅长的东西:相机近处三角形太多,而真正需要的地方又太少。`Terrain` 组件把它画成一棵围绕视点的四叉树,每个方块都是同一块小面片,所以脚下的山丘用细格子画,地平线上的同一座山丘用大好多倍的格子画。
## 组件
-给某个实体加一个 `Terrain` 组件。地形以该实体为中心;它的子实体就是这些块,在场景运行时(以及编辑器的场景视图里)生成,字段一变就重建。没有任何东西被烘焙,除了组件本身也没有任何东西被保存。
+给某个实体加一个 `Terrain` 组件。地形放在该实体的位置;它的子实体就是四叉树的节点,在场景运行时(以及编辑器的场景视图里)生成,随视点移动而调整。没有任何东西被烘焙进场景,除了组件本身也没有任何东西被保存。
+
+地面有两种,由 `extent` 选择:
+
+- **Area(区域)**:以实体为中心、`sizeX` 乘 `sizeZ` 的一片地面,来自高度图或一次性生成的噪声。可以被雨水侵蚀,也可以用 splatmap 上色。
+- **Infinite(无限)**:在世界中任意一点读取的噪声,所以地面没有边缘。同一个种子在任何地方都是同一片地面,编辑器里、游戏里、服务器上都一样。它围绕视点绘制到 `drawDistance` 为止。
| 字段 | 作用 |
|---|---|
-| `heightmap` | 一张图:红色通道是高度。如果绿色通道不全是灰,它被当作 16 位高度的低字节,所以导出到每通道 8 位 PNG 里的 16 位高度图也能保留精度。为空则用噪声生成地面。 |
-| `sizeX`、`sizeZ`、`height` | 地形的世界尺寸,以及最高处地面的世界高度。 |
-| `chunks` | 每边的块数。每块单独剔除和绘制,块越多剔除越好、绘制调用越多。 |
-| `chunkCells` | 一块在最细层级时每边的格数。 |
-| `lodLevels`、`detail` | 每块有几个层级(每级比前一级粗一半),以及细层级能延伸多远的倍率。 |
-| `seed`、`frequency`、`octaves`、`persistence`、`ridged`、`resolution` | 噪声地面:换一个种子就是另一片地面;`ridged` 从起伏的丘陵到尖锐的山脊。 |
-| `splatmap` | 一张图,四个通道是四个图层的权重。为空则按高度和坡度上色。 |
-| `rockSlope`、`dirtHeight`、`snowHeight` | 地面要多陡才变成岩石,以及草地转为泥土、雪开始出现的高度。 |
+| `extent` | `Area` 或 `Infinite`,见上。 |
+| `heightmap` | 仅区域。一张图:红色通道是高度。如果绿色通道不全是灰,它被当作 16 位高度的低字节,所以导出到每通道 8 位 PNG 里的 16 位高度图也能保留精度。为空则用噪声生成地面。 |
+| `sizeX`、`sizeZ`、`height` | 区域的世界尺寸,以及最高处地面的世界高度。对无限地面,`sizeX` 是 `frequency` 座山丘铺开的跨度,`sizeZ` 不使用。 |
+| `cellSize` | 地面网格最细的格子,以世界单位计:视点近处地面按它绘制。再远一些,格子逐级加倍。 |
+| `detail` | 最细格子在地面变粗之前能延伸多远的倍率。大于 1 时远处更细,三角形也更多。 |
+| `drawDistance` | 仅无限地面。地面从视点向外绘制多远。 |
+| `seed`、`frequency`、`octaves`、`persistence`、`ridged` | 噪声:换一个种子就是另一片地面;`frequency` 是横跨有几座山丘;`ridged` 从起伏的丘陵到尖锐的山脊。 |
+| `resolution` | 仅区域。区域噪声地面每边的采样数。 |
+| `warp` | 0 到 1:噪声被它自己的另一层噪声扭曲多远,得到蜿蜒的山谷和弯折的山脊,而不是圆圆的山丘。 |
+| `erosion` | 0 到 4:雨水把地面侵蚀多少(见下)。0 保持生成时的样子。 |
+| `splatmap` | 仅区域。一张图,四个通道是四个图层的权重。为空则按高度和坡度上色。 |
+| `rockSlope`、`dirtHeight`、`snowHeight` | 地面要多陡才变成岩石,以及草地转为泥土、雪开始出现的高度(占 `height` 的比例)。 |
| `layerColor0`–`3` | 草、泥土、雪、岩石:没有材质时地面绘制用的颜色。 |
| `material` | 用 **Terrain** 着色器的材质(见下)。为空则直接把图层颜色画在地面上。 |
+改 `detail`、`drawDistance` 或图层颜色在下一帧生效。改其他任何字段都会重新构建地面。
+
+### 侵蚀
+
+在区域上,`erosion` 让雨水一滴一滴落在场上,为 1 时场上每个采样点落一滴:每滴向下坡流,在还能多带泥土的地方冲刷地面,在流速变慢的地方把泥土放下,于是坡上留下冲沟,冲沟之间的山脊更锋利,谷底积起淤泥。同一个种子侵蚀出同一片地面。它对高度图的场和噪声的场都有效。水流过的地方草被带走,换成泥土,所以水道能看得出来。在大场上下雨要花好几秒,所以它被分摊到多帧里进行,并在进行中显示进度(见[分帧构建](#分帧构建))。
+
+无限地面没有让雨水流走的边缘,所以它的侵蚀是噪声自己的:噪声的每个八度在它下面的八度已经很陡的地方被压低。这样会在尖锐的山脊之间留下平缓的山谷,和雨水的效果很像,除了噪声本身之外不花额外代价。它不画泥土水道。
+
## 图层与 Terrain 材质
-四个图层是草、泥土、雪和岩石。每个顶点会得到它在每个图层上的权重,来自 splatmap 或来自那里有多高多陡,**Terrain** 着色器(`builtin:terrain`)把它们画出来:`u_layer0`–`3` 是纹理,`u_tint0`–`3` 是它们的颜色,`u_tile0`–`3` 是一次重复覆盖多少世界单位。纹理按*世界位置*平铺,所以图层从一块到下一块不会断开,岩石从三个方向投影,所以悬崖不会被拉成一团。`u_sharpness` 让接缝更硬,`u_macroVariation` 在草地上叠一层缓慢的变化,让草地不是同一块图案的反复。
+四个图层是草、泥土、雪和岩石。地面每一点在每个图层上都有一个权重,来自 splatmap 或来自那里有多高多陡,**Terrain** 着色器(`builtin:terrain`)把它们画出来。纹理按*世界位置*平铺,所以不管地面怎么切分,图层都连续不断;岩石从三个方向投影,所以悬崖不会被拉成一团。
+
+| 参数 | 作用 |
+|---|---|
+| `u_layer0`–`3` | 各图层的纹理:草、泥土、雪、岩石。 |
+| `u_tint0`–`3` | 各图层的颜色,与它的纹理相乘。 |
+| `u_tile0`–`3` | 每个图层一次重复覆盖多少世界单位。 |
+| `u_sharpness` | 接缝处权重更大的图层接管多少:1 是普通混合,更高则边缘更硬。 |
+| `u_heightBlend` | 接缝跟随图层自身起伏(即它们的亮度)的程度:草地退去之前,岩石先从草的缝隙里露出来;沙子填满石头之间的低处。0 是按权重普通混合。 |
+| `u_antiTile` | 每个图层再读一次的比例,这次旋转过、平铺得更宽,并用缓慢的噪声混入,让整片地上看不出重复。它随距离淡出,远处的重复小到看不见。 |
+| `u_macroVariation` | 叠在每个图层上的大尺度、缓慢的亮度变化,让大片草地不是同一种颜色。 |
+| `u_relief` | 图层的起伏(亮度当作高度)在光照里画多深,以世界单位计。0 让地面保持平滑。 |
+| `u_wetLevel`、`u_wetBand` | 水边的地面:低于 `u_wetLevel + u_wetBand`(世界高度)时变暗变亮泽,在 `u_wetLevel` 处最强。带宽为 0 则不画。 |
+| `u_roughness` | 干燥处地面的粗糙度。 |
+
+着色器的其他参数(`u_heightPages`、`u_lodRange0` 之类)由 `Terrain` 组件每帧写入;材质不用管它们。
-## 细节层级如何工作
+## 地面如何绘制
-每块是每边 `chunkCells` 格的网格,再按一半、四分之一……一直到两格各构建一次。引擎自己的[细节层级](/docs/zh-cn/performance/level-of-detail/)按这块在屏幕上有多大来选择。两个层级不同的邻居在共用的边上顶点数不同,会留下裂缝;所以每块都有一圈**裙边**,从边缘垂下的一堵墙,使一块可以单独构建,换层级时也不必重建任何其他块。每个层级的高度和法线都读自同一个场,所以一块在变粗时光照保持不变,只有轮廓变化。
+地面由一棵四叉树切分。树上每个节点,在每一级,都用同一块 32 乘 32 格的面片绘制,缩放到节点的方块大小:第 0 级节点宽 `32 * cellSize`,每往上一级宽一倍,所以格子也大一倍。每一级有一个离视点的范围。第 0 级的范围是 `2.5 * 32 * cellSize * detail`,每一级的范围是下一级的两倍。离视点比下一级的范围还近的节点会被分成四个子节点,所以地面在视点周围最细,随距离逐级变粗。区域有足够的级数让顶层节点和区域一样宽;无限地面有足够的级数让顶层的范围达到 `drawDistance`。
+
+节点是地形实体下普通的 `MeshRenderer` 子实体,全部用同一个网格和同一个材质,渲染器把它们当作实例绘制:整片地面只是寥寥几次绘制。面片本身是平的。Terrain 着色器从节点的页(见下)读出地面高度,把每个顶点抬上去,并从同一页取出地面的法线。
+
+在一级范围的末段,着色器把每个顶点滑到上一级的网格上(几何形变,geomorphing)。到范围边缘时,细节点的每个顶点都落在旁边粗节点有顶点的位置,所以两级沿同一条线相接:没有裂缝,也就不需要任何东西去遮,视点移动时也不会有东西突然跳变。
+
+### 页
+
+节点绘制所用的高度存在一个**页**里:节点的方块每边采样 64 次(每个面片格子两个采样),边缘外再多一个,这样边缘处的坡度也已知。页把高度按地面的高度范围以 16 位存储,连同法线放在一张图集纹理里,四个图层的权重放在另一张里;每张图集 2048 乘 2048,能放 900 页。粗节点的页用同样的 64 个采样覆盖更大的地面,所以它的细节随格子一起变粗。
+
+页按四叉树的需要生成,每帧在时间预算内做几页,粗的、近的优先。节点的页做好之前,用它最近的、已有页的祖先节点代为绘制,所以地面立刻就在,随着页陆续做好而变清晰。图集满了时,最久没被绘制的页让出位置。
+
+### 分帧构建
+
+生成页和用雨水侵蚀区域都分摊到多帧里进行,而不是一次做完,所以都不会卡住某一帧。进行期间,它们和进度一起("Building the terrain"、"Eroding the terrain")列在编辑器的状态栏里,游戏也可以读取同一份列表来显示加载画面。
## 从代码里用
@@ -44,10 +90,13 @@ const y = terrains.heightAt(x, z); // 世界高度,没有地形的地方
const n = terrains.normalAt(x, z); // 那里的单位法线,或 null
```
+它们读的是生成页所用的同一个来源(区域的场,或无限的噪声),所以和画出来的一致,而且任何一点都能立刻回答,不管那里的页做没做好。无限地面处处都有高度。区域的图读完、侵蚀做完之前,`heightAt` 返回 null,`terrains.loading` 为 true。
+
## 限制
- **没有碰撞。** 地面只是被绘制,不被模拟:要把东西立在上面用 `heightAt`,物理刚体会穿过它,除非项目自己加碰撞体。
-- **高度只是一个场。** Splat 权重是逐顶点的,精度就是最细层级的网格精度。
+- **上色精度就是页的精度。** 图层权重和高度一起采样,每格两个,所以近处比这更细的 splatmap 会被平均,远处更粗。
- **没有洞、洞穴或悬挑。** 高度场在每个位置只有一个高度。洞穴和桥是和它放在一起的网格。
-- **`heightAt` 不考虑旋转。** 地形的位置和均匀缩放会被考虑,绕 y 的旋转不会。
-- **块每帧构建几个**,所以大地形会在一小会儿内出现,而不是卡住某一帧。
+- **只有位置。** 地形跟随实体的位置;它的旋转和缩放不被考虑,`heightAt` 不考虑,抬起地面的着色器也不考虑。让实体保持不旋转、不缩放,用 `sizeX`、`sizeZ` 和 `height` 设定地面大小。
+- **无限地面只有噪声。** 它不接受高度图或 splatmap,它的侵蚀也不画泥土。
+- **页每帧只做几个**,所以在最初几帧,或视点突然跳远之后,地面会先粗一会儿再变清晰,而不是卡住某一帧。