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
- Type: Struct
- Namespace: Unity.Animations
- Assembly: UnityEngine.AnimationCoreModule
[BurstCompile]public ref struct AnimationStream
Remarks
This is . It is not AnimationStream, which an receives from an Animator in a PlayableGraph. That type doesn't work with the functions of this namespace.
Unity.Animations.AnimationStreamIAnimationJobA 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 , 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 method of the job. The rig definition must stay valid while you use the stream.
ref structExecuteHow the safety system checks a stream depends on how you create it:
-
AnimationStream.Create allocates a buffer that the stream owns. Call AnimationStream.Dispose to free it, for example with astatement.
using -
AnimationStream.Create and AnimationStream.CreateReadOnly return a view of the array that uses the safety handle of the array. The array keeps its memory, and you dispose it as usual. The job system checks the array when you pass it to jobs.
-
AnimationStream.FromDefaultValues and AnimationStream.GetDefaultValues return streams that the safety system can't check.
If safety checks are enabled, a stream throws an if you use it after you dispose its memory. Safety checks are always enabled in the Editor and never in a built Player.
ObjectDisposedExceptionThe following example requires the , , , , and 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.
Unity.BurstUnity.CollectionsUnity.EntitiesUnity.JobsUnityEngineExamples
// 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 |
|---|---|
| BoneCount | Returns the total bone count. |
| DiscreteDataChunkCount | Returns the total discrete chunk count |
| DiscreteDataChunks | Returns the discrete data chunks. |
| DiscreteDataChunksReadOnly | The discrete data chunks of the stream, as a read-only array. |
| EntityIdCount | The number of entity ID channels that the rig animates. |
| FloatCount | The number of generic float channels in the stream. |
| FrameMask | Channel masks that record which channels have been modified during the current frame. |
| IntCount | The number of generic integer channels in the stream. |
| InterpolatedDataChunkCount | Returns the total interpolated chunk count |
| InterpolatedDataChunks | Returns the interpolated data chunks. |
| InterpolatedDataChunksReadOnly | The interpolated data chunks of the stream, as a read-only array. |
| IsCreated | Whether the stream has a rig definition and a buffer. |
| IsReadOnly | Whether the stream is read-only. |
| PassMask | Channel masks that record which channels have been modified during the current pass. |
| RigDefinition | The AnimationStream.RigDefinition that describes the layout of the stream. |
| RotationCount | The number of rotation channels in the stream. |
| RotationDataChunkCount | Returns the total rotation data chunk count. |
| RotationDataChunks | Returns the rotation data chunks. |
| RotationDataChunksReadOnly | The rotation data chunks of the stream, as a read-only array. |
| ScaleCount | The number of scale channels in the stream. |
| SocketCount | Returns the total socket count |
| TranslationCount | The number of translation channels in the stream. |
Methods
Method | Description |
|---|---|
| AndMasks | AND all channel masks with other AnimationStream channel masks. |
| ClearFrameMasks | Clears frame channel masks |
| ClearMasks | Clears both frame and pass channel masks. |
| ClearPassMasks | Clears pass channel masks |
| CopyFrom | Copies all the data from the specified AnimationStream into this AnimationStream. This includes all the animation channel data and channel masks. |
| CopyMasksFrom | Copy all channel masks from a source AnimationStream. |
| Dispose | Releases the stream. |
| ExtractRootMotionBone | Removes the root motion from the pose by expressing the root motion bone relative to a root transform. |
| GetDefaultValues | Returns a read-only AnimationStream containing the default values of the RigDefinition. |
| GetEntityId | Returns the entity ID at the specified index. |
| GetFloat | Returns the generic float value at the specified index. |
| GetInt | Returns the generic integer value at the specified index. |
| GetLocalToCharacterInverseMatrix | Returns the local to character inverse matrix of the bone at the specified index. |
| GetLocalToCharacterMatrix | Returns the local to character matrix of the bone at the specified index. |
| GetLocalToCharacterRotation | Returns the local to character rotation of the bone at the specified index. |
| GetLocalToCharacterScale | Returns the local to character scale of the bone at the specified index. |
| GetLocalToCharacterTR | Returns the local to character translation and rotation of the bone at the specified index. |
| GetLocalToCharacterTranslation | Returns the local to character translation of the bone at the specified index. |
| GetLocalToCharacterTRS | Returns the local to character translation, rotation and scale of the bone at the specified index. |
| GetLocalToParentInverseMatrix | Returns the local to parent inverse matrix of the bone at the specified index. |
| GetLocalToParentMatrix | Returns the local to parent matrix of the bone at the specified index. |
| GetLocalToParentRotation | Returns the local to parent rotation of the bone at the specified index. |
| GetLocalToParentScale | Returns the local to parent scale of the bone at the specified index. |
| GetLocalToParentTR | Returns the local to parent translation and rotation of the bone at the specified index. |
| GetLocalToParentTranslation | Returns the local to parent translation of the bone at the specified index. |
| GetLocalToParentTRS | Returns the local to parent translation, rotation and scale of the bone at the specified index. |
| GetRotation | Returns a rotation value at the specified index. |
| GetSocketLocalToCharacterInverseMatrix | Returns the inverse matrix of the socket's transform in character space |
| GetSocketLocalToCharacterMatrix | Returns the matrix of the socket's transform in character space. |
| GetSocketLocalToCharacterRotation | Returns the rotation of the socket in character space. |
| GetSocketLocalToCharacterScale | Returns the scale of the socket in character space. |
| GetSocketLocalToCharacterTR | Returns the translation and rotation of the socket in character space. |
| GetSocketLocalToCharacterTranslation | Returns the translation of the socket in character space. |
| GetSocketLocalToCharacterTRS | Returns the translation, rotation and scale of the socket in character space. |
| GetSocketLocalToParentInverseMatrix | Returns the inverse matrix of the socket's transform relative to its parent bone |
| GetSocketLocalToParentMatrix | Returns the matrix of the socket's transform relative to its parent bone. |
| GetSocketLocalToParentRotation | Returns the static rotation offset of the socket relative to its parent bone. |
| GetSocketLocalToParentScale | Returns the static scale offset of the socket relative to its parent bone. |
| GetSocketLocalToParentTR | Returns the static translation and rotation offset of the socket relative to its parent bone. |
| GetSocketLocalToParentTranslation | Returns the static translation offset of the socket relative to its parent bone. |
| GetSocketLocalToParentTRS | Returns the static translation and rotation offset of the socket relative to its parent bone. |
| InverseTransformDirection | Transforms 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. |
| InverseTransformPosition | Transforms a position in character space to local space. You can perform the opposite conversion, from local to character space using AnimationStream.TransformPosition. |
| InverseTransformPositionRotation | Transforms a position and rotation in character space to local space. You can perform the opposite conversion, from local to character space using AnimationStream.TransformPositionRotation. |
| InverseTransformRotation | Transforms a rotation in character space to local space. You can perform the opposite conversion, from local to character space using AnimationStream.TransformRotation. |
| InverseTransformVector | Transforms 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. |
| OrMasks | OR all channel masks with other AnimationStream channel masks. |
| ResetRotationsToIdentity | Reset the stream's rotation to identity. |
| ResetToDefaultValues | Resets the channel values of the stream to the default values of the rig, and clears the channel masks. |
| ResetToZero | Reset the AnimationStream to 0. This includes all animation channel data and channel masks. |
| SetEntityId | Sets the entity ID value at the specified index. |
| SetFloat | Sets the generic float value at the specified index. |
| SetFrameMasks | Sets the frame mask values. |
| SetInt | Sets the generic integer value at the specified index. |
| SetLocalToCharacterRotation | Sets the local to character rotation of the bone at the specified index. |
| SetLocalToCharacterScale | Sets the local to character scale of the bone at the specified index. |
| SetLocalToCharacterTR | Sets the local to character translation and rotation of the bone at the specified index. |
| SetLocalToCharacterTranslation | Sets the local to character translation of the bone at the specified index. |
| SetLocalToCharacterTRS | Sets the local to character translation, rotation and scale of the bone at the specified index. |
| SetLocalToParentRotation | Sets the local to parent rotation of the bone at the specified index. |
| SetLocalToParentScale | Sets the local to parent scale of the bone at the specified index. |
| SetLocalToParentTR | Sets the local to parent translation and rotation of the bone at the specified index. |
| SetLocalToParentTranslation | Sets the local to parent translation of the bone at the specified index. |
| SetLocalToParentTRS | Sets the local to parent translation, rotation and scale of the bone at the specified index. |
| SetMasks | Sets both the frame and pass channel mask values. |
| SetPassMasks | Sets the pass mask values. |
| SetRotation | Sets the rotation value at the specified index. |
| TransformDirection | Transforms 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. |
| TransformPosition | Transforms a position in local space to character space. You can perform the opposite conversion, from character to local space using AnimationStream.InverseTransformPosition. |
| TransformPositionRotation | Transforms a position and rotation in local space to character space. You can perform the opposite conversion, from character to local space using AnimationStream.InverseTransformPositionRotation. |
| TransformRotation | Transforms a rotation in local space to character space. You can perform the opposite conversion, from character to local space using AnimationStream.InverseTransformRotation. |
| TransformVector | Transforms 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 |
|---|---|
| Create | Creates a read-write AnimationStream that allocates and owns its buffer. |
| CreateReadOnly | Creates a read-only AnimationStream from a RigDefinition and a NativeArray of AnimatedData. |
| FromDefaultValues | Creates a read-only AnimationStream from the default values of a RigDefinition. |