From 18dccde65e6892e1095a96a978d33e098fd55b24 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 1 Nov 2023 19:03:49 +0900 Subject: [PATCH] =?UTF-8?q?2tab=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/MeshIntegrator.cs | 23 +++- .../MeshUtility/MeshIntegratorUtility.cs | 4 +- .../MeshUtility/Vrm10MeshUtilityDialog.cs | 26 +--- .../Runtime/MeshUtility/Vrm10MeshUtility.cs | 113 ++++++++++++------ 4 files changed, 104 insertions(+), 62 deletions(-) diff --git a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrator.cs b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrator.cs index 8bd4e7514..b24c16ce1 100644 --- a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrator.cs +++ b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegrator.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using UnityEngine; @@ -6,9 +7,14 @@ namespace UniGLTF.MeshUtility { public class MeshIntegrator { - private MeshIntegrator() + public enum BlendShapeOperation { - + // No BlendShape(drop if mesh has blendshape) + None, + // Use blendShape(keep blendshape) + Use, + // Integrate to two mesh that is with blendShape and is without blendshape + Split, } struct SubMesh @@ -233,7 +239,7 @@ namespace UniGLTF.MeshUtility } } - public static MeshIntegrationResult Integrate(MeshIntegrationGroup group, bool useBlendShape) + public static MeshIntegrationResult Integrate(MeshIntegrationGroup group, BlendShapeOperation op) { var integrator = new MeshUtility.MeshIntegrator(); foreach (var x in group.Renderers) @@ -247,11 +253,16 @@ namespace UniGLTF.MeshUtility integrator.Push(mr); } } - return integrator.Integrate(group.Name, useBlendShape); + return integrator.Integrate(group.Name, op); } - public MeshIntegrationResult Integrate(string name, bool useBlendShape) + public MeshIntegrationResult Integrate(string name, BlendShapeOperation op) { + if (op == BlendShapeOperation.Split) + { + throw new NotImplementedException(); + } + var mesh = new Mesh(); if (Positions.Count > ushort.MaxValue) @@ -271,7 +282,7 @@ namespace UniGLTF.MeshUtility mesh.SetIndices(SubMeshes[i].Indices.ToArray(), MeshTopology.Triangles, i); } mesh.bindposes = BindPoses.ToArray(); - if (useBlendShape) + if (op == BlendShapeOperation.Use) { AddBlendShapesToMesh(mesh); } diff --git a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegratorUtility.cs b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegratorUtility.cs index 1478c8494..cee998e1a 100644 --- a/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegratorUtility.cs +++ b/Assets/UniGLTF/Runtime/MeshUtility/MeshIntegratorUtility.cs @@ -99,7 +99,9 @@ namespace UniGLTF.MeshUtility } } - return MeshIntegrator.Integrate(group, useBlendShape); + return MeshIntegrator.Integrate(group, useBlendShape + ? MeshIntegrator.BlendShapeOperation.Use + : MeshIntegrator.BlendShapeOperation.None); } public static IEnumerable EnumerateSkinnedMeshRenderer(Transform root, MeshEnumerateOption hasBlendShape) diff --git a/Assets/VRM10/Editor/MeshUtility/Vrm10MeshUtilityDialog.cs b/Assets/VRM10/Editor/MeshUtility/Vrm10MeshUtilityDialog.cs index a7a58bcd9..df63b56a7 100644 --- a/Assets/VRM10/Editor/MeshUtility/Vrm10MeshUtilityDialog.cs +++ b/Assets/VRM10/Editor/MeshUtility/Vrm10MeshUtilityDialog.cs @@ -13,9 +13,8 @@ namespace UniVRM10 const string TITLE = "Vrm10 Mesh Utility Window"; enum Tabs { - MeshFreeze, - MeshIntegration, - MeshSplitter, + Freeze, + IntegrateSplit, } Tabs _tab; @@ -78,7 +77,7 @@ namespace UniVRM10 switch (_tab) { - case Tabs.MeshFreeze: + case Tabs.Freeze: { if (MeshFreezeGui()) { @@ -87,7 +86,7 @@ namespace UniVRM10 break; } - case Tabs.MeshIntegration: + case Tabs.IntegrateSplit: { if (MeshIntegrateGui()) { @@ -95,15 +94,6 @@ namespace UniVRM10 } break; } - - case Tabs.MeshSplitter: - { - if (MeshSplitGui()) - { - modified = true; - } - break; - } } EditorGUILayout.EndScrollView(); @@ -147,6 +137,7 @@ namespace UniVRM10 bool MeshIntegrateGui() { var firstPerson = ToggleIsModified("FirstPerson == AUTO の生成", ref _meshUtility.GenerateMeshForFirstPersonAuto); + var split = ToggleIsModified("Separate by BlendShape", ref _meshUtility.SplitByBlendShape); var p = position; var last = GUILayoutUtility.GetLastRect(); var y = last.y + last.height; @@ -160,12 +151,7 @@ namespace UniVRM10 - 30 }; var mod = _meshIntegration.OnGui(rect); - return firstPerson || mod; - } - - bool MeshSplitGui() - { - return ToggleIsModified("Separate by BlendShape", ref _meshUtility.SplitByBlendShape); + return firstPerson || split || mod; } } } \ No newline at end of file diff --git a/Assets/VRM10/Runtime/MeshUtility/Vrm10MeshUtility.cs b/Assets/VRM10/Runtime/MeshUtility/Vrm10MeshUtility.cs index 021bdc8a3..ab19245e6 100644 --- a/Assets/VRM10/Runtime/MeshUtility/Vrm10MeshUtility.cs +++ b/Assets/VRM10/Runtime/MeshUtility/Vrm10MeshUtility.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using UniGLTF; using UniGLTF.MeshUtility; using UniHumanoid; using UnityEngine; @@ -117,56 +118,98 @@ namespace UniVRM10 } } - public void Process(GameObject go) + void RemoveComponent(T c) where T : Component { - // TODO: UNDO - - if (ForceUniqueName) - { - throw new NotImplementedException(); - } - - // 正規化されたヒエラルキーを作る - var (normalized, boneMap) = BoneNormalizer.NormalizeHierarchyFreezeMesh(go, - removeScaling: FreezeScaling, - removeRotation: FreezeRotation, - freezeBlendShape: FreezeBlendShape); - - // TODO: update: spring - // TODO: update: constraint - // TODO: update: firstPoint offset - // write back normalized transform to boneMap - BoneNormalizer.WriteBackResult(go, normalized, boneMap); + var t = c.transform; if (Application.isPlaying) { - GameObject.Destroy(normalized); + GameObject.Destroy(c); } else { - GameObject.DestroyImmediate(normalized); + GameObject.DestroyImmediate(c); } - var animator = go.GetComponent(); - var newAvatar = AvatarDescription.RecreateAvatar(animator); - animator.avatar = newAvatar; - - // TODO: integration - foreach (var group in MeshIntegrationGroups) + if (t.childCount == 0) { - var result = MeshIntegrator.Integrate(group, true); - // TODO: firstperson - - // TODO: split - if (SplitByBlendShape) + var list = t.GetComponents(); + // Debug.Log($"{list[0].GetType().Name}"); + if (list.Length == 1 && list[0] == t) { - // var withBlendShape, withoutBlendShape + if (Application.isPlaying) + { + GameObject.Destroy(t.gameObject); + } + else + { + GameObject.DestroyImmediate(t.gameObject); + } + } + } + } + + public void Process(GameObject go) + { + // TODO unpack prefab + + if (ForceUniqueName) + { + // TODO: UNDO + throw new NotImplementedException(); + + // 必用? + var animator = go.GetComponent(); + var newAvatar = AvatarDescription.RecreateAvatar(animator); + animator.avatar = newAvatar; + } + + // 正規化されたヒエラルキーを作る + if (FreezeBlendShape || FreezeRotation || FreezeScaling) + { + // TODO: UNDO + var (normalized, boneMap) = BoneNormalizer.NormalizeHierarchyFreezeMesh(go, + removeScaling: FreezeScaling, + removeRotation: FreezeRotation, + freezeBlendShape: FreezeBlendShape); + + // TODO: update: spring + // TODO: update: constraint + // TODO: update: firstPoint offset + // write back normalized transform to boneMap + BoneNormalizer.WriteBackResult(go, normalized, boneMap); + if (Application.isPlaying) + { + GameObject.Destroy(normalized); } else { + GameObject.DestroyImmediate(normalized); } - // TODO: remove old renderer - result.AddIntegratedRendererTo(go); + var animator = go.GetComponent(); + var newAvatar = AvatarDescription.RecreateAvatar(animator); + animator.avatar = newAvatar; + } + + { + // TODO: UNDO + foreach (var group in MeshIntegrationGroups) + { + var result = MeshIntegrator.Integrate(group, SplitByBlendShape + ? MeshIntegrator.BlendShapeOperation.Split + : MeshIntegrator.BlendShapeOperation.None); + + foreach (var r in result.SourceMeshRenderers) + { + RemoveComponent(r); + } + foreach (var r in result.SourceSkinnedMeshRenderers) + { + RemoveComponent(r); + } + + result.AddIntegratedRendererTo(go); + } } } }