Unity 3D网格生成三角形的四边形

发布于 2025-01-25 07:39:23 字数 3556 浏览 3 评论 0原文

我的目标是创建一个带有顶点网格的简单平坦网格。然后将这些顶点操纵以创建一个简单的地形,某些区域像高原一样抬高和平坦,并且这些高原通过斜坡连接到下面的区域。

问题是当我创建顶点,然后创建三角形并将其全部设置为网格时,三角形将不允许修改地形,因为如果某些顶点在y轴上移动,则它不会产生平坦像我需要的那样,高原带有对称的斜率。

在这种情况下,图片会说一百万个单词。

我不想要的:

您可以看到斜坡三角形的左侧与右侧的左侧不同,在左侧和右侧的图像中,有相同的三角形来创建斜率的外部。这就是我试图实现的目标。

错误的三角形的另一个角度,在这里,您可以在这里清楚地覆盖每一侧,在斜坡的侧面。

////////////////////////////////////////

我想要什么: ”

我知道差异看起来很小但是,当创建这些随机的平坦高原时,我试图使外观平滑地抬起三角形的平坦网格。

我认为实现正确结果的解决方案可能是首先创建四边形,根据需要提高顶点以制作平坦的平原,然后以使网格对称和良好外观的方式将所有四方转换为三角形。

这是我的代码:

public class GridMeshGenerator : MonoBehaviour
{
    public int xSize, zSize, totalVertices;
    public int[] plateauSizes;

    public float plateauHeight;

    public List<int> plateauVertexList;
    int[] plateauVertexArray;


    Mesh mesh;
private void Start()
{
    totalVertices = (xSize + 1) * (zSize + 1);
    GeneratePlateauVertexList();
    GenerateGridMesh();
}

void GeneratePlateauVertexList()
{
    plateauVertexList = new List<int>();
    var plateaus = 3;

    for (int j = 0; j < plateaus; j++)
    {
        var rndSize = plateauSizes[Random.Range(0, plateauSizes.Length)];
        var rndVertex = Random.Range((xSize * 2) + 2, totalVertices - ((zSize * rndSize) + 2));

        plateauVertexArray = new int[rndSize * rndSize];

        for (int k = 0, i = 0; k < rndSize; k++)
        {
            for (int l = 0; l < rndSize; l++, i++)
            {
                plateauVertexArray[i] = (rndVertex + ((xSize + 1) * k)) + l;

                if (!plateauVertexList.Contains(plateauVertexArray[i]))
                    plateauVertexList.Add(plateauVertexArray[i]);
            }
        }
    }
}

void GenerateGridMesh()
{
    GetComponent<MeshFilter>().mesh = mesh = new Mesh();
    mesh.name = "Procedural Grid";


    var vertices = new Vector3[totalVertices];
    for (int i = 0, z = 0; z <= zSize; z++)
    {
        for (int x = 0; x <= xSize; x++, i++)
        {
            vertices[i] = new Vector3(x, 0, z);
        }
    }

    foreach (int ii in plateauVertexList)
    {
        vertices[ii] = new Vector3(vertices[ii].x, plateauHeight, vertices[ii].z);
    }



    mesh.vertices = vertices;



    int[] triangles = new int[xSize * zSize * 6];
    for (int ti = 0, vi = 0, z = 0; z < zSize; z++, vi++)
    {
        for (int x = 0; x < xSize; x++, ti += 6, vi++)
        {
            triangles[ti] = vi;
            triangles[ti + 3] = triangles[ti + 2] = vi + 1;
            triangles[ti + 4] = triangles[ti + 1] = vi + xSize + 1;
            triangles[ti + 5] = vi + xSize + 2;
            
            //triangles[ti + 0] = vi + 0;
            //triangles[ti + 1] = vi + xSize + 1;
            //triangles[ti + 2] = vi + 1;
            //triangles[ti + 3] = vi + 1;
            //triangles[ti + 4] = vi + xSize + 1;
            //triangles[ti + 5] = vi + xSize + 2;
        }
    }
    mesh.triangles = triangles;
}

}

这最后一部分我想我需要更改三角形的制作方式,高原的某些角落应该与其他三角形的连接不同,但是我要拉出头发,试图弄清楚如何检查哪个顶点从列表中是一个角落,然后是哪个角落以及应连接三角形的方式。我迷路了,请帮忙!

