From 43049094afcb8940fe9213d0321ab60cce840b2f Mon Sep 17 00:00:00 2001 From: max Date: Thu, 18 Feb 2021 17:35:05 +0100 Subject: [PATCH] Small MeshSimplifier optimalization. List -> Array. Skip LOD when quality is 1. --- Editor/Scripts/ModelBaker/VA_ModelBaker.cs | 1 - .../Scripts/ModelBaker/MeshLodGenerator.cs | 8 +++- Runtime/Scripts/ModelBaker/MeshSimplifier.cs | 40 +++++++++---------- 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/Editor/Scripts/ModelBaker/VA_ModelBaker.cs b/Editor/Scripts/ModelBaker/VA_ModelBaker.cs index e1a2749..babc65c 100644 --- a/Editor/Scripts/ModelBaker/VA_ModelBaker.cs +++ b/Editor/Scripts/ModelBaker/VA_ModelBaker.cs @@ -1,7 +1,6 @@ using System.Collections.Generic; using UnityEngine; using UnityEditor; -using System.Linq; namespace TAO.VertexAnimation.Editor { diff --git a/Runtime/Scripts/ModelBaker/MeshLodGenerator.cs b/Runtime/Scripts/ModelBaker/MeshLodGenerator.cs index e6ba71d..723fb1d 100644 --- a/Runtime/Scripts/ModelBaker/MeshLodGenerator.cs +++ b/Runtime/Scripts/ModelBaker/MeshLodGenerator.cs @@ -11,7 +11,13 @@ namespace TAO.VertexAnimation for (int lm = 0; lm < lodMeshes.Length; lm++) { lodMeshes[lm] = mesh.Copy(); - lodMeshes[lm] = lodMeshes[lm].Simplify(quality[lm]); + + // Only simplify when needed. + if (quality[lm] < 1.0f) + { + lodMeshes[lm] = lodMeshes[lm].Simplify(quality[lm]); + } + lodMeshes[lm].name = string.Format("{0}_LOD{1}", lodMeshes[lm].name, lm); } diff --git a/Runtime/Scripts/ModelBaker/MeshSimplifier.cs b/Runtime/Scripts/ModelBaker/MeshSimplifier.cs index e1ab8cb..d7cccd3 100644 --- a/Runtime/Scripts/ModelBaker/MeshSimplifier.cs +++ b/Runtime/Scripts/ModelBaker/MeshSimplifier.cs @@ -20,8 +20,8 @@ namespace TAO.VertexAnimation // UVs (UV0, UV1, ..., UV7) // Other... - public List vertices = new List(3); - public List normals = new List(3); + public Vector3[] vertices = new Vector3[3]; + public Vector3[] normals = new Vector3[3]; public Dictionary> uvs = new Dictionary>(); public float Perimeter() @@ -46,7 +46,7 @@ namespace TAO.VertexAnimation float distance = Mathf.Infinity; int closestVertex = -1; - for (int v = 0; v < vertices.Count; v++) + for (int v = 0; v < vertices.Length; v++) { if (vertices[v] != vertex) { @@ -83,7 +83,7 @@ namespace TAO.VertexAnimation if (sourceTriangle != this) { Vector3 sourceVertex = sourceTriangle.vertices[sourceVertexIndex]; - int index = vertices.IndexOf(sourceVertex); + int index = System.Array.IndexOf(vertices, sourceVertex); if (index != -1) { @@ -155,10 +155,10 @@ namespace TAO.VertexAnimation public static List ToTriangles(this Mesh mesh) { List triangles = new List(); - - List verts = new List(mesh.vertices); - List normals = new List(mesh.normals); - List tris = new List(mesh.triangles); + + Vector3[] verts = mesh.vertices; + Vector3[] normals = mesh.normals; + int[] tris = mesh.triangles; Dictionary> uvs = new Dictionary>(); for (int u = 0; u < 8; u++) @@ -172,18 +172,18 @@ namespace TAO.VertexAnimation } } - for (int t = 0; t < tris.Count; t += 3) + for (int t = 0; t < tris.Length; t += 3) { Triangle tri = new Triangle(); - tri.vertices.Add(verts[tris[t + 0]]); - tri.vertices.Add(verts[tris[t + 1]]); - tri.vertices.Add(verts[tris[t + 2]]); - - tri.normals.Add(normals[tris[t + 0]]); - tri.normals.Add(normals[tris[t + 1]]); - tri.normals.Add(normals[tris[t + 2]]); + tri.vertices[0] = verts[tris[t + 0]]; + tri.vertices[1] = verts[tris[t + 1]]; + tri.vertices[2] = verts[tris[t + 2]]; + tri.normals[0] = normals[tris[t + 0]]; + tri.normals[1] = normals[tris[t + 1]]; + tri.normals[2] = normals[tris[t + 2]]; + foreach (var uv in uvs) { if (tri.uvs.TryGetValue(uv.Key, out List coordinates)) @@ -215,14 +215,14 @@ namespace TAO.VertexAnimation mesh.Clear(); List vertices = new List(triangles.Count * 3); - List tris = new List(triangles.Count * 3); List normals = new List(triangles.Count * 3); + List tris = new List(triangles.Count * 3); Dictionary> uvs = new Dictionary>(); - + int skipped = 0; for (int t = 0; t < triangles.Count; t++) { - for (int v = 0; v < triangles[t].vertices.Count; v++) + for (int v = 0; v < triangles[t].vertices.Length; v++) { // Check for existing matching vert. int vIndex = vertices.IndexOf(triangles[t].vertices[v]); @@ -275,7 +275,7 @@ namespace TAO.VertexAnimation { mesh.SetUVs(uv.Key, uv.Value); } - + mesh.triangles = tris.ToArray(); mesh.Optimize();