modular-avatar/Editor/ScaleAdjusterPass.cs
bd_ f077d24d48
fix: improved Scale Adjuster implementation (#730)
This new implementation avoids creating any internal objects within the avatar
itself, instead creating them as hidden scene root objects. We also move all update
processing to be driven by camera callbacks as well. These changes help improve
compatibility with tools such as FaceEmo, as well as being less likely to break when
other tools manipulate the avatar's hierarchy directly.
2024-03-05 17:19:54 +09:00

62 lines
2.0 KiB
C#

#region
using System.Collections.Generic;
using nadena.dev.ndmf;
using UnityEngine;
#endregion
namespace nadena.dev.modular_avatar.core.editor
{
internal class ScaleAdjusterPass : Pass<ScaleAdjusterPass>
{
protected override void Execute(ndmf.BuildContext context)
{
Dictionary<Transform, Transform> boneMappings = new Dictionary<Transform, Transform>();
foreach (var adjuster in context.AvatarRootObject.GetComponentsInChildren<ModularAvatarScaleAdjuster>(true))
{
var proxyObject = new GameObject("ScaleProxy");
var proxyTransform = proxyObject.transform;
proxyTransform.SetParent(adjuster.transform, false);
proxyTransform.localPosition = Vector3.zero;
proxyTransform.localRotation = Quaternion.identity;
proxyTransform.localScale = adjuster.Scale;
boneMappings.Add(adjuster.transform, proxyTransform);
Object.DestroyImmediate(adjuster);
}
// Legacy cleanup
foreach (var sar in context.AvatarRootObject.GetComponentsInChildren<ScaleAdjusterRenderer>())
{
Object.DestroyImmediate(sar.gameObject);
}
foreach (var sar in context.AvatarRootObject.GetComponentsInChildren<ScaleProxy>())
{
Object.DestroyImmediate(sar.gameObject);
}
if (boneMappings.Count == 0)
{
return;
}
foreach (var smr in context.AvatarRootObject.GetComponentsInChildren<SkinnedMeshRenderer>())
{
var bones = smr.bones;
for (int i = 0; i < bones.Length; i++)
{
if (bones[i] != null && boneMappings.TryGetValue(bones[i], out var newBone))
{
bones[i] = newBone;
}
}
smr.bones = bones;
}
}
}
}