Documentation

Unity Engine

Download Unity

Unity Engine

AnimationStream

A proxy for reading and writing the animated channel values of a rig, such as translations, rotations, floats, integers, and entity IDs.
Read time 10 minutesLast updated 3 hours ago

Definition

[BurstCompile]public ref struct AnimationStream

Remarks

This is
Unity.Animations.AnimationStream
. It is not AnimationStream, which an
IAnimationJob
receives from an Animator in a PlayableGraph. That type doesn't work with the functions of this namespace.
A stream gives you access to the animated values of one rig. It holds a value for each translation, rotation, and scale binding of the rig. It also holds the float, integer, and entity ID channels that the rig defines.
ClipSampler samples an animation clip into a stream. The methods of AnimationStreamBlendingExtensions blend streams. Call AnimationStream.FromDefaultValues to read the default values of a rig.
To create a stream, pass a reference to a AnimationStream.RigDefinition and either an allocator or a buffer of AnimatedData with BindingSet.StreamSize elements. To read or write a channel, find its index with BindingUtility.FindBindingIndex, then use a method such as AnimationStream.GetFloat or AnimationStream.SetFloat.
A stream is a
ref struct
, so it lives only on the stack, for the scope where you create it. It can't be a field of a class, of a job or of another struct. To work on a stream in a job, store the BlobObjectReference<T> of the rig definition and the buffer of AnimatedData in the job, and create the stream in the
Execute
method of the job. The rig definition must stay valid while you use the stream.
How the safety system checks a stream depends on how you create it:
If safety checks are enabled, a stream throws an
ObjectDisposedException
if you use it after you dispose its memory. Safety checks are always enabled in the Editor and never in a built Player.
The following example requires the
Unity.Burst
,
Unity.Collections
,
Unity.Entities
,
Unity.Jobs
, and
UnityEngine
namespaces. It runs a job that creates a stream on an array of animated data, and a job that reads the default values of a rig.

Examples

// A stream can't be a job field. The job stores the rig and the buffer, and creates the stream in Execute.// The safety system checks the buffer like any NativeArray field.[BurstCompile]public struct ScaleFloatJob : IJob{ public BlobObjectReference<RigDefinition> Rig; public NativeArray<AnimatedData> Buffer; public int Index; public float Factor; public void Execute() { AnimationStream stream = AnimationStream.Create(ref Rig.Value, Buffer); stream.SetFloat(Index, stream.GetFloat(Index) * Factor); }}// A job can also read the default values of the rig through a stream that it creates in Execute.[BurstCompile]public struct ReadDefaultFloatJob : IJob{ public BlobObjectReference<RigDefinition> Rig; public int Index; public NativeArray<float> Result; public void Execute() => Result[0] = AnimationStream.FromDefaultValues(ref Rig.Value).GetFloat(Index);}// Scale a channel in a buffer that starts with the default values of the rig.// The method returns the default value of the channel multiplied by factor.public static float ScaleDefaultFloat(BlobObjectReference<RigDefinition> rig, int index, float factor){ using NativeArray<AnimatedData> buffer = new NativeArray<AnimatedData>(rig.Value.Bindings.StreamSize, Allocator.TempJob); AnimationStream stream = AnimationStream.Create(ref rig.Value, buffer); stream.ResetToDefaultValues(); new ScaleFloatJob { Rig = rig, Buffer = buffer, Index = index, Factor = factor }.Schedule().Complete(); return stream.GetFloat(index);}// Read the default value of a channel in a job.public static float ReadDefaultFloat(BlobObjectReference<RigDefinition> rig, int index){ using NativeArray<float> result = new NativeArray<float>(1, Allocator.TempJob); new ReadDefaultFloatJob { Rig = rig, Index = index, Result = result }.Schedule().Complete(); return result[0];}

Properties

Property

Description

