运行时优化中的UV解包 [英] UV unwrap on Runtime optimization
问题描述
我正在尝试在运行时创建UV,我正在使用BOX类型的UV(类似于3ds max中的BOX UVW),并根据脸部方向进行计算.
I'm trying to create UVs @ runtime, I'm using a BOX type UVs (similar to BOX UVW in 3ds max) and based my calculation on the face orientation.
我知道创建一个运行时不是一个好选择,但是我别无选择:(它在计算后保存,所以我做了一次.
I know it's not a good option to create it a runtime but I have no choice :( it's saved after calculation, so I made it once.
但是我需要40秒才能获得30000个顶点...太长了
BUT I take 40 seconds for a 30000 Vertices... too long
我的代码中可以进行任何优化吗?
Is there any optimization in my code that can be made?
.
如果您有< 5000顶点网格:
Here is my code feel free to use if you have <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");
}
推荐答案
我强烈建议缓存mesh.vertices
的副本.
I would strongly recommend caching a copy of mesh.vertices
.
vertices
属性的文档部分指出:>
返回顶点位置的副本或分配新的顶点位置数组.
Returns a copy of the vertex positions or assigns a new vertex positions array.
请注意返回副本"-您在循环内访问此属性22次,因此将大致创建该数组的22n / 3
个副本.对于具有30,000个顶点的网格,在后台进行的复制操作超过200,000.
Note "returns a copy" -- you access this property 22 times inside your loop, so this will create roughly 22n / 3
copies of that array. For a mesh with 30,000 vertices, that's over 200,000 unnecessary copy operations going on in the background.
如果创建一个临时数组来保存顶点数据(就像您已经在使用mesh.uvs
一样),您应该会看到性能上的显着提高.
If you create a temporary array to hold vertex data (like you're already doing with mesh.uvs
), you should see a dramatic performance improvement.
您还可以检查mesh.triangles
是否为复制操作.我预计可能会这样,但是文档未指定.
You might also check if mesh.triangles
is a copy operation. I anticipate that it could be, but the docs don't specify.
这篇关于运行时优化中的UV解包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!