Runtime Terrain Deformation with Marching Cubes
GPU-accelerated procedural mesh generation system for real-time digging mechanics in Unity.
Client: realtimecapsule
Developed for an archaeology game prototype, this system uses the Marching Cubes algorithm to generate editable voxel terrain at runtime.
The project evolved from an experimental CPU prototype into a GPU-driven solution capable of real-time mesh regeneration, interactive digging, and stylized rendering integration.
Project Context
The project was created for a game studio during the prototyping phase of an archaeology-themed game concept.
The core requirement was the implementation of an interactive digging mechanic that allowed players to excavate terrain dynamically during gameplay.
Traditional terrain systems were too restrictive for the desired level of deformation and runtime control, so I explored procedural mesh generation techniques and chose the Marching Cubes algorithm as the foundation for the system.
The Goal
Fully editable terrain
Runtime mesh generation
Additive and subtractive sculpting
Smooth terrain surfaces
Performance suitable for gameplay prototyping
Dynamic masking for chamber placement
CPU Prototype
The first implementation was entirely CPU-driven and used a 3D grid structure to store scalar density values.
Terrain editing was achieved by modifying voxel weights around a brush position:
Subtractive editing removed terrain
Additive editing restored terrain
While the prototype validated the workflow successfully, mesh generation costs increased rapidly with terrain resolution, making CPU generation unsuitable for larger runtime scenarios.
GPU Port & Optimization
To improve scalability and runtime performance, the mesh generation pipeline was migrated to the GPU using compute shaders.
The compute shader handled:
Density evaluation
Triangle generation
Normal calculation
Vertex buffer creation
This dramatically reduced CPU overhead and enabled significantly faster terrain updates during gameplay.
Profiling Data
Running the Marching Cubes Algorithm on Update on CPU and GPU for comparison
CPU based Marching Cubes
30 fps average
24 ms for Marching Cubes Algorithm
31.5 ms on CPU Main Thread
GPU based Marching Cubes
100 fps average
5.6 ms for Marching Cubes Algorithm (on CPU)
11.5 ms on CPU Main Thread
12 ms on GPU
For even better results you could split the grid into individual chunks for even less computation time. But in this case the results are sufficient since these profiling tests were made while executing the algorithm every frame, which in a gameplay scenario should not be the case.
Integration into Production Prototype
Once the core terrain system was stable, the feature was integrated directly into the studio’s gameplay prototype.
The digging mechanic was connected to the player controller and interaction systems, allowing terrain excavation during normal gameplay flow.
To better match the visual direction of the project, I created a custom stylized shader for the generated terrain mesh that aligned with the game’s pixel-art aesthetic.
Final Touch
To reduce visual popping when updating the terrain mesh, I introduced a double buffered approach using two separate mesh objects. Each of them would then be faded out while the other is fading in.
This created a much smoother transition and also helped the visual language of the terrain because of the dithered fading effect.
Connection to procedural Chamber Generation
The Prototype already had a generation system for procedurally adding underground chambers the player has to find and enter.
This created another problem for the integration of the marching cubes system since it required hollow areas where the chambers would be placed dynamically.
My solution was to add bounding box masks that would be subtracted from the marching cubes 3D texture upon initialization. After connecting them to the procedural chamber generator, the feature was complete and gameplay ready.
Technical Challenges
Several technical challenges emerged during development:
-
Maintaining stable topology during continuous terrain edits required careful handling of chunk boundaries and vertex generation consistency.
-
Efficient communication between compute buffers and mesh generation pipelines was essential to avoid synchronization bottlenecks.
-
Terrain resolution had to be balanced carefully to maintain responsive gameplay performance.
Outcome
The final system successfully provided:
Real-time editable terrain
GPU-accelerated mesh generation
Interactive digging gameplay
Stylized rendering integration
Scalable runtime performance
The project demonstrated a full pipeline from technical research and prototyping to gameplay integration and visual polish.