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.worldX- World X coordinateworldZ- World Z coordinate
getProceduralHeightAt()
Get procedural terrain height bypassing flat zones. Useful for positioning objects above terrain.worldX- World X coordinateworldZ- World Z coordinate
registerFlatZone()
Register a flat zone for terrain flattening under buildings/stations.zone.id- Unique identifierzone.centerX- Center X coordinatezone.centerZ- Center Z coordinatezone.width- Width in meterszone.depth- Depth in meterszone.height- Flat height in meterszone.blendRadius- Smooth transition radius
- Updates spatial index for O(1) lookups
- Regenerates affected terrain tiles to reflect flat zone heights
- Emits
TERRAIN_TILE_REGENERATEDevents 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).getTerrainColorAt()
Get terrain color at a position by sampling nearest terrain tile vertex.prefetchTile()
Request speculative loading of a terrain tile.getTerrainGenerator()
Get the unified terrain generator for standalone terrain queries.@hyperscape/procgen
isReady()
Check if terrain system is ready for player spawning.getTileSize()
Get the terrain tile size in meters.getBiomeData()
Get biome configuration by ID.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 forgetBaseHeightAt() to embed in worker string.
Usage Examples
Custom Terrain Parameters
To modify terrain generation, editTerrainHeightParams.ts:
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