Character Controls
W↑Move Forward
S↓Move Backward
A←Move Left
D→Move Right
Character Controller
Third-person character controller that walks on procedural terrain
This example demonstrates a third-person character controller that walks on procedural terrain using the library's performant height testing API.
Key Features
- Capsule Character: A capsule geometry represents the character, properly grounded on the terrain surface
- Third-Person Camera: Smooth camera following with configurable distance and height
- WASD/Arrow Controls: Standard movement controls for navigating the terrain
- Performant Height Testing: Uses
queryHeightAtPosition()for fast terrain height lookups
Performant Height Testing
The character controller uses TerrainMesh.queryHeightAtPosition(position) to query the terrain height. This method:
- Finds the leaf quadtree node containing the position
- Converts world position to local UV coordinates within that node
- Performs bilinear interpolation on the CPU-cached heightmap
This approach is extremely fast (typically <0.1ms) because:
- It samples from a CPU-side cache of the heightmap
- No GPU readback is required per-frame
- The cache is automatically updated when terrain tiles change
// Example usage in your character controller
const terrainHeight = terrainMesh.queryHeightAtPosition(characterPosition);
if (terrainHeight !== null) {
characterPosition.y = terrainHeight;
}