From 30f893fa1be0dfa1cf66cb51d965a1db9aa002df Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 4 Jan 2024 16:15:20 +0900 Subject: [PATCH 1/2] =?UTF-8?q?avatar=20=E4=BB=A3=E5=85=A5=E3=81=AE=20work?= =?UTF-8?q?around?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/VRM/Editor/Format/VRMEditorExporter.cs | 6 ++--- .../Format/VRMHumanoidNormalizerMenu.cs | 3 +-- .../SkinnedMeshUtility/VRMBoneNormalizer.cs | 24 ++++++++++++------- .../VRMRuntimeExporter.cs | 12 ++-------- 4 files changed, 21 insertions(+), 24 deletions(-) diff --git a/Assets/VRM/Editor/Format/VRMEditorExporter.cs b/Assets/VRM/Editor/Format/VRMEditorExporter.cs index 74bc3a710..f727df52f 100644 --- a/Assets/VRM/Editor/Format/VRMEditorExporter.cs +++ b/Assets/VRM/Editor/Format/VRMEditorExporter.cs @@ -199,12 +199,10 @@ namespace VRM } } - // 正規化 if (settings.PoseFreeze) { - // BoneNormalizer.Execute は Copy を作って正規化する。UNDO無用 - target = VRMBoneNormalizer.Execute(target, settings.ForceTPose); - destroy.Add(target); + // 正規化 + VRMBoneNormalizer.Execute(target, settings.ForceTPose); } var fp = target.GetComponent(); diff --git a/Assets/VRM/Editor/Format/VRMHumanoidNormalizerMenu.cs b/Assets/VRM/Editor/Format/VRMHumanoidNormalizerMenu.cs index 47c8665ff..777de6e79 100644 --- a/Assets/VRM/Editor/Format/VRMHumanoidNormalizerMenu.cs +++ b/Assets/VRM/Editor/Format/VRMHumanoidNormalizerMenu.cs @@ -46,8 +46,7 @@ namespace VRM { var go = Selection.activeObject as GameObject; - // BoneNormalizer.Execute はコピーを正規化する。UNDO無用 - Selection.activeGameObject = VRMBoneNormalizer.Execute(go, true); + VRMBoneNormalizer.Execute(go, true); } } } diff --git a/Assets/VRM/Runtime/SkinnedMeshUtility/VRMBoneNormalizer.cs b/Assets/VRM/Runtime/SkinnedMeshUtility/VRMBoneNormalizer.cs index 81f630c52..a9075becb 100644 --- a/Assets/VRM/Runtime/SkinnedMeshUtility/VRMBoneNormalizer.cs +++ b/Assets/VRM/Runtime/SkinnedMeshUtility/VRMBoneNormalizer.cs @@ -45,7 +45,7 @@ namespace VRM /// 対象モデルのルート /// 強制的にT-Pose化するか /// 正規化済みのモデル - public static GameObject Execute(GameObject go, bool forceTPose) + public static void Execute(GameObject go, bool forceTPose) { // // T-Poseにする @@ -67,18 +67,26 @@ namespace VRM } // Meshの焼きこみ - var newMesh = BoneNormalizer.NormalizeHierarchyFreezeMesh(go, true); + var newMeshMap = BoneNormalizer.NormalizeHierarchyFreezeMesh(go, true); // 焼いたMeshで置き換える - BoneNormalizer.Replace(go, newMesh, true, true); + BoneNormalizer.Replace(go, newMeshMap, true, true); // 新しいヒエラルキーからAvatarを作る - var newAnimator = go.GetComponent(); - var newAvatar = UniHumanoid.AvatarDescription.RecreateAvatar(newAnimator); - newAnimator.avatar = newAvatar; + var animator = go.GetComponent(); + var newAvatar = UniHumanoid.AvatarDescription.RecreateAvatar(animator); - // CopyVRMComponents(go, normalized, bMap); + // workaround: animator を作り直す + if (Application.isPlaying) + { + GameObject.Destroy(animator); + } + else + { + GameObject.DestroyImmediate(animator); + } + animator = go.AddComponent(); - return go; + animator.avatar = newAvatar; } /// diff --git a/Assets/VRM_Samples/RuntimeExporterSample/VRMRuntimeExporter.cs b/Assets/VRM_Samples/RuntimeExporterSample/VRMRuntimeExporter.cs index 3d396b37b..7eaf769bc 100644 --- a/Assets/VRM_Samples/RuntimeExporterSample/VRMRuntimeExporter.cs +++ b/Assets/VRM_Samples/RuntimeExporterSample/VRMRuntimeExporter.cs @@ -141,17 +141,9 @@ namespace VRM.RuntimeExporterSample static byte[] ExportCustom(GameObject exportRoot, bool forceTPose = false) { // normalize - var target = VRMBoneNormalizer.Execute(exportRoot, forceTPose); + VRMBoneNormalizer.Execute(exportRoot, forceTPose); - try - { - return ExportSimple(target); - } - finally - { - // cleanup - GameObject.Destroy(target); - } + return ExportSimple(exportRoot); } void OnExported(UniGLTF.glTF vrm) From 7cf69ad57aa264d7e646f01c4eb7e1ee707ac9ae Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 9 Jan 2024 13:40:41 +0900 Subject: [PATCH 2/2] update comment of VRMBoneNormalizer.Execute --- .../SkinnedMeshUtility/VRMBoneNormalizer.cs | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/Assets/VRM/Runtime/SkinnedMeshUtility/VRMBoneNormalizer.cs b/Assets/VRM/Runtime/SkinnedMeshUtility/VRMBoneNormalizer.cs index a9075becb..7f1a25d14 100644 --- a/Assets/VRM/Runtime/SkinnedMeshUtility/VRMBoneNormalizer.cs +++ b/Assets/VRM/Runtime/SkinnedMeshUtility/VRMBoneNormalizer.cs @@ -41,17 +41,26 @@ namespace VRM /// /// モデルの正規化を実行する + /// + /// v0.115 ヒエラルキーのコピーをしなくまりました(仕様変更) + /// v0.116 Animator.avatar 代入の副作用回避修正 + /// + /// v0.114以前: 非破壊 + /// - return コピーされて正規化されたヒエラルキー + /// v0.115以降: 対象のヒエラルキーが正規化されます。 + /// - Transform が変更されます。 + /// - Animator.avatar が差し替えられます。 + /// - SkinnedMeshRenderer.sharedMesh が差し替えられます。 + /// - MeshFilter.sharedMesh が差し替えられます。 + /// - return void /// /// 対象モデルのルート /// 強制的にT-Pose化するか - /// 正規化済みのモデル public static void Execute(GameObject go, bool forceTPose) { - // - // T-Poseにする - // if (forceTPose) { + // T-Poseにする var hips = go.GetComponent().GetBoneTransform(HumanBodyBones.Hips); var hipsPosition = hips.position; var hipsRotation = hips.rotation; @@ -66,16 +75,19 @@ namespace VRM } } - // Meshの焼きこみ + // Transform の回転とスケールを Mesh に適用します。 + // - 回転とスケールが反映された新しい Mesh が作成されます + // - Transform の回転とスケールはクリアされます。world position を維持します。 var newMeshMap = BoneNormalizer.NormalizeHierarchyFreezeMesh(go, true); - // 焼いたMeshで置き換える + + // SkinnedMeshRenderer.sharedMesh と MeshFilter.sharedMesh を新しいMeshで置き換える BoneNormalizer.Replace(go, newMeshMap, true, true); - // 新しいヒエラルキーからAvatarを作る + // 回転とスケールが除去された新しいヒエラルキーからAvatarを作る var animator = go.GetComponent(); var newAvatar = UniHumanoid.AvatarDescription.RecreateAvatar(animator); - // workaround: animator を作り直す + // Animator.avatar を代入したときに副作用でTransformが変更されるのを回避するために削除します。 if (Application.isPlaying) { GameObject.Destroy(animator);