简体   繁体   English

行进立方体绘图三角形统一

[英]marching cubes drawing triangles unity

I am trying to implement marching cubes into my game, and so far I have set up a system for drawing triangles with four points in world space.我正在尝试在我的游戏中实现行进立方体,到目前为止,我已经建立了一个用于在世界空间中绘制具有四个点的三角形的系统。 I have got the system working where I set the points, but then when I draw the two triangles to make it a mesh, it only draws one, but I set up a print() statement, and it shows that 2 triangles are drawn each time, so that is normal.我已经让系统在我设置点的地方工作,但是当我绘制两个三角形以使其成为网格时,它只绘制一个,但我设置了一个print()语句,它显示每个绘制了 2 个三角形时间,所以这很正常。 I can't figure out what I need to do.我不知道我需要做什么。 Here is my code, and comment on this post if you need any more pics/code:这是我的代码,如果您需要更多图片/代码,请评论这篇文章:

    private void Triangulate()
    {
        vertices.Clear();
        triangles.Clear();
        mesh.Clear();

        TriangulateCellRows();
        print(triangles.Count);
        mesh.vertices = vertices.ToArray();
        mesh.triangles = triangles.ToArray();
    }
    private void TriangulateCellRows()
    {
        int cells = resolution - 1; int heightCells = heightRes - 1;
        for (int i = 0, y = 0; y < heightCells; y++)
        {
            for (int x = 0; x < cells; x++, i++)
            {
                for (int z = 0; z < cells; z++, i++)
                {
                    TriangulateCell(
                        voxels[i],
                        voxels[i + 1],
                        voxels[i + resolution],
                        voxels[i + resolution + 1],
                        voxels[i + resolution * resolution],
                        voxels[i + resolution * resolution + 1],
                        voxels[i + resolution * resolution + resolution],
                        voxels[i + resolution * resolution + resolution + 1]);
                }
            }
        }
    }
    private void TriangulateCell(Voxel a, Voxel b, Voxel c, Voxel d, Voxel e, Voxel f, Voxel g, Voxel h)
    {
        int cellType = 0;
        if (a.state)
        {
            cellType |= 1;
        }
        if (b.state)
        {
            cellType |= 2;
        }
        if (c.state)
        {
            cellType |= 4;
        }
        if (d.state)
        {
            cellType |= 8;
        }
        if (e.state)
        {
            cellType |= 16;
        }
        if (f.state)
        {
            cellType |= 32;
        }
        if (g.state)
        {
            cellType |= 64;
        }
        if (h.state)
        {
            cellType |= 128;
        }
        switch (cellType)
        {
            case 0:
                return;/*
            case 1:
                AddTriangle(a.xEdgePosition, b.yEdgePosition, c.zEdgePosition);
                break;*/
            case 15:
                AddQuad(a.yEdgePosition, b.yEdgePosition, c.yEdgePosition, d.yEdgePosition);
                print(a.yEdgePosition + "- " + b.yEdgePosition + "- " + c.yEdgePosition + "- " + d.yEdgePosition);
                print(a.position + "/ " + b.position + "/ " + c.position + "/ " + d.position);
                break;
        }
    }
...
    private void AddQuad(Vector3 a, Vector3 b, Vector3 c, Vector3 d)
    {
        int vertCount = vertices.Count;
        vertices.Add(a);
        vertices.Add(b);
        vertices.Add(c);
        vertices.Add(d);
        triangles.Add(vertCount);
        triangles.Add(vertCount + 1);
        triangles.Add(vertCount + 2);
        triangles.Add(vertCount + 1);
        triangles.Add(vertCount + 2);
        triangles.Add(vertCount + 3);
        print(triangles.Count);
    }

I'm pretty sure your issue here is order .我很确定您的问题是order

Unity uses a clockwise winding order . Unity 使用顺时针缠绕顺序 This means that这意味着

  • if you provide the triangles in clockwise order the normal will face towards you and you will see the triangle.如果您按顺时针顺序提供三角形,法线将面向您,您将看到三角形。

    在此处输入图像描述

  • if you provide the triangle in counter-clockwise order the normal will face away from you and you will not see the triangle.如果您以逆时针顺序提供三角形,则法线将背对您,您将看不到三角形。

    在此处输入图像描述


Now looking at your code (at least as far as I can tell) your vertices look somewhat like现在看看你的代码(至少据我所知)你的顶点看起来有点像

C--D
|\ |
| \|
A--B

and your triangles are ABC and BCD.你的三角形是ABC和BCD。

So as you can see one of them is counter-clockwise (ABC), the other one is clockwise (BCD)!所以你可以看到其中一个是逆时针(ABC),另一个是顺时针(BCD)!

So depending from which side you are looking on these you will either see one or the other but never both at the same time.因此,根据您从哪一侧看这些,您会看到一个或另一个,但永远不会同时看到两者。


You either rather want你要么宁愿

triangles.Add(vertCount);
triangles.Add(vertCount + 2);
triangles.Add(vertCount + 1);

triangles.Add(vertCount + 1);
triangles.Add(vertCount + 2);
triangles.Add(vertCount + 3);

or或者

triangles.Add(vertCount);
triangles.Add(vertCount + 1);
triangles.Add(vertCount + 2);

triangles.Add(vertCount + 1);
triangles.Add(vertCount + 3);
triangles.Add(vertCount + 2);

depending on your needs and in which direction the resulting triangles should face取决于您的需要以及生成的三角形应朝向的方向

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM