Documentation

Unity Engine

Download Unity

Unity Engine

InertialBlend(ref AnimationStream, ref AnimationStream, ReadOnly, ReadOnly, float, float)

Applies an inertial blend to an input pose and writes the blended pose to an output stream.
Read time 3 minutesLast updated 4 hours ago

Definition

InertialBlend(AnimationStream, AnimationStream, ReadOnly, ReadOnly, float, float)

public static void InertialBlend(ref AnimationStream currentInput, ref AnimationStream outputPose, NativeArray<Core.InertialBlendingCoefficientsData>.ReadOnly interpolationFactors, NativeArray<float3>.ReadOnly interpolationDirections, float duration, float remainingTime)

Parameters

currentInput

The pose of the destination animation for the current update.

outputPose

The stream that receives the blended pose. It must use the same rig as
currentInput
.

interpolationFactors

The coefficients from
ComputeInertialBlendingCoefficients
, computed once at the start of the transition.

interpolationDirections

The offset directions from
ComputeInertialBlendingCoefficients
, computed once at the start of the transition.

duration

The duration of the blend, in seconds, as passed to
ComputeInertialBlendingCoefficients
.

remainingTime

The time left in the blend, in seconds, from 0 to
duration
. At 0, the output pose matches the input pose.

Remarks

Call this method on each update of a transition, after you compute the coefficients once with Core.ComputeInertialBlendingCoefficients. For each translation, rotation, scale, and float channel, the method applies the offset at the current blend time to the value in
currentInput
. It writes the result to
outputPose
. The method doesn't blend integer channels, because discrete values can't be blended. On every call, the method copies the integer values of
currentInput
, so they switch to the destination values when the blend starts.
The following example requires a rig definition. It uses the
Unity.Animations
,
Unity.Collections
,
Unity.Entities
, and
Unity.Mathematics
namespaces. It computes the coefficients with
ComputeInertialBlendingCoefficients
for a switch to a new pose, then applies the blend 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;}