Skip to main content

TerrainSystem

The main terrain orchestrator that manages procedural terrain generation, flat zones, and resource placement. Location: packages/shared/src/systems/shared/world/TerrainSystem.ts

Public Methods

getHeightAt()

Get terrain height at a world position. Checks flat zones first, then falls back to cached or computed height.
Parameters:
  • worldX - World X coordinate
  • worldZ - World Z coordinate
Returns: Height in meters Example:

getProceduralHeightAt()

Get procedural terrain height bypassing flat zones. Useful for positioning objects above terrain.
Parameters:
  • worldX - World X coordinate
  • worldZ - World Z coordinate
Returns: Procedural height in meters (ignores flat zones)

registerFlatZone()

Register a flat zone for terrain flattening under buildings/stations.
Parameters:
  • zone.id - Unique identifier
  • zone.centerX - Center X coordinate
  • zone.centerZ - Center Z coordinate
  • zone.width - Width in meters
  • zone.depth - Depth in meters
  • zone.height - Flat height in meters
  • zone.blendRadius - Smooth transition radius
Example:
Side Effects:
  • Updates spatial index for O(1) lookups
  • Regenerates affected terrain tiles to reflect flat zone heights
  • Emits TERRAIN_TILE_REGENERATED events for affected tiles

unregisterFlatZone()

Remove a flat zone by ID.

isInFlatZone()

Check if a position is inside a flat zone’s core area (not blend area).
Used by: ProceduralGrassSystem to exclude grass from artificial flat areas

getTerrainColorAt()

Get terrain color at a position by sampling nearest terrain tile vertex.
Returns: RGB color (0-1 range) or null if no terrain data available Used by: ProceduralGrassSystem to match grass color to terrain

prefetchTile()

Request speculative loading of a terrain tile.
Used by: VegetationSystem to pre-generate tiles in player movement direction

getTerrainGenerator()

Get the unified terrain generator for standalone terrain queries.
Returns: TerrainGenerator instance from @hyperscape/procgen

isReady()

Check if terrain system is ready for player spawning.
Returns: true if initial tiles loaded and noise initialized

getTileSize()

Get the terrain tile size in meters.
Returns: 100 (meters)

getBiomeData()

Get biome configuration by ID.
Used by: VegetationSystem to get vegetation config for biomes

TerrainHeightParams

Centralized terrain generation constants ensuring consistency between main thread and web workers. Location: packages/shared/src/systems/shared/world/TerrainHeightParams.ts

Noise Layer Definitions

NoiseLayerDef Interface

Noise Layers

Island Configuration

Pond Configuration

Coastline Noise

Mountain Boost

Worker Code Generation

buildGetBaseHeightAtJS()

Generate JavaScript source for getBaseHeightAt() to embed in worker string.
Returns: JavaScript function string with all constants baked in Usage: Called by TerrainWorker to inject constants into inline worker code Example Output:

Usage Examples

Custom Terrain Parameters

To modify terrain generation, edit TerrainHeightParams.ts:
Changes automatically apply to both main thread and workers.

Querying Terrain

Creating Flat Zones

Performance Characteristics

Worker-Based Computation

  • Height calculation: ~0ms on main thread (worker handles all noise)
  • Normal calculation: ~0ms on main thread (worker computes with overflow grid)
  • Tile generation: ~5-10ms total (worker + transfer + geometry build)
  • Flat zone tiles: ~15-20ms (requires main thread recomputation)

Spatial Indexing

  • Flat zone lookup: O(1) via tile-based spatial index
  • Typical world: 5-10 flat zones, 10-20 tile keys
  • Memory overhead: ~1KB per zone