Documentation

Unity Engine

Download Unity

Unity Engine

RootMotionUtility.DeltaMixer

Mixes several weighted root motion deltas, the way Core.PoseMixer mixes the poses of several clips.
Read time 4 minutesLast updated 4 hours ago

Definition

public struct RootMotionUtility.DeltaMixer

Remarks

Use it with the same weights as the poses, for example the weights that a blend space computes. Call RootMotionUtility.DeltaMixer.Add once for each delta and its weight, then RootMotionUtility.DeltaMixer.End to get the result. The default value is ready to use. If the weights sum to less than 1, the remaining weight keeps the character still.
The following example uses the
Unity.Animations
,
Unity.Collections
,
Unity.Entities
,
Unity.IntegerTime
,
Unity.Mathematics
, and
UnityEngine
namespaces. It mixes the poses and the root motion of the clips of a 1D blend space with the same weights, and moves a GameObject with the result.

Examples

// This example moves the GameObject of this component with a walk, jog, and run blend space. Assign the walk, jog,// and run clips, which are placed at speeds 1, 3, and 6 in the blend space, and set the speed of the character.public class RootMotionBlendSpaceExample : MonoBehaviour{ public AnimationClip[] clips; public float speed; BlobObjectReference<RigDefinition> m_RigDefinition; ClipSampler[] m_Samplers; TimerState[] m_Timers; NativeArray<AnimatedData> m_Poses; NativeArray<AnimatedData> m_Output; NativeArray<float> m_ClipSpeeds; NativeArray<float> m_Weights; void OnEnable() { if (clips == null || clips.Length == 0) return; // Create the rig, the samplers, the timers, and the pose buffers once, and keep them between frames. using RigBuilder rigBuilder = RigBuilder.Create(transform, clips, Allocator.Temp); m_RigDefinition = rigBuilder.ToRigDefinition(); m_Samplers = new ClipSampler[clips.Length]; m_Timers = new TimerState[clips.Length]; for (int i = 0; i < clips.Length; ++i) { m_Samplers[i] = clips[i].ToClipSampler(ref m_RigDefinition.Value, Allocator.Persistent); m_Timers[i] = TimerState.Create( new DiscreteTimeInterval(DiscreteTime.Zero, new DiscreteTime(clips[i].length)), clips[i].isLooping ? WrapMode.Loop : WrapMode.ClampForever); } // A stream can't be a field or an array element, so keep the buffers of the streams instead. The poses // buffer holds the pose of each clip, one after the other. int streamSize = m_RigDefinition.Value.Bindings.StreamSize; m_Poses = new NativeArray<AnimatedData>(streamSize * clips.Length, Allocator.Persistent); m_Output = new NativeArray<AnimatedData>(streamSize, Allocator.Persistent); for (int i = 0; i < clips.Length; ++i) { AnimationStream pose = AnimationStream.Create(ref m_RigDefinition.Value, GetPose(i)); pose.ResetToDefaultValues(); } m_ClipSpeeds = new NativeArray<float>(new float[] { 1f, 3f, 6f }, Allocator.Persistent); m_Weights = new NativeArray<float>(clips.Length, Allocator.Persistent); } void OnDisable() { if (!m_RigDefinition.IsValid) return; // Dispose the samplers and the buffers before the rig definition that they use. for (int i = 0; i < m_Samplers.Length; ++i) m_Samplers[i].Dispose(); m_Poses.Dispose(); m_Output.Dispose(); m_ClipSpeeds.Dispose(); m_Weights.Dispose(); m_RigDefinition.Dispose(); } void Update() { Move(Time.deltaTime); } NativeArray<AnimatedData> GetPose(int index) { int streamSize = m_RigDefinition.Value.Bindings.StreamSize; return m_Poses.GetSubArray(index * streamSize, streamSize); } // Updates the blend space for one frame, and moves the GameObject with the blended root motion. public void Move(float deltaTime) { // The rig isn't valid when the component has no clips. if (!m_RigDefinition.IsValid) return; BlendSpaceUtility.ComputeBlendSpace1DWeights(speed, m_ClipSpeeds, false, 0f, 0f, m_Weights); // Mix the poses and the root motion of the clips with the same weights. Each stream is a view of its // buffer, so the poses stay between frames. AnimationStream output = AnimationStream.Create(ref m_RigDefinition.Value, m_Output); Core.PoseMixer poseMixer = default; RootMotionUtility.DeltaMixer deltaMixer = default; poseMixer.Begin(ref output); for (int i = 0; i < m_Samplers.Length; ++i) { AnimationStream pose = AnimationStream.Create(ref m_RigDefinition.Value, GetPose(i)); m_Timers[i].Update(new DiscreteTime(deltaTime)); // Keep the root motion out of the pose. Otherwise the pose and the root motion both move the character. m_Samplers[i].SampleExtractingRootMotion(ref pose, m_Timers[i]); MotionOutput motion = m_Samplers[i].SampleRootMotion(m_Timers[i]); poseMixer.Add(ref output, pose, m_Weights[i]); deltaMixer.Add(motion.RootMotion, m_Weights[i]); } // The weights sum to 1, so the default pose has no influence. poseMixer.End(ref output, default); // The root motion is the move of the root since the previous frame, relative to the character. RigidTransform delta = deltaMixer.End(); transform.SetPositionAndRotation( transform.position + transform.rotation * (Vector3)delta.pos, transform.rotation * delta.rot); }}

Methods

Method

Description

AddAdds a weighted root motion delta to the mix.
BeginClears the deltas that the mixer has collected, so that you can start a new mix.
EndFinishes the mix and returns the mixed delta.