在Unity中创建过程网格时,规范化值存在问题。我一直在关注catlikecoding中的精彩教程,当我尝试对顶点使用归一化值时,我陷入了奇怪的行为。在某些情况下,xSize和ySize网格组合均可使用,但在其他组合中,网格会变形。让我给你举几个例子
我用头球表示每个第10个顶点的前2种情况。 35x25 case 350x250 case
我正在使用Unity3d 2018.3
private void Generate()
{
GetComponent<MeshFilter>().mesh = mesh = new Mesh();
mesh.name = "Procedural Grid";
vertices = new Vector3[(xSize + 1) * (ySize + 1)];
Vector2[] uv = new Vector2[vertices.Length];
float multX = 1 / (float)xSize;
float multY = 1 / (float)ySize;
for (int i = 0, y = 0; y <= ySize; y++)
{
for (int x = 0; x <= xSize; x++, i++)
{
//vertices[i] = new Vector3(x, y);
var xNormalized = x * multX;
var yNormalized = y * multY;
vertices[i] = new Vector3(xNormalized, yNormalized);
uv[i] = new Vector2(xNormalized, yNormalized);
}
}
mesh.vertices = vertices;
mesh.uv = uv;
var triangles = new int[xSize * ySize * 6];
for (int ti = 0, vi = 0, y = 0; y < ySize; y++, vi++)
{
for (int x = 0; x < xSize; x++, ti += 6, vi++)
{
triangles[ti] = vi;
triangles[ti + 3] = triangles[ti + 2] = vi + 1;
triangles[ti + 4] = triangles[ti + 1] = vi + xSize + 1;
triangles[ti + 5] = vi + xSize + 2;
}
}
mesh.triangles = triangles;
mesh.RecalculateNormals();
}
我希望无论哪种情况,无论我使用网格的xSize或ySize,网格都是1x1。任何人都可以建议如何实现这一目标?
答案 0 :(得分:0)
所以我的朋友向我解释,默认情况下,Unity中的网格的顶点限制为65535。我必须很好地询问我是否想要更多。 我必须添加
mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
之后
mesh.name = "Procedural Grid";
here还有更多..
突然所有工作都按预期进行。谢谢大家的支持。