modular-avatar/Packages/nadena.dev.modular-avatar/Editor/MeshSettingsPass.cs

110 lines
3.5 KiB
C#
Raw Normal View History

2023-06-05 19:18:46 +08:00
using UnityEngine;
namespace nadena.dev.modular_avatar.core.editor
{
internal static class InheritModeExtension
{
internal static bool NotFinal(this ModularAvatarMeshSettings.InheritMode mode)
{
return mode == ModularAvatarMeshSettings.InheritMode.Inherit;
}
}
internal class MeshSettingsPass
{
private readonly BuildContext context;
public MeshSettingsPass(BuildContext context)
{
this.context = context;
}
public void OnPreprocessAvatar()
{
foreach (var mesh in context.AvatarDescriptor.GetComponentsInChildren<Renderer>(true))
{
ProcessMesh(mesh);
}
}
internal struct MergedSettings
{
public bool SetAnchor, SetBounds;
public Transform ProbeAnchor;
public Transform RootBone;
public Bounds Bounds;
}
private static bool Inherit(ref ModularAvatarMeshSettings.InheritMode mode,
ModularAvatarMeshSettings.InheritMode srcmode)
{
if (mode != ModularAvatarMeshSettings.InheritMode.Inherit ||
srcmode == ModularAvatarMeshSettings.InheritMode.Inherit)
return false;
mode = srcmode;
return true;
}
internal static MergedSettings MergeSettings(Transform avatarRoot, Transform referenceObject)
{
MergedSettings merged = new MergedSettings();
Transform current = referenceObject;
ModularAvatarMeshSettings.InheritMode inheritProbeAnchor = ModularAvatarMeshSettings.InheritMode.Inherit;
ModularAvatarMeshSettings.InheritMode inheritBounds = ModularAvatarMeshSettings.InheritMode.Inherit;
do
{
var settings = current.GetComponent<ModularAvatarMeshSettings>();
if (current == avatarRoot)
{
current = null;
}
else
{
current = current.transform.parent;
}
if (settings == null)
{
continue;
}
if (Inherit(ref inheritProbeAnchor, settings.InheritProbeAnchor))
{
merged.ProbeAnchor = settings.ProbeAnchor.Get(settings)?.transform;
}
if (Inherit(ref inheritBounds, settings.InheritBounds))
{
merged.RootBone = settings.RootBone.Get(settings)?.transform;
merged.Bounds = settings.Bounds;
}
} while (current != null && (inheritProbeAnchor.NotFinal() || inheritBounds.NotFinal()));
merged.SetAnchor = inheritProbeAnchor == ModularAvatarMeshSettings.InheritMode.Set;
merged.SetBounds = inheritBounds == ModularAvatarMeshSettings.InheritMode.Set;
return merged;
}
private void ProcessMesh(Renderer mesh)
{
MergedSettings settings = MergeSettings(context.AvatarDescriptor.transform, mesh.transform);
if (settings.SetAnchor)
{
mesh.probeAnchor = settings.ProbeAnchor;
}
if (settings.SetBounds && mesh is SkinnedMeshRenderer smr)
{
smr.rootBone = settings.RootBone;
smr.localBounds = settings.Bounds;
}
}
}
}