RigDefinition
The blob representation of a Rig Definition. It contains the skeleton topology, a set of animation channels with unique identifiers and a buffer of default values for each animation channel.
Read time 4 minutesLast updated 3 hours ago
Definition
- Type: Struct
- Namespace: Unity.Animations
- Assembly: UnityEngine.AnimationCoreModule
public struct RigDefinition
Remarks
This is . It is unrelated to the rigs of the Animation Rigging package.
Unity.Animations.RigDefinitionBuild a rig definition with RigBuilder.ToRigDefinition. To get a builder with the bones and channels that clips animate, use RigBuilder.Create. An Avatar also stores a rig definition with a bone for each of its transforms. Read it with AvatarExtensions.ToRigReference.
The following example is a that plays a clip on the hierarchy under its GameObject with the rig definition that an Avatar stores. The last code block is the Burst-compiled function that writes the pose to the transforms. The function isn't part of the API, so copy it into your project.
MonoBehaviour``<![CDATA[using Unity.Burst; using Unity.Collections; using Unity.Mathematics; using UnityEngine;
namespace Unity.Animations.DocumentationExamples { // Writes the pose in a stream to the transform handles that // RigDefinitionUtility.BindTransforms outputs. Entry index of the // handles is the transform of bone index. [BurstCompile] public static class WriteTransformsUtility { // A Burst-compiled static method runs on the main thread, so it // writes the transforms without the cost of scheduling a job. [BurstCompile] public static void WriteTransforms( in AnimationStream stream, in NativeArray<TransformHandle> transforms) { for (int i = 0; i < transforms.Length; ++i) { // The entry of a bone that has no transform is None, and a // destroyed transform is no longer valid. TransformHandle transform = transforms[i]; if (!transform.IsValid()) { continue; }
stream.GetLocalToParentTRS( i, out float3 translation, out quaternion rotation, out float3 scale); transform.SetLocalPositionAndRotation(translation, rotation); transform.localScale = scale; } } } } ]]>
Examples
using Unity.Collections;using Unity.IntegerTime;using UnityEngine;namespace Unity.Animations.DocumentationExamples{ // This example plays a clip on the hierarchy under the GameObject of this component, with the rig definition that // an Avatar stores. Assign the Avatar of this hierarchy, and a clip that animates transforms under this GameObject. public class SampleClipWithAvatarExample : MonoBehaviour { public Avatar avatar; public AnimationClip animationClip; BlobObjectReference<RigDefinition> m_RigReference; NativeArray<TransformHandle> m_BoneTransforms; ClipSampler m_Sampler; float m_Time; void OnEnable() { if (avatar == null || animationClip == null) return; // An Avatar stores a rig definition with a bone for each transform of the Avatar. The reference keeps the // rig definition alive until you dispose it, even if the Avatar is destroyed. m_RigReference = avatar.ToRigReference(); if (!m_RigReference.IsValid) { m_RigReference.Dispose(); m_RigReference = default; return; } // Find the transform of each bone. The identifier of a bone of an Avatar is the StringHash of its path // relative to the root of the Avatar, so pass the transform that the Avatar was created from. RigDefinitionUtility.BindTransforms( ref m_RigReference.Value, transform, Allocator.Persistent, out m_BoneTransforms); // Create the sampler once, and keep it between frames to sample faster. m_Sampler = animationClip.ToClipSampler(ref m_RigReference.Value, Allocator.Persistent); m_Time = 0f; } void OnDisable() { // Dispose the sampler before the reference that keeps the rig definition alive. if (m_Sampler.IsCreated) m_Sampler.Dispose(); if (m_BoneTransforms.IsCreated) m_BoneTransforms.Dispose(); m_RigReference.Dispose(); } void Update() { m_Time += Time.deltaTime; ApplyClipPose(m_Time); } // Samples the clip at a time and writes the pose to the transforms. public void ApplyClipPose(float time) { // The sampler isn't created when the component has no Avatar or no clip. if (!m_Sampler.IsCreated) return; // The stream starts with the default values of the rig, which are the local transforms of the hierarchy // when the Avatar was created. The sample overwrites only the channels that the clip animates. AnimationStream stream = AnimationStream.Create(ref m_RigReference.Value, Allocator.Temp); // When the clip has Loop Time enabled, the time repeats. Otherwise, the pose stays at the end of the clip. m_Sampler.Sample(ref stream, new DiscreteTime(time)); // Write the pose to the transforms with a Burst-compiled function. The function isn't part of the API, // so copy it into your project. WriteTransformsUtility.WriteTransforms(stream, m_BoneTransforms); // Dispose the stream explicitly, because a using declaration can't be passed by reference. stream.Dispose(); } }}
Fields
Value | Description |
|---|---|
| Bindings | The set of animated channels with unique identifiers. |
| DefaultValues | Buffer of default values for all animated channels. |
| RootMotionBoneIndex | The index of the bone whose motion gives the root motion of a clip. |
| Skeleton | The skeleton topology. |
| SocketValues | Buffer of static socket offsets relative to their bone parent. |