diff --git a/Assets/UniGLTF/Runtime/MeshUtility/MeshExclude.cs b/Assets/UniGLTF/Runtime/MeshUtility/MeshExclude.cs new file mode 100644 index 000000000..7361e86f5 --- /dev/null +++ b/Assets/UniGLTF/Runtime/MeshUtility/MeshExclude.cs @@ -0,0 +1,59 @@ +using System.Collections.Generic; +using UnityEngine; + +namespace UniGLTF.MeshUtility +{ + public class MeshExclude + { + List _excludes = new List(); + + public MeshExclude(IEnumerable excludes) + { + if (excludes != null) + { + _excludes.AddRange(excludes); + } + } + + public bool IsExcluded(SkinnedMeshRenderer smr) + { + if (smr == null) + { + return true; + } + if (smr.sharedMesh == null) + { + return true; + } + if (_excludes.Contains(smr.sharedMesh)) + { + Debug.LogFormat("{0} has excluded", smr); + return true; + } + return false; + } + + public bool IsExcluded(MeshRenderer mr) + { + if (mr == null) + { + return true; + } + var filter = mr.GetComponent(); + if (filter == null) + { + return true; + } + if (filter.sharedMesh == null) + { + return true; + } + if (_excludes.Contains(filter.sharedMesh)) + { + Debug.LogFormat("{0} has excluded", mr); + return true; + } + return false; + } + } +} diff --git a/Assets/UniGLTF/Runtime/MeshUtility/MeshExclude.cs.meta b/Assets/UniGLTF/Runtime/MeshUtility/MeshExclude.cs.meta new file mode 100644 index 000000000..186817aff --- /dev/null +++ b/Assets/UniGLTF/Runtime/MeshUtility/MeshExclude.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5ff8f78a008cc074daa97b33c95a7b16 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UniGLTF/Runtime/MeshUtility/MeshExtensions.cs b/Assets/UniGLTF/Runtime/MeshUtility/MeshExtensions.cs index a7377bbb5..9f28ce281 100644 --- a/Assets/UniGLTF/Runtime/MeshUtility/MeshExtensions.cs +++ b/Assets/UniGLTF/Runtime/MeshUtility/MeshExtensions.cs @@ -6,6 +6,22 @@ namespace UniGLTF.MeshUtility { public static class MeshExtensions { + public static Mesh GetMesh(this Renderer r) + { + if (r is SkinnedMeshRenderer smr) + { + return smr.sharedMesh; + } + if (r is MeshRenderer mr) + { + if (mr.GetComponent() is MeshFilter mf) + { + return mf.sharedMesh; + } + } + return null; + } + public static Mesh Copy(this Mesh src, bool copyBlendShape) { var dst = new Mesh(); diff --git a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrationResult.cs b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrationResult.cs index 9d8968337..50e0d3679 100644 --- a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrationResult.cs +++ b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrationResult.cs @@ -16,24 +16,6 @@ namespace UniGLTF.MeshUtility public List SourceMeshRenderers = new List(); public SkinnedMeshRenderer IntegratedRenderer; - public MeshMap MeshMap; - - public void CreateMeshMap() - { - MeshMap = new MeshMap - { - Integrated = IntegratedRenderer.sharedMesh - }; - - foreach (var x in SourceSkinnedMeshRenderers) - { - MeshMap.Sources.Add(x.sharedMesh); - } - foreach (var x in SourceMeshRenderers) - { - var filter = x.GetComponent(); - MeshMap.Sources.Add(filter.sharedMesh); - } - } + public MeshMap MeshMap = new MeshMap(); } } diff --git a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrator.cs b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrator.cs index cc400b853..b63570d11 100644 --- a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrator.cs +++ b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrator.cs @@ -6,8 +6,6 @@ namespace UniGLTF.MeshUtility { public class MeshIntegrator { - List _excludes = new List(); - public struct SubMesh { public List Indices; @@ -76,13 +74,8 @@ namespace UniGLTF.MeshUtility } } - public MeshIntegrator(IEnumerable excludes) + public MeshIntegrator() { - if (excludes != null) - { - _excludes.AddRange(excludes); - } - Result = new MeshIntegrationResult(); Positions = new List(); @@ -122,12 +115,8 @@ namespace UniGLTF.MeshUtility Debug.LogWarningFormat("{0} has no mesh", renderer.name); return; } - if (_excludes.Contains(mesh)) - { - Debug.LogFormat("{0} has excluded", renderer.name); - return; - } Result.SourceMeshRenderers.Add(renderer); + Result.MeshMap.Sources.Add(mesh); var indexOffset = Positions.Count; var boneIndexOffset = Bones.Count; @@ -195,12 +184,8 @@ namespace UniGLTF.MeshUtility Debug.LogWarningFormat("{0} has no mesh", renderer.name); return; } - if (_excludes.Contains(mesh)) - { - Debug.LogFormat("{0} has excluded", renderer.name); - return; - } Result.SourceSkinnedMeshRenderers.Add(renderer); + Result.MeshMap.Sources.Add(mesh); var indexOffset = Positions.Count; var boneIndexOffset = Bones.Count; @@ -317,6 +302,7 @@ namespace UniGLTF.MeshUtility integrated.sharedMaterials = SubMeshes.Select(x => x.Material).ToArray(); integrated.bones = Bones.ToArray(); Result.IntegratedRenderer = integrated; + Result.MeshMap.Integrated = mesh; } } } diff --git a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegratorUtility.cs b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegratorUtility.cs index facdc9238..48bffe5fe 100644 --- a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegratorUtility.cs +++ b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegratorUtility.cs @@ -1,7 +1,5 @@ using System.Collections.Generic; -using System; using UnityEngine; -using System.Linq; namespace UniGLTF.MeshUtility { @@ -18,13 +16,18 @@ namespace UniGLTF.MeshUtility /// public static MeshIntegrationResult Integrate(GameObject go, bool onlyBlendShapeRenderers, IEnumerable excludes = null) { - // レンダラから情報を集める - var integrator = new MeshUtility.MeshIntegrator(excludes); + var exclude = new MeshExclude(excludes); + + var integrator = new MeshUtility.MeshIntegrator(); if (onlyBlendShapeRenderers) { foreach (var x in EnumerateSkinnedMeshRenderer(go.transform, true)) { + if (exclude.IsExcluded(x)) + { + continue; + } integrator.Push(x); } } @@ -32,19 +35,25 @@ namespace UniGLTF.MeshUtility { foreach (var x in EnumerateSkinnedMeshRenderer(go.transform, false)) { + if (exclude.IsExcluded(x)) + { + continue; + } integrator.Push(x); } foreach (var x in EnumerateMeshRenderer(go.transform)) { + if (exclude.IsExcluded(x)) + { + continue; + } integrator.Push(x); } } integrator.Intgrate(onlyBlendShapeRenderers); integrator.Result.IntegratedRenderer.transform.SetParent(go.transform, false); - - integrator.Result.CreateMeshMap(); return integrator.Result; } diff --git a/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorWizard.cs b/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorWizard.cs index 4a196af92..9a1df032a 100644 --- a/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorWizard.cs +++ b/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorWizard.cs @@ -60,7 +60,7 @@ namespace VRM [Serializable] public class ExcludeItem { - public Renderer Renderer; + public Mesh Mesh; public bool Exclude; } @@ -150,23 +150,32 @@ namespace VRM .ToArray() ; - var exclude_map = new Dictionary(); + var exclude_map = new Dictionary(); var excludes = new List(); foreach (var x in m_root.GetComponentsInChildren()) { - var item = new ExcludeItem { Renderer = x }; + var mesh = x.GetMesh(); + if (mesh == null) + { + continue; + } + var item = new ExcludeItem { Mesh = mesh }; excludes.Add(item); - exclude_map[x] = item; + exclude_map[mesh] = item; } foreach (var x in m_excludes) { - if (exclude_map.TryGetValue(x.Renderer, out ExcludeItem item)) + if (exclude_map.TryGetValue(x.Mesh, out ExcludeItem item)) { // update item.Exclude = x.Exclude; } } - m_excludes = excludes; + m_excludes.Clear(); + foreach (var kv in exclude_map) + { + m_excludes.Add(kv.Value); + } } void OnWizardUpdate() @@ -196,28 +205,7 @@ namespace VRM return; } - var excludes = new List(); - foreach (var exclude in m_excludes) - { - if (exclude.Renderer is SkinnedMeshRenderer smr) - { - if (smr.sharedMesh != null) - { - excludes.Add(smr.sharedMesh); - } - } - else if (exclude.Renderer is MeshRenderer mr) - { - if (mr.GetComponent() is MeshFilter mf) - { - if (mf.sharedMesh != null) - { - excludes.Add(mf.sharedMesh); - } - } - } - } - + var excludes = m_excludes.Where(x => x.Exclude).Select(x => x.Mesh); integrationResults = MeshIntegratorEditor.Integrate(m_root, assetPath, excludes).Select(x => x.MeshMap).ToArray(); }