Documentation

Unity Engine

Download Unity

Unity Engine

ComputeInertialBlendingCoefficients

Computes the inertial blending coefficients of every channel of a rig at the start of a transition.
Read time 5 minutesLast updated 4 hours ago

Definition

ComputeInertialBlendingCoefficients(AnimationStream, AnimationStream, AnimationStream, float, float, NativeArray<InertialBlendingCoefficientsData>, NativeArray<float3>)

public static void ComputeInertialBlendingCoefficients(ref AnimationStream currentInput, ref AnimationStream lastOutput, ref AnimationStream secondLastOutput, float deltaTime, float duration, NativeArray<Core.InertialBlendingCoefficientsData> outCoefficients, NativeArray<float3> outDirections)

Parameters

currentInput

The pose of the destination animation at the start of the transition.

lastOutput

The output pose of the previous update. It must use the same rig as
currentInput
.

secondLastOutput

The output pose of the update before the previous one. It must use the same rig as
currentInput
.

deltaTime

The time between
secondLastOutput
and
lastOutput
, in seconds. It must be greater than 0.

duration

The requested duration of the blend, in seconds. It must be 0 or greater. A channel finishes sooner than this duration if the offset of the channel moves toward zero quickly at the start.

The buffer that receives the coefficients. Its length must equal the value that Core.InertialBlendingCoefficientsData.RequiredCoefficientBufferSize returns for the rig of
currentInput
.

outDirections

The buffer that receives the offset directions. Its length must equal the value that Core.InertialBlendingCoefficientsData.RequiredDirectionBufferSize returns for the rig of
currentInput
.

Remarks

Call this method once when a transition to a new pose starts, then call Core.InertialBlend on each update until the blend ends. An inertial blend is an alternative to a cross-fade. To compute the coefficients, this method needs the first pose of the destination animation and the last two output poses. After the transition starts,
InertialBlend
needs only the current pose of the destination animation. The blend never samples the source animation during the transition. Compared with a cross-fade, this approach saves a clip sample on each update. The blend also works with poses from any source, such as poses that procedural constraints modify.
The method computes the velocity of each channel from the last two output poses. The blend starts with that velocity, so the motion stays continuous. The blend doesn't take the motion limits of the bones of your character into account. With a fast motion and a long blend, the result can exceed those limits. To stay within the limits, use a shorter duration or correct the bones after the blend. Because the blend depends on the previous poses, it's also sensitive to sudden changes of the time step.
The method writes the coefficients of the translation, rotation, scale, and float channels, in this order, to
outCoefficients
. It writes the direction of the offset of the translation, rotation, and scale channels, in this order, to
outDirections
. A rotation direction is a rotation axis.
The following example requires a rig definition. It uses the
Unity.Animations
,
Unity.Collections
,
Unity.Entities
, and
Unity.Mathematics
namespaces. It computes the coefficients for a switch to a new pose, then applies the blend with
InertialBlend
at a given elapsed time. It also returns the translation of the blended pose and the offset of the first channel.

Examples

// This example starts an inertial blend from the last output pose to a new input pose, then// returns the x translation of the first translation channel after elapsedTime seconds of the blend.// The out parameter receives the remaining offset of that channel. The offset reaches zero at the// end of the blend.public static float BlendTranslationX( ref RigDefinition rig, float blendDuration, float elapsedTime, out float channelOffset){ // Each stream owns a buffer that starts with the default values of the rig. AnimationStream currentInput = AnimationStream.Create(ref rig, Allocator.Temp); AnimationStream lastOutput = AnimationStream.Create(ref rig, Allocator.Temp); AnimationStream secondLastOutput = AnimationStream.Create(ref rig, Allocator.Temp); AnimationStream blended = AnimationStream.Create(ref rig, Allocator.Temp); // The pose before the switch is at rest. The pose of the new animation is five units away on the x-axis. currentInput.SetLocalToParentTranslation(0, math.float3(5f, 0f, 0f)); // Size the buffers. There is one coefficient entry for each translation, rotation, scale, and float // channel, and one direction for each translation, rotation, and scale channel. int coefficientCount = Core.InertialBlendingCoefficientsData.RequiredCoefficientBufferSize(ref rig); int directionCount = Core.InertialBlendingCoefficientsData.RequiredDirectionBufferSize(ref rig); using NativeArray<Core.InertialBlendingCoefficientsData> coefficients = new NativeArray<Core.InertialBlendingCoefficientsData>(coefficientCount, Allocator.Temp); using NativeArray<float3> directions = new NativeArray<float3>(directionCount, Allocator.Temp); // Compute the coefficients once, when the switch happens. The time step between the two previous poses is // 0.01 seconds. The poses are identical, so the initial velocity is zero. float deltaTime = 0.01f; Core.ComputeInertialBlendingCoefficients( ref currentInput, ref lastOutput, ref secondLastOutput, deltaTime, blendDuration, coefficients, directions); // On each update, apply the blend to the new input pose. At elapsedTime 0 the result equals the // last output pose, and at blendDuration it equals the input pose. // InertialBlend takes the time left in the blend, so subtract the elapsed time from the duration. float remainingTime = blendDuration - elapsedTime; Core.InertialBlend( ref currentInput, ref blended, coefficients.AsReadOnly(), directions.AsReadOnly(), blendDuration, remainingTime); // To read the offset of one channel directly, evaluate its coefficients. Translation channels come first. float4 timePowers = Core.InertialBlendingCoefficientsData.ComputeTimePowers(elapsedTime); Core.InertialBlendingCoefficientsData firstCoefficients = coefficients[0]; channelOffset = firstCoefficients.Interpolate(timePowers, elapsedTime); float3 translation = blended.GetLocalToParentTranslation(0); // Dispose the streams explicitly, because a using declaration can't be passed by reference. currentInput.Dispose(); lastOutput.Dispose(); secondLastOutput.Dispose(); blended.Dispose(); return translation.x;}