繁体   English   中英

在 Unity 中渲染网格时出现索引越界错误

[英]Index out of bounds error when rendering mesh in Unity

我正在尝试使用这些坐标统一渲染六边形
https://qph.fs.quoracdn.net/main-qimg-9ad01ef3babb64b57d378a1558f468a7

我得到的是这个错误:

Failed setting triangles. Some indices are referencing out of bounds vertices. IndexCount: 18, VertexCount: 7

任何想法这段代码有什么问题?

    void Start()
    {
        MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>();

        Mesh mesh = new Mesh();

        Vector3[] vertices = new Vector3[7]
        {
            new Vector3(0, 0),
            new Vector3(-1.0f, 0),
            new Vector3(-0.5f, Mathf.Sqrt(3/2)),
            new Vector3(0.5f, Mathf.Sqrt(3/2)),
            new Vector3(1, 0),
            new Vector3(0.5f, - Mathf.Sqrt(3/2)),
            new Vector3(-0.5f, - Mathf.Sqrt(3/2))
        };
        mesh.vertices = vertices;

        int[] tris = new int[18]
        {
            
            0, 2, 1,
            0, 3, 2,
            0, 4, 3,
            0, 5, 4,
            0, 6, 5,
            0, 7, 6
        };
        mesh.triangles = tris;

        meshFilter.mesh = mesh;
    }

最后一个三角形使用索引 7 处的顶点,该顶点不存在,因为您将六边形指定为只有 7 个点。 如果您不知道,索引从 0 开始,这就是为什么这不起作用(尽管从外观上您已经知道这一点,尽管您可能盲目地遵循教程,这就是我这么说的原因)。

它应该是 1 而不是 7,因为您必须返回并连接索引 6(最后一个索引)和第一个周边索引(索引 1)之间的三角形。

暂无
暂无

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

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