Documentation

Unity Engine

Download Unity

Unity Engine

Core.InertialBlendingCoefficientsData

The coefficients of the polynomial that describes the offset of one animation channel during an inertial blend.
Read time 4 minutesLast updated 4 hours ago

Definition

public struct Core.InertialBlendingCoefficientsData

Remarks

An inertial blend smooths the transition to a new pose. At the start of the transition, each channel gets an offset. The offset equals the difference between the last output value and the new input value. The blend applies this offset to the input and reduces the offset to zero over the duration of the blend. This struct stores the coefficients of the polynomial that gives the offset of one channel at a given time.
Most code doesn't create this struct directly. Call Core.ComputeInertialBlendingCoefficients once at the start of a transition to compute the coefficients of every channel of a rig. Then call Core.InertialBlend on each update to apply the coefficients.
For a float channel, the size of the offset is the signed difference. For a translation or scale channel, the size is the length of the difference vector, and a separate value holds the direction. For a rotation channel, the size is the angle of the rotation in radians, and a separate value holds the rotation axis.
To get the offset at a given time, call Core.InertialBlendingCoefficientsData.ComputeTimePowers for that time, then pass the result and the time to Core.InertialBlendingCoefficientsData.Interpolate.
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 and applies the blend at a given elapsed time. It then reads the offset of one channel with Core.InertialBlendingCoefficientsData.Interpolate.

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;}

Constructors

Constructor

Description

InertialBlendingCoefficientsData(System.Single,System.Single,System.Single)Creates the coefficients of an inertial blend for one animation channel.

Methods

Method

Description

InterpolateComputes the offset of the channel at a given time since the start of the blend.

Static Methods

Method

Description

ComputeTimePowersComputes the powers of a time value that the Interpolate method needs.
RequiredCoefficientBufferSizeComputes the required inertial blending coefficient buffer size needed for a specified rig definition.
RequiredDirectionBufferSizeComputes the required inertial blending direction buffer size needed for a specified rig definition.