using System; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace LibMMD.Unity3D { /// Owns main-thread Unity Mesh creation from background-prepared model data. internal static class MmdMeshBuilder { internal static IEnumerator BuildIncremental(ModelLoadPreparer.Result prepared, MmdModelOptions options, Action singleMeshCreated, Action, List>> partMeshesCreated) { var slice = new MmdLoadTimeSlicer(options); if (!prepared.UsesParts) { var mesh = new Mesh(); singleMeshCreated(mesh); mesh.vertices = prepared.Vertices; if (slice.ShouldYield()) yield return null; mesh.normals = prepared.Normals; if (slice.ShouldYield()) yield return null; mesh.uv = prepared.Uv; if (slice.ShouldYield()) yield return null; mesh.boneWeights = prepared.BoneWeights; if (slice.ShouldYield()) yield return null; mesh.subMeshCount = prepared.SubMeshTriangles.Length; for (var i = 0; i < prepared.SubMeshTriangles.Length; i++) { mesh.SetTriangles(prepared.SubMeshTriangles[i], i); if (slice.ShouldYield()) yield return null; } mesh.RecalculateBounds(); yield break; } var meshes = new List(); var indexes = new List>(); partMeshesCreated(meshes, indexes); foreach (var part in prepared.Parts) { var mesh = new Mesh(); meshes.Add(mesh); indexes.Add(part.VertexIndexes); mesh.vertices = part.Vertices; if (slice.ShouldYield()) yield return null; mesh.normals = part.Normals; if (slice.ShouldYield()) yield return null; mesh.uv = part.Uv; if (slice.ShouldYield()) yield return null; mesh.boneWeights = part.BoneWeights; if (slice.ShouldYield()) yield return null; mesh.triangles = part.Triangles; mesh.RecalculateBounds(); if (slice.ShouldYield()) yield return null; } } } }