Skip to content

Voxel terrain ​

VoxelTerrain (from @maomi/sge/terrain/voxels) generates a blocky heightfield from simplex noise and keeps its physics affordable by streaming bodies around moving entities instead of materializing a rigid body per block.

ts
const terrain = new VoxelTerrain(
  128,                                        // width (blocks per side)
  materialMap,                                // which material each block gets
  { seed: 31, scale: 60, magnitude: 9, offset: 1 },  // noise params
  'full',                                     // render mode
);
terrain.generate();
fps.add(terrain);

Height map ​

generate() fills heightMap[x][z] from seeded simplex noise — deterministic per seed — then (in 'full' mode) builds the meshes.

Greedy meshing ​

Rendering is InstancedMesh boxes, but not one per block: adjacent same-height runs merge into single stretched instances. A 128×128 map renders in a handful of instanced draws. Regenerating (generate() again after tweaking noiseParams) disposes the old geometry first — a live tweak UI doesn't leak GPU memory.

Materials ​

The MaterialMap decides looks per block:

ts
const materials = {
  types: {
    grass: new THREE.MeshLambertMaterial({ color: 0x4c9f45 }),
    rock:  new THREE.MeshLambertMaterial({ color: 0x8a8577 }),
  },
  getTypeAt(x, y, z, terrain) { return y > 6 ? 'rock' : 'grass'; },
};

Materials are shared across instances (and deliberately never disposed by the terrain).

Streamed physics ​

Wherever a dynamic entity moves, the terrain materializes static bodies for the blocks in a radius around it, and drops the ones nothing is near. The player walks on solid ground everywhere, but the world only ever holds a working set of bodies.

Render mode 'streamed' draws only those materialized bodies instead of the full mesh — a live visualization of the physics working set, useful for debugging and tests.

Grid ​

Grid (from @maomi/sge/terrain/grid) is the trivial cousin: a flat static plane with a GridHelper, the right floor for demos and playgrounds.