UV 展开运行时优化

问题描述 投票:0回答:2

我正在尝试在运行时创建 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");
}
c# unity-game-engine optimization uv-mapping
2个回答
2
投票

我强烈建议缓存

mesh.vertices
的副本。

vertices
属性的文档部分说明:

返回顶点位置的副本或分配新的顶点位置数组。

注意“返回副本”——您在循环内访问此属性 22 次,因此这将创建该数组的大约

22n / 3
副本。对于具有 30,000 个顶点的网格,后台会进行超过 200,000 次不必要的复制操作。

如果您创建一个临时数组来保存顶点数据(就像您已经使用

mesh.uvs
所做的那样),您应该会看到性能的显着提高。

您还可以检查

mesh.triangles
是否是复制操作。我预计可能是这样,但文档没有具体说明。


2
投票

这是我的代码,感谢@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;
}
© www.soinside.com 2019 - 2024. All rights reserved.