[英]The length of this integer array is 0?
I've been trying to create a plane mesh in Unity using code, and I've come across a very interesting problem.我一直在尝试使用代码在 Unity 中创建平面网格,但遇到了一个非常有趣的问题。 I created an
int[]
, filled it up with some values, and it's length is somehow zero.我创建了一个
int[]
,用一些值填充它,它的长度在某种程度上为零。 I've never encountered anything this quirky, so I'd enjoy a bit of help.我从来没有遇到过这么古怪的事情,所以我希望得到一些帮助。
mesh.triangles = new int[]
{
4, 6, 5, 5, 6, 7
};
... // Not important stuff
Debug.Log(mesh.triangles.Length);
I don't know what is happening, so I really haven't tried anything.我不知道发生了什么,所以我真的没有尝试过任何东西。 But in the console, there is an error message stating
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
Failed setting triangles. Some indices are referencing out of bounds vertices. IndexCount: 6, VertexCount: 4
.This is probably really important, but I don't understand some parts of the message(especially the last part). Failed setting triangles. Some indices are referencing out of bounds vertices. IndexCount: 6, VertexCount: 4
。这可能真的很重要,但我不明白消息的某些部分(尤其是最后一部分)。 And if it makes a difference, I have an array concatenation method being called to add the first triangles to these ones.如果它有所不同,我会调用一个数组连接方法来将第一个三角形添加到这些三角形中。 I initially identified this problem when the half of my mesh still wasn't appearing.
当我的一半网格仍未出现时,我最初发现了这个问题。 I would really appreciate help;
我真的很感激帮助; thanks.
谢谢。
Edit: To cut confusion, I'm just going to paste my whole entire method.编辑:为了避免混淆,我将粘贴我的整个方法。
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);
}
}
You first need to set the vertex array before altering the triangles.您首先需要在更改三角形之前设置顶点数组。 As Unity writes "It is recommended to assign a triangle array after assigning the vertex array, in order to avoid out of bounds errors."
正如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};
You only have FOUR vertices!你只有四个顶点!
mesh.vertices = new Vector3[]
{
Vector3.zero + offset,
Vector3.right + offset,
Vector3.forward + offset,
new Vector3(1, 0, 1) + offset
};
So the indices 4, 6, 5, 5, 6, 7
are all invalid !所以索引
4, 6, 5, 5, 6, 7
都是无效的! If you have only four vertices you can maximum have the indices 0, 1, 2, 3
如果您只有四个顶点,您最多可以拥有索引
0, 1, 2, 3
=> Unity simply rejects them all. => Unity 只是拒绝所有这些。 You should have already taken that hint from the error you get
你应该已经从你得到的错误中得到了这个提示
Failed setting triangles.
设置三角形失败。 Some indices are referencing out of bounds vertices .
一些索引引用越界顶点。 IndexCount: 6, VertexCount: 4
索引计数:6,顶点计数:4
Now it is a bit unclear what exactly you are trying to achieve here but现在有点不清楚你到底想在这里实现什么,但是
either you want to REPLACE the vertices: In this case there is no reason to set new triangle instances etc at all!要么你想替换顶点:在这种情况下,根本没有理由设置新的三角形实例等! It is enough to connect them only once:
将它们连接一次就足够了:
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 }; } }
the other properties can simply be left untouched since you only want to update the vertex positions.其他属性可以简单地保持不变,因为您只想更新顶点位置。
Or you actually wanted to ADD more faces.或者您实际上想添加更多面孔。 In that case you rather want to append to the existing arrays:
在这种情况下,您宁愿附加到现有数组:
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.