A WebGL2-based 3D terrain simulator featuring procedural terrain generation, heightmap manipulation, and interactive camera control.
- Procedural terrain generation using randomized faults and height normalization
- Triangle rasterization with per-vertex normals for shading
- Real-time lighting with Lambertian and Blinn-Phong reflection models
- Z-buffer (depth test) ensures correct rendering of overlapping geometry
- Interactive camera controls:
W/S→ move forward/backwardA/D→ move left/right- Arrow keys → pan and pitch
- Responsive full-screen canvas
- JavaScript
- WebGL2
- GLSL shaders (vertex and fragment)
3D_terrain_simulator.html– main HTML file containing shaders and JavaScript codemath.js– helper math functions (vectors, matrices, etc.)
- Generate a random terrain grid and apply faults to create elevation variations
- Normalize heights for visualization
- Create geometry (triangles and vertex attributes) and upload to GPU buffers
- Vertex and fragment shaders handle 3D transformations and lighting
- Use a Vertex Array Object (VAO) to organize vertex data for rendering
- Depth testing (Z-buffer) ensures correct occlusion between terrain surfaces
- Interactive camera allows exploration of the terrain in real time
| Key | Action |
|---|---|
W / S |
Move forward / backward |
A / D |
Move left / right |
Arrow Keys |
Pan and pitch |
compileShader(vs_source, fs_source)– compiles vertex and fragment shaders and links them into a WebGL programsupplyDataBuffer(data, loc, mode)– uploads per-vertex data to the GPUsetupGeomery(geom)– creates a VAO with triangles and vertex attributesgenerateGrid(gridsize)– initializes the height gridfaulting(faults)– applies procedural faults to the terrainnormalizeHeights(faults)– normalizes terrain heightsgenerateGeom()– generates geometry from height dataaddNormals(geom)– calculates per-vertex normals for lighting
- Clone the repository:
git clone https://github.com/NICKLIN13/3D-terrain-simulator.git
- Open 3D_terrain_simulator.html in a WebGL2-capable browser (Chrome, Firefox, Edge, etc.)
- Use the keyboard controls to explore the terrain