public void RemoveZeroWeightsInput() { int size = mixer.GetInputCount(); for (int i = 0; i < size; i++) { if (mixer.GetInputWeight(i) <= 0) { mixer.GetInput(i).Destroy(); for (int j = i + 1; j < size; j++) { // double localTime = ((AnimationClipPlayable)mixer.GetInput(j)).GetTime(); float _weight = mixer.GetInputWeight(j); Playable clip = mixer.GetInput(j); // clip.SetTime(localTime); mixer.DisconnectInput(j); mixer.ConnectInput(j - 1, clip, 0); mixer.SetInputWeight(j - 1, _weight); } i--; size--; mixer.SetInputCount(size); } } }
public void ApplyPresentationState(float deltaTime) { if (m_AnimStateData.charLocoState != m_CurrentAnimationState) { var previousState = m_CurrentAnimationState; var prevController = (int)m_CurrentAnimationState < m_AnimStates.Length ? m_AnimStates[(int)previousState].controller : null; m_CurrentAnimationState = m_AnimStateData.charLocoState; var newController = m_AnimStates[(int)m_CurrentAnimationState].controller; if (newController != prevController) { m_PreviousAnimationState = m_PreviousAnimationState == LocomotionState.MaxValue ? m_CurrentAnimationState : previousState; } } var interpolationDuration = m_AnimStates[(int)m_CurrentAnimationState].transitionTimes[(int)m_PreviousAnimationState]; var blendVel = interpolationDuration > 0 ? 1.0f / interpolationDuration : 1.0f / deltaTime; m_StateTranstion.Update(m_AnimStates[(int)m_CurrentAnimationState].port, blendVel, deltaTime); for (var i = 0; i < (int)LocomotionState.MaxValue; i++) { if (m_AnimStates[i].controller != null && m_Mixer.GetInputWeight(m_AnimStates[i].port) > 0f) { m_AnimStates[i].controller.ApplyPresentationState(deltaTime); } } }
public void ApplyPresentationState(float deltaTime) { float rotateAngleRemaining = 0f; if (m_AnimState.turnDirection != 0) { rotateAngleRemaining = Mathf.DeltaAngle(m_AnimState.rotation, m_AnimState.turnStartAngle) + m_Settings.animTurnAngle * m_AnimState.turnDirection; } if (m_AnimState.turnDirection == 0) { m_Transition.Update((int)LocoMixerPort.Idle, m_Settings.turnTransitionSpeed, Time.deltaTime); } else { var fraction = 1f - Mathf.Abs(rotateAngleRemaining / m_Settings.animTurnAngle); var mixerPort = (m_AnimState.turnDirection == -1) ? (int)LocoMixerPort.TurnL : (int)LocoMixerPort.TurnR; var anim = (m_AnimState.turnDirection == -1) ? m_AnimTurnL : m_AnimTurnR; m_Transition.Update(mixerPort, m_Settings.turnTransitionSpeed, Time.deltaTime); anim.SetTime(anim.GetAnimationClip().length *fraction); if (m_LocomotionMixer.GetInputWeight((int)LocoMixerPort.Idle) < 0.01f) { m_AnimIdle.SetTime(0f); } } float aimPitchFraction = m_AnimState.aimPitch / 180.0f; m_AnimAimLeft.SetTime(aimPitchFraction * m_AnimAimLeft.GetDuration()); m_AnimAimMid.SetTime(aimPitchFraction * m_AnimAimMid.GetDuration()); m_AnimAimRight.SetTime(aimPitchFraction * m_AnimAimRight.GetDuration()); float aimYawLocal = Mathf.DeltaAngle(m_AnimState.rotation, m_AnimState.aimYaw); float aimYawFraction = Mathf.Abs(aimYawLocal / m_Settings.aimYawAngle); m_AimMixer.SetInputWeight((int)AimMixerPort.AimMid, 1.0f - aimYawFraction); if (aimYawLocal < 0) { m_AimMixer.SetInputWeight((int)AimMixerPort.AimLeft, aimYawFraction); m_AimMixer.SetInputWeight((int)AimMixerPort.AimRight, 0.0f); } else { m_AimMixer.SetInputWeight((int)AimMixerPort.AimLeft, 0.0f); m_AimMixer.SetInputWeight((int)AimMixerPort.AimRight, aimYawFraction); } var job = m_FootIk.GetJobData <FootIkJob>(); job.normalLeftFoot = m_AnimState.footIkNormalLeft; job.normalRightFoot = m_AnimState.footIkNormaRight; job.ikOffset = m_AnimState.footIkOffset; m_FootIk.SetJobData(job); }
/* * Normalize Weights in mixerEmotionPlayable */ private void normalizeWeights() { int length = mixerEmotionPlayable.GetInputCount(); float sumOfWeights = 0; for (int i = 0; i < length; i++) { if (mixerEmotionPlayable.GetInputWeight(i) > 0f) { sumOfWeights += mixerEmotionPlayable.GetInputWeight(i); } } for (int i = 0; i < length; i++) { if (mixerEmotionPlayable.GetInputWeight(i) > 0f) { mixerEmotionPlayable.SetInputWeight(i, mixerEmotionPlayable.GetInputWeight(i) / sumOfWeights); } } normalize = false; }
public void Update(MotionMatchingPlayableGraph graph, bool passIK, bool passFootIK, float deltaTime) { currentClipTime += deltaTime; float seqLocalTime = GetLocalTime(); for (int i = 0; i < data.clips.Count; i++) { float currentPlayableTime = GetPlayableTimeInSequenceLocalTime(seqLocalTime, i); float currentWeight = mixer.GetInputWeight(i); float desiredWeight = GetPlayableWeightInPlayableTime(currentPlayableTime, i); if (currentWeight <= 0f && desiredWeight > 0f) { mixer.GetInput(i).SetTime(currentPlayableTime - Time.deltaTime); mixer.GetInput(i).SetTime(currentPlayableTime); } mixer.SetInputWeight(i, desiredWeight); } NormalizeMixerInputWeights(); }
/// <summary> /// When blending between different clips that have different durations, it's often neccessary to speed the playables up or down so their durations /// match. Consider blending a walk and run animation with different lengths - unless the feet hit the ground at the same time every time the clips /// loop, the character's feet will be in a strange, in-between position. /// The way we handle this is to always have all of the clips - even ones with weight 0 - always playing at a speed such that their duration matches /// the currently played clip's duration. /// </summary> private void CompensateForDurations(int idxBefore, int idxAfter) { float durationBefore = runtimeData[idxBefore].duration; float durationAfter = runtimeData[idxAfter].duration; float loopDuration = Mathf.Lerp(durationBefore, durationAfter, mixer.GetInputWeight(idxAfter)); for (int i = 0; i < runtimeData.Length; i++) { var clipDuration = runtimeData[i].duration; var requiredSpeed = clipDuration / loopDuration; runtimeData[i].playable.SetSpeed(requiredSpeed); } }
public void RemoveZeroWeightsInputsAnimations( AnimationMixerPlayable mixer, List <float> blendingWeights, List <float> currentWeights, List <int> curretClipsIndex, List <LogicAnimationsSequence> animationsSequences ) { int size = mixer.GetInputCount(); for (int i = 0; i < size; i++) { if (currentWeights[i] <= 0f) { int sequenceMixerInputsCount = mixer.GetInput(i).GetInputCount(); if (sequenceMixerInputsCount > 0) { for (int mixerInputInputsIndex = 0; mixerInputInputsIndex < sequenceMixerInputsCount; mixerInputInputsIndex++) { mixer.GetInput(i).GetInput(mixerInputInputsIndex).Destroy(); } for (int as_Index = 0; as_Index < animationsSequences.Count; as_Index++) { if (curretClipsIndex[i] == animationsSequences[as_Index].dataIndex) { animationsSequences.RemoveAt(as_Index); break; } } } mixer.GetInput(i).Destroy(); blendingWeights.RemoveAt(i); curretClipsIndex.RemoveAt(i); currentWeights.RemoveAt(i); for (int j = i + 1; j < size; j++) { // double localTime = ((AnimationClipPlayable)mixer.GetInput(j)).GetTime(); float _weight = mixer.GetInputWeight(j); Playable clip = mixer.GetInput(j); // clip.SetTime(localTime); mixer.DisconnectInput(j); mixer.ConnectInput(j - 1, clip, 0); mixer.SetInputWeight(j - 1, _weight); } i--; size--; mixer.SetInputCount(size); } } }
void Update() { if (!mixer.IsValid() || Speed == 0) { return; } var cWeight = mixer.GetInputWeight(0); if (cWeight < 1) { cWeight = Mathf.Lerp(0, 1, (Time.time - changeTime) / blendTime); mixer.SetInputWeight(0, cWeight); mixer.SetInputWeight(1, 1 - cWeight); } graph.Evaluate(Time.deltaTime * Speed); }
public float getWeight() { return(mixerEmotionPlayable.GetInputWeight(playablesDict[playablesEmotionKey])); }
public void CreateBlendMotionMatchingAnimation( MotionMatchingData data, int dataIndex, AnimationMixerPlayable stateMixer, double localTime, float blendTime, List <float> blendingSpeeds, List <float> currentWeights, List <LogicAnimationsSequence> animationsSequences, bool passIK, bool passFootIK, float newInputStartWeight = 0f, float minWeightToAchive = 0f, float speedMulti = 1f ) { if (stateMixer.GetInputCount() > 0) { if (currentWeights[currentWeights.Count - 1] >= minWeightToAchive) { blendingSpeeds[blendingSpeeds.Count - 1] = -(stateMixer.GetInputWeight(stateMixer.GetInputCount() - 1) / blendTime); } } currentWeights.Add(newInputStartWeight); blendingSpeeds.Add(1f / blendTime); switch (data.dataType) { case AnimationDataType.SingleAnimation: AnimationClipPlayable playable_SA = AnimationClipPlayable.Create(graph, data.clips[0]); playable_SA.SetApplyPlayableIK(passIK); playable_SA.SetApplyFootIK(passFootIK); playable_SA.SetTime(localTime - Time.deltaTime); playable_SA.SetTime(localTime); playable_SA.SetSpeed(speedMulti); stateMixer.AddInput(playable_SA, 0, newInputStartWeight); break; case AnimationDataType.BlendTree: AnimationMixerPlayable mixerPlayable = AnimationMixerPlayable.Create(this.graph); stateMixer.AddInput(mixerPlayable, 0, newInputStartWeight); for (int i = 0; i < data.clips.Count; i++) { AnimationClipPlayable playable_BT = AnimationClipPlayable.Create(this.graph, data.clips[i]); playable_BT.SetApplyPlayableIK(passIK); playable_BT.SetApplyFootIK(passFootIK); playable_BT.SetTime(localTime - Time.deltaTime); playable_BT.SetTime(localTime); playable_BT.SetSpeed(speedMulti); mixerPlayable.AddInput(playable_BT, 0, data.blendTreeWeights[i]); } break; case AnimationDataType.AnimationSequence: animationsSequences.Add(new LogicAnimationsSequence(data, dataIndex)); int new_ASIndex = animationsSequences.Count - 1; animationsSequences[new_ASIndex].mixer = AnimationMixerPlayable.Create(this.graph); stateMixer.AddInput(animationsSequences[new_ASIndex].mixer, 0, newInputStartWeight); animationsSequences[new_ASIndex].CreateAnimationsInTime((float)localTime, this, passIK, passFootIK); break; } }
public float GetAnimatorWeight() { return(m_RootMixer.GetInputWeight(0)); }