BoneCountReturns the total bone count.
DiscreteDataChunkCountReturns the total discrete chunk count
DiscreteDataChunksReturns the discrete data chunks.
DiscreteDataChunksReadOnlyThe discrete data chunks of the stream, as a read-only array.
EntityIdCountThe number of entity ID channels that the rig animates.
FloatCountThe number of generic float channels in the stream.
FrameMaskChannel masks that record which channels have been modified during the current frame.
IntCountThe number of generic integer channels in the stream.
InterpolatedDataChunkCountReturns the total interpolated chunk count
InterpolatedDataChunksReturns the interpolated data chunks.
InterpolatedDataChunksReadOnlyThe interpolated data chunks of the stream, as a read-only array.
IsCreatedWhether the stream has a rig definition and a buffer.
IsReadOnlyWhether the stream is read-only.
PassMaskChannel masks that record which channels have been modified during the current pass.
RigDefinitionThe AnimationStream.RigDefinition that describes the layout of the stream.
RotationCountThe number of rotation channels in the stream.
RotationDataChunkCountReturns the total rotation data chunk count.
RotationDataChunksReturns the rotation data chunks.
RotationDataChunksReadOnlyThe rotation data chunks of the stream, as a read-only array.
ScaleCountThe number of scale channels in the stream.
SocketCountReturns the total socket count
TranslationCountThe number of translation channels in the stream.

Methods

Method

Description

