Last active
May 6, 2023 15:41
-
-
Save lilxyzw/094162cd16987b3ce8b19befc6c49f1b to your computer and use it in GitHub Desktop.
BlendShapeGatherer.cs( https://gist.github.com/lilxyzw/d6ac5cc6b1e04dd501c534f0aa6fee95 )がベースです。適当にフォルダ作って2つのファイルを入れれば動くはず。Hierarchyでアバターを右クリックして`AutoFreezeBlendShapes`で自動設定されます。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| This is free and unencumbered software released into the public domain. | |
| Anyone is free to copy, modify, publish, use, compile, sell, or | |
| distribute this software, either in source code form or as a compiled | |
| binary, for any purpose, commercial or non-commercial, and by any | |
| means. | |
| In jurisdictions that recognize copyright laws, the author or authors | |
| of this software dedicate any and all copyright interest in the | |
| software to the public domain. We make this dedication for the benefit | |
| of the public at large and to the detriment of our heirs and | |
| successors. We intend this dedication to be an overt act of | |
| relinquishment in perpetuity of all present and future rights to this | |
| software under copyright law. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
| EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
| MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
| IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
| OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
| ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
| OTHER DEALINGS IN THE SOFTWARE. | |
| For more information, please refer to <http://unlicense.org/> | |
| */ | |
| #if UNITY_EDITOR | |
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using UnityEditor; | |
| using UnityEngine; | |
| using VRC.SDK3.Avatars.Components; | |
| using VRC.SDKBase; | |
| using Anatawa12.AvatarOptimizer.PrefabSafeSet; | |
| using System.Reflection; | |
| /* | |
| # Freeze BlendShape自動化で考慮すべき要素 | |
| - SkinnedMeshRendererの初期値 | |
| - Animator | |
| - Playable Layers | |
| - LipSync | |
| - Eye Look/Eyelids | |
| # Merge Bone自動化で考慮すべき要素 | |
| - Componentがあるか | |
| - Animator | |
| - Humanoid | |
| - Playable Layers | |
| - LipSync | |
| - Eye Look | |
| */ | |
| namespace AutoOptimizer | |
| { | |
| public class AutoBlendShapeFreezer | |
| { | |
| [MenuItem("GameObject/AutoFreezeBlendShapes", false, 21)] | |
| private static void AutoFreezeBlendShapes() | |
| { | |
| var obj = (GameObject)Selection.activeObject; | |
| AutoFreezeBlendShapes(obj); | |
| EditorUtility.DisplayDialog("AutoBlendShapeFreezer", "Finished!", "OK"); | |
| } | |
| public static void AutoFreezeBlendShapes(GameObject obj) | |
| { | |
| var usedBlendShapes = GatherUsedBlendShapes(obj); | |
| var skinnedMeshRenderers = obj.GetComponentsInChildren<SkinnedMeshRenderer>(true); | |
| foreach(var skinnedMeshRenderer in skinnedMeshRenderers) | |
| { | |
| var mesh = skinnedMeshRenderer.sharedMesh; | |
| if(mesh == null || mesh.blendShapeCount == 0) continue; | |
| var shapes = Enumerable.Range(0,mesh.blendShapeCount).Select(index => mesh.GetBlendShapeName(index)).ToArray(); | |
| var freezeBlendShape = InitializeFreezeBlendShape(skinnedMeshRenderer, mesh, shapes); | |
| var so = new SerializedObject(freezeBlendShape); | |
| so.Update(); | |
| var shapeKeysSet = EditorUtil<string>.Create( | |
| so.FindProperty("shapeKeysSet"), | |
| PrefabSafeSetUtil.PrefabNestCount(so.targetObject), | |
| x => x.stringValue, | |
| (x, v) => x.stringValue = v | |
| ); | |
| if(usedBlendShapes.ContainsKey(skinnedMeshRenderer)) | |
| { | |
| var usedShapes = usedBlendShapes[skinnedMeshRenderer]; | |
| foreach(var shape in shapes) | |
| shapeKeysSet.GetElementOf(shape).SetExistence(!usedShapes.Contains(shape)); | |
| } | |
| else | |
| { | |
| foreach(var shape in shapes) | |
| shapeKeysSet.GetElementOf(shape).SetExistence(true); | |
| } | |
| so.ApplyModifiedProperties(); | |
| if(so.FindProperty("shapeKeysSet.mainSet").arraySize == 0) | |
| { | |
| UnityEngine.Object.Destroy(freezeBlendShape); | |
| } | |
| } | |
| } | |
| private static Component InitializeFreezeBlendShape(SkinnedMeshRenderer skinnedMeshRenderer, Mesh mesh, string[] shapes) | |
| { | |
| var type = Assembly.Load("com.anatawa12.avatar-optimizer.runtime").GetType("Anatawa12.AvatarOptimizer.FreezeBlendShape"); | |
| var freezeBlendShape = skinnedMeshRenderer.gameObject.GetComponent(type); | |
| if(freezeBlendShape == null) | |
| { | |
| freezeBlendShape = skinnedMeshRenderer.gameObject.AddComponent(type); | |
| } | |
| return freezeBlendShape; | |
| } | |
| // ここに無いBlendShapeは固定して良い(はず) | |
| // MMDのBlendShapeなど残しておきたいものがあれば適宜追加 | |
| private static Dictionary<SkinnedMeshRenderer, HashSet<string>> GatherUsedBlendShapes(GameObject obj) | |
| { | |
| var usedBlendShapes = new Dictionary<SkinnedMeshRenderer, HashSet<string>>(); | |
| var descriptor = obj.GetComponent<VRCAvatarDescriptor>(); | |
| var skinnedMeshRenderers = obj.GetComponentsInChildren<SkinnedMeshRenderer>(true); | |
| // SkinnedMeshRendererの初期値とAnimationClipから収集 | |
| var blendShapeValues = GatherBlendShapeValues(obj); | |
| foreach(var pair in blendShapeValues) | |
| { | |
| if(pair.Value.Count() <= 1) continue; | |
| AddBlendShape(usedBlendShapes, pair.Key.Item1, pair.Key.Item2); | |
| } | |
| if(descriptor == null) return usedBlendShapes; | |
| // AvatarDescriptorから収集 | |
| if( | |
| descriptor.lipSync == VRC_AvatarDescriptor.LipSyncStyle.VisemeBlendShape && | |
| descriptor.VisemeSkinnedMesh != null && | |
| skinnedMeshRenderers.Contains(descriptor.VisemeSkinnedMesh) | |
| ) | |
| { | |
| var skinnedMeshRenderer = descriptor.VisemeSkinnedMesh; | |
| var mesh = skinnedMeshRenderer.sharedMesh; | |
| foreach(var shape in descriptor.VisemeBlendShapes) | |
| { | |
| if(mesh.GetBlendShapeIndex(shape) == -1) continue; | |
| AddBlendShape(usedBlendShapes, skinnedMeshRenderer, shape); | |
| } | |
| } | |
| if( | |
| descriptor.lipSync == VRC_AvatarDescriptor.LipSyncStyle.JawFlapBlendShape && | |
| descriptor.VisemeSkinnedMesh != null && | |
| skinnedMeshRenderers.Contains(descriptor.VisemeSkinnedMesh) | |
| ) | |
| { | |
| var skinnedMeshRenderer = descriptor.VisemeSkinnedMesh; | |
| var mesh = skinnedMeshRenderer.sharedMesh; | |
| var shape = descriptor.MouthOpenBlendShapeName; | |
| if(mesh.GetBlendShapeIndex(shape) != -1) | |
| { | |
| AddBlendShape(usedBlendShapes, skinnedMeshRenderer, shape); | |
| } | |
| } | |
| if( | |
| descriptor.customEyeLookSettings.eyelidType == VRCAvatarDescriptor.EyelidType.Blendshapes && | |
| descriptor.customEyeLookSettings.eyelidsSkinnedMesh != null && | |
| skinnedMeshRenderers.Contains(descriptor.customEyeLookSettings.eyelidsSkinnedMesh) | |
| ) | |
| { | |
| var skinnedMeshRenderer = descriptor.customEyeLookSettings.eyelidsSkinnedMesh; | |
| var mesh = skinnedMeshRenderer.sharedMesh; | |
| foreach(var index in descriptor.customEyeLookSettings.eyelidsBlendshapes) | |
| { | |
| if(index > mesh.blendShapeCount) continue; | |
| var shape = mesh.GetBlendShapeName(index); | |
| AddBlendShape(usedBlendShapes, skinnedMeshRenderer, shape); | |
| } | |
| } | |
| return usedBlendShapes; | |
| } | |
| private static void AddBlendShape(Dictionary<SkinnedMeshRenderer, HashSet<string>> usedBlendShapes, SkinnedMeshRenderer skinnedMeshRenderer, string shape) | |
| { | |
| if(!usedBlendShapes.ContainsKey(skinnedMeshRenderer)) | |
| { | |
| usedBlendShapes.Add(skinnedMeshRenderer, new HashSet<string>()); | |
| } | |
| usedBlendShapes[skinnedMeshRenderer].Add(shape); | |
| } | |
| // BlendShapeが取りうる値のまとめ | |
| private static Dictionary<(SkinnedMeshRenderer, string), HashSet<float>> GatherBlendShapeValues(GameObject obj) | |
| { | |
| var blendShapeValues = new Dictionary<(SkinnedMeshRenderer, string), HashSet<float>>(); | |
| var descriptor = obj.GetComponent<VRCAvatarDescriptor>(); | |
| foreach(var skinnedMeshRenderer in obj.GetComponentsInChildren<SkinnedMeshRenderer>(true)) | |
| { | |
| GatherBlendShapeValues(blendShapeValues, skinnedMeshRenderer); | |
| } | |
| foreach(var animator in obj.GetComponentsInChildren<Animator>(true)) | |
| { | |
| GatherBlendShapeValues(blendShapeValues, animator.gameObject, animator.runtimeAnimatorController); | |
| } | |
| if(descriptor == null) return blendShapeValues; | |
| foreach(var layer in descriptor.specialAnimationLayers) | |
| { | |
| GatherBlendShapeValues(blendShapeValues, obj, layer.animatorController); | |
| } | |
| if(descriptor.customizeAnimationLayers) | |
| { | |
| foreach(var layer in descriptor.baseAnimationLayers) | |
| { | |
| GatherBlendShapeValues(blendShapeValues, obj, layer.animatorController); | |
| } | |
| } | |
| return blendShapeValues; | |
| } | |
| private static void GatherBlendShapeValues(Dictionary<(SkinnedMeshRenderer, string), HashSet<float>> blendShapeValues, GameObject obj, RuntimeAnimatorController controller) | |
| { | |
| if(controller == null) return; | |
| foreach(var clip in controller.animationClips) | |
| { | |
| foreach(EditorCurveBinding binding in AnimationUtility.GetCurveBindings(clip)) | |
| { | |
| if(binding.type != typeof(SkinnedMeshRenderer) || !binding.propertyName.StartsWith("blendShape.")) | |
| { | |
| continue; | |
| } | |
| var skinnedMeshRenderer = (SkinnedMeshRenderer)AnimationUtility.GetAnimatedObject(obj, binding); | |
| if(skinnedMeshRenderer == null) | |
| { | |
| continue; | |
| } | |
| string shapeName = binding.propertyName.Substring(11); | |
| var curve = AnimationUtility.GetEditorCurve(clip, binding); | |
| var shape = (skinnedMeshRenderer, shapeName); | |
| if(!blendShapeValues.ContainsKey(shape)) blendShapeValues.Add(shape, new HashSet<float>()); | |
| foreach(var value in curve.keys.Select(key => key.value)) | |
| { | |
| blendShapeValues[shape].Add(value); | |
| } | |
| } | |
| } | |
| } | |
| private static void GatherBlendShapeValues(Dictionary<(SkinnedMeshRenderer, string), HashSet<float>> blendShapeValues, SkinnedMeshRenderer skinnedMeshRenderer) | |
| { | |
| var mesh = skinnedMeshRenderer.sharedMesh; | |
| if(mesh == null) return; | |
| for(int i = 0; i < mesh.blendShapeCount; i++) | |
| { | |
| float value = skinnedMeshRenderer.GetBlendShapeWeight(i); | |
| string shapeName = mesh.GetBlendShapeName(i); | |
| var shape = (skinnedMeshRenderer, shapeName); | |
| if(!blendShapeValues.ContainsKey(shape)) blendShapeValues.Add(shape, new HashSet<float>()); | |
| blendShapeValues[shape].Add(value); | |
| } | |
| } | |
| } | |
| } | |
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "AutoOptimizer", | |
| "references": [ | |
| "GUID:b9880ca0b6584453a2627bd3c038759f" | |
| ], | |
| "includePlatforms": [ | |
| "Editor" | |
| ], | |
| "excludePlatforms": [], | |
| "allowUnsafeCode": false, | |
| "overrideReferences": false, | |
| "precompiledReferences": [], | |
| "autoReferenced": false, | |
| "defineConstraints": [], | |
| "versionDefines": [], | |
| "noEngineReferences": false | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment