// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik // #pragma warning disable CS0649 // Field is never assigned to, and will always have its default value. using System; using System.Collections.Generic; using System.Text; using UnityEngine; using UnityEngine.Animations; using UnityEngine.Playables; using Object = UnityEngine.Object; namespace Animancer { /// [Pro-Only] /// An which blends multiple child states /// by allowing you to control their manually. /// /// /// This mixer type is similar to the Direct Blend Type in Mecanim Blend Trees. /// The official Direct Blend Trees /// tutorial explains their general concepts and purpose which apply to s as well. /// /// Documentation: /// /// Mixers /// /// https://kybernetik.com.au/animancer/api/Animancer/ManualMixerState /// public partial class ManualMixerState : AnimancerState, ICopyable, IParametizedState, IUpdatable { /************************************************************************************************************************/ #region Properties /************************************************************************************************************************/ /// The states connected to this mixer. /// Only states up to the should be assigned. protected AnimancerState[] ChildStates { get; private set; } = Array.Empty(); /************************************************************************************************************************/ private int _ChildCount; /// public sealed override int ChildCount => _ChildCount; /************************************************************************************************************************/ /// The size of the internal array of . /// This value starts at 0 then expands to when the first child is added. public int ChildCapacity { get => ChildStates.Length; set { if (value == ChildStates.Length) return; #if UNITY_ASSERTIONS if (value <= 1 && OptionalWarning.MixerMinChildren.IsEnabled()) OptionalWarning.MixerMinChildren.Log( $"The {nameof(ChildCapacity)} of '{this}' is being set to {value}." + $" The purpose of a mixer is to mix multiple child states so this may be a mistake.", Graph?.Component); #endif var newChildStates = new AnimancerState[value]; if (value > _ChildCount)// Increase size. { Array.Copy(ChildStates, newChildStates, _ChildCount); } else// Decrease size. { for (int i = value; i < _ChildCount; i++) ChildStates[i].Destroy(); Array.Copy(ChildStates, newChildStates, value); _ChildCount = value; } ChildStates = newChildStates; if (_Playable.IsValid()) { _Playable.SetInputCount(value); } else if (Graph != null) { CreatePlayable(); } OnChildCapacityChanged(); } } /// Called when the is changed. protected virtual void OnChildCapacityChanged() { } /// starts at 0 then expands to this value when the first child is added. /// Default 8. public static int DefaultChildCapacity { get; set; } = 8; /// /// Ensures that the remaining unused /// is greater than or equal to the specified `minimumCapacity`. /// public void EnsureRemainingChildCapacity(int minimumCapacity) { minimumCapacity += _ChildCount; if (ChildCapacity < minimumCapacity) { var capacity = Math.Max(ChildCapacity, DefaultChildCapacity); while (capacity < minimumCapacity) capacity *= 2; ChildCapacity = capacity; } } /************************************************************************************************************************/ /// public sealed override AnimancerState GetChild(int index) => ChildStates[index]; /// public sealed override FastEnumerator GetEnumerator() => new(ChildStates, _ChildCount); /************************************************************************************************************************/ /// protected override void OnSetIsPlaying() { var isPlaying = IsPlaying; for (int i = _ChildCount - 1; i >= 0; i--) ChildStates[i].IsPlaying = isPlaying; } /************************************************************************************************************************/ /// If greater than 0 then is true. private int _LoopingChildCount; /// Are any child states looping? public override bool IsLooping => _LoopingChildCount > 0; /// Sets and informs the s. private void AddIsLooping(int offset) { var wasLooping = IsLooping; _LoopingChildCount += offset; var isLooping = IsLooping; if (wasLooping != isLooping) OnIsLoopingChangedRecursive(isLooping); } /// protected override void OnChildIsLoopingChanged(bool value) => AddIsLooping(value ? 1 : -1); /************************************************************************************************************************/ /// The weighted average of each child state. /// /// If there are any , /// only those states will be included in the getter calculation. /// public override double RawTime { get { GetTimeDetails(out var totalWeight, out var normalizedTime, out var length); if (totalWeight == 0) return base.RawTime; totalWeight *= totalWeight; return normalizedTime * length / totalWeight; } set { if (value == 0) goto SetToZero; var length = Length; if (length == 0) goto SetToZero; value /= length;// Normalize. for (int i = _ChildCount - 1; i >= 0; i--) ChildStates[i].NormalizedTimeD = value; return; // If the value is 0, we can set the child times more efficiently. SetToZero: for (int i = _ChildCount - 1; i >= 0; i--) ChildStates[i].TimeD = 0; } } /************************************************************************************************************************/ /// public override void MoveTime(double time, bool normalized) { base.MoveTime(time, normalized); for (int i = _ChildCount - 1; i >= 0; i--) ChildStates[i].MoveTime(time, normalized); } /************************************************************************************************************************/ /// public override void GetEventDispatchInfo( out float length, out float normalizedTime, out bool isLooping) { GetTimeDetails(out _, out normalizedTime, out length); isLooping = _LoopingChildCount > 0; } /************************************************************************************************************************/ /// /// Gets the time details based on the synchronized child states if any are active, /// otherwise recalculates based on all child states. /// private void GetTimeDetails(out float totalWeight, out float normalizedTime, out float length) { if (_SynchronizedChildren != null) { GetTimeDetails( _SynchronizedChildren, _SynchronizedChildren.Count, out totalWeight, out normalizedTime, out length); if (totalWeight > MinimumSynchronizeChildrenWeight) return; } GetTimeDetails( ChildStates, _ChildCount, out totalWeight, out normalizedTime, out length); } /// Gets the time details based on the `states`. private void GetTimeDetails( IList states, int count, out float totalWeight, out float normalizedTime, out float length) { totalWeight = 0; normalizedTime = 0; length = 0; for (int i = count - 1; i >= 0; i--) { var state = states[i]; var weight = state.Weight; if (weight == 0) continue; var stateLength = state.Length; if (stateLength == 0) continue; totalWeight += weight; normalizedTime += state.Time / stateLength * weight; length += stateLength * weight; } } /************************************************************************************************************************/ /// The weighted average of each child state. public override float Length { get { var length = 0f; var totalChildWeight = 0f; if (_SynchronizedChildren != null) { for (int i = _SynchronizedChildren.Count - 1; i >= 0; i--) { var state = _SynchronizedChildren[i]; var weight = state.Weight; if (weight == 0) continue; var stateLength = state.Length; if (stateLength == 0) continue; totalChildWeight += weight; length += stateLength * weight; } } if (totalChildWeight > 0) return length / totalChildWeight; totalChildWeight = CalculateTotalWeight(ChildStates, _ChildCount); if (totalChildWeight <= 0) return 0; for (int i = _ChildCount - 1; i >= 0; i--) { var state = ChildStates[i]; length += state.Length * state.Weight; } return length / totalChildWeight; } } /************************************************************************************************************************/ #endregion /************************************************************************************************************************/ #region Initialization /************************************************************************************************************************/ /// Creates and assigns the managed by this state. protected override void CreatePlayable(out Playable playable) { playable = AnimationMixerPlayable.Create(Graph._PlayableGraph, ChildCapacity); } /************************************************************************************************************************/ /// Connects the `state` to this mixer at its . protected internal override void OnAddChild(AnimancerState state) { Validate.AssertGraph(state, Graph); var capacity = ChildCapacity; if (_ChildCount >= capacity) ChildCapacity = Math.Max(DefaultChildCapacity, capacity * 2); state.Index = _ChildCount; ChildStates[_ChildCount] = state; _ChildCount++; state.IsPlaying = IsPlaying; if (Graph != null) ConnectChildUnsafe(state.Index, state); if (SynchronizeNewChildren) Synchronize(state); if (state.IsLooping) AddIsLooping(1); #if UNITY_ASSERTIONS _CachedToString = null; #endif } /************************************************************************************************************************/ /// Disconnects the `state` from this mixer at its . protected internal override void OnRemoveChild(AnimancerState state) { DontSynchronize(state); Validate.AssertCanRemoveChild(state, ChildStates, _ChildCount); // Shuffle all subsequent children down one place. if (Graph == null || !Graph._PlayableGraph.IsValid()) { Array.Copy( ChildStates, state.Index + 1, ChildStates, state.Index, _ChildCount - state.Index - 1); for (int i = state.Index; i < _ChildCount - 1; i++) ChildStates[i].Index = i; } else { Graph._PlayableGraph.Disconnect(_Playable, state.Index); for (int i = state.Index + 1; i < _ChildCount; i++) { var otherChild = ChildStates[i]; Graph._PlayableGraph.Disconnect(_Playable, otherChild.Index); otherChild.Index = i - 1; ChildStates[i - 1] = otherChild; ConnectChildUnsafe(i - 1, otherChild); } } _ChildCount--; ChildStates[_ChildCount] = null; if (state.IsLooping) AddIsLooping(-1); #if UNITY_ASSERTIONS _CachedToString = null; #endif } /************************************************************************************************************************/ /// public override void Destroy() { DestroyChildren(); base.Destroy(); } /************************************************************************************************************************/ /// public override AnimancerState Clone(CloneContext context) { var clone = new ManualMixerState(); clone.CopyFrom(this, context); return clone; } /************************************************************************************************************************/ /// public sealed override void CopyFrom(AnimancerState copyFrom, CloneContext context) => this.CopyFromBase(copyFrom, context); /// public virtual void CopyFrom(ManualMixerState copyFrom, CloneContext context) { base.CopyFrom(copyFrom, context); DestroyChildren(); var synchronizeNewChildren = SynchronizeNewChildren; var childCount = copyFrom.ChildCount; EnsureRemainingChildCapacity(childCount); for (int i = 0; i < childCount; i++) { var child = copyFrom.ChildStates[i]; SynchronizeNewChildren = copyFrom.IsSynchronized(child); child = context.Clone(child); Add(child); } SynchronizeNewChildren = synchronizeNewChildren; } /************************************************************************************************************************/ #endregion /************************************************************************************************************************/ #region Child Configuration /************************************************************************************************************************/ /// Assigns the `state` as a child of this mixer. /// This is the same as calling . public void Add(AnimancerState state) => state.SetParent(this); /// Creates and returns a new to play the `clip` as a child of this mixer. public ClipState Add(AnimationClip clip) { var state = new ClipState(clip); Add(state); return state; } /// Calls then . public AnimancerState Add(ITransition transition) { var state = transition.CreateStateAndApply(Graph); Add(state); return state; } /// Calls one of the other overloads as appropriate for the `child`. public AnimancerState Add(object child) { if (child is AnimationClip clip) return Add(clip); if (child is ITransition transition) return Add(transition); if (child is AnimancerState state) { Add(state); return state; } MarkAsUsed(this); throw new ArgumentException($"Failed to {nameof(Add)} '{AnimancerUtilities.ToStringOrNull(child)}'" + $" as child of '{this}' because it isn't an" + $" {nameof(AnimationClip)}, {nameof(ITransition)}, or {nameof(AnimancerState)}."); } /************************************************************************************************************************/ /// Calls for each of the `clips`. public void AddRange(IList clips) { var count = clips.Count; EnsureRemainingChildCapacity(count); for (int i = 0; i < count; i++) Add(clips[i]); } /// Calls for each of the `clips`. public void AddRange(params AnimationClip[] clips) => AddRange((IList)clips); /************************************************************************************************************************/ /// Calls for each of the `transitions`. public void AddRange(IList transitions) { var count = transitions.Count; EnsureRemainingChildCapacity(count); for (int i = 0; i < count; i++) Add(transitions[i]); } /// Calls for each of the `transitions`. public void AddRange(params ITransition[] transitions) => AddRange((IList)transitions); /************************************************************************************************************************/ /// Calls for each of the `children`. public void AddRange(IList children) { var count = children.Count; EnsureRemainingChildCapacity(count); for (int i = 0; i < count; i++) Add(children[i]); } /// Calls for each of the `children`. public void AddRange(params object[] children) => AddRange((IList)children); /************************************************************************************************************************/ /// Removes the child at the specified `index`. public void Remove(int index, bool destroy) => Remove(ChildStates[index], destroy); /// Removes the specified `child`. public void Remove(AnimancerState child, bool destroy) { #if UNITY_ASSERTIONS if (child.Parent != this) Debug.LogWarning($"Attempting to remove a state which is not a child of this {GetType().Name}." + $" This will remove the child from its actual parent so you should directly call" + $" child.{nameof(child.Destroy)} or child.{nameof(child.SetParent)}(null, -1) instead." + $"\n• Child: {child}" + $"\n• Removing From: {this}" + $"\n• Actual Parent: {child.Parent}", Graph?.Component as Object); #endif if (destroy) child.Destroy(); else child.SetParent(null); } /************************************************************************************************************************/ /// Replaces the `child` at the specified `index`. public void Set(int index, AnimancerState child, bool destroyPrevious) { #if UNITY_ASSERTIONS if ((uint)index >= _ChildCount) { MarkAsUsed(this); MarkAsUsed(child); throw new IndexOutOfRangeException( $"Invalid child index. Must be 0 <= index < {nameof(ChildCount)} ({ChildCount})."); } #endif if (child.Parent != null) child.SetParent(null); var previousChild = ChildStates[index]; DontSynchronize(previousChild); previousChild.SetParentInternal(null); child.SetGraph(Graph); ChildStates[index] = child; child.SetParentInternal(this, index); child.IsPlaying = IsPlaying; if (Graph != null) { Graph._PlayableGraph.Disconnect(_Playable, index); ConnectChildUnsafe(index, child); } var loopingOffset = 0; if (previousChild.IsLooping) loopingOffset--; if (child.IsLooping) loopingOffset++; if (loopingOffset != 0) AddIsLooping(loopingOffset); child.CopyIKFlags(this); if (SynchronizeNewChildren) Synchronize(child); if (destroyPrevious) previousChild.Destroy(); #if UNITY_ASSERTIONS _CachedToString = null; #endif } /// Replaces the child at the specified `index` with a new . public ClipState Set(int index, AnimationClip clip, bool destroyPrevious) { var state = new ClipState(clip); Set(index, state, destroyPrevious); return state; } /// Replaces the child at the specified `index` with a . public AnimancerState Set(int index, ITransition transition, bool destroyPrevious) { var state = transition.CreateStateAndApply(Graph); Set(index, state, destroyPrevious); return state; } /// Calls one of the other overloads as appropriate for the `child`. public AnimancerState Set(int index, object child, bool destroyPrevious) { if (child is AnimationClip clip) return Set(index, clip, destroyPrevious); if (child is ITransition transition) return Set(index, transition, destroyPrevious); if (child is AnimancerState state) { Set(index, state, destroyPrevious); return state; } MarkAsUsed(this); throw new ArgumentException($"Failed to {nameof(Set)} '{AnimancerUtilities.ToStringOrNull(child)}'" + $" as child of '{this}' because it isn't an" + $" {nameof(AnimationClip)}, {nameof(ITransition)}, or {nameof(AnimancerState)}."); } /************************************************************************************************************************/ /// Returns the index of the specified `child` state. public int IndexOf(AnimancerState child) => Array.IndexOf(ChildStates, child, 0, _ChildCount); /************************************************************************************************************************/ /// /// Destroys all connected to this mixer. This operation cannot be undone. /// public void DestroyChildren() { for (int i = _ChildCount - 1; i >= 0; i--) ChildStates[i].Destroy(); Array.Clear(ChildStates, 0, _ChildCount); _ChildCount = 0; } /************************************************************************************************************************/ #endregion /************************************************************************************************************************/ #region Jobs /************************************************************************************************************************/ /// /// Creates an to run the specified Animation Job instead of the usual /// . /// /// /// /// Example: /// void CreatePlayableExample(AnimancerComponent animancer) /// } /// var job = new MyJob();// A struct that implements IAnimationJob. /// var mixer = new WhateverMixerState();// e.g. LinearMixerState. /// mixer.CreatePlayable(animancer, job); /// // Use mixer.Initialize, CreateChild, and SetChild to configure the children as normal. /// } /// /// See also: /// public AnimationScriptPlayable CreatePlayable( AnimancerGraph graph, T job, bool processInputs = false) where T : struct, IAnimationJob { // Can't just use SetGraph normally because it would call the regular CreatePlayable method. SetGraph(null); Graph = graph; graph.States.Register(this); var playable = AnimationScriptPlayable.Create(graph._PlayableGraph, job, _ChildCount); if (!processInputs) playable.SetProcessInputs(false); for (int i = _ChildCount - 1; i >= 0; i--) ChildStates[i].SetGraph(graph); return playable; } /************************************************************************************************************************/ /// /// Creates an to run the specified Animation Job instead of the usual /// . /// /// /// /// /// Documentation: /// /// Creating Custom States /// /// Example: /// public class MyMixer : LinearMixerState /// { /// protected override void CreatePlayable(out Playable playable) /// { /// CreatePlayable(out playable, new MyJob()); /// } /// /// private struct MyJob : IAnimationJob /// { /// public void ProcessAnimation(AnimationStream stream) /// { /// } /// /// public void ProcessRootMotion(AnimationStream stream) /// { /// } /// } /// } /// /// See also: /// protected void CreatePlayable( out Playable playable, T job, bool processInputs = false) where T : struct, IAnimationJob { var scriptPlayable = AnimationScriptPlayable.Create(Graph._PlayableGraph, job, ChildCount); if (!processInputs) scriptPlayable.SetProcessInputs(false); playable = scriptPlayable; } /************************************************************************************************************************/ /// /// Gets the Animation Job data from the . /// /// /// This mixer was not initialized using /// or . /// public T GetJobData() where T : struct, IAnimationJob => ((AnimationScriptPlayable)_Playable).GetJobData(); /// /// Sets the Animation Job data in the . /// /// /// This mixer was not initialized using /// or . /// public void SetJobData(T value) where T : struct, IAnimationJob => ((AnimationScriptPlayable)_Playable).SetJobData(value); /************************************************************************************************************************/ #endregion /************************************************************************************************************************/ #region Updates /************************************************************************************************************************/ /// public int UpdatableIndex { get; set; } = IUpdatable.List.NotInList; /// Recalculates the weights of child states and synchronizes their times if necessary. public virtual void Update() { if (!ApplySynchronizeChildren()) Graph.CancelPreUpdate(this); } /************************************************************************************************************************/ /// protected internal override void UpdateEvents() => UpdateEventsRecursive(this); /************************************************************************************************************************/ /// public override void SetGraph(AnimancerGraph graph) { if (Graph == graph) return; Graph?.CancelPreUpdate(this); base.SetGraph(graph); } /************************************************************************************************************************/ #endregion /************************************************************************************************************************/ #region Synchronize Children /************************************************************************************************************************/ /// Should newly added children be automatically added to the synchronization list? Default true. public static bool SynchronizeNewChildren { get; set; } = true; /// The minimum total weight of all children for their times to be synchronized. Default 0.01. public static float MinimumSynchronizeChildrenWeight { get; set; } = 0.01f; /************************************************************************************************************************/ private List _SynchronizedChildren; /// A copy of the internal list of child states that will have their times synchronized. /// /// If this mixer is a child of another mixer, its synchronized children will be managed by the parent. /// /// The getter allocates a new array if is greater than zero. /// public AnimancerState[] SynchronizedChildren { get => SynchronizedChildCount > 0 ? _SynchronizedChildren.ToArray() : Array.Empty(); set { if (_SynchronizedChildren == null) _SynchronizedChildren = new(); else _SynchronizedChildren.Clear(); for (int i = 0; i < value.Length; i++) Synchronize(value[i]); } } /// The number of . public int SynchronizedChildCount => _SynchronizedChildren != null ? _SynchronizedChildren.Count : 0; /************************************************************************************************************************/ /// Is the `state` in the ? public bool IsSynchronized(AnimancerState state) { var synchronizer = GetParentMixer(); return synchronizer._SynchronizedChildren != null && synchronizer._SynchronizedChildren.Contains(state); } /************************************************************************************************************************/ /// Adds the `state` to the . /// /// The `state` must be a child of this mixer. /// /// If this mixer is a child of another mixer, the `state` will be added to the parent's /// instead. /// public void Synchronize(AnimancerState state) { if (state == null) return; #if UNITY_ASSERTIONS if (!IsChildOf(state, this)) { MarkAsUsed(this); throw new ArgumentException( $"State is not a child of the mixer." + $"\n• State: {state}" + $"\n• Mixer: {this}", nameof(state)); } #endif var synchronizer = GetParentMixer(); synchronizer.SynchronizeDirect(state); } /// The internal implementation of . private void SynchronizeDirect(AnimancerState state) { if (state == null) return; // If the state is a mixer, steal all its synchronized children instead of synchronizing the mixer itself. if (state is ManualMixerState mixer) { if (mixer._SynchronizedChildren != null) { for (int i = 0; i < mixer._SynchronizedChildren.Count; i++) Synchronize(mixer._SynchronizedChildren[i]); mixer._SynchronizedChildren.Clear(); } return; } #if UNITY_ASSERTIONS if (OptionalWarning.MixerSynchronizeZeroLength.IsEnabled() && state.Length == 0) OptionalWarning.MixerSynchronizeZeroLength.Log( $"Adding a state with zero {nameof(AnimancerState.Length)} to the synchronization list: '{state}'." + $"\n\nSynchronization is based on the {nameof(NormalizedTime)}" + $" which can't be calculated if the {nameof(Length)} is 0." + $" Some state types can change their {nameof(Length)}, in which case you can just disable this warning." + $" But otherwise, the indicated state probably shouldn't be added to the synchronization list.", Graph?.Component); #endif _SynchronizedChildren ??= new(); #if UNITY_ASSERTIONS if (_SynchronizedChildren.Contains(state)) Debug.LogError($"{state} is already in the {nameof(SynchronizedChildren)} list."); #endif _SynchronizedChildren.Add(state); Graph?.RequirePreUpdate(this); } /************************************************************************************************************************/ /// Removes the `state` from the . public void DontSynchronize(AnimancerState state) { var synchronizer = GetParentMixer(); if (synchronizer._SynchronizedChildren != null && synchronizer._SynchronizedChildren.Remove(state) && state._Playable.IsValid()) state._Playable.SetSpeed(state.Speed); } /************************************************************************************************************************/ /// Removes all children of this mixer from the . public void DontSynchronizeChildren() { var synchronizer = GetParentMixer(); var synchronizedChildren = synchronizer._SynchronizedChildren; if (synchronizedChildren == null) return; if (synchronizer == this) { for (int i = synchronizedChildren.Count - 1; i >= 0; i--) { var state = synchronizedChildren[i]; if (state._Playable.IsValid()) state._Playable.SetSpeed(state.Speed); } synchronizedChildren.Clear(); } else { for (int i = synchronizedChildren.Count - 1; i >= 0; i--) { var state = synchronizedChildren[i]; if (IsChildOf(state, this)) { if (state._Playable.IsValid()) state._Playable.SetSpeed(state.Speed); synchronizedChildren.RemoveAt(i); } } } } /************************************************************************************************************************/ /// Initializes the internal list. /// /// The array can be null or empty. Any elements not in the array will be treated as true. /// /// This method can only be called before any are added and also before this /// mixer is made the child of another mixer. /// public void InitializeSynchronizedChildren(params bool[] synchronizeChildren) { AnimancerUtilities.Assert(GetParentMixer() == this, $"{nameof(InitializeSynchronizedChildren)} cannot be used on a mixer that is a child of another mixer."); AnimancerUtilities.Assert(_SynchronizedChildren == null, $"{nameof(InitializeSynchronizedChildren)} cannot be used on a mixer already has synchronized children."); int flagCount; if (synchronizeChildren != null) { flagCount = synchronizeChildren.Length; for (int i = 0; i < flagCount; i++) if (synchronizeChildren[i]) SynchronizeDirect(ChildStates[i]); } else flagCount = 0; for (int i = flagCount; i < _ChildCount; i++) SynchronizeDirect(ChildStates[i]); } /************************************************************************************************************************/ /// /// Returns the highest in the hierarchy above this mixer /// or this mixer itself if there are none above it. /// public ManualMixerState GetParentMixer() { var mixer = this; var parent = Parent; while (parent != null) { if (parent is ManualMixerState parentMixer) mixer = parentMixer; parent = parent.Parent; } return mixer; } /// Returns the highest in the hierarchy above the `state` (inclusive). public static ManualMixerState GetParentMixer(AnimancerNodeBase node) { ManualMixerState mixer = null; while (node != null) { if (node is ManualMixerState parentMixer) mixer = parentMixer; node = node.Parent; } return mixer; } /************************************************************************************************************************/ /// Is the `child` a child of the `parent`? public static bool IsChildOf(AnimancerNodeBase child, AnimancerNodeBase parent) { while (true) { child = child.Parent; if (child == parent) return true; else if (child == null) return false; } } /************************************************************************************************************************/ /// /// Synchronizes the s of the by /// modifying their internal playable speeds. /// private bool ApplySynchronizeChildren() { if (Weight == 0 || !IsPlaying || _SynchronizedChildren == null || _SynchronizedChildren.Count <= 1) return false; var deltaTime = AnimancerGraph.DeltaTime * CalculateRealEffectiveSpeed(); if (deltaTime == 0) return true; var count = _SynchronizedChildren.Count; // Calculate the weighted average normalized time and normalized speed of all children. var totalWeight = 0f; var weightedNormalizedTime = 0f; var weightedNormalizedSpeed = 0f; for (int i = 0; i < count; i++) { var state = _SynchronizedChildren[i]; var weight = CalculateRelativeWeight(state); if (weight == 0) continue; var length = state.Length; if (length == 0) continue; totalWeight += weight; weight /= length; weightedNormalizedTime += state.Time * weight; weightedNormalizedSpeed += state.Speed * weight; } #if UNITY_ASSERTIONS if (!(totalWeight >= 0) || totalWeight == float.PositiveInfinity)// Reversed comparison includes NaN. { MarkAsUsed(this); throw new ArgumentOutOfRangeException(nameof(totalWeight), totalWeight, $"Total weight {Strings.MustBeFinite} and must be positive"); } if (!weightedNormalizedTime.IsFinite()) { MarkAsUsed(this); throw new ArgumentOutOfRangeException(nameof(weightedNormalizedTime), weightedNormalizedTime, $"Time {Strings.MustBeFinite}"); } if (!weightedNormalizedSpeed.IsFinite()) { MarkAsUsed(this); throw new ArgumentOutOfRangeException(nameof(weightedNormalizedSpeed), weightedNormalizedSpeed, $"Speed {Strings.MustBeFinite}"); } #endif // If the total weight is too small, pretend they are all at Weight = 1. if (totalWeight < MinimumSynchronizeChildrenWeight) { weightedNormalizedTime = 0; weightedNormalizedSpeed = 0; var nonZeroCount = 0; for (int i = 0; i < count; i++) { var state = _SynchronizedChildren[i]; var length = state.Length; if (length == 0) continue; length = 1f / length; weightedNormalizedTime += state.Time * length; weightedNormalizedSpeed += state.Speed * length; nonZeroCount++; } totalWeight = nonZeroCount; } // Increment that time value according to delta time. weightedNormalizedTime += deltaTime * weightedNormalizedSpeed; weightedNormalizedTime /= totalWeight; var inverseDeltaTime = 1f / deltaTime; // Modify the speed of all children to go from their current normalized time to the average in one frame. for (int i = 0; i < count; i++) { var state = _SynchronizedChildren[i]; var length = state.Length; if (length == 0) continue; var normalizedTime = state.Time / length; var speed = (weightedNormalizedTime - normalizedTime) * length * inverseDeltaTime; state._Playable.SetSpeed(speed); } // After this, all the playables will update and advance according to their new speeds this frame. return true; } /************************************************************************************************************************/ /// Calculates the weight of the `child` multiplied by its parents up to this mixer. private float CalculateRelativeWeight(AnimancerState child) { var weight = child.Weight; var parent = child.Parent; while (parent != this && parent != null) { weight *= parent.BaseWeight; parent = parent.Parent; } return weight; } /************************************************************************************************************************/ /// /// The multiplied of this mixer and its parents down the /// hierarchy to determine the actual speed its output is being played at. /// /// /// This can be different from the because the /// have their playable speed modified without setting their /// . /// public float CalculateRealEffectiveSpeed() { var speed = _Playable.GetSpeed(); var parent = Parent; while (parent != null) { speed *= parent.Playable.GetSpeed(); parent = parent.Parent; } return (float)speed; } /************************************************************************************************************************/ #endregion /************************************************************************************************************************/ #region Inverse Kinematics /************************************************************************************************************************/ private bool _ApplyAnimatorIK; /// public override bool ApplyAnimatorIK { get => _ApplyAnimatorIK; set => base.ApplyAnimatorIK = _ApplyAnimatorIK = value; } /************************************************************************************************************************/ private bool _ApplyFootIK; /// public override bool ApplyFootIK { get => _ApplyFootIK; set => base.ApplyFootIK = _ApplyFootIK = value; } /************************************************************************************************************************/ #endregion /************************************************************************************************************************/ #region Other Methods /************************************************************************************************************************/ /// Calculates the sum of the of all `states`. public static float CalculateTotalWeight(AnimancerState[] states, int count) { var total = 0f; for (int i = count - 1; i >= 0; i--) total += states[i].Weight; return total; } /************************************************************************************************************************/ /// /// Sets for all . /// public void SetChildrenTime(float value, bool normalized = false) { for (int i = _ChildCount - 1; i >= 0; i--) { var state = ChildStates[i]; if (normalized) state.NormalizedTime = value; else state.Time = value; } } /************************************************************************************************************************/ /// Sets the weight of all states after the `previousIndex` to 0. protected void DisableRemainingStates(int previousIndex) { for (int i = previousIndex + 1; i < _ChildCount; i++) Playable.SetChildWeight(ChildStates[i], 0); } /************************************************************************************************************************/ private static float[] _TemporaryWeights = Array.Empty(); /// Returns an array at least as large as the `count`. /// /// The same array is returned by subsequent calls as long as it's large enough /// and it isn't cleared between calls so it will contain the previous data. /// public static float[] GetTemporaryFloatArray(int count) { if (_TemporaryWeights.Length < count) { if (count <= 16) count = 16; else count = Mathf.NextPowerOfTwo(count); _TemporaryWeights = new float[count]; } return _TemporaryWeights; } /************************************************************************************************************************/ /// Divides `weights` by the `totalWeight` and applies them to the child states. public void NormalizeAndApplyWeights(float totalWeight, float[] weights) { totalWeight = 1f / totalWeight; for (int i = _ChildCount - 1; i >= 0; i--) { var state = ChildStates[i]; var weight = weights[i] * totalWeight; Playable.SetChildWeight(state, weight); } } /************************************************************************************************************************/ /// Gets a user-friendly key to identify the `state` in the Inspector. public virtual string GetDisplayKey(AnimancerState state) => $"[{state.Index}]"; /************************************************************************************************************************/ /// public override Vector3 AverageVelocity { get { var velocity = default(Vector3); for (int i = _ChildCount - 1; i >= 0; i--) { var state = ChildStates[i]; velocity += state.AverageVelocity * state.Weight; } return velocity; } } /************************************************************************************************************************/ /// /// Recalculates the of all child states so that they add up to 1. /// public void NormalizeDurations() { int divideBy = 0; float totalDuration = 0f; // Count the number of states that exist and their total duration. for (int i = 0; i < _ChildCount; i++) { divideBy++; totalDuration += ChildStates[i].Duration; } // Calculate the average duration. totalDuration /= divideBy; // Set all states to that duration. for (int i = 0; i < _ChildCount; i++) { ChildStates[i].Duration = totalDuration; } } /************************************************************************************************************************/ #if UNITY_ASSERTIONS /// [Assert-Only] A string built by to describe this mixer. private string _CachedToString; #endif /// /// Returns a string describing the type of this mixer and the name of states connected to it. /// public override string ToString() { #if UNITY_ASSERTIONS if (NameCache.TryToString(DebugName, out var name)) return name; if (_CachedToString != null) return _CachedToString; #endif // Gather child names. var childNames = ListPool.Acquire(); var allSimple = true; for (int i = 0; i < _ChildCount; i++) { var state = ChildStates[i]; if (state == null) continue; if (state.MainObject != null) { childNames.Add(state.MainObject.name); } else { childNames.Add(state.ToString()); allSimple = false; } } // If they all have a main object, check if they all have the same prefix so it doesn't need to be repeated. int prefixLength = 0; var count = childNames.Count; if (count <= 1 || !allSimple) { prefixLength = 0; } else { var prefix = childNames[0]; var shortest = prefixLength = prefix.Length; for (int iName = 0; iName < count; iName++) { var childName = childNames[iName]; if (shortest > childName.Length) { shortest = prefixLength = childName.Length; } for (int iCharacter = 0; iCharacter < prefixLength; iCharacter++) { if (childName[iCharacter] != prefix[iCharacter]) { prefixLength = iCharacter; break; } } } if (prefixLength < 3 ||// Less than 3 characters probably isn't an intentional prefix. prefixLength >= shortest) prefixLength = 0; } // Build the mixer name. var mixerName = StringBuilderPool.Instance.Acquire(); var type = GetType().Name; if (type.EndsWith("State")) mixerName.Append(type, 0, type.Length - 5); else mixerName.Append(type); mixerName.Append('('); if (count > 0) { if (prefixLength > 0) mixerName.Append(childNames[0], 0, prefixLength).Append('['); for (int i = 0; i < count; i++) { if (i > 0) mixerName.Append(", "); var childName = childNames[i]; mixerName.Append(childName, prefixLength, childName.Length - prefixLength); } mixerName.Append(']'); } ListPool.Release(childNames); mixerName.Append(')'); var result = mixerName.ReleaseToString(); #if UNITY_ASSERTIONS _CachedToString = result; #endif return result; } /************************************************************************************************************************/ /// protected override void AppendDetails(StringBuilder text, string separator) { base.AppendDetails(text, separator); text.Append(separator) .Append("SynchronizedChildren: "); if (SynchronizedChildCount == 0) { text.Append("0"); } else { text.Append(_SynchronizedChildren.Count); separator += Strings.Indent; for (int i = 0; i < _SynchronizedChildren.Count; i++) { text.Append(separator) .Append(_SynchronizedChildren[i]); } } } /************************************************************************************************************************/ /// public override void GatherAnimationClips(ICollection clips) => clips.GatherFromSource(ChildStates); /************************************************************************************************************************/ /// public virtual void GetParameters(List parameters) { } /// public virtual void SetParameters(List parameters) { } /************************************************************************************************************************/ #endregion /************************************************************************************************************************/ } }