Skip to content

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.

  • Local and world space. Terrain methods named paint, sculpt and heightAt take the terrain’s local coordinates; the …At methods take a world point. Every entity is a scene object, so worldToLocal and localToWorld convert 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.
ComponentTypeWhat it does
Terraincomp-terrainProcedural terrain from one displaced grid, paintable with textures.
Terrain Texturecomp-terrain-textureOne texture the terrain can be painted with.

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.

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.

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.

Member Description
heightAt(x, z) 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.
paintAt(point, radius, what, value = 1, hardness = 1, lay?) 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.
sculptAt(point, radius, brush, seconds) 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).

  • 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 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.75 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.

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.

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.

SnapToTerrain.jx
#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);
}
}
  • 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.
  • setPosition moves 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.

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.

MudTrail.jx
#pragma lifecycle(startup, update)
const STEP = 0.25; // seconds between strokes
let 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 });
}
  • 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.

TerraformTool.jx
#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;
}
  • Brushes are plain objects built once, keyed by the key codes Key.R, Key.F and Key.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.
  • One terrain is one draw. Changing preset, size, seed, heightScale or quality rebuilds 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 .ktx2 files 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.
  • Mixing local and world coordinates. heightAt, paint and sculpt take local coordinates; paintAt and sculptAt take world points. Convert with worldToLocal and localToWorld.
  • 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.