我正在尝试创建UV @运行时,我正在使用BOX类型的UV(类似于3ds max中的BOX UVW)并基于面部方向的计算。
我知道将它创建为运行时不是一个好选择,但我别无选择:(它在计算后保存,所以我做了一次。
但我需要40秒才能获得30000个顶点......太长了
我的代码中是否有任何可以优化的优化?
如果你有< 5000 vertices Mesh:
,我的代码可以随意使用public static void CreateUV(ref Mesh mesh)
{
int i = 0;
Vector3 p = Vector3.up;
Vector3 u = Vector3.Cross(p, Vector3.forward);
if (Vector3.Dot(u, u) < 0.001f)
{
u = Vector3.right;
}
else
{
u = Vector3.Normalize(u);
}
Vector3 v = Vector3.Normalize(Vector3.Cross(p, u));
Vector2[] uvs = new Vector2[mesh.vertices.Length];
for (i = 0; i < mesh.triangles.Length; i += 3)
{
Vector3 a = mesh.vertices[mesh.triangles[i]];
Vector3 b = mesh.vertices[mesh.triangles[i + 1]];
Vector3 c = mesh.vertices[mesh.triangles[i + 2]];
Vector3 side1 = b - a;
Vector3 side2 = c - a;
Vector3 N = Vector3.Cross(side1, side2);
N = new Vector3(Mathf.Abs(N.normalized.x), Mathf.Abs(N.normalized.y), Mathf.Abs(N.normalized.z));
if (N.x > N.y && N.x > N.z)
{
uvs[mesh.triangles[i]] = new Vector2(mesh.vertices[mesh.triangles[i]].z, mesh.vertices[mesh.triangles[i]].y);
uvs[mesh.triangles[i + 1]] = new Vector2(mesh.vertices[mesh.triangles[i + 1]].z, mesh.vertices[mesh.triangles[i + 1]].y);
uvs[mesh.triangles[i + 2]] = new Vector2(mesh.vertices[mesh.triangles[i + 2]].z, mesh.vertices[mesh.triangles[i + 2]].y);
}
else if (N.y > N.x && N.y > N.z)
{
uvs[mesh.triangles[i]] = new Vector2(mesh.vertices[mesh.triangles[i]].x, mesh.vertices[mesh.triangles[i]].z);
uvs[mesh.triangles[i + 1]] = new Vector2(mesh.vertices[mesh.triangles[i + 1]].x, mesh.vertices[mesh.triangles[i + 1]].z);
uvs[mesh.triangles[i + 2]] = new Vector2(mesh.vertices[mesh.triangles[i + 2]].x, mesh.vertices[mesh.triangles[i + 2]].z);
}
else if (N.z > N.x && N.z > N.y)
{
uvs[mesh.triangles[i]] = new Vector2(mesh.vertices[mesh.triangles[i]].x, mesh.vertices[mesh.triangles[i]].y);
uvs[mesh.triangles[i + 1]] = new Vector2(mesh.vertices[mesh.triangles[i + 1]].x, mesh.vertices[mesh.triangles[i + 1]].y);
uvs[mesh.triangles[i + 2]] = new Vector2(mesh.vertices[mesh.triangles[i + 2]].x, mesh.vertices[mesh.triangles[i + 2]].y);
}
}
mesh.uv = uvs;
Debug.Log("Finish");
}
答案 0 :(得分:1)
我强烈建议缓存mesh.vertices
的副本。
vertices
属性的文档部分说明:
返回顶点位置的副本或指定新的顶点位置数组。
注意“返回副本” - 您在循环内访问此属性22次,因此这将创建该数组的大约22n / 3
个副本。对于具有30,000个顶点的网格,这是在后台进行的超过200,000次不必要的复制操作。
如果您创建一个临时数组来保存顶点数据(就像您已经使用mesh.uvs
一样),您应该会看到显着的性能提升。
您还可以检查mesh.triangles
是否为复制操作。我预计它可能会,但文档没有指定。
答案 1 :(得分:0)
这是我的代码优化,感谢@rutter
public static Vector2[] CreateUV(ref Mesh mesh)
{
int i = 0;
Vector3 p = Vector3.up;
Vector3 u = Vector3.Cross(p, Vector3.forward);
if (Vector3.Dot(u, u) < 0.001f)
{
u = Vector3.right;
}
else
{
u = Vector3.Normalize(u);
}
Vector3 v = Vector3.Normalize(Vector3.Cross(p, u));
Vector3[] vertexs = mesh.vertices;
int[] tris = mesh.triangles;
Vector2[] uvs = new Vector2[vertexs.Length];
for (i = 0; i < tris.Length; i += 3)
{
Vector3 a = vertexs[tris[i]];
Vector3 b = vertexs[tris[i + 1]];
Vector3 c = vertexs[tris[i + 2]];
Vector3 side1 = b - a;
Vector3 side2 = c - a;
Vector3 N = Vector3.Cross(side1, side2);
N = new Vector3(Mathf.Abs(N.normalized.x), Mathf.Abs(N.normalized.y), Mathf.Abs(N.normalized.z));
if (N.x > N.y && N.x > N.z)
{
uvs[tris[i]] = new Vector2(vertexs[tris[i]].z, vertexs[tris[i]].y);
uvs[tris[i + 1]] = new Vector2(vertexs[tris[i + 1]].z, vertexs[tris[i + 1]].y);
uvs[tris[i + 2]] = new Vector2(vertexs[tris[i + 2]].z, vertexs[tris[i + 2]].y);
}
else if (N.y > N.x && N.y > N.z)
{
uvs[tris[i]] = new Vector2(vertexs[tris[i]].x, vertexs[tris[i]].z);
uvs[tris[i + 1]] = new Vector2(vertexs[tris[i + 1]].x, vertexs[tris[i + 1]].z);
uvs[tris[i + 2]] = new Vector2(vertexs[tris[i + 2]].x, vertexs[tris[i + 2]].z);
}
else if (N.z > N.x && N.z > N.y)
{
uvs[tris[i]] = new Vector2(vertexs[tris[i]].x, vertexs[tris[i]].y);
uvs[tris[i + 1]] = new Vector2(vertexs[tris[i + 1]].x, vertexs[tris[i + 1]].y);
uvs[tris[i + 2]] = new Vector2(vertexs[tris[i + 2]].x, vertexs[tris[i + 2]].y);
}
}
mesh.uv = uvs;
return uvs;
}