// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
using System;
using System.Collections.Generic;
namespace Animancer
{
///
/// Fades the child weights of a to a new parameter value
/// instead of fading the actual parameter.
///
///
///
///
/// Documentation:
///
/// Smoothing
///
/// Example:
/// Imagine a Linear Mixer with thresholds 0, 1, 2 and child states A, B, C.
/// If you fade its Parameter from 0 to 1 the states would go from A to B to C.
/// But if you use this system instead, the states would go directly from A to C.
/// Usage
///
/// [SerializeField] private AnimancerComponent _Animancer;
/// [SerializeField] private LinearMixerTransition _Mixer;
///
/// public void FadeMixerTo(float parameter, float fadeDuration)
/// {
/// _Mixer.State.FadeChildWeights(parameter, fadeDuration);
/// }
///
///
/// https://kybernetik.com.au/animancer/api/Animancer/MixerChildFade
///
public static class MixerChildFade
{
/************************************************************************************************************************/
private static readonly List
ChildWeights = new();
/************************************************************************************************************************/
///
/// Fades the child weights of a to a new parameter value instead of fading
/// the actual parameter.
///
/// See for a usage example.
public static void FadeChildWeights(
this MixerState mixer,
TParameter parameter,
float fadeDuration)
{
ChildWeights.Clear();
var childCount = mixer.ChildCount;
for (int i = 0; i < childCount; i++)
ChildWeights.Add(mixer.GetChild(i).Weight);
mixer.Parameter = parameter;
if (!mixer.RecalculateWeights())
return;
var mixerPlayable = mixer.Playable;
for (int i = 0; i < childCount; i++)
{
var child = mixer.GetChild(i);
mixerPlayable.SetChildWeight(child, ChildWeights[i]);
child.StartFade(Math.Max(child.TargetWeight, float.Epsilon), fadeDuration);
}
}
/************************************************************************************************************************/
}
}