Render3D. HeightMap

HeightMap - Terrain built from a grid of heights, with a mesh, a height lookup and a raycast

  • heights is a 2D array [row][column] of 0 to 1 values
  • Row 0 is the far edge at -Z and column 0 is the left edge at -X
  • It can be an image instead, where the red channel is the height
  • colors is an optional 2D array of Colors or an image, sampled per vertex
  • images are read through a canvas, so they must be same origin or loaded with crossOrigin set

Constructor

new HeightMap(heights, sizeopt, heightopt, colorsopt)

Create a height map from an array or an image

Parameters:
NameTypeAttributesDefaultDescription
heightsArray.<Array.<number>> | HTMLImageElement | HTMLCanvasElement | OffscreenCanvas | TextureInfo
sizeVector2<optional>

World size along X and Z

heightnumber<optional>
1

World height of a full value

colorsArray.<Array.<Color>> | HTMLImageElement | HTMLCanvasElement | OffscreenCanvas | TextureInfo<optional>
Example
const terrain = new HeightMap(heightImage, vec2(100, 100), 10, colorImage);
new EngineObject3D(vec3(), terrain.buildMesh());
const y = terrain.getHeight(x, z); // stand things on it

Members

colors :Array.<Array.<Color>>|undefined

Type:
  • Array.<Array.<Color>> | undefined
Properties
TypeDescription
Array.<Array.<Color>> | undefined

Vertex colors as [row][column], undefined for white

columns

Number of columns, along X

height

Properties
TypeDescription
number

World height of a full value

heights

Properties
TypeDescription
Array.<Array.<number>>

Heights 0-1 as [row][column], rows along Z

rows

Number of rows, along Z

size

Properties
TypeDescription
Vector2

World size along X and Z

Methods

buildMesh(smoothopt) → {Mesh}

Build the terrain mesh, one vertex per sample, centered on the origin

Parameters:
NameTypeAttributesDescription
smoothboolean<optional>

Defaults to render3D.smoothShading

Returns:
Type: 
Mesh

getColor(x, zopt) → {Color}

Color of the nearest sample to a position, white when there are no colors

Parameters:
NameTypeAttributesDescription
xnumber | Vector3

X, or a position to take X and Z from

znumber<optional>
Returns:
Type: 
Color

getHeight(x, zopt) → {number}

World height at a position, exactly the height of the mesh buildMesh draws there, clamped at the edges

Parameters:
NameTypeAttributesDescription
xnumber | Vector3

X, or a position to take X and Z from

znumber<optional>
Returns:
Type: 
number

getNormal(x, zopt) → {Vector3}

Surface normal at a position, from the slope across a sample

Parameters:
NameTypeAttributesDescription
xnumber | Vector3

X, or a position to take X and Z from

znumber<optional>
Returns:
Type: 
Vector3

raycast(ray) → {number|undefined}

Distance along a ray to where it crosses the terrain surface, or undefined for a miss

  • Steps along the ray half a cell at a time, then narrows in on the exact spot
  • A ray that starts under the ground crosses on its way out, so the hit is still on the surface
Parameters:
NameTypeDescription
rayRay3D

From screenToRay, or any ray

Returns:
Type: 
number | undefined