LittleJS 1.30.0

Render3D

HeightMap

HeightMap - Terrain from a grid of heights: an object that draws itself, and that objects with collideLevel stand on

  • 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
  • pos3D is the center of the map, its grid spans mapSize on X and Z around it, and a full value is height above it
  • getHeight, getNormal, getColor and raycast are in world space, with the map's position taken off
  • It stays upright and unscaled, its lookups do not turn with it

Extends EngineObject3D

Constructor

new HeightMap(heights, mapSize, height, colors, pos3D, smooth)render3dExtras.js:448

Create a height map from an array or an image, it draws itself and joins the level's collision

heights Array<Array<number>>|HTMLImageElement|HTMLCanvasElement|OffscreenCanvas|TextureInfo
mapSize Vector2 optional

World size along X and Z

height number optional, default 1

World height of a full value

colors Array<Array<Color>>|HTMLImageElement|HTMLCanvasElement|OffscreenCanvas|TextureInfo optional
pos3D Vector3 optional

Center of the map

smooth boolean optional

Defaults to render3D.smoothShading

const terrain = new HeightMap(heightImage, vec2(100, 100), 10, colorImage);
const y = terrain.getHeight(x, z); // stand things on it, or give them collideLevel

Members

Methods

Inherited from EngineObject3D

Inherited from EngineObject

Members

colors : Array<Array<Color>>|undefinedrender3dExtras.js:471

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

columns : numberrender3dExtras.js:490

Number of columns, along X

height : numberrender3dExtras.js:475

World height of a full value

heights : Array<Array<number>>render3dExtras.js:468

Heights 0-1 as [row][column], rows along Z, rebuild() after changing them

World size along X and Z

Number of rows, along Z

smooth : booleanrender3dExtras.js:477

Smooth shading, rebuild() after changing it

Methods

buildMesh(smooth) → Meshrender3dExtras.js:607

Build the terrain mesh, one vertex per sample, centered on the map's own origin

smooth boolean optional

Defaults to render3D.smoothShading

destroy(immediate)render3dExtras.js:693

Destroy the map, it leaves the level's collision

immediate boolean optional
getColor(x, z) → Colorrender3dExtras.js:531

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

x number|Vector3

X, or a position to take X and Z from

z number optional
getHeight(x, z) → numberrender3dExtras.js:496

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

x number|Vector3

X, or a position to take X and Z from

z number optional
getNormal(x, z) → Vector3render3dExtras.js:517

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

x number|Vector3

X, or a position to take X and Z from

z number optional
raycast(ray) → number|undefinedrender3dExtras.js:550

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

  • Exact: the ground is flat inside each triangle, so the ray is checked between each grid line and cell diagonal it crosses, and a hill it only grazes is still hit
  • A ray that starts under the ground crosses on its way out, so the hit is still on the surface
ray Ray3D

From screenToRay, or any ray

Make the mesh again from the heights and colors, after changing them or smooth

Keeps an eye on its placement, called automatically each frame