Skip to content

Terrain

Updated

Procedural terrain from one displaced grid, paintable with textures.

Type
ent-terrain
Script class
TerrainEntity
Family
Meshes

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. 0 is 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().

  1. Add a Terrain entity to the scene (see Scenes and hierarchy) and choose a preset.
  2. Set its size, then try a few seed values and a heightScale until the landscape fits.
  3. 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.
  4. Add one Terrain Texture per image you want to paint with (grass, rock, sand, mud), then paint.
  5. Add Grass to grow blades on it, and a Shadow to let it receive shadows.

Goal: drop a list of trees and rocks onto the surface at startup, wherever the terrain is placed and without a raycast.

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);
}
}
  • heightAt(x, z) takes the terrain’s local coordinates. worldToLocal converts the prop’s world position first, and localToWorld converts the result back.
  • The height includes any sculpting, and needs no raycast.
  • setPosition moves 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.

Goal: a level designer’s helper. Pressing N rolls a new seed, and ↑ and ↓ make the relief taller or flatter.

LandscapeRoller.jx
#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) });
}
}
  • isKeyReleased is 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.
  • heightScale stays between 0 and 10 here, inside its accepted range of -10 to 10.
  • Each change rebuilds the grid; its collider, shadows and grass follow.

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.

MudTrail.jx
#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.
  • camera is 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, paint and sculpt take local coordinates; paintAt and sculptAt take 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 .ktx2 images of the same size and format keeps them compressed, the lighter choice on phones and headsets.

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-transform 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.001 ≤ x ≤ 10000
seed 1
heightScale 1 -10 ≤ x ≤ 10
quality "auto"

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.

Run it in the browser, read its scripts, and download it as an XR Creator project.

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.

TerrainEntity.jx
#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 });
}