繁体   English   中英

这个整数数组的长度是 0?

[英]The length of this integer array is 0?

我一直在尝试使用代码在 Unity 中创建平面网格,但遇到了一个非常有趣的问题。 我创建了一个int[] ,用一些值填充它,它的长度在某种程度上为零。 我从来没有遇到过这么古怪的事情,所以我希望得到一些帮助。

mesh.triangles = new int[]
{
    4, 6, 5, 5, 6, 7
};
... // Not important stuff
Debug.Log(mesh.triangles.Length);

我不知道发生了什么,所以我真的没有尝试过任何东西。 但是在控制台中,有一条错误消息指出Failed setting triangles. Some indices are referencing out of bounds vertices. IndexCount: 6, VertexCount: 4 Failed setting triangles. Some indices are referencing out of bounds vertices. IndexCount: 6, VertexCount: 4 Failed setting triangles. Some indices are referencing out of bounds vertices. IndexCount: 6, VertexCount: 4 。这可能真的很重要,但我不明白消息的某些部分(尤其是最后一部分)。 如果它有所不同,我会调用一个数组连接方法来将第一个三角形添加到这些三角形中。 当我的一半网格仍未出现时,我最初发现了这个问题。 我真的很感激帮助; 谢谢。

编辑:为了避免混淆,我将粘贴我的整个方法。

private void CreateQuad(ref Mesh mesh, Vector3 offset, bool first)
    {
        if (first)
        {
            mesh.vertices = new Vector3[]
            {
                Vector3.zero, Vector3.right, Vector3.forward, new Vector3(1, 0, 1)
            };
            mesh.triangles = new int[]
            {
                0, 2, 1, 1, 2, 3
            };
            mesh.normals = new Vector3[]
            {
                Vector3.back, Vector3.back, Vector3.back, Vector3.back
            };
            mesh.tangents = new Vector4[]
            {
                new Vector4(1, 0, 0, -1),
                new Vector4(1, 0, 0, -1),
                new Vector4(1, 0, 0, -1),
                new Vector4(1, 0, 0, -1)
            };
            mesh.uv = new Vector2[]
            {
                Vector2.zero, Vector2.right, Vector2.up, Vector2.one
            };
        }
        else if (!first)
        {
            mesh.vertices = new Vector3[]
            {
                Vector3.zero + offset,
                Vector3.right + offset,
                Vector3.forward + offset,
                new Vector3(1, 0, 1) + offset
            };
            mesh.triangles = new int[]
            {
                4, 6, 5, 5, 6, 7
            };
            mesh.normals = new Vector3[]
            {
                Vector3.back, Vector3.back, Vector3.back, Vector3.back
            };
            mesh.tangents = new Vector4[]
            {
                new Vector4(1, 0, 0, -1),
                new Vector4(1, 0, 0, -1),
                new Vector4(1, 0, 0, -1),
                new Vector4(1, 0, 0, -1)
            };
            mesh.uv = new Vector2[]
            {
                Vector2.zero, Vector2.right, Vector2.up, Vector2.one
            };
            Debug.Log(mesh.triangles.Length);
        }
    }

您首先需要在更改三角形之前设置顶点数组。 正如Unity所写“建议在分配顶点数组后分配一个三角形数组,以避免越界错误。”

mesh.vertices = new Vector3[] { new Vector3(-1,0,1), new Vector3(-1,0,-1),
    new Vector3(1,0,-1), new Vector3(1,0,1) };
mesh.triangles = new int[] {0,1,2,0,2,3};

你只有四个顶点!

mesh.vertices = new Vector3[]
{
    Vector3.zero + offset,
    Vector3.right + offset,
    Vector3.forward + offset,
    new Vector3(1, 0, 1) + offset
};

所以索引4, 6, 5, 5, 6, 7都是无效的 如果您只有四个顶点,您最多可以拥有索引0, 1, 2, 3

=> Unity 只是拒绝所有这些。 你应该已经从你得到的错误中得到了这个提示

设置三角形失败。 一些索引引用越界顶点 索引计数:6,顶点计数:4


现在有点不清楚你到底想在这里实现什么,但是

  • 要么你想替换顶点:在这种情况下,根本没有理由设置新的三角形实例等! 将它们连接一次就足够了:

     private void CreateQuad(ref Mesh mesh, Vector3 offset, bool first) { if (first) { mesh.vertices = new Vector3[] { Vector3.zero, Vector3.right, Vector3.forward, new Vector3(1, 0, 1) }; mesh.triangles = new int[] { 0, 2, 1, 1, 2, 3 }; mesh.normals = new Vector3[] { Vector3.back, Vector3.back, Vector3.back, Vector3.back }; mesh.tangents = new Vector4[] { new Vector4(1, 0, 0, -1), new Vector4(1, 0, 0, -1), new Vector4(1, 0, 0, -1), new Vector4(1, 0, 0, -1) }; mesh.uv = new Vector2[] { Vector2.zero, Vector2.right, Vector2.up, Vector2.one }; } else if (!first) { mesh.vertices = new Vector3[] { Vector3.zero + offset, Vector3.right + offset, Vector3.forward + offset, new Vector3(1, 0, 1) + offset }; } }

    其他属性可以简单地保持不变,因为您只想更新顶点位置。

  • 或者您实际上想添加更多面孔。 在这种情况下,您宁愿附加到现有数组:

     private void CreateQuad(ref Mesh mesh, Vector3 offset, bool first) { if (first) { mesh.vertices = new Vector3[] { Vector3.zero, Vector3.right, Vector3.forward, new Vector3(1, 0, 1) }; mesh.triangles = new int[] { 0, 2, 1, 1, 2, 3 }; mesh.normals = new Vector3[] { Vector3.back, Vector3.back, Vector3.back, Vector3.back }; mesh.tangents = new Vector4[] { new Vector4(1, 0, 0, -1), new Vector4(1, 0, 0, -1), new Vector4(1, 0, 0, -1), new Vector4(1, 0, 0, -1) }; mesh.uv = new Vector2[] { Vector2.zero, Vector2.right, Vector2.up, Vector2.one }; } else if (!first) { // fist get already existing verts etc var oldVerts = mesh.vertices; var oldTris = mesh.triangles; // create new vertices and triangles arrays with additional space for the new quad var newVerts = new Vector3[oldVerts.Length + 4]; var newTris = new int[oldTris.Length + 6]; // copy over the existing vertices and triangles Array.Copy(oldVerts, newVerts, olVerts.Length); Array.Copy(oldTris, newtris, oldtris.Length); // then append the new vertices newVerts[oldverts.Length + 0] = Vector3.zero + offset; newVerts[oldverts.Length + 1] = Vector3.right + offset; newVerts[oldverts.Length + 2] = Vector3.forward + offset; newVerts[oldverts.Length + 3] = new Vector3(1, 0, 1) + offset; // append the new triangles newTris[oldTris.Length + 0] = oldverts.Length + 0; newTris[oldTris.Length + 1] = oldverts.Length + 2; newTris[oldTris.Length + 2] = oldverts.Length + 1; newTris[oldTris.Length + 3] = oldverts.Length + 1; newTris[oldTris.Length + 4] = oldverts.Length + 2; newTris[oldTris.Length + 5] = oldverts.Length + 3; // get the min and max points for filling the uvs (not the most efficient way probably but it is what it is ^^) // we later want to spread out the UV values linear between 0 (min) and 1 (max) on the given vertices var min = Vector3.zero; var max = Vector3.zero; foreach(var vertex in newVerts) { min = Vector3.Min(min, vertex); max = Vector3.Max(max, vertex); } // also fill new tangents and normals and uvs (if really necessary) var newNormals = new Vector3[newVerts.Length]; var newTangents = new Vector4[newVerts.Length]; var newUVs = new Vector2[newVerts.Length]; for(var i = 0; i < newVerts.Length; i++) { var vertex = newVerts[i]; newUVs[i] = new Vector2((vertex.x - min.x) / (max.x - min.x), (vertex.z - min.z) / (max.z - min.z)); newNormals[i] = Vector3.back; newTangents[i] = new Vector4(1, 0, 0, -1); }; // finally set them all back mesh.vertices = newVerts; mesh.triangles = newTris; mesh.normals = newNormals; mesh.tangents = newTangents; mesh.uv = newUs; } }

暂无
暂无

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

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