diff --git a/Assets/UniGLTF/Runtime/MeshUtility/BoneNormalizer.cs b/Assets/UniGLTF/Runtime/MeshUtility/BoneNormalizer.cs index a94a45c7f..f9d5a2cfb 100644 --- a/Assets/UniGLTF/Runtime/MeshUtility/BoneNormalizer.cs +++ b/Assets/UniGLTF/Runtime/MeshUtility/BoneNormalizer.cs @@ -71,7 +71,7 @@ namespace UniGLTF.MeshUtility } } - public static MeshAttachInfo CreateMeshInfo(Transform src, Dictionary boneMap, bool freezeBlendShape) + public static MeshAttachInfo CreateMeshInfo(Transform src, Dictionary boneMap) { Transform dst; if (!boneMap.TryGetValue(src, out dst)) @@ -81,11 +81,9 @@ namespace UniGLTF.MeshUtility // SkinnedMeshRenderer var smr = src.GetComponent(); - var (mesh, dstBones) = MeshFreezer.NormalizeSkinnedMesh( + var mesh = MeshFreezer.NormalizeSkinnedMesh( smr, - boneMap, - dst.localToWorldMatrix, - freezeBlendShape); + boneMap); if (mesh != null) { var info = new MeshAttachInfo @@ -132,8 +130,7 @@ namespace UniGLTF.MeshUtility public static (GameObject, Dictionary, Dictionary) NormalizeHierarchyFreezeMesh( GameObject go, bool removeScaling = true, - bool removeRotation = true, - bool freezeBlendShape = true + bool removeRotation = true ) { // @@ -147,7 +144,7 @@ namespace UniGLTF.MeshUtility var result = new Dictionary(); foreach (var src in go.transform.Traverse()) { - var info = CreateMeshInfo(src, boneMap, freezeBlendShape); + var info = CreateMeshInfo(src, boneMap); if (info != null) { result.Add(src, info); diff --git a/Assets/UniGLTF/Runtime/MeshUtility/GltfMeshUtility.cs b/Assets/UniGLTF/Runtime/MeshUtility/GltfMeshUtility.cs index 9960b6058..d230b666e 100644 --- a/Assets/UniGLTF/Runtime/MeshUtility/GltfMeshUtility.cs +++ b/Assets/UniGLTF/Runtime/MeshUtility/GltfMeshUtility.cs @@ -164,8 +164,7 @@ namespace UniGLTF.MeshUtility { var (normalized, boneMap, newMesh) = BoneNormalizer.NormalizeHierarchyFreezeMesh(go, removeScaling: FreezeScaling, - removeRotation: FreezeRotation, - freezeBlendShape: FreezeBlendShape); + removeRotation: FreezeRotation); foreach (var (k, v) in newMesh) { diff --git a/Assets/UniGLTF/Runtime/MeshUtility/MeshAttachInfo.cs b/Assets/UniGLTF/Runtime/MeshUtility/MeshAttachInfo.cs index 0eb16c63f..16b4f0fdf 100644 --- a/Assets/UniGLTF/Runtime/MeshUtility/MeshAttachInfo.cs +++ b/Assets/UniGLTF/Runtime/MeshUtility/MeshAttachInfo.cs @@ -1,3 +1,4 @@ +using System.Linq; using UnityEngine; namespace UniGLTF.MeshUtility @@ -12,6 +13,9 @@ namespace UniGLTF.MeshUtility { if (Bones != null) { + // recalc bindposes + Mesh.bindposes = Bones.Select(x => x.worldToLocalMatrix * dst.transform.localToWorldMatrix).ToArray(); + var dstRenderer = dst.AddComponent(); dstRenderer.sharedMesh = Mesh; dstRenderer.sharedMaterials = Materials; diff --git a/Assets/UniGLTF/Runtime/MeshUtility/MeshFreezer.cs b/Assets/UniGLTF/Runtime/MeshUtility/MeshFreezer.cs index 98c4f8af1..4b65f6094 100644 --- a/Assets/UniGLTF/Runtime/MeshUtility/MeshFreezer.cs +++ b/Assets/UniGLTF/Runtime/MeshUtility/MeshFreezer.cs @@ -126,14 +126,11 @@ namespace UniGLTF.MeshUtility /// /// /// 正規化前のボーンから正規化後のボーンを得る - /// /// /// - public static (Mesh, Transform[]) NormalizeSkinnedMesh( + public static Mesh NormalizeSkinnedMesh( SkinnedMeshRenderer src, - Dictionary boneMap, - Matrix4x4 dstLocalToWorldMatrix, - bool FreezeBlendShape = true) + Dictionary boneMap) { if (src == null || !src.enabled @@ -180,16 +177,6 @@ namespace UniGLTF.MeshUtility src.sharedMesh = srcMesh; } - var blendShapeBackup = new List(); - if (!FreezeBlendShape) - { - for (int i = 0; i < srcMesh.blendShapeCount; ++i) - { - blendShapeBackup.Add(src.GetBlendShapeWeight(i)); - src.SetBlendShapeWeight(i, 0); - } - } - // BakeMesh var mesh = srcMesh.Copy(false); mesh.name = srcMesh.name + ".baked"; @@ -198,9 +185,6 @@ namespace UniGLTF.MeshUtility // 新しい骨格のボーンウェイトを作成する mesh.boneWeights = MapBoneWeight(srcMesh.boneWeights, boneMap, src.bones, dstBones); - // recalc bindposes - mesh.bindposes = dstBones.Select(x => x.worldToLocalMatrix * dstLocalToWorldMatrix).ToArray(); - //var m = src.localToWorldMatrix; // include scaling var m = default(Matrix4x4); m.SetTRS(Vector3.zero, src.transform.rotation, Vector3.one); // rotation only @@ -211,10 +195,19 @@ namespace UniGLTF.MeshUtility // CopyBlendShapes(src, srcMesh, mesh, m); - for (int i = 0; i < blendShapeBackup.Count; ++i) - { - src.SetBlendShapeWeight(i, blendShapeBackup[i]); - } + // var blendShapeBackup = new List(); + // if (!FreezeBlendShape) + // { + // for (int i = 0; i < srcMesh.blendShapeCount; ++i) + // { + // blendShapeBackup.Add(src.GetBlendShapeWeight(i)); + // src.SetBlendShapeWeight(i, 0); + // } + // } + // for (int i = 0; i < blendShapeBackup.Count; ++i) + // { + // src.SetBlendShapeWeight(i, blendShapeBackup[i]); + // } if (!hasBoneWeight) { @@ -223,7 +216,7 @@ namespace UniGLTF.MeshUtility src.sharedMesh = originalSrcMesh; } - return (mesh, dstBones); + return mesh; } private static void CopyBlendShapes(SkinnedMeshRenderer src, Mesh srcMesh, Mesh mesh, Matrix4x4 m)