Files
2026-03-04 09:37:33 +08:00

71 lines
2.7 KiB
C#

using System.Collections.Generic;
using KINEMATION.KAnimationCore.Runtime.Rig;
using Unity.Collections;
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Playables;
namespace KINEMATION.MagicBlend.Runtime
{
public class MagicBlendLibrary
{
public static NativeArray<BlendStreamAtom> SetupBlendAtoms(Animator animator, KRigComponent rigComponent)
{
Transform[] rigTransforms = rigComponent.GetRigTransforms();
int num = rigTransforms.Length;
NativeArray<BlendStreamAtom> result = new NativeArray<BlendStreamAtom>(num, Allocator.Persistent);
for (int i = 0; i < num; i++)
{
Transform transform = rigTransforms[i];
result[i] = new BlendStreamAtom
{
handle = animator.BindStreamTransform(transform)
};
}
return result;
}
public static void ConnectPose(AnimationScriptPlayable playable, PlayableGraph graph, AnimationClip pose, float speed = 0f)
{
if (playable.GetInput(0).IsValid())
{
playable.DisconnectInput(0);
}
AnimationClipPlayable animationClipPlayable = AnimationClipPlayable.Create(graph, pose);
animationClipPlayable.SetSpeed(speed);
animationClipPlayable.SetApplyFootIK(value: false);
playable.ConnectInput(0, animationClipPlayable, 0, 1f);
}
public static void ConnectOverlays(AnimationScriptPlayable playable, PlayableGraph graph, AnimationClip pose, List<OverrideOverlay> overrides, float speed = 0f)
{
if (playable.GetInput(0).IsValid())
{
playable.DisconnectInput(0);
}
if (overrides == null || overrides.Count == 0)
{
ConnectPose(playable, graph, pose, speed);
return;
}
AnimationLayerMixerPlayable animationLayerMixerPlayable = AnimationLayerMixerPlayable.Create(graph);
AnimationClipPlayable animationClipPlayable = AnimationClipPlayable.Create(graph, pose);
animationClipPlayable.SetDuration(pose.length);
animationClipPlayable.SetSpeed(speed);
animationClipPlayable.SetApplyFootIK(value: false);
animationLayerMixerPlayable.AddInput(animationClipPlayable, 0, 1f);
foreach (OverrideOverlay @override in overrides)
{
AnimationClipPlayable animationClipPlayable2 = AnimationClipPlayable.Create(graph, @override.overlay);
animationClipPlayable2.SetDuration(@override.overlay.length);
animationClipPlayable2.SetSpeed(speed);
animationClipPlayable2.SetApplyFootIK(value: false);
int layerIndex = animationLayerMixerPlayable.AddInput(animationClipPlayable2, 0, @override.weight);
AvatarMask mask = ((@override.mask == null) ? new AvatarMask() : @override.mask);
animationLayerMixerPlayable.SetLayerMaskFromAvatarMask((uint)layerIndex, mask);
}
playable.ConnectInput(0, animationLayerMixerPlayable, 0, 1f);
}
}
}