diff --git a/Assets/VRM/UniGLTF/Scripts/IO/MeshExporter.cs b/Assets/VRM/UniGLTF/Scripts/IO/MeshExporter.cs index f22021c01..fcccbbcd6 100644 --- a/Assets/VRM/UniGLTF/Scripts/IO/MeshExporter.cs +++ b/Assets/VRM/UniGLTF/Scripts/IO/MeshExporter.cs @@ -232,7 +232,7 @@ namespace UniGLTF }; } - public static void ExportMeshes(glTF gltf, int bufferIndex, + public static IEnumerable<(Mesh, glTFMesh, Dictionary)> ExportMeshes(glTF gltf, int bufferIndex, List unityMeshes, List unityMaterials, bool useSparseAccessorForMorphTarget, bool exportOnlyBlendShapePosition, @@ -248,6 +248,8 @@ namespace UniGLTF x.Renderer.name, mesh, materials, unityMaterials, removeVertexColor); + var blendShapeIndexMap = new Dictionary(); + int exportBlendShapes = 0; for (int j = 0; j < mesh.blendShapeCount; ++j) { var morphTarget = ExportMorphTarget(gltf, bufferIndex, @@ -255,6 +257,10 @@ namespace UniGLTF useSparseAccessorForMorphTarget, exportOnlyBlendShapePosition); + // maybe skip + + blendShapeIndexMap.Add(j, exportBlendShapes++); + // // all primitive has same blendShape // @@ -265,7 +271,7 @@ namespace UniGLTF } } - gltf.meshes.Add(gltfMesh); + yield return (mesh, gltfMesh, blendShapeIndexMap); } } } diff --git a/Assets/VRM/UniGLTF/Scripts/IO/gltfExporter.cs b/Assets/VRM/UniGLTF/Scripts/IO/gltfExporter.cs index 4e5eb6a98..c60cc83a6 100644 --- a/Assets/VRM/UniGLTF/Scripts/IO/gltfExporter.cs +++ b/Assets/VRM/UniGLTF/Scripts/IO/gltfExporter.cs @@ -27,7 +27,7 @@ namespace UniGLTF var go = Selection.activeObject as GameObject; if (go.transform.position == Vector3.zero && - go.transform.rotation == Quaternion.identity && + go.transform.rotation == Quaternion.identity && go.transform.localScale == Vector3.one) { var path = EditorUtility.SaveFilePanel( @@ -91,6 +91,18 @@ namespace UniGLTF private set; } + /// + /// Mesh毎に、元のBlendShapeIndex => ExportされたBlendShapeIndex の対応を記録する + /// + /// BlendShape が空の場合にスキップするので + /// + /// + public Dictionary> MeshBlendShapeIndexMap + { + get; + private set; + } + public List Nodes { get; @@ -261,8 +273,15 @@ namespace UniGLTF return true; }) .ToList(); - MeshExporter.ExportMeshes(gltf, bufferIndex, unityMeshes, Materials, useSparseAccessorForMorphTarget, - ExportOnlyBlendShapePosition, removeVertexColor); + + MeshBlendShapeIndexMap = new Dictionary>(); + foreach (var (mesh, gltfMesh, blendShapeIndexMap) in MeshExporter.ExportMeshes( + gltf, bufferIndex, unityMeshes, Materials, useSparseAccessorForMorphTarget, + ExportOnlyBlendShapePosition, removeVertexColor)) + { + gltf.meshes.Add(gltfMesh); + MeshBlendShapeIndexMap.Add(mesh, blendShapeIndexMap); + } Meshes = unityMeshes.Select(x => x.Mesh).ToList(); #endregion diff --git a/Assets/VRM/UniVRM/Scripts/Format/VRMExporter.cs b/Assets/VRM/UniVRM/Scripts/Format/VRMExporter.cs index 4f683aa1e..02cc32588 100644 --- a/Assets/VRM/UniVRM/Scripts/Format/VRMExporter.cs +++ b/Assets/VRM/UniVRM/Scripts/Format/VRMExporter.cs @@ -100,10 +100,9 @@ namespace VRM var avatar = master.BlendShapeAvatar; if (avatar != null) { - var meshes = exporter.Meshes; foreach (var x in avatar.Clips) { - gltf.extensions.VRM.blendShapeMaster.Add(x, exporter.Copy.transform, meshes); + gltf.extensions.VRM.blendShapeMaster.Add(x, exporter); } } } diff --git a/Assets/VRM/UniVRM/Scripts/Format/glTF_VRMExtensions.cs b/Assets/VRM/UniVRM/Scripts/Format/glTF_VRMExtensions.cs index b708500f2..d4dc817e8 100644 --- a/Assets/VRM/UniVRM/Scripts/Format/glTF_VRMExtensions.cs +++ b/Assets/VRM/UniVRM/Scripts/Format/glTF_VRMExtensions.cs @@ -10,12 +10,14 @@ namespace VRM public static class glTF_VRMExtensions { [Obsolete("Use Create(root, meshes, binding)")] - public static glTF_VRM_BlendShapeBind Cerate(Transform root, List meshes, BlendShapeBinding binding) + public static glTF_VRM_BlendShapeBind Cerate(Transform root, BlendShapeBinding binding, + gltfExporter exporter) { - return Create(root, meshes, binding); + return Create(root, binding, exporter); } - public static glTF_VRM_BlendShapeBind Create(Transform root, List meshes, BlendShapeBinding binding) + public static glTF_VRM_BlendShapeBind Create(Transform root, BlendShapeBinding binding, + gltfExporter exporter) { var transform = UniGLTF.UnityExtensions.GetFromPath(root.transform, binding.RelativePath); var renderer = transform.GetComponent(); @@ -24,35 +26,55 @@ namespace VRM return null; } - if(!renderer.gameObject.activeInHierarchy) + if (!renderer.gameObject.activeInHierarchy) { return null; } var mesh = renderer.sharedMesh; - var meshIndex = meshes.IndexOf(mesh); + var meshIndex = exporter.Meshes.IndexOf(mesh); if (meshIndex == -1) { return null; } + if(!exporter.MeshBlendShapeIndexMap.TryGetValue(mesh, out Dictionary blendShapeIndexMap)) + { + // この Mesh は エクスポートされていない + return null; + } + + if (!blendShapeIndexMap.TryGetValue(binding.Index, out int blendShapeIndex)) + { + // この blendShape は エクスポートされていない(空だった?) + return null; + } + return new glTF_VRM_BlendShapeBind { mesh = meshIndex, - index = binding.Index, + index = blendShapeIndex, weight = binding.Weight, }; } + /// + /// + /// + /// + /// + /// + /// + /// エクスポート中にBlendShapeIndexが変わったかもしれない public static void Add(this glTF_VRM_BlendShapeMaster master, - BlendShapeClip clip, Transform transform, List meshes) + BlendShapeClip clip, gltfExporter exporter) { var list = new List(); if (clip.Values != null) { foreach (var value in clip.Values) { - var bind = Create(transform, meshes, value); + var bind = Create(exporter.Copy.transform, value, exporter); if (bind == null) { // Debug.LogFormat("{0}: skip blendshapebind", clip.name);