From fe9ae881956770b9ffc77d790f55b3cf1df09ea8 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 9 Sep 2021 14:54:35 +0900 Subject: [PATCH] CreateErasedMeshAsync --- .../Runtime/MeshUtility/BoneMeshEraser.cs | 44 ++++++++++++++++--- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/Assets/UniGLTF/Runtime/MeshUtility/BoneMeshEraser.cs b/Assets/UniGLTF/Runtime/MeshUtility/BoneMeshEraser.cs index 32952aead..eb4976284 100644 --- a/Assets/UniGLTF/Runtime/MeshUtility/BoneMeshEraser.cs +++ b/Assets/UniGLTF/Runtime/MeshUtility/BoneMeshEraser.cs @@ -1,7 +1,8 @@ using System; using System.Collections.Generic; +using System.Threading.Tasks; using UnityEngine; - +using VRMShaders; namespace UniGLTF.MeshUtility { @@ -35,6 +36,13 @@ namespace UniGLTF.MeshUtility } } + /// + /// skip triangles that has weight for exclude, return valid triangle count + /// + /// + /// + /// + /// static int ExcludeTriangles(int[] triangles, BoneWeight[] bws, int[] exclude) { int count = 0; @@ -116,8 +124,28 @@ namespace UniGLTF.MeshUtility return new ExcludeBoneIndex(b0, b1, b2, b3); } - public static Mesh CreateErasedMesh(Mesh src, int[] eraseBoneIndices) + /// + /// Erase triangles that has boneWeight for eraseBone + /// + /// + /// + /// + /// + public static int[] GetExcludedIndices(int[] indices, BoneWeight[] boneWeights, int[] eraseBoneIndices) { + var count = ExcludeTriangles(indices, boneWeights, eraseBoneIndices); + var dst = new int[count]; + Array.Copy(indices, 0, dst, 0, count); + return dst; + } + + public static async Task CreateErasedMeshAsync(Mesh src, int[] eraseBoneIndices, IAwaitCaller awaitCaller) + { + if (awaitCaller == null) + { + awaitCaller = new ImmediateCaller(); + } + /* Debug.LogFormat("{0} exclude: {1}", src.name, @@ -140,16 +168,20 @@ namespace UniGLTF.MeshUtility mesh.subMeshCount = src.subMeshCount; for (int i = 0; i < src.subMeshCount; ++i) { - var indices = src.GetIndices(i); - var count = ExcludeTriangles(indices, mesh.boneWeights, eraseBoneIndices); - var dst = new int[count]; - Array.Copy(indices, 0, dst, 0, count); + var dst = await awaitCaller.Run(() => GetExcludedIndices(src.GetIndices(i), mesh.boneWeights, eraseBoneIndices)); mesh.SetIndices(dst, MeshTopology.Triangles, i); } return mesh; } + public static Mesh CreateErasedMesh(Mesh src, int[] eraseBoneIndices) + { + var task = CreateErasedMeshAsync(src, eraseBoneIndices, new ImmediateCaller()); + task.Wait(); + return task.Result; + } + public static IEnumerable Ancestor(this Transform t) { yield return t;