Small MeshSimplifier optimalization.

List -> Array.
Skip LOD when quality is 1.
This commit is contained in:
max 2021-02-18 17:35:05 +01:00
parent bc8aa08193
commit 43049094af
3 changed files with 27 additions and 22 deletions

View File

@ -1,7 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEditor; using UnityEditor;
using System.Linq;
namespace TAO.VertexAnimation.Editor namespace TAO.VertexAnimation.Editor
{ {

View File

@ -11,7 +11,13 @@ namespace TAO.VertexAnimation
for (int lm = 0; lm < lodMeshes.Length; lm++) for (int lm = 0; lm < lodMeshes.Length; lm++)
{ {
lodMeshes[lm] = mesh.Copy(); 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); lodMeshes[lm].name = string.Format("{0}_LOD{1}", lodMeshes[lm].name, lm);
} }

View File

@ -20,8 +20,8 @@ namespace TAO.VertexAnimation
// UVs (UV0, UV1, ..., UV7) // UVs (UV0, UV1, ..., UV7)
// Other... // Other...
public List<Vector3> vertices = new List<Vector3>(3); public Vector3[] vertices = new Vector3[3];
public List<Vector3> normals = new List<Vector3>(3); public Vector3[] normals = new Vector3[3];
public Dictionary<int, List<Vector2>> uvs = new Dictionary<int, List<Vector2>>(); public Dictionary<int, List<Vector2>> uvs = new Dictionary<int, List<Vector2>>();
public float Perimeter() public float Perimeter()
@ -46,7 +46,7 @@ namespace TAO.VertexAnimation
float distance = Mathf.Infinity; float distance = Mathf.Infinity;
int closestVertex = -1; int closestVertex = -1;
for (int v = 0; v < vertices.Count; v++) for (int v = 0; v < vertices.Length; v++)
{ {
if (vertices[v] != vertex) if (vertices[v] != vertex)
{ {
@ -83,7 +83,7 @@ namespace TAO.VertexAnimation
if (sourceTriangle != this) if (sourceTriangle != this)
{ {
Vector3 sourceVertex = sourceTriangle.vertices[sourceVertexIndex]; Vector3 sourceVertex = sourceTriangle.vertices[sourceVertexIndex];
int index = vertices.IndexOf(sourceVertex); int index = System.Array.IndexOf(vertices, sourceVertex);
if (index != -1) if (index != -1)
{ {
@ -155,10 +155,10 @@ namespace TAO.VertexAnimation
public static List<Triangle> ToTriangles(this Mesh mesh) public static List<Triangle> ToTriangles(this Mesh mesh)
{ {
List<Triangle> triangles = new List<Triangle>(); List<Triangle> triangles = new List<Triangle>();
List<Vector3> verts = new List<Vector3>(mesh.vertices); Vector3[] verts = mesh.vertices;
List<Vector3> normals = new List<Vector3>(mesh.normals); Vector3[] normals = mesh.normals;
List<int> tris = new List<int>(mesh.triangles); int[] tris = mesh.triangles;
Dictionary<int, List<Vector2>> uvs = new Dictionary<int, List<Vector2>>(); Dictionary<int, List<Vector2>> uvs = new Dictionary<int, List<Vector2>>();
for (int u = 0; u < 8; u++) 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(); Triangle tri = new Triangle();
tri.vertices.Add(verts[tris[t + 0]]); tri.vertices[0] = verts[tris[t + 0]];
tri.vertices.Add(verts[tris[t + 1]]); tri.vertices[1] = verts[tris[t + 1]];
tri.vertices.Add(verts[tris[t + 2]]); tri.vertices[2] = 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.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) foreach (var uv in uvs)
{ {
if (tri.uvs.TryGetValue(uv.Key, out List<Vector2> coordinates)) if (tri.uvs.TryGetValue(uv.Key, out List<Vector2> coordinates))
@ -215,14 +215,14 @@ namespace TAO.VertexAnimation
mesh.Clear(); mesh.Clear();
List<Vector3> vertices = new List<Vector3>(triangles.Count * 3); List<Vector3> vertices = new List<Vector3>(triangles.Count * 3);
List<int> tris = new List<int>(triangles.Count * 3);
List<Vector3> normals = new List<Vector3>(triangles.Count * 3); List<Vector3> normals = new List<Vector3>(triangles.Count * 3);
List<int> tris = new List<int>(triangles.Count * 3);
Dictionary<int, List<Vector2>> uvs = new Dictionary<int, List<Vector2>>(); Dictionary<int, List<Vector2>> uvs = new Dictionary<int, List<Vector2>>();
int skipped = 0; int skipped = 0;
for (int t = 0; t < triangles.Count; t++) 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. // Check for existing matching vert.
int vIndex = vertices.IndexOf(triangles[t].vertices[v]); int vIndex = vertices.IndexOf(triangles[t].vertices[v]);
@ -275,7 +275,7 @@ namespace TAO.VertexAnimation
{ {
mesh.SetUVs(uv.Key, uv.Value); mesh.SetUVs(uv.Key, uv.Value);
} }
mesh.triangles = tris.ToArray(); mesh.triangles = tris.ToArray();
mesh.Optimize(); mesh.Optimize();