Terrain
Updated
A Terrain is a procedural landscape drawn in one draw call: hills, dunes, canyons or mountains generated from a preset and a seed. You can paint it with colours and Terrain Textures, and sculpt it into hills and holes, in the editor or from a script.
Scripts read the ground height to place objects, paint paths where players walk, and dig and raise the ground live. To cover the ground with wind-blown blades, see Grass; for lakes and shores, see Water.
Before you start
Section titled “Before you start”- Local and world space. Terrain methods named
paint,sculptandheightAttake the terrain’s local coordinates; the…Atmethods take a world point. Every entity is a scene object, soworldToLocalandlocalToWorldconvert between the two. See Working with entities. - Raycasts. To find where the pointer touches the ground, use
GraphicsRaycaster. See Input and interaction. - Skies and fog that go with a landscape are in Lighting.
| Component | Type | What it does |
|---|---|---|
| Terrain | comp-terrain | Procedural terrain from one displaced grid, paintable with textures. |
| Terrain Texture | comp-terrain-texture | One texture the terrain can be painted with. |
Quality tiers
Section titled “Quality tiers”Terrain has a quality prop: 'auto' (the default), 'low' or 'high'. auto picks low on phones and headsets and high elsewhere, once per session. Leave it on auto unless you have a reason to force one.
Terrain
Section titled “Terrain”TerrainComponent is required on a Terrain entity (ent-terrain). It is one displaced grid generated from noise. Its lowest point rests on local y = 0, so the entity’s transform places it in the world. It is lit like a Standard material, with shadows and fog.
Terrain props
Section titled “Terrain props”| Prop | Type | Default | Description |
|---|---|---|---|
preset |
'plains' | 'hills' | 'mountains' | 'dunes' | 'canyon' | 'alien' |
'hills' |
Landform and colour palette. |
size |
number |
128 |
Side length of the square terrain, in local units, 0.001 to 10000. |
seed |
number |
1 |
Noise seed: another seed gives another landscape of the same kind. |
heightScale |
number |
1 |
Multiplier on the preset’s peak height, -10 to 10. 0 is flat; negative values invert the landforms. |
quality |
'auto' | 'low' | 'high' |
'auto' |
Grid detail: low has 96 and high 180 subdivisions per side. |
paint |
object | none | The colours and textures painted on it, as commit() saves them. |
sculpt |
object | none | The sculpted relief, as commit() saves it. |
Changing preset, size, seed, heightScale or quality rebuilds the grid; its collider, shadows, outline and grass follow. paint and sculpt reload without a rebuild.
The Terrain entity supports Terrain Texture, Grass, Shadow, Contact Shadow, Mesh Collider (the collider that fits a terrain), NavMesh Walkable, Selectable, Outline, Silhouette, Signal Emitter, Enable on Signal and Networkable.
Terrain members
Section titled “Terrain members”| Member | Description |
|---|---|
height |
Surface height at local (x, z), sculpting included, without a raycast. Local units in and out. |
paint(x, z, radius, what, value = 1, hardness = 1, lay?) |
Paint (value 1) or erase (value 0) what over a disc at local (x, z). what is a Terrain Texture component or a colour string. hardness 0 is feathered, 1 crisp. |
paint |
The same at a world point (THREE.Vector3) with a world radius. |
fill(what, value, lay?) |
Cover the whole terrain with what (value 1), or remove it everywhere (0). |
sculpt(x, z, radius, brush, seconds) |
Work a disc at local (x, z) for seconds. Call it every frame with dt to keep working. |
sculpt |
The same at a world point with a world radius. |
clearRelief() |
Remove all sculpting. |
commit() |
Save what changed since the last commit as the paint and sculpt props. A reshaped terrain then updates its collider, raycasts, grass and navigation mesh, once. |
textures |
The Terrain Textures it draws, in order. Read-only. |
Painting and sculpting show at once; only commit() saves them. In play, the entity’s owner also commits by itself 15 frames after its strokes stop, so a networked terrain’s paint reaches every player and late joiners (NetworkTraitType.Terrain).
Painting
Section titled “Painting”- Colours: any colour string, mixed where strokes overlap.
- Textures: the Terrain Texture components on the same entity.
- The latest paint covers: painting a texture or colour makes the others give way under it; erasing shows what lies beneath, down to the terrain’s own colour bands.
A texture stroke can take a lay, an object that turns, scales, mirrors or stamps the image. Without one, one copy of the image covers every 4 local units, upright seen from above with north (-Z) up.
| Lay field | Type | Default | Description |
|---|---|---|---|
rotation |
number |
0 |
Degrees, counter-clockwise seen from above. |
scale |
number |
1 |
Times the 4-unit tile size, or the stamp’s size. |
flipX |
boolean |
false |
Mirror left to right. |
flipY |
boolean |
false |
Mirror top to bottom. |
center |
{ x, z } |
none | Stamp one copy centred at this local point instead of tiling. |
Sculpting
Section titled “Sculpting”Sculpting raises and digs the grid itself, and heightAt includes it. A brush is an object:
| Brush field | Type | Default | Description |
|---|---|---|---|
tool |
'raise' | 'lower' | 'smooth' | 'flatten' | 'noise' |
required | What the brush does. |
strength |
number |
required | 0 to 1: how fast it works. raise and lower move the ground strength × 0. units a second per unit of radius. |
hardness |
number |
0 |
0 to 1: share of the radius at full strength before the falloff. |
falloff |
'smooth' | 'linear' | 'spherical' | 'tip' |
'smooth' |
Profile from the hard core to the edge. |
height |
number |
ground under the brush | For flatten: the local height it pulls toward. |
scale |
number |
0.25 |
For noise: bump size, as a share of the radius. |
smooth evens out bumps smaller than the brush; flatten and smooth ease toward their goal and never overshoot; noise adds seeded bumps, the same on every client. The relief reaches at most 64 local units up or down.
Terrain Texture
Section titled “Terrain Texture”TerrainTextureComponent goes on a Terrain entity, several per entity. Each one is an image the terrain can be painted with; it says what is painted, never where.
| Prop | Type | Default | Description |
|---|---|---|---|
src |
string |
'' |
The repeating image: PNG, JPEG, WebP or .ktx2. Its alpha shows the terrain beneath. |
Read them with entity.getComponents(TerrainTextureComponent) or terrain.textures. Removing or disabling one stops drawing everything it painted.
A ball rolls in circles over a meadow terrain and paints a trail of the terrain's own Terrain Textures under it, switching between the two images each lap: the textures of the runtime Sky & Terrain demo.
Watch the trail of images appear behind the ball, a different image each lap. Drag to orbit the camera, scroll to zoom.
Examples
Section titled “Examples”1. Place objects on the ground
Section titled “1. Place objects on the ground”The simplest use of a terrain from a script: drop a list of objects onto its surface without raycasting. The conversion to and from local space makes it work wherever the terrain is placed.
#pragma lifecycle(startup)
const PROPS = ['Tree 1', 'Tree 2', 'Rock', 'Signpost'];const point = new THREE.Vector3();
function startup() { const ground = findEntity('Terrain'); const terrain = ground.getComponent(TerrainComponent);
for (const name of PROPS) { const prop = findEntity(name); if (!prop) continue;
prop.getWorldPosition(point); ground.worldToLocal(point); point.y = terrain.heightAt(point.x, point.z); ground.localToWorld(point);
prop.setPosition(point.x, point.y, point.z); }}Two trees, a rock and a signpost saved floating above a hilly terrain drop onto its surface at startup: a Terrain component read with heightAt by a Behavior script, with no raycast.
Orbit around: every object rests on the slope under it. Drag to orbit the camera, scroll to zoom.
getWorldPosition(point)reads where the prop is in the world;ground.worldToLocal(point)turns that into the terrain’s local coordinates.terrain.heightAt(x, z)returns the local surface height at that spot, sculpting included, without a raycast.ground.localToWorld(point)turns the result back into world space.setPositionmoves the prop and keeps any physics body in step. This example assumes the props sit at the top of the scene, where their local position is a world position.
2. Paint a path where the player walks
Section titled “2. Paint a path where the player walks”A muddy trail that appears behind the player: every quarter of a second the script paints a soft disc of a mud texture under the camera. In play the terrain saves the strokes on its own once they stop.
#pragma lifecycle(startup, update)
const STEP = 0.25; // seconds between strokeslet terrain;let mud;let timer = 0;const feet = new THREE.Vector3();
function startup() { const ground = findEntity('Terrain'); terrain = ground.getComponent(TerrainComponent); mud = ground.getComponents(TerrainTextureComponent)[0];}
function update(dt) { if (!camera || !mud) return; timer += dt; if (timer < STEP) return; timer = 0;
camera.getWorldPosition(feet); terrain.paintAt(feet, 0.8, mud, 1, 0.3, { scale: 0.5 });}Every quarter of a second, a script paints a soft disc of the terrain's first texture under the camera with paintAt, leaving a trail wherever the camera goes.
Drag to orbit the camera, or move it with WASD: a trail is painted on the ground beneath it. Scroll to zoom.
getComponents(TerrainTextureComponent)[0]takes the first Terrain Texture on the terrain: here, the mud image.- A timer limits painting to four strokes a second, which is enough for a walking pace and cheap: a stroke uploads only the area it touched.
paintAt(feet, 0.8, mud, 1, 0.3, …)paints at a world point with a 0.8 m radius, fully (1), with a soft edge (0.3). The lay{ scale: 0.5 }makes the mud tile twice as small as the default.camera.getWorldPosition(feet)reads the player’s position into a vector made once, so nothing is allocated per stroke.- No
commit()call is needed in play: the owner commits 15 frames after strokes stop.
3. Dig and raise the ground under the pointer
Section titled “3. Dig and raise the ground under the pointer”A sculpting tool for a building game: hold R to raise the ground under the mouse, F to lower it and G to smooth it. When the key is released, the change is committed so colliders and grass follow.
#pragma lifecycle(startup, update)
const RADIUS = 3;let ground;let terrain;let working = false;
const BRUSHES = { [Key.R]: { tool: 'raise', strength: 0.5, falloff: 'smooth' }, [Key.F]: { tool: 'lower', strength: 0.5, falloff: 'smooth' }, [Key.G]: { tool: 'smooth', strength: 0.8 },};
function startup() { ground = findEntity('Terrain'); terrain = ground.getComponent(TerrainComponent);}
function activeBrush() { for (const key of [Key.R, Key.F, Key.G]) { if (Input.keyboard.isKeyPressed(key)) return BRUSHES[key]; } return null;}
function update(dt) { const brush = activeBrush();
if (!brush) { if (working) { terrain.commit(); working = false; } return; }
const hit = GraphicsRaycaster.fromScreen(Input.mouse.position()).hit(ground); if (!hit) return;
terrain.sculptAt(hit.point, RADIUS, brush, dt); working = true;}Holding R, F or G sculpts the terrain under the mouse every frame with sculptAt, raising, lowering or smoothing it; releasing the key commits the change.
Point at the ground and hold R to raise it, F to lower it or G to smooth it. Drag to orbit the camera, scroll to zoom.
- Brushes are plain objects built once, keyed by the key codes
Key.R,Key.FandKey.G. Input.keyboard.isKeyPressed(key)is true while the key is held, so the brush keeps working every frame.- The ray from the pointer is tested only against the terrain entity, which is faster than testing the whole scene.
sculptAt(point, RADIUS, brush, dt)works the ground for this frame’s duration, so the speed is the same at any frame rate.commit()runs once, when the key is released, so the collider, raycasts, grass and navigation mesh are updated once per stroke instead of every frame.
Performance
Section titled “Performance”- One terrain is one draw. Changing
preset,size,seed,heightScaleorqualityrebuilds the grid: avoid doing it every frame or in a tween. - Textures: up to 20 Terrain Textures are drawn on every device. Each image is drawn at most 1024 pixels across on desktop and 512 on phones and headsets. Using
.ktx2files of the same size and format for all of them keeps them compressed, the lighter choice on headsets and phones. - Painting uploads only the area a stroke touched; nothing is drawn per frame.
commit()updates the collider, raycasts, grass and navigation mesh: call it once per stroke, not per frame.
Common mistakes
Section titled “Common mistakes”- Mixing local and world coordinates.
heightAt,paintandsculpttake local coordinates;paintAtandsculptAttake world points. Convert withworldToLocalandlocalToWorld. - Committing every frame. Commit when a stroke ends. In play, strokes also commit on their own after 15 frames of rest.
- Using a box collider for a terrain. Only a Mesh Collider fits a terrain’s shape. See Physics.
- Expecting more than 20 textures to show. Textures past 20 are kept but not drawn.