public bool FadeOthers(string name, float targetWeight, float fadeTime) { int index = FindAnimation(name, out var state); if (index == -1 || !state) { return(false); } byte layer = state.Layer; for (int i = 0; i < animations_.Count; ++i) { if (i != index) { AnimationControl control = animations_[i]; AnimationState otherState = GetAnimationState(control.hash_); if (otherState && otherState.Layer == layer) { control.targetWeight_ = glm.clamp(targetWeight, 0.0f, 1.0f); control.fadeTime_ = fadeTime; } } } return(true); }
public void StopAll(float fadeOutTime) { if (animations_.Count > 0) { for (int i = 0; i < animations_.Count;) { AnimationControl ctrl = animations_[i]; { ctrl.targetWeight_ = 0.0f; ctrl.fadeTime_ = fadeOutTime; } } } }
public bool Play(String name, byte layer, bool looped, float fadeInTime) { // Get the animation resource first to be able to get the canonical resource name // (avoids potential adding of duplicate animations) Animation newAnimation = Resources.Instance.Load <Animation>(name); if (!newAnimation) { return(false); } // Check if already exists int index = FindAnimation(newAnimation.AnimationName, out var state); if (!state) { state = AddAnimationState(newAnimation); if (!state) { return(false); } } if (index == -1) { AnimationControl newControl = new AnimationControl(); newControl.name_ = newAnimation.AnimationName; newControl.hash_ = newAnimation.NameHash; animations_.Push(newControl); index = animations_.Count - 1; } state.SetLayer(layer); state.SetLooped(looped); animations_[index].targetWeight_ = 1.0f; animations_[index].fadeTime_ = fadeInTime; return(true); }
public virtual void Update(float timeStep) { // Loop through animations for (int i = 0; i < animations_.Count;) { AnimationControl ctrl = animations_[i]; AnimationState state = GetAnimationState(ctrl.hash_); bool remove = false; if (!state) { remove = true; } else { // Advance the animation if (ctrl.speed_ != 0.0f) { state.AddTime(ctrl.speed_ * timeStep); } float targetWeight = ctrl.targetWeight_; float fadeTime = ctrl.fadeTime_; // If non-looped animation at the end, activate autofade as applicable if (!state.IsLooped && state.Time >= state.Length && ctrl.autoFadeTime_ > 0.0f) { targetWeight = 0.0f; fadeTime = ctrl.autoFadeTime_; } // Process weight fade float currentWeight = state.Weight; if (currentWeight != targetWeight) { if (fadeTime > 0.0f) { float weightDelta = 1.0f / fadeTime * timeStep; if (currentWeight < targetWeight) { currentWeight = Math.Min(currentWeight + weightDelta, targetWeight); } else if (currentWeight > targetWeight) { currentWeight = Math.Max(currentWeight - weightDelta, targetWeight); } state.SetWeight(currentWeight); } else { state.SetWeight(targetWeight); } } // Remove if weight zero and target weight zero if (state.Weight == 0.0f && (targetWeight == 0.0f || fadeTime == 0.0f) && ctrl.removeOnCompletion_) { remove = true; } } // Decrement the command time-to-live values if (ctrl.setTimeTtl_ > 0.0f) { ctrl.setTimeTtl_ = Math.Max(ctrl.setTimeTtl_ - timeStep, 0.0f); } if (ctrl.setWeightTtl_ > 0.0f) { ctrl.setWeightTtl_ = Math.Max(ctrl.setWeightTtl_ - timeStep, 0.0f); } if (remove) { if (state) { RemoveAnimationState(state); } animations_.RemoveAt(i); } else { ++i; } } // Node hierarchy animations need to be applied manually foreach (var state in nodeAnimationStates_) { state.Apply(); } }