Terrain
Updated
Procedural terrain from one displaced grid, paintable with textures.
- Type
ent-terrain- Script class
TerrainEntity- Family
- Meshes
Overview
Section titled “Overview”A Terrain is a procedural landscape: hills, dunes, canyons or mountains generated from a preset and a seed, and drawn in one draw call. It is one square grid pushed up and down by noise. Its lowest point rests on the entity’s local y = 0, so the entity’s transform places it in the world. It is lit like a Standard material, with shadows and fog.
Everything lives in the entity’s required Terrain component (comp-terrain). You can paint it with colours and with the images of Terrain Texture components on the same entity, and sculpt it into hills and holes, in the editor or from a script. Painting and sculpting show at once; commit() saves them, and in play the entity’s owner commits on its own 15 frames after the strokes stop.
Use a Terrain for outdoor ground that is not flat. For a flat area, a Floor is simpler, and for lakes and seas add a Water Mesh over it.
The props that matter most:
preset: the landform and colour palette:'plains','hills','mountains','dunes','canyon'or'alien'.size: the side length of the square, in local units.seed: another seed gives another landscape of the same kind.heightScale: a multiplier on the preset’s peak height.0is flat; negative values turn the landforms upside down.quality: grid detail.'auto'picks'low'on phones and headsets and'high'elsewhere.
Changing preset, size, seed, heightScale or quality rebuilds the grid, and its collider, shadows, outline and grass follow. The component’s members read and shape the ground: heightAt(x, z), paint, paintAt, fill, sculpt, sculptAt, clearRelief() and commit().
Set it up
Section titled “Set it up”- Add a Terrain entity to the scene (see Scenes and hierarchy) and choose a
preset. - Set its
size, then try a fewseedvalues and aheightScaleuntil the landscape fits. - Add a Mesh Collider so players and objects stand on it: it is the only collider that fits a terrain’s shape. Add Navmesh Walkable if characters navigate on it.
- Add one Terrain Texture per image you want to paint with (grass, rock, sand, mud), then paint.
- Add Grass to grow blades on it, and a Shadow to let it receive shadows.
Examples
Section titled “Examples”Place objects on the ground
Section titled “Place objects on the ground”Goal: drop a list of trees and rocks onto the surface at startup, wherever the terrain is placed and without a raycast.
#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); }}heightAt(x, z)takes the terrain’s local coordinates.worldToLocalconverts the prop’s world position first, andlocalToWorldconverts the result back.- The height includes any sculpting, and needs no raycast.
setPositionmoves the prop and keeps any physics body in step. The props sit at the top of the scene, where their local position is a world position.
Generate a new landscape on a key press
Section titled “Generate a new landscape on a key press”Goal: a level designer’s helper. Pressing N rolls a new seed, and ↑ and ↓ make the relief taller or flatter.
#pragma lifecycle(startup, update)
let terrain;
function startup() { terrain = findEntity('Terrain').getComponent(TerrainComponent);}
function update() { const keyboard = Input.keyboard;
if (keyboard.isKeyReleased(Key.N)) { const seed = Random.integer(1, 100000); terrain.setProps({ seed }); log('Seed', seed); } if (keyboard.isKeyReleased(Key.ArrowUp)) { terrain.setProps({ heightScale: Math.min(10, terrain.props.heightScale + 0.25) }); } if (keyboard.isKeyReleased(Key.ArrowDown)) { terrain.setProps({ heightScale: Math.max(0, terrain.props.heightScale - 0.25) }); }}isKeyReleasedis true once per key press, so each press rebuilds the grid only once.Random.integer(1, 100000)picks a whole number, both ends included. The seed is logged so a landscape you like can be saved by typing it in.heightScalestays between 0 and 10 here, inside its accepted range of -10 to 10.- Each change rebuilds the grid; its collider, shadows and grass follow.
Paint a trail where the player walks
Section titled “Paint a trail where the player walks”Goal: a muddy trail appears behind the player as they walk across the terrain.
The Terrain has a Terrain Texture with a mud image as its first texture.
#pragma lifecycle(startup, update)
const STEP = 0.25;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]is the first Terrain Texture on the entity: the mud.camerais the camera the player sees through. Its world position is read into a vector created once.paintAt(point, radius, what, value, hardness, lay)paints at a world point: a 0.8 m disc, fully (1), with a soft edge (0.3). The lay{ scale: 0.5 }tiles the mud image at half size.- Four strokes a second are enough at walking pace. No
commit()is needed in play: the owner commits once the strokes stop.
heightAt,paintandsculpttake local coordinates;paintAtandsculptAttake world points.- Call
commit()once a stroke ends, not every frame: it updates the collider, raycasts, grass and navigation mesh. - Up to 20 Terrain Textures are drawn. Using
.ktx2images of the same size and format keeps them compressed, the lighter choice on phones and headsets.
Components
Section titled “Components”What a new Terrain is made of. Required components are always present and cannot be removed; the others can. Per entity says whether an entity can hold one of that component or several. Layer, Lock and Enable are on every placed entity, and Transform holds its position, rotation and scale.
| Component | Type | Role | Per entity | What it does |
|---|---|---|---|---|
| Layer | comp-layer |
Required | One | Puts the entity on a render/raycast layer so cameras and rays can include or skip it. |
| Lock | comp-lock |
Required | One | Editor-only flag that stops the entity from being selected or moved. |
| Enable | comp-enable |
Required | One | Turns the entity and its children on or off: hides them and stops their behavior. |
| Transform | comp- |
Required | One | Position, rotation and scale of the entity, with optional editor-only per-axis locks. |
| Terrain | comp-terrain |
Required | One | Procedural terrain from one displaced grid, paintable with textures. |
You can also add 17 optional components, such as Animation Graph, Animation Timeline, Contact Shadow, Enable On Signal, Grass, Mesh Collider.
The settings of a new entity, as the runtime creates it. Change them in the Properties panel of XR Creator, or from a script with getComponent(…).setProps({ … }).
| Prop | Default | Range |
|---|---|---|
preset |
"hills" |
|
size |
128 |
0. |
seed |
1 |
|
heightScale |
1 |
-10 ≤ x ≤ 10 |
quality |
"auto" |
Events
Section titled “Events”With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.
It reports the events every entity has: Transform::change (and position-change, rotation-change, scale-change), Enable::change, Layer::change and Lock::change.
Live example
Section titled “Live example”Run it in the browser, read its scripts, and download it as an XR Creator project.
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.
From a script
Section titled “From a script”Find the entity by its name, check its class, and work with its components. Name the entity in the Hierarchy first; this example expects My Terrain.
#pragma lifecycle(startup)
function startup() { // Find the entity by the name it has in the Hierarchy. const entity = findEntity('My Terrain'); if (!(entity instanceof TerrainEntity)) return;
// Its Terrain component holds the settings listed under Props. const component = entity.getComponent(TerrainComponent); log('size is', component.props.size); component.setProps({ size: 192 });}