Skip to content

Instantly share code, notes, and snippets.

@lilxyzw
Last active February 26, 2023 11:35
Show Gist options
  • Select an option

  • Save lilxyzw/d6ac5cc6b1e04dd501c534f0aa6fee95 to your computer and use it in GitHub Desktop.

Select an option

Save lilxyzw/d6ac5cc6b1e04dd501c534f0aa6fee95 to your computer and use it in GitHub Desktop.
/*
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/>
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using VRC.SDK3.Avatars.Components;
using VRC.SDKBase;
public class BlendShapeGatherer
{
[MenuItem("GameObject/Test", false, 21)]
private static void Test()
{
var obj = (GameObject)Selection.activeObject;
var usedBlendShapes = GatherUsedBlendShapes(obj);
Debug.Log(
string.Join(
"\r\n",
usedBlendShapes.Select(pair => pair.Key.name + ": " + string.Join(", ", pair.Value))
)
);
}
// ここに無いBlendShapeは固定して良い(はず)
// MMDのBlendShapeなど残しておきたいものがあれば適宜追加
public 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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment