簡體   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