多谢。

My goal is to create a simple flat mesh with a grid of vertices. These vertices are then manipulated to create a simple terrain with some areas being raised and flat like a plateau, and these plateaus are connected to the areas below by slopes.

The problem is when I create the vertices, then create the triangles, and set it all up as a mesh, the triangles will not allow the terrain to be modified because if some vertices are moved up in the Y axis, then it wont produce flat plateaus with symmetrical slopes all around like I required.

In this case a picture speaks a million words.

What I dont want:
enter image description here

You can see that the left side of the slope's triangles are different than those on the right, in the image below the left and right sides have the same triangles to create the outside of the slopes. This is what im trying to achieve.

mmm
Another angle of the wrong triangles, here you can clearly seach each side is different, on the sides of the slope.

////////////////////////////////

What i want: enter image description here

I know the difference looks minimal but when creating these random flat plateaus raised over an otherwise flat grid of triangles I am trying to achieve a somewhat smooth look.

A solution I think to achieve the correct result could maybe be to create quads first, raise the vertices as desired to make random flat plateaus, and then convert all the quads to triangles in a way that will make the mesh symmetrical and good looking.

Heres my code:

public class GridMeshGenerator : MonoBehaviour
{
    public int xSize, zSize, totalVertices;
    public int[] plateauSizes;

    public float plateauHeight;

    public List<int> plateauVertexList;
    int[] plateauVertexArray;


    Mesh mesh;
private void Start()
{
    totalVertices = (xSize + 1) * (zSize + 1);
    GeneratePlateauVertexList();
    GenerateGridMesh();
}

void GeneratePlateauVertexList()
{
    plateauVertexList = new List<int>();
    var plateaus = 3;

    for (int j = 0; j < plateaus; j++)
    {
        var rndSize = plateauSizes[Random.Range(0, plateauSizes.Length)];
        var rndVertex = Random.Range((xSize * 2) + 2, totalVertices - ((zSize * rndSize) + 2));

        plateauVertexArray = new int[rndSize * rndSize];

        for (int k = 0, i = 0; k < rndSize; k++)
        {
            for (int l = 0; l < rndSize; l++, i++)
            {
                plateauVertexArray[i] = (rndVertex + ((xSize + 1) * k)) + l;

                if (!plateauVertexList.Contains(plateauVertexArray[i]))
                    plateauVertexList.Add(plateauVertexArray[i]);
            }
        }
    }
}

void GenerateGridMesh()
{
    GetComponent<MeshFilter>().mesh = mesh = new Mesh();
    mesh.name = "Procedural Grid";


    var vertices = new Vector3[totalVertices];
    for (int i = 0, z = 0; z <= zSize; z++)
    {
        for (int x = 0; x <= xSize; x++, i++)
        {
            vertices[i] = new Vector3(x, 0, z);
        }
    }

    foreach (int ii in plateauVertexList)
    {
        vertices[ii] = new Vector3(vertices[ii].x, plateauHeight, vertices[ii].z);
    }



    mesh.vertices = vertices;



    int[] triangles = new int[xSize * zSize * 6];
    for (int ti = 0, vi = 0, z = 0; z < zSize; z++, vi++)
    {
        for (int x = 0; x < xSize; x++, ti += 6, vi++)
        {
            triangles[ti] = vi;
            triangles[ti + 3] = triangles[ti + 2] = vi + 1;
            triangles[ti + 4] = triangles[ti + 1] = vi + xSize + 1;
            triangles[ti + 5] = vi + xSize + 2;
            
            //triangles[ti + 0] = vi + 0;
            //triangles[ti + 1] = vi + xSize + 1;
            //triangles[ti + 2] = vi + 1;
            //triangles[ti + 3] = vi + 1;
            //triangles[ti + 4] = vi + xSize + 1;
            //triangles[ti + 5] = vi + xSize + 2;
        }
    }
    mesh.triangles = triangles;
}

}

This last part I think I need to change how the triangles are made, some corners of the plateau should connect their triangles differently than others, but I'm pulling my hair out trying to figure out how to check which vertex from the List is a corner and then which corner and in which way the triangles should be connected. I'm lost, please help!

Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文