如何优化Marching Cubes算法?如何组合相似的面部组?

时间:2014-02-26 03:01:42

标签: c# unity3d polygon procedural-generation marching-cubes

在使用256个查找表进行行进立方体算法的3-4天后,我终于让它工作了。现在,我有它工作,我想尝试优化它。我想到的一件事是用一个浮动区域移除大的平坦区域。

示例图片显示我在想什么 Example Image to show what I am thinking

我如何使用查找表生成网格

 /* Cube description:
 *        p7 ________p6           ____e6__          
 *         /|       /|        e7/|       /|         
 *       /  |     /  |        /  |     /e5|      
 *  p4 /_______ /    |      /__e4___ /    e10     
 *    |     |  |p5   |     |    e11 |     |     
 *    |   p3|__|_____|p2   |     |__|__2__|     
 *    |    /   |    /     e8  e3/   e9   /      
 *    |  /     |  /        |  /     |  /e1       
 *    |/_______|/          |/__e0___|/          
 *   p0        p1                  
 */

 for (int x = 0; x < chunkSize; x++)
    {
        for (int y = 0; y < chunkSize; y++)
        {
            for(int z = 0; z < chunkSize; z++)
            {
                Vector3 point = new Vector3(x, y, z);

                int value = 0;

                if (voxelData.Contains(point))
                    value += (int)LookupTable.VoxelPoint.p0;
                if (voxelData.Contains(point + Vector3.forward))
                    value += (int)LookupTable.VoxelPoint.p3;
                if (voxelData.Contains(point + Vector3.right))
                    value += (int)LookupTable.VoxelPoint.p1;
                if (voxelData.Contains(point + Vector3.right + Vector3.forward))
                    value += (int)LookupTable.VoxelPoint.p2;
                if (voxelData.Contains(point + Vector3.up))
                    value += (int)LookupTable.VoxelPoint.p4;
                if (voxelData.Contains(point + Vector3.up + Vector3.forward))
                    value += (int)LookupTable.VoxelPoint.p7;
                if (voxelData.Contains(point + Vector3.up + Vector3.right))
                    value += (int)LookupTable.VoxelPoint.p5;
                if (voxelData.Contains(point + Vector3.up + Vector3.forward + Vector3.right))
                    value += (int)LookupTable.VoxelPoint.p6;

                Vector3[] vertsOutput = LookupTable.GetVerticies(value);

                    if(vertsOutput == null)
                        continue;

                foreach (var vert in vertsOutput)
                {
                    tris.Add(verts.Count);
                    verts.Add(point + vert);
                }
            }
        }
    }

    mesh.vertices = verts.ToArray();
    mesh.triangles = tris.ToArray();

感谢您以正确的方式指出我或帮助我解决这个问题。

修改* What im working on

0 个答案:

没有答案