This is a little demo (left below) to implement the Marching Cubes (MC) function in an interesting game: Townscape (right below). MC is a computer graphics algorithm that is concerned with medical visualizations originally. However, the Townscaper utilizes this algorithm to render the stylized building surface and make it into an interesting gameplay interaction with the Wave Function Collapse algorithm. The MC function excels in rendering performance and is capable of producing special and appealing artistic effects within games. On this page, I will introduce the general steps about how I make this demo.
1.  SMOOTH GRID
To generate smooth grids (like the right figure), we first create a hexagonal lattice (points). Next, we connect all points within the lattice to form triangles. Subsequently, triangles are randomly merged into rhombuses until no adjacent triangles remain. Then, we subdivide them by connecting the midpoints of opposite sides across all shapes. Finally, we smooth all cells to achieve smooth grids.
2.  MARCHING CUBES
The first step in the Marching Cubes process involves transforming the two-dimensional grid generated in the previous phase into a three-dimensional mesh. This is achieved by creating vertices at various unit heights (along the Y-axis) that share the same X and Z values as those in the two-dimensional grid, thus forming a three-dimensional mesh. Each vertex can be in one of two states: activated or deactivated (indicating whether this spatial position contains volume). Based on this, we can derive an 8-bit binary value for all the three-dimensional grid cells. At this point, we assign names to the existing white models and import them according to the different bit value states. A script was written to generate the corresponding model for each grid cell based on the states (i.e., bit values) of its eight vertices.
3. BUILDING SYSTEM
To better leverage the Marching Cubes mechanism for reconstructing the building process, we also need a building system to handle player interactions. Initially, we add colliders to the ground grid, enabling players to click on empty ground to add module models. At the same time, we managed to make the interaction more intuitive by displaying the construction module's unit position through the UI. Moreover, we attach colliders to every face of each added model, which allows players to delete or attach modules on the specific face. Finally, we implement a straightforward camera controller, giving players the ability to observe and build from multiple angles.
CONCLUSION
​​​​​​​Thus far, by creating a Smooth Grid, constructing the Marching Cubes, and developing a Building System, we have implemented the basic functionalities of the demo. Due to the absence of a sufficient number of stylized models, we are temporarily unable to achieve the Wave Function Collapse effect. We hope to collaborate with suitable modelers in the future to complete the Wave Function Collapse part, and implement a dynamic and interesting building gameplay like Townscaper.
Back to Top