You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

地形顶点出现可见接缝问题,如何解决?

程序化地形生成接缝/裂缝问题修复

我是一名新手,正在学习程序化地形生成教程,但生成的平面存在大量可见接缝/裂缝:

程序化生成的地形

以下是我的MeshGenerator脚本:

public static class MeshGenerator
{
    public static MeshData GenerateTerrainMesh(float[,] heightMap, float heightMultiplier, AnimationCurve heightCurve)
    {
        int width = heightMap.GetLength(0);
        int height = heightMap.GetLength(1);
        float topLeftX = (width - 1) / -2f;
        float topLeftZ = (height - 1) / 2f;

        MeshData meshData = new MeshData(width, height);
        int vertexIndex = 0;
        
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                meshData.vertices[vertexIndex] = new Vector3(topLeftX + x, heightCurve.Evaluate(heightMap[x,y]) * heightMultiplier, topLeftZ - y);
                meshData.uvs[vertexIndex] = new Vector2(x / (float)width, y / (float)height);
                
                if (x < width - 1 && y < height - 1)
                {
                    meshData.AddTriangle(vertexIndex, vertexIndex + width + 1, vertexIndex + width);
                    meshData.AddTriangle(vertexIndex, + width + 1, vertexIndex + 1);
                }

                vertexIndex++;
            }
        }
        return meshData;
    }
}

public class MeshData
{
    public Vector3[] vertices;
    public int[] triangles;
    public Vector2[] uvs;

    int triangleIndex;

    public MeshData(int meshWidth, int meshHeight)
    {
        vertices = new Vector3[meshWidth * meshHeight];
        uvs = new Vector2[meshWidth * meshHeight];
        triangles = new int[(meshWidth-1) * (meshHeight-1)*6];
    }

    public void AddTriangle(int a, int b, int c)
    {
        triangles[triangleIndex] = a;
        triangles[triangleIndex+1] = b;
        triangles[triangleIndex+2] = c;
        triangleIndex += 3;
    }
    public Mesh CreateMesh()
    {
        Mesh mesh = new Mesh();
        mesh.vertices = vertices;
        mesh.triangles = triangles;
        mesh.uv = uvs;
        mesh.RecalculateNormals();
        return mesh;
    }
}

问题原因与修复

代码中第二个AddTriangle调用存在语法错误:

meshData.AddTriangle(vertexIndex, + width + 1, vertexIndex + 1);

这里的+ width +1遗漏了vertexIndex前缀,导致使用了错误的顶点索引,网格面无法正确拼接,最终出现接缝。

修正后的代码行应为:

meshData.AddTriangle(vertexIndex, vertexIndex + width + 1, vertexIndex + 1);

修正后,每个四边形单元格会被两个正确的三角形填充,网格的顶点索引逻辑恢复正常,接缝问题即可解决。

内容的提问来源于stack exchange,提问作者Nicolai H.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 12:30:53