Marching Cube Function - 等值面提取构建

这是一个Demo演示(左下图),这里主要实现还原了Marching Cubes (MC) 算法在游戏场合的表现。有一款有趣的游戏—《Townscaper》(右下图)中应用了MC,即等值面提取算法,用于游戏内动态建筑的生成。MC 是一种计算机图形学算法,最初主要用于医学可视化领域。不过,《Townscaper》利用这一算法来渲染风格化的建筑表面,并将其与 Wave Function Collapse 算法结合,形成有趣的玩法交互。MC 在渲染性能方面表现出色,能够在游戏中生成独特且富有吸引力的艺术效果。在本页面中,我将介绍制作这个演示的步骤。

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 originally concerned with medical visualizations. 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 for making this demo.

a88a8691 ed34 4159 a3b7 cfcc4dad7777 rw 1200
d6471532 95d1 4af2 a4a7 d2648ed382b0 rw 600

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.

a6af7461 85a0 4135 9581 f2ba4efe6605 rw 1200

2. 等值面提取 marching cubes

在 Marching Cubes 过程的第一步中,需要将事先生成的二维网格 转换为三维网格。具体做法是:在不同的单位高度(沿 Y 轴方向)创建顶点,这些顶点在 X 和 Z 坐标上与二维网格中的顶点保持一致,从而构建出一个三维网格结构。在该三维网格中,每个顶点都有两种可能状态:激活或未激活(用于表示该空间位置是否包含体积)。基于八个顶点的状态,我们可以为每一个三维网格单元计算出一个 8 位的二进制值。接下来,我们为已有的白模资源进行命名,并根据不同的二进制状态导入对应模型。同时,我编写了一个脚本,根据每个网格单元八个顶点的状态(即其对应的二进制值),自动生成匹配的模型。

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.

551aaf40 e2b6 4661 8e27 359ee9877cb6 rw 1920

3. 建筑系统 BUILDING SYSTEM

为了更好地利用 Marching Cubes 机制来表现建筑生成的过程,我们还需要一个建筑系统来处理玩家交互。首先,我们为地面网格添加碰撞体,使玩家能够点击空地来添加模块模型。同时,通过 UI 显示建筑模块的单元位置,使交互更加直观清晰。此外,我们为每个已添加模型的各个面都附加了碰撞体,从而允许玩家在指定的面上删除或继续拼接新的模块。最后,我们实现了一个简洁的相机控制器,使玩家能够从多个角度进行观察和建造,提升整体操作体验。

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 to a specific face. Finally, we implement a straightforward camera controller, giving players the ability to observe and build from multiple angles.

 

6ba40f65 4a42 4ccd b4e3 0e9fd661936a rw 1200

总结 CONCLUSION

截至目前,通过构建平滑网格(Smooth Grid)、实现 Marching Cubes 算法以及开发建筑系统,这里已经完成了该 Demo 的基础功能。由于目前缺乏足够数量的风格化模型资源,暂时还无法实现 Wave Function Collapse 的最终效果。未来我们希望能够与合适的建模师合作(也许是AI建模),完善 Wave Function Collapse 部分的内容,最终实现类似 Townscaper 那样富有动态性和趣味性的建筑玩法。

​​​​​​​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.