AndMasksAND all channel masks with other AnimationStream channel masks.
ClearFrameMasksClears frame channel masks
ClearMasksClears both frame and pass channel masks.
ClearPassMasksClears pass channel masks
CopyFromCopies all the data from the specified AnimationStream into this AnimationStream. This includes all the animation channel data and channel masks.
CopyMasksFromCopy all channel masks from a source AnimationStream.
DisposeReleases the stream.
ExtractRootMotionBoneRemoves the root motion from the pose by expressing the root motion bone relative to a root transform.
GetDefaultValuesReturns a read-only AnimationStream containing the default values of the RigDefinition.
GetEntityIdReturns the entity ID at the specified index.
GetFloatReturns the generic float value at the specified index.
GetIntReturns the generic integer value at the specified index.
GetLocalToCharacterInverseMatrixReturns the local to character inverse matrix of the bone at the specified index.
GetLocalToCharacterMatrixReturns the local to character matrix of the bone at the specified index.
GetLocalToCharacterRotationReturns the local to character rotation of the bone at the specified index.
GetLocalToCharacterScaleReturns the local to character scale of the bone at the specified index.
GetLocalToCharacterTRReturns the local to character translation and rotation of the bone at the specified index.
GetLocalToCharacterTranslationReturns the local to character translation of the bone at the specified index.
GetLocalToCharacterTRSReturns the local to character translation, rotation and scale of the bone at the specified index.
GetLocalToParentInverseMatrixReturns the local to parent inverse matrix of the bone at the specified index.
GetLocalToParentMatrixReturns the local to parent matrix of the bone at the specified index.
GetLocalToParentRotationReturns the local to parent rotation of the bone at the specified index.
GetLocalToParentScaleReturns the local to parent scale of the bone at the specified index.
GetLocalToParentTRReturns the local to parent translation and rotation of the bone at the specified index.
GetLocalToParentTranslationReturns the local to parent translation of the bone at the specified index.
GetLocalToParentTRSReturns the local to parent translation, rotation and scale of the bone at the specified index.
GetRotationReturns a rotation value at the specified index.
GetSocketLocalToCharacterInverseMatrixReturns the inverse matrix of the socket's transform in character space
GetSocketLocalToCharacterMatrixReturns the matrix of the socket's transform in character space.
GetSocketLocalToCharacterRotationReturns the rotation of the socket in character space.
GetSocketLocalToCharacterScaleReturns the scale of the socket in character space.
GetSocketLocalToCharacterTRReturns the translation and rotation of the socket in character space.
GetSocketLocalToCharacterTranslationReturns the translation of the socket in character space.
GetSocketLocalToCharacterTRSReturns the translation, rotation and scale of the socket in character space.
GetSocketLocalToParentInverseMatrixReturns the inverse matrix of the socket's transform relative to its parent bone
GetSocketLocalToParentMatrixReturns the matrix of the socket's transform relative to its parent bone.
GetSocketLocalToParentRotationReturns the static rotation offset of the socket relative to its parent bone.
GetSocketLocalToParentScaleReturns the static scale offset of the socket relative to its parent bone.
GetSocketLocalToParentTRReturns the static translation and rotation offset of the socket relative to its parent bone.
GetSocketLocalToParentTranslationReturns the static translation offset of the socket relative to its parent bone.
GetSocketLocalToParentTRSReturns the static translation and rotation offset of the socket relative to its parent bone.
InverseTransformDirectionTransforms direction from character space to local space. This operation isn't affected by scale or position of the transforms and the returned vector has the same length as the original direction. You can perform the opposite conversion, from local to character space using AnimationStream.TransformDirection.
InverseTransformPositionTransforms a position in character space to local space. You can perform the opposite conversion, from local to character space using AnimationStream.TransformPosition.
InverseTransformPositionRotationTransforms a position and rotation in character space to local space. You can perform the opposite conversion, from local to character space using AnimationStream.TransformPositionRotation.
InverseTransformRotationTransforms a rotation in character space to local space. You can perform the opposite conversion, from local to character space using AnimationStream.TransformRotation.
InverseTransformVectorTransforms a vector from character space to local space. This operation isn't affected by position of the transforms, but it is affected by scale. The returned vector may have a different length than the original vector. You can perform the opposite conversion, from local to character space using AnimationStream.TransformVector.
OrMasksOR all channel masks with other AnimationStream channel masks.
ResetRotationsToIdentityReset the stream's rotation to identity.
ResetToDefaultValuesResets the channel values of the stream to the default values of the rig, and clears the channel masks.
ResetToZeroReset the AnimationStream to 0. This includes all animation channel data and channel masks.
SetEntityIdSets the entity ID value at the specified index.
SetFloatSets the generic float value at the specified index.
SetFrameMasksSets the frame mask values.
SetIntSets the generic integer value at the specified index.
SetLocalToCharacterRotationSets the local to character rotation of the bone at the specified index.
SetLocalToCharacterScaleSets the local to character scale of the bone at the specified index.
SetLocalToCharacterTRSets the local to character translation and rotation of the bone at the specified index.
SetLocalToCharacterTranslationSets the local to character translation of the bone at the specified index.
SetLocalToCharacterTRSSets the local to character translation, rotation and scale of the bone at the specified index.
SetLocalToParentRotationSets the local to parent rotation of the bone at the specified index.
SetLocalToParentScaleSets the local to parent scale of the bone at the specified index.
SetLocalToParentTRSets the local to parent translation and rotation of the bone at the specified index.
SetLocalToParentTranslationSets the local to parent translation of the bone at the specified index.
SetLocalToParentTRSSets the local to parent translation, rotation and scale of the bone at the specified index.
SetMasksSets both the frame and pass channel mask values.
SetPassMasksSets the pass mask values.
SetRotationSets the rotation value at the specified index.
TransformDirectionTransforms direction from local space to character space. This operation isn't affected by scale or position of the transforms and the returned vector has the same length as the original direction. You can perform the opposite conversion, from character to local space using AnimationStream.InverseTransformDirection.
TransformPositionTransforms a position in local space to character space. You can perform the opposite conversion, from character to local space using AnimationStream.InverseTransformPosition.
TransformPositionRotationTransforms a position and rotation in local space to character space. You can perform the opposite conversion, from character to local space using AnimationStream.InverseTransformPositionRotation.
TransformRotationTransforms a rotation in local space to character space. You can perform the opposite conversion, from character to local space using AnimationStream.InverseTransformRotation.
TransformVectorTransforms a vector from local space to character space. This operation isn't affected by position of the transforms, but it is affected by scale. The returned vector may have a different length than the original vector. You can perform the opposite conversion, from character to local space using AnimationStream.InverseTransformVector.

Static Methods

Method

Description

CreateCreates a read-write AnimationStream that allocates and owns its buffer.
CreateReadOnlyCreates a read-only AnimationStream from a RigDefinition and a NativeArray of AnimatedData.
FromDefaultValuesCreates a read-only AnimationStream from the default values of a RigDefinition.