From b160e4cfb8b4c503b39d36af26e73f0862f466e0 Mon Sep 17 00:00:00 2001 From: Kohei-Yanagida Date: Thu, 29 Aug 2019 22:14:06 +0900 Subject: [PATCH] fix original blendshap clip changed --- .../Scripts/Format/VRMExportSettings.cs | 35 +++++++++++++------ 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/Assets/VRM/UniVRM/Scripts/Format/VRMExportSettings.cs b/Assets/VRM/UniVRM/Scripts/Format/VRMExportSettings.cs index 1c0a3e463..720bbca57 100644 --- a/Assets/VRM/UniVRM/Scripts/Format/VRMExportSettings.cs +++ b/Assets/VRM/UniVRM/Scripts/Format/VRMExportSettings.cs @@ -133,7 +133,7 @@ namespace VRM var dstColliderGroup = dst.gameObject.AddComponent(); dstColliderGroup.Colliders = src.Colliders.Select(y => { - var offset = dst.worldToLocalMatrix.MultiplyPoint(src.transform.localToWorldMatrix.MultiplyPoint(y.Offset)); + var offset =dst.worldToLocalMatrix.MultiplyPoint(src.transform.localToWorldMatrix.MultiplyPoint(y.Offset)); return new VRMSpringBoneColliderGroup.SphereCollider { Offset = offset, @@ -279,29 +279,38 @@ namespace VRM if (ReduceBlendshapeSize) { var proxy = target.GetComponent(); - var blendShapClips = proxy.BlendShapeAvatar.Clips; - var skinMeshedRenderers = target.GetComponentsInChildren(); - + // 元のBlendShapeClipに変更を加えないように複製 + var copyBlendShapeAvatar = GameObject.Instantiate(proxy.BlendShapeAvatar); + var copyBlendShapClips = new List(); + + foreach (var clip in proxy.BlendShapeAvatar.Clips) + { + copyBlendShapClips.Add(GameObject.Instantiate(clip)); + } + + var skinnedMeshRenderers = target.GetComponentsInChildren(); + var names = new Dictionary(); var vs = new Dictionary(); var ns = new Dictionary(); var ts = new Dictionary(); - foreach (SkinnedMeshRenderer smr in skinMeshedRenderers) + foreach (SkinnedMeshRenderer smr in skinnedMeshRenderers) { Mesh mesh = smr.sharedMesh; if (mesh == null) continue; if (mesh.blendShapeCount == 0) continue; var copyMesh = mesh.Copy(true); - var vCount = copyMesh.vertexCount;names.Clear(); - + var vCount = copyMesh.vertexCount; + names.Clear(); + vs.Clear(); ns.Clear(); ts.Clear(); - var usedBlendshapeIndexArray = blendShapClips + var usedBlendshapeIndexArray = copyBlendShapClips .SelectMany(clip => clip.Values) .Where(val => target.transform.Find(val.RelativePath) == smr.transform) .Select(val => val.Index) @@ -323,7 +332,7 @@ namespace VRM } copyMesh.ClearBlendShapes(); - + foreach (var i in usedBlendshapeIndexArray) { copyMesh.AddBlendShapeFrame(names[i], 100f, vs[i], ns[i], ts[i]); @@ -333,7 +342,7 @@ namespace VRM .Select((x, i) => new {x, i}) .ToDictionary(pair => pair.x, pair => pair.i); - foreach (var clip in proxy.BlendShapeAvatar.Clips) + foreach (var clip in copyBlendShapClips) { for (var i = 0; i < clip.Values.Length; ++i) { @@ -344,6 +353,10 @@ namespace VRM } } + copyBlendShapeAvatar.Clips = copyBlendShapClips; + + proxy.BlendShapeAvatar = copyBlendShapeAvatar; + smr.sharedMesh = copyMesh; } } @@ -359,7 +372,7 @@ namespace VRM Debug.LogFormat("Export elapsed {0}", sw.Elapsed); } - // PrefabUtility.RevertPrefabInstance(target); + PrefabUtility.RevertPrefabInstance(target); if (path.StartsWithUnityAssetPath()) {