From 1a1d42bb446152e87ad3596d69d193d2a8502020 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 4 Mar 2022 18:53:56 +0900 Subject: [PATCH 1/8] add excludes --- .../Runtime/MeshUtility/MeshIntegrator.cs | 66 +++++++++++++++++-- .../MeshUtility/MeshIntegratorUtility.cs | 63 ++---------------- 2 files changed, 67 insertions(+), 62 deletions(-) diff --git a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrator.cs b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrator.cs index 76b4bffeb..c194ff9bf 100644 --- a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrator.cs +++ b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrator.cs @@ -21,8 +21,8 @@ namespace UniGLTF.MeshUtility public Vector3[] Normals; public Vector3[] Tangents; } - -// public List Renderers { get; private set; } + + public MeshIntegrationResult Result; public List Positions { get; private set; } public List Normals { get; private set; } public List UV { get; private set; } @@ -76,7 +76,7 @@ namespace UniGLTF.MeshUtility public MeshIntegrator() { -// Renderers = new List(); + Result = new MeshIntegrationResult(); Positions = new List(); Normals = new List(); @@ -103,6 +103,8 @@ namespace UniGLTF.MeshUtility public void Push(MeshRenderer renderer) { + Result.SourceMeshRenderers.Add(renderer); + var meshFilter = renderer.GetComponent(); if (meshFilter == null) { @@ -176,6 +178,8 @@ namespace UniGLTF.MeshUtility public void Push(SkinnedMeshRenderer renderer) { + Result.SourceSkinnedMeshRenderers.Add(renderer); + var mesh = renderer.sharedMesh; if (mesh == null) { @@ -183,8 +187,6 @@ namespace UniGLTF.MeshUtility return; } -// Renderers.Add(renderer); - var indexOffset = Positions.Count; var boneIndexOffset = Bones.Count; @@ -249,5 +251,57 @@ namespace UniGLTF.MeshUtility }); } } + + public const string INTEGRATED_MESH_NAME = "MeshesIntegrated"; + public const string INTEGRATED_MESH_BLENDSHAPE_NAME = "MeshesBlendShapeIntegrated"; + + public void Intgrate(bool onlyBlendShapeRenderers) + { + var mesh = new Mesh(); + + if (Positions.Count > ushort.MaxValue) + { + Debug.LogFormat("exceed 65535 vertices: {0}", Positions.Count); + mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32; + } + + mesh.vertices = Positions.ToArray(); + mesh.normals = Normals.ToArray(); + mesh.uv = UV.ToArray(); + mesh.tangents = Tangents.ToArray(); + mesh.boneWeights = BoneWeights.ToArray(); + mesh.subMeshCount = SubMeshes.Count; + for (var i = 0; i < SubMeshes.Count; ++i) + { + mesh.SetIndices(SubMeshes[i].Indices.ToArray(), MeshTopology.Triangles, i); + } + mesh.bindposes = BindPoses.ToArray(); + + if (onlyBlendShapeRenderers) + { + AddBlendShapesToMesh(mesh); + mesh.name = INTEGRATED_MESH_BLENDSHAPE_NAME; + } + else + { + mesh.name = INTEGRATED_MESH_NAME; + } + + var meshNode = new GameObject(); + if (onlyBlendShapeRenderers) + { + meshNode.name = "MeshIntegrator(BlendShape)"; + } + else + { + meshNode.name = "MeshIntegrator"; + } + + var integrated = meshNode.AddComponent(); + integrated.sharedMesh = mesh; + integrated.sharedMaterials = SubMeshes.Select(x => x.Material).ToArray(); + integrated.bones = Bones.ToArray(); + Result.IntegratedRenderer = integrated; + } } -} \ No newline at end of file +} diff --git a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegratorUtility.cs b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegratorUtility.cs index 5ae99d60a..b91ec7d5a 100644 --- a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegratorUtility.cs +++ b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegratorUtility.cs @@ -6,29 +6,17 @@ namespace UniGLTF.MeshUtility { public static class MeshIntegratorUtility { - public const string INTEGRATED_MESH_NAME = "MeshesIntegrated"; - public const string INTEGRATED_MESH_BLENDSHAPE_NAME = "MeshesBlendShapeIntegrated"; + public static string INTEGRATED_MESH_NAME => MeshIntegrator.INTEGRATED_MESH_NAME; + public static string INTEGRATED_MESH_BLENDSHAPE_NAME => MeshIntegrator.INTEGRATED_MESH_BLENDSHAPE_NAME; + /// /// go を root としたヒエラルキーから Renderer を集めて、統合された Mesh 作成する /// /// /// BlendShapeを保持するSkinnedMeshRendererのみ/BlendShapeを保持しないSkinnedMeshRenderer + MeshRenderer /// - public static MeshIntegrationResult Integrate(GameObject go, bool onlyBlendShapeRenderers) + public static MeshIntegrationResult Integrate(GameObject go, bool onlyBlendShapeRenderers, IReadOnlyList excludes = null) { - var result = new MeshIntegrationResult(); - - var meshNode = new GameObject(); - if (onlyBlendShapeRenderers) - { - meshNode.name = "MeshIntegrator(BlendShape)"; - } - else - { - meshNode.name = "MeshIntegrator"; - } - meshNode.transform.SetParent(go.transform, false); - // レンダラから情報を集める var integrator = new MeshUtility.MeshIntegrator(); @@ -37,7 +25,6 @@ namespace UniGLTF.MeshUtility foreach (var x in EnumerateSkinnedMeshRenderer(go.transform, true)) { integrator.Push(x); - result.SourceSkinnedMeshRenderers.Add(x); } } else @@ -45,53 +32,17 @@ namespace UniGLTF.MeshUtility foreach (var x in EnumerateSkinnedMeshRenderer(go.transform, false)) { integrator.Push(x); - result.SourceSkinnedMeshRenderers.Add(x); } foreach (var x in EnumerateMeshRenderer(go.transform)) { integrator.Push(x); - result.SourceMeshRenderers.Add(x); } } - var mesh = new Mesh(); - - if (integrator.Positions.Count > ushort.MaxValue) - { - Debug.LogFormat("exceed 65535 vertices: {0}", integrator.Positions.Count); - mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32; - } - - mesh.vertices = integrator.Positions.ToArray(); - mesh.normals = integrator.Normals.ToArray(); - mesh.uv = integrator.UV.ToArray(); - mesh.tangents = integrator.Tangents.ToArray(); - mesh.boneWeights = integrator.BoneWeights.ToArray(); - mesh.subMeshCount = integrator.SubMeshes.Count; - for (var i = 0; i < integrator.SubMeshes.Count; ++i) - { - mesh.SetIndices(integrator.SubMeshes[i].Indices.ToArray(), MeshTopology.Triangles, i); - } - mesh.bindposes = integrator.BindPoses.ToArray(); - - if (onlyBlendShapeRenderers) - { - integrator.AddBlendShapesToMesh(mesh); - mesh.name = INTEGRATED_MESH_BLENDSHAPE_NAME; - } - else - { - mesh.name = INTEGRATED_MESH_NAME; - } - - var integrated = meshNode.AddComponent(); - integrated.sharedMesh = mesh; - integrated.sharedMaterials = integrator.SubMeshes.Select(x => x.Material).ToArray(); - integrated.bones = integrator.Bones.ToArray(); - result.IntegratedRenderer = integrated; - - return result; + integrator.Intgrate(onlyBlendShapeRenderers); + integrator.Result.IntegratedRenderer.transform.SetParent(go.transform, false); + return integrator.Result; } public static IEnumerable EnumerateSkinnedMeshRenderer(Transform root, bool hasBlendShape) From c0afd7f4d5a74ef7374002ef964957a79c856eb9 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 4 Mar 2022 19:07:31 +0900 Subject: [PATCH 2/8] move MeshIntegratorWizard menu --- .../VRM/Editor/SkinnedMeshUtility/MeshIntegratorWizard.cs | 7 ++----- Assets/VRM/Editor/VrmTopMenu.cs | 3 +++ 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorWizard.cs b/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorWizard.cs index 03f67b694..672f22332 100644 --- a/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorWizard.cs +++ b/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorWizard.cs @@ -10,8 +10,6 @@ namespace VRM { public class MeshIntegratorWizard : ScriptableWizard { - const string MENU_KEY = "Assets/UnityEditorScripts/MeshIntegratorWizard"; - [SerializeField] GameObject m_root; @@ -26,7 +24,7 @@ namespace VRM public override bool Equals(object obj) { - if(!(obj is MaterialKey)) + if (!(obj is MaterialKey)) { return base.Equals(obj); } @@ -60,8 +58,7 @@ namespace VRM [Header("Result")] public MeshIntegrationResult[] integrationResults; - [MenuItem(MENU_KEY)] - static void CreateWizard() + public static void CreateWizard() { ScriptableWizard.DisplayWizard("MeshIntegrator", "Integrate and close window", "Integrate"); } diff --git a/Assets/VRM/Editor/VrmTopMenu.cs b/Assets/VRM/Editor/VrmTopMenu.cs index dfd6bebd5..0b96ce481 100644 --- a/Assets/VRM/Editor/VrmTopMenu.cs +++ b/Assets/VRM/Editor/VrmTopMenu.cs @@ -33,6 +33,9 @@ namespace VRM [MenuItem(UserMenuPrefix + "/MeshIntegration", priority = 21)] private static void MeshIntegration() => MeshIntegratorEditor.Integrate(); + [MenuItem(UserMenuPrefix + "/MeshIntegratorWizard", priority = 21)] + private static void OpenMeshIntegratorWizard() => MeshIntegratorWizard.CreateWizard(); + [MenuItem(UserMenuPrefix + "/Save SpringBone to JSON", validate = true)] private static bool SaveSpringBoneToJsonValidation() => VRMSpringBoneUtilityEditor.SaveSpringBoneToJsonValidation(); From d6c4a2bea6b15eb9a4238acffff41d7d8aae2270 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 4 Mar 2022 19:45:55 +0900 Subject: [PATCH 3/8] =?UTF-8?q?prefab=20=E4=BD=9C=E6=88=90=E5=85=88?= =?UTF-8?q?=E3=82=92=E6=8C=87=E5=AE=9A=E3=81=99=E3=82=8B=20dialog=20?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MeshIntegratorEditor.cs | 63 +++++++------------ .../MeshIntegratorWizard.cs | 17 ++++- Assets/VRM/Editor/VrmTopMenu.cs | 6 -- 3 files changed, 40 insertions(+), 46 deletions(-) diff --git a/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorEditor.cs b/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorEditor.cs index 87a7f1665..5cc142875 100644 --- a/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorEditor.cs +++ b/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorEditor.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -12,7 +11,8 @@ using Object = UnityEngine.Object; namespace VRM { /// - /// 複数のメッシュをまとめて、BlendShapeClipに変更を反映する + /// 複数のメッシュを統合する。 + /// VRM の場合、BlendShapeClip の調整が必用なのでこれも実行する。 /// [DisallowMultipleComponent] public static class MeshIntegratorEditor @@ -26,28 +26,11 @@ namespace VRM SkinnedMeshUtility.IsPrefab(Selection.activeObject); } - public static void Integrate() - { - var go = Selection.activeObject as GameObject; - - Integrate(go); - } - - //[MenuItem("Assets/fooa", false)] - //private static void Foo() - //{ - // var go = Selection.activeObject as GameObject; - - // Debug.Log(SkinnedMeshUtility.IsPrefab(go)); - //} - - - public static List Integrate(GameObject prefab) + public static List Integrate(GameObject prefab, UniGLTF.UnityPath writeAssetPath) { Undo.RecordObject(prefab, "Mesh Integration"); var instance = SkinnedMeshUtility.InstantiatePrefab(prefab); - var clips = new List(); var proxy = instance.GetComponent(); if (proxy != null && proxy.BlendShapeAvatar != null) @@ -65,12 +48,29 @@ namespace VRM // Execute var results = VRMMeshIntegratorUtility.Integrate(instance, clips); - foreach (var res in results) + foreach (var result in results) { - if (res.IntegratedRenderer == null) continue; + if (result.IntegratedRenderer == null) continue; - SaveMeshAsset(res.IntegratedRenderer.sharedMesh, instance, res.IntegratedRenderer.gameObject.name); - Undo.RegisterCreatedObjectUndo(res.IntegratedRenderer.gameObject, "Integrate Renderers"); + var assetPath = writeAssetPath.Parent.Child($"{result.IntegratedRenderer.gameObject.name}{ASSET_SUFFIX}"); + Debug.LogFormat("CreateAsset: {0}", assetPath); + assetPath.CreateAsset(result.IntegratedRenderer.sharedMesh); + Undo.RegisterCreatedObjectUndo(result.IntegratedRenderer.gameObject, "Integrate Renderers"); + } + + // Apply to Prefab + var prefabPath = UniGLTF.UnityPath.FromUnityPath(AssetDatabase.GetAssetPath(prefab)); + if (prefabPath.Equals(writeAssetPath)) + { + SkinnedMeshUtility.ApplyChangesToPrefab(instance); + } + else + { + PrefabUtility.SaveAsPrefabAsset(instance, writeAssetPath.Value, out bool success); + if (!success) + { + throw new System.Exception($"PrefabUtility.SaveAsPrefabAsset: {writeAssetPath}"); + } } // destroy source renderers @@ -88,9 +88,6 @@ namespace VRM renderer.gameObject.SetActive(false); } } - - // Apply to Prefab - SkinnedMeshUtility.ApplyChangesToPrefab(instance); Object.DestroyImmediate(instance); return results; @@ -143,17 +140,5 @@ namespace VRM assetPath = assetPath.Replace(@"\", @"/"); return assetPath; } - - private static void SaveMeshAsset(Mesh mesh, GameObject go, string name) - { - var assetPath = Path.Combine(GetRootPrefabPath(go), string.Format("{0}{1}", - name, - ASSET_SUFFIX - )); - - Debug.LogFormat("CreateAsset: {0}", assetPath); - AssetDatabase.CreateAsset(mesh, assetPath); - } - } } diff --git a/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorWizard.cs b/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorWizard.cs index 672f22332..70b0b4816 100644 --- a/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorWizard.cs +++ b/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorWizard.cs @@ -5,6 +5,7 @@ using System.Linq; using System; using System.Collections.Generic; using UniGLTF.MeshUtility; +using System.IO; namespace VRM { @@ -147,7 +148,21 @@ namespace VRM return; } - integrationResults = MeshIntegratorEditor.Integrate(m_root).ToArray(); + var prefabPath = AssetDatabase.GetAssetPath(m_root); + var path = EditorUtility.SaveFilePanel("save prefab", Path.GetDirectoryName(prefabPath), m_root.name, "prefab"); + if (string.IsNullOrEmpty(path)) + { + return; + } + + var assetPath = UniGLTF.UnityPath.FromFullpath(path); + if (!assetPath.IsUnderAssetsFolder) + { + Debug.LogWarning($"{path} is not asset path"); + return; + } + + integrationResults = MeshIntegratorEditor.Integrate(m_root, assetPath).ToArray(); } void OnWizardCreate() diff --git a/Assets/VRM/Editor/VrmTopMenu.cs b/Assets/VRM/Editor/VrmTopMenu.cs index 0b96ce481..a79dc302a 100644 --- a/Assets/VRM/Editor/VrmTopMenu.cs +++ b/Assets/VRM/Editor/VrmTopMenu.cs @@ -27,12 +27,6 @@ namespace VRM [MenuItem(UserMenuPrefix + "/Freeze T-Pose", priority = 20)] private static void FreezeTPose() => VRMHumanoidNormalizerMenu.Normalize(); - [MenuItem(UserMenuPrefix + "/MeshIntegration", validate = true)] - private static bool MeshIntegrationValidation() => MeshIntegratorEditor.IntegrateValidation(); - - [MenuItem(UserMenuPrefix + "/MeshIntegration", priority = 21)] - private static void MeshIntegration() => MeshIntegratorEditor.Integrate(); - [MenuItem(UserMenuPrefix + "/MeshIntegratorWizard", priority = 21)] private static void OpenMeshIntegratorWizard() => MeshIntegratorWizard.CreateWizard(); From e7390ecf2cbb39b6cbc21f4b904148b095535733 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 4 Mar 2022 20:18:42 +0900 Subject: [PATCH 4/8] MeshMap --- .../MeshUtility/MeshIntegrationResult.cs | 26 ++++++++++++++++ .../MeshUtility/MeshIntegratorUtility.cs | 2 ++ .../MeshIntegratorEditor.cs | 30 ++++++++++--------- .../MeshIntegratorWizard.cs | 7 +++-- 4 files changed, 48 insertions(+), 17 deletions(-) diff --git a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrationResult.cs b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrationResult.cs index 56ffa4456..9d8968337 100644 --- a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrationResult.cs +++ b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrationResult.cs @@ -4,10 +4,36 @@ using UnityEngine; namespace UniGLTF.MeshUtility { [System.Serializable] + public class MeshMap + { + public List Sources = new List(); + public Mesh Integrated; + } + public class MeshIntegrationResult { public List SourceSkinnedMeshRenderers = new List(); 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); + } + } } } diff --git a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegratorUtility.cs b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegratorUtility.cs index b91ec7d5a..0f35644b6 100644 --- a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegratorUtility.cs +++ b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegratorUtility.cs @@ -42,6 +42,8 @@ namespace UniGLTF.MeshUtility integrator.Intgrate(onlyBlendShapeRenderers); integrator.Result.IntegratedRenderer.transform.SetParent(go.transform, false); + + integrator.Result.CreateMeshMap(); return integrator.Result; } diff --git a/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorEditor.cs b/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorEditor.cs index 5cc142875..c999d68ad 100644 --- a/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorEditor.cs +++ b/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorEditor.cs @@ -48,6 +48,22 @@ namespace VRM // Execute var results = VRMMeshIntegratorUtility.Integrate(instance, clips); + // disable source renderer + foreach (var res in results) + { + foreach (var renderer in res.SourceSkinnedMeshRenderers) + { + Undo.RecordObject(renderer.gameObject, "Deactivate old renderer"); + renderer.gameObject.SetActive(false); + } + + foreach (var renderer in res.SourceMeshRenderers) + { + Undo.RecordObject(renderer.gameObject, "Deactivate old renderer"); + renderer.gameObject.SetActive(false); + } + } + foreach (var result in results) { if (result.IntegratedRenderer == null) continue; @@ -74,20 +90,6 @@ namespace VRM } // destroy source renderers - foreach (var res in results) - { - foreach (var renderer in res.SourceSkinnedMeshRenderers) - { - Undo.RecordObject(renderer.gameObject, "Deactivate old renderer"); - renderer.gameObject.SetActive(false); - } - - foreach (var renderer in res.SourceMeshRenderers) - { - Undo.RecordObject(renderer.gameObject, "Deactivate old renderer"); - renderer.gameObject.SetActive(false); - } - } Object.DestroyImmediate(instance); return results; diff --git a/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorWizard.cs b/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorWizard.cs index 70b0b4816..54659be65 100644 --- a/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorWizard.cs +++ b/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorWizard.cs @@ -14,6 +14,7 @@ namespace VRM [SerializeField] GameObject m_root; + [Header("Validation")] [SerializeField] Material[] m_uniqueMaterials; @@ -57,11 +58,11 @@ namespace VRM MaterialList[] m_duplicateMaterials; [Header("Result")] - public MeshIntegrationResult[] integrationResults; + public MeshMap[] integrationResults; public static void CreateWizard() { - ScriptableWizard.DisplayWizard("MeshIntegrator", "Integrate and close window", "Integrate"); + ScriptableWizard.DisplayWizard("MeshIntegratorWizard", "Integrate and close window", "Integrate"); } private void OnEnable() @@ -162,7 +163,7 @@ namespace VRM return; } - integrationResults = MeshIntegratorEditor.Integrate(m_root, assetPath).ToArray(); + integrationResults = MeshIntegratorEditor.Integrate(m_root, assetPath).Select(x => x.MeshMap).ToArray(); } void OnWizardCreate() From 1900344bb68fa9109d288433c8094a5513a3859e Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 4 Mar 2022 20:51:07 +0900 Subject: [PATCH 5/8] implement exclude --- .../Runtime/MeshUtility/MeshIntegrator.cs | 44 ++++++++++++++++--- .../MeshUtility/MeshIntegratorUtility.cs | 5 ++- .../MeshIntegratorEditor.cs | 4 +- .../MeshIntegratorWizard.cs | 42 ++++++++++++++++-- .../VRMMeshIntegratorUtility.cs | 8 ++-- 5 files changed, 86 insertions(+), 17 deletions(-) diff --git a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrator.cs b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrator.cs index c194ff9bf..fb0fb02f2 100644 --- a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrator.cs +++ b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrator.cs @@ -6,6 +6,8 @@ namespace UniGLTF.MeshUtility { public class MeshIntegrator { + List _excludes = new List(); + public struct SubMesh { public List Indices; @@ -74,8 +76,32 @@ namespace UniGLTF.MeshUtility } } - public MeshIntegrator() + public MeshIntegrator(IReadOnlyList excludes) { + if (excludes != null) + { + foreach (var exclude in excludes) + { + if (exclude is SkinnedMeshRenderer smr) + { + if (smr.sharedMesh != null) + { + _excludes.Add(smr.sharedMesh); + } + } + else if (exclude is MeshRenderer mr) + { + if (mr.GetComponent() is MeshFilter mf) + { + if (mf.sharedMesh != null) + { + _excludes.Add(mf.sharedMesh); + } + } + } + } + } + Result = new MeshIntegrationResult(); Positions = new List(); @@ -103,8 +129,6 @@ namespace UniGLTF.MeshUtility public void Push(MeshRenderer renderer) { - Result.SourceMeshRenderers.Add(renderer); - var meshFilter = renderer.GetComponent(); if (meshFilter == null) { @@ -117,6 +141,12 @@ 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); var indexOffset = Positions.Count; var boneIndexOffset = Bones.Count; @@ -178,14 +208,18 @@ namespace UniGLTF.MeshUtility public void Push(SkinnedMeshRenderer renderer) { - Result.SourceSkinnedMeshRenderers.Add(renderer); - var mesh = renderer.sharedMesh; if (mesh == null) { 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); var indexOffset = Positions.Count; var boneIndexOffset = Bones.Count; diff --git a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegratorUtility.cs b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegratorUtility.cs index 0f35644b6..cf7158353 100644 --- a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegratorUtility.cs +++ b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegratorUtility.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; -using System.Linq; +using System; using UnityEngine; +using System.Linq; namespace UniGLTF.MeshUtility { @@ -18,7 +19,7 @@ namespace UniGLTF.MeshUtility public static MeshIntegrationResult Integrate(GameObject go, bool onlyBlendShapeRenderers, IReadOnlyList excludes = null) { // レンダラから情報を集める - var integrator = new MeshUtility.MeshIntegrator(); + var integrator = new MeshUtility.MeshIntegrator(excludes); if (onlyBlendShapeRenderers) { diff --git a/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorEditor.cs b/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorEditor.cs index c999d68ad..e49189608 100644 --- a/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorEditor.cs +++ b/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorEditor.cs @@ -26,7 +26,7 @@ namespace VRM SkinnedMeshUtility.IsPrefab(Selection.activeObject); } - public static List Integrate(GameObject prefab, UniGLTF.UnityPath writeAssetPath) + public static List Integrate(GameObject prefab, UniGLTF.UnityPath writeAssetPath, IReadOnlyList excludes) { Undo.RecordObject(prefab, "Mesh Integration"); var instance = SkinnedMeshUtility.InstantiatePrefab(prefab); @@ -46,7 +46,7 @@ namespace VRM BackupVrmPrefab(prefab); // Execute - var results = VRMMeshIntegratorUtility.Integrate(instance, clips); + var results = VRMMeshIntegratorUtility.Integrate(instance, clips, excludes); // disable source renderer foreach (var res in results) diff --git a/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorWizard.cs b/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorWizard.cs index 54659be65..bd9477183 100644 --- a/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorWizard.cs +++ b/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorWizard.cs @@ -57,8 +57,20 @@ namespace VRM [SerializeField] MaterialList[] m_duplicateMaterials; + [Serializable] + public class ExcludeItem + { + public Renderer Renderer; + public bool Exclude; + } + + [Header("Options")] + [SerializeField] + List m_excludes = new List(); + [Header("Result")] - public MeshMap[] integrationResults; + [SerializeField] + MeshMap[] integrationResults; public static void CreateWizard() { @@ -115,14 +127,17 @@ namespace VRM void OnValidate() { - Debug.Log("OnValidate"); - if (m_root == null) + if (m_root == null + || !PrefabUtility.IsPartOfAnyPrefab(m_root)) { + Debug.LogWarning("Invalidate"); m_uniqueMaterials = new Material[] { }; m_duplicateMaterials = new MaterialList[] { }; + m_excludes.Clear(); return; } + Debug.Log("OnValidate"); m_uniqueMaterials = MeshIntegratorUtility.EnumerateSkinnedMeshRenderer(m_root.transform, false) .SelectMany(x => x.sharedMaterials) .Distinct() @@ -134,6 +149,24 @@ namespace VRM .Where(x => x.Materials.Length > 1) .ToArray() ; + + var exclude_map = new Dictionary(); + var excludes = new List(); + foreach (var x in m_root.GetComponentsInChildren()) + { + var item = new ExcludeItem { Renderer = x }; + excludes.Add(item); + exclude_map[x] = item; + } + foreach (var x in m_excludes) + { + if (exclude_map.TryGetValue(x.Renderer, out ExcludeItem item)) + { + // update + item.Exclude = x.Exclude; + } + } + m_excludes = excludes; } void OnWizardUpdate() @@ -163,7 +196,8 @@ namespace VRM return; } - integrationResults = MeshIntegratorEditor.Integrate(m_root, assetPath).Select(x => x.MeshMap).ToArray(); + var excludes = m_excludes.Where(x => x.Renderer != null && x.Exclude).Select(x => x.Renderer).ToArray(); + integrationResults = MeshIntegratorEditor.Integrate(m_root, assetPath, excludes).Select(x => x.MeshMap).ToArray(); } void OnWizardCreate() diff --git a/Assets/VRM/Runtime/SkinnedMeshUtility/VRMMeshIntegratorUtility.cs b/Assets/VRM/Runtime/SkinnedMeshUtility/VRMMeshIntegratorUtility.cs index 29a0fb5af..48f9fcb66 100644 --- a/Assets/VRM/Runtime/SkinnedMeshUtility/VRMMeshIntegratorUtility.cs +++ b/Assets/VRM/Runtime/SkinnedMeshUtility/VRMMeshIntegratorUtility.cs @@ -20,7 +20,7 @@ namespace VRM if (avatar == null) return false; var clips = avatar.Clips; - var results = Integrate(vrmRootObject, clips); + var results = Integrate(vrmRootObject, clips, null); if (results.Any(x => x.IntegratedRenderer == null)) return false; foreach (var result in results) @@ -39,17 +39,17 @@ namespace VRM return true; } - public static List Integrate(GameObject root, List blendshapeClips) + public static List Integrate(GameObject root, List blendshapeClips, IReadOnlyList excludes) { var result = new List(); - var withoutBlendShape = MeshIntegratorUtility.Integrate(root, onlyBlendShapeRenderers: false); + var withoutBlendShape = MeshIntegratorUtility.Integrate(root, onlyBlendShapeRenderers: false, excludes: excludes); if (withoutBlendShape.IntegratedRenderer != null) { result.Add(withoutBlendShape); } - var onlyBlendShape = MeshIntegratorUtility.Integrate(root, onlyBlendShapeRenderers: true); + var onlyBlendShape = MeshIntegratorUtility.Integrate(root, onlyBlendShapeRenderers: true, excludes: excludes); if (onlyBlendShape.IntegratedRenderer != null) { result.Add(onlyBlendShape); From 564245d4a50e93f7c682dc1776895d982c2364fc Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 4 Mar 2022 20:57:28 +0900 Subject: [PATCH 6/8] exclude mesh --- .../Runtime/MeshUtility/MeshIntegrator.cs | 29 ++++--------------- .../MeshUtility/MeshIntegratorUtility.cs | 2 +- .../MeshIntegratorEditor.cs | 2 +- .../MeshIntegratorWizard.cs | 25 ++++++++++++++-- .../VRMMeshIntegratorUtility.cs | 2 +- 5 files changed, 31 insertions(+), 29 deletions(-) diff --git a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrator.cs b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrator.cs index fb0fb02f2..cc400b853 100644 --- a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrator.cs +++ b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrator.cs @@ -76,30 +76,11 @@ namespace UniGLTF.MeshUtility } } - public MeshIntegrator(IReadOnlyList excludes) + public MeshIntegrator(IEnumerable excludes) { if (excludes != null) { - foreach (var exclude in excludes) - { - if (exclude is SkinnedMeshRenderer smr) - { - if (smr.sharedMesh != null) - { - _excludes.Add(smr.sharedMesh); - } - } - else if (exclude is MeshRenderer mr) - { - if (mr.GetComponent() is MeshFilter mf) - { - if (mf.sharedMesh != null) - { - _excludes.Add(mf.sharedMesh); - } - } - } - } + _excludes.AddRange(excludes); } Result = new MeshIntegrationResult(); @@ -174,7 +155,7 @@ namespace UniGLTF.MeshUtility return; } var bindpose = bone.worldToLocalMatrix; - + BoneWeights.AddRange(Enumerable.Range(0, mesh.vertices.Length) .Select(x => new BoneWeight() { @@ -182,10 +163,10 @@ namespace UniGLTF.MeshUtility weight0 = 1, }) ); - + BindPoses.Add(bindpose); Bones.Add(bone); - + for (int i = 0; i < mesh.subMeshCount; ++i) { var indices = mesh.GetIndices(i).Select(x => x + indexOffset); diff --git a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegratorUtility.cs b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegratorUtility.cs index cf7158353..facdc9238 100644 --- a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegratorUtility.cs +++ b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegratorUtility.cs @@ -16,7 +16,7 @@ namespace UniGLTF.MeshUtility /// /// BlendShapeを保持するSkinnedMeshRendererのみ/BlendShapeを保持しないSkinnedMeshRenderer + MeshRenderer /// - public static MeshIntegrationResult Integrate(GameObject go, bool onlyBlendShapeRenderers, IReadOnlyList excludes = null) + public static MeshIntegrationResult Integrate(GameObject go, bool onlyBlendShapeRenderers, IEnumerable excludes = null) { // レンダラから情報を集める var integrator = new MeshUtility.MeshIntegrator(excludes); diff --git a/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorEditor.cs b/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorEditor.cs index e49189608..793972b4c 100644 --- a/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorEditor.cs +++ b/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorEditor.cs @@ -26,7 +26,7 @@ namespace VRM SkinnedMeshUtility.IsPrefab(Selection.activeObject); } - public static List Integrate(GameObject prefab, UniGLTF.UnityPath writeAssetPath, IReadOnlyList excludes) + public static List Integrate(GameObject prefab, UniGLTF.UnityPath writeAssetPath, IEnumerable excludes) { Undo.RecordObject(prefab, "Mesh Integration"); var instance = SkinnedMeshUtility.InstantiatePrefab(prefab); diff --git a/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorWizard.cs b/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorWizard.cs index bd9477183..4a196af92 100644 --- a/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorWizard.cs +++ b/Assets/VRM/Editor/SkinnedMeshUtility/MeshIntegratorWizard.cs @@ -128,7 +128,7 @@ namespace VRM void OnValidate() { if (m_root == null - || !PrefabUtility.IsPartOfAnyPrefab(m_root)) + || !PrefabUtility.IsPartOfAnyPrefab(m_root) || m_root.transform.parent != null) { Debug.LogWarning("Invalidate"); m_uniqueMaterials = new Material[] { }; @@ -196,7 +196,28 @@ namespace VRM return; } - var excludes = m_excludes.Where(x => x.Renderer != null && x.Exclude).Select(x => x.Renderer).ToArray(); + 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); + } + } + } + } + integrationResults = MeshIntegratorEditor.Integrate(m_root, assetPath, excludes).Select(x => x.MeshMap).ToArray(); } diff --git a/Assets/VRM/Runtime/SkinnedMeshUtility/VRMMeshIntegratorUtility.cs b/Assets/VRM/Runtime/SkinnedMeshUtility/VRMMeshIntegratorUtility.cs index 48f9fcb66..81f61e0d5 100644 --- a/Assets/VRM/Runtime/SkinnedMeshUtility/VRMMeshIntegratorUtility.cs +++ b/Assets/VRM/Runtime/SkinnedMeshUtility/VRMMeshIntegratorUtility.cs @@ -39,7 +39,7 @@ namespace VRM return true; } - public static List Integrate(GameObject root, List blendshapeClips, IReadOnlyList excludes) + public static List Integrate(GameObject root, List blendshapeClips, IEnumerable excludes) { var result = new List(); From 9b6d0e08da0242cdc66f38808c43f9fbb82574dd Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 4 Mar 2022 21:36:33 +0900 Subject: [PATCH 7/8] =?UTF-8?q?MeshIntegrator=E3=81=8B=E3=82=89Exclude?= =?UTF-8?q?=E3=82=92=E5=88=86=E9=9B=A2=E3=80=82Wizard=E3=81=AE=E9=99=A4?= =?UTF-8?q?=E5=A4=96=E5=AF=BE=E8=B1=A1=E3=82=92Renderer=E3=81=8B=E3=82=89M?= =?UTF-8?q?esh=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Runtime/MeshUtility/MeshExclude.cs | 59 +++++++++++++++++++ .../Runtime/MeshUtility/MeshExclude.cs.meta | 11 ++++ .../Runtime/MeshUtility/MeshExtensions.cs | 16 +++++ .../MeshUtility/MeshIntegrationResult.cs | 20 +------ .../Runtime/MeshUtility/MeshIntegrator.cs | 22 ++----- .../MeshUtility/MeshIntegratorUtility.cs | 21 +++++-- .../MeshIntegratorWizard.cs | 44 +++++--------- 7 files changed, 122 insertions(+), 71 deletions(-) create mode 100644 Assets/UniGLTF/Runtime/MeshUtility/MeshExclude.cs create mode 100644 Assets/UniGLTF/Runtime/MeshUtility/MeshExclude.cs.meta 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(); } From 16d13e1289dc40c1fbc764b0df4b0444cb644add Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 7 Mar 2022 14:21:58 +0900 Subject: [PATCH 8/8] private --- .../Runtime/MeshUtility/MeshIntegrator.cs | 57 ++++++------------- 1 file changed, 16 insertions(+), 41 deletions(-) diff --git a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrator.cs b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrator.cs index b63570d11..569fa4bdf 100644 --- a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrator.cs +++ b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrator.cs @@ -6,13 +6,16 @@ namespace UniGLTF.MeshUtility { public class MeshIntegrator { - public struct SubMesh + public const string INTEGRATED_MESH_NAME = "MeshesIntegrated"; + public const string INTEGRATED_MESH_BLENDSHAPE_NAME = "MeshesBlendShapeIntegrated"; + + struct SubMesh { public List Indices; public Material Material; } - public class BlendShape + class BlendShape { public int VertexOffset; public string Name; @@ -22,24 +25,17 @@ namespace UniGLTF.MeshUtility public Vector3[] Tangents; } - public MeshIntegrationResult Result; - public List Positions { get; private set; } - public List Normals { get; private set; } - public List UV { get; private set; } - public List Tangents { get; private set; } - public List BoneWeights { get; private set; } - - public List SubMeshes - { - get; - private set; - } - - public List BindPoses { get; private set; } - public List Bones { get; private set; } - - public List BlendShapes { get; private set; } - public void AddBlendShapesToMesh(Mesh mesh) + public MeshIntegrationResult Result { get; } = new MeshIntegrationResult(); + List Positions { get; } = new List(); + List Normals { get; } = new List(); + List UV { get; } = new List(); + List Tangents { get; } = new List(); + List BoneWeights { get; } = new List(); + List SubMeshes { get; } = new List(); + List BindPoses { get; } = new List(); + List Bones { get; } = new List(); + List BlendShapes { get; } = new List(); + void AddBlendShapesToMesh(Mesh mesh) { Dictionary map = new Dictionary(); @@ -74,24 +70,6 @@ namespace UniGLTF.MeshUtility } } - public MeshIntegrator() - { - Result = new MeshIntegrationResult(); - - Positions = new List(); - Normals = new List(); - UV = new List(); - Tangents = new List(); - BoneWeights = new List(); - - SubMeshes = new List(); - - BindPoses = new List(); - Bones = new List(); - - BlendShapes = new List(); - } - static BoneWeight AddBoneIndexOffset(BoneWeight bw, int boneIndexOffset) { if (bw.weight0 > 0) bw.boneIndex0 += boneIndexOffset; @@ -252,9 +230,6 @@ namespace UniGLTF.MeshUtility } } - public const string INTEGRATED_MESH_NAME = "MeshesIntegrated"; - public const string INTEGRATED_MESH_BLENDSHAPE_NAME = "MeshesBlendShapeIntegrated"; - public void Intgrate(bool onlyBlendShapeRenderers) { var mesh = new Mesh();