Documentation

Unity Engine

Download Unity

Unity Engine

RigRemapTable

Maps the channels of a source rig to the channels of a destination rig.
Read time 7 minutesLast updated 3 hours ago

Definition

public struct RigRemapTable

Remarks

Create a table with RigRemapTable.Create, which matches the channels that have the same identifier in both rigs. Use the channel filter to choose which kinds of channels to match, the offset overrides to transform translations and rotations on the way, and the match function to choose which identifiers to match. Then call Core.Remap to copy a pose from a stream of the source rig to a stream of the destination rig. RigRemapTableExtensions.Flip creates the table in the other direction.
The following example is a
MonoBehaviour
that animates two characters with different proportions with the same clip. It remaps the rotation of every bone, and the translation of the hips scaled by the ratio of the heights of the hips.
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.
``<![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 Unity.Mathematics;using UnityEngine;namespace Unity.Animations.DocumentationExamples{ // This example animates two characters with different proportions with the same clip. The source character is the // character that the clip was made for, and plays the clip as it is. This character has bones with the same names // but different lengths, and gets the pose of the source character remapped: the rotation of every bone, so it // keeps its own proportions, and the translation of the hips, scaled by the ratio of the heights of the hips, so it // moves up and down in proportion to its size. // // Assign the clip, the source character, and the skeleton root of each character, the top bone of its skeleton, a // direct child of the character. The paths in the clip are relative to the source character, and also match the // bones of this character. public class RemapExample : MonoBehaviour { public AnimationClip clip; public Transform sourceCharacter; public Transform sourceSkeletonRootTransform; public Transform skeletonRootTransform; public string hipsPath = "Hips"; BlobObjectReference<RigDefinition> m_SourceRig; BlobObjectReference<RigDefinition> m_TargetRig; NativeArray<TransformHandle> m_SourceBoneTransforms; NativeArray<TransformHandle> m_TargetBoneTransforms; ClipSampler m_Sampler; BlobObjectReference<RigRemapTable> m_Rotations; BlobObjectReference<RigRemapTable> m_HipsTranslation; float m_Time; void OnEnable() { if (clip == null || sourceCharacter == null || sourceSkeletonRootTransform == null || skeletonRootTransform == null) return; // Build one rig for each character. Both rigs use the clip, so they have the bones that it animates. // The references keep the rig definitions alive until you dispose them. var clips = new[] { clip }; using RigBuilder sourceBuilder = RigBuilder.Create( sourceCharacter, sourceSkeletonRootTransform, clips, Allocator.Temp); m_SourceRig = sourceBuilder.ToRigDefinition(); RigDefinitionUtility.BindTransforms( ref m_SourceRig.Value, sourceCharacter, Allocator.Persistent, out m_SourceBoneTransforms); using RigBuilder targetBuilder = RigBuilder.Create(transform, skeletonRootTransform, clips, Allocator.Temp); m_TargetRig = targetBuilder.ToRigDefinition(); RigDefinitionUtility.BindTransforms( ref m_TargetRig.Value, transform, Allocator.Persistent, out m_TargetBoneTransforms); m_Sampler = clip.ToClipSampler(ref m_SourceRig.Value, Allocator.Persistent); m_Time = 0f; var hipsId = new StringHash(hipsPath); int sourceHips = m_SourceRig.Value.Skeleton.FindBoneIndex(hipsId); int targetHips = m_TargetRig.Value.Skeleton.FindBoneIndex(hipsId); if (sourceHips < 0 || targetHips < 0) return; // The height ratio is the height of the target hips divided by the height of the source hips, above each // character, in the default pose of each rig. A ratio needs a source height that isn't 0. float sourceHeight = DefaultHeight(ref m_SourceRig.Value, sourceHips); float targetHeight = DefaultHeight(ref m_TargetRig.Value, targetHips); if (math.abs(sourceHeight) < 1e-4f) { Debug.LogWarning($"RemapExample: the hips of {sourceCharacter.name} are at height 0, so they give no height ratio.", this); return; } float heightRatio = targetHeight / sourceHeight; // Match the rotation channels that have the same identifier in both rigs. The filter leaves out the // translations, so the target keeps the bone lengths of its default pose. m_Rotations = RigRemapTable.Create(ref m_SourceRig.Value, ref m_TargetRig.Value, RigRemapTable.ChannelFilter.Rotation); // Match the translation of the hips only, and scale it with an offset. An offset override applies to the // channel of the target rig with the same identifier, as Rotation * (value * Scale + Translation). using var offsets = new RigRemapTable.OffsetOverrides(1, Allocator.Temp); offsets.AddTranslationOffsetOverride(hipsId, new RigRemapTable.TranslationOffset { Translation = float3.zero, Scale = heightRatio, Rotation = quaternion.identity, Space = RigRemapSpace.LocalToParent }); m_HipsTranslation = RigRemapTable.Create( ref m_SourceRig.Value, ref m_TargetRig.Value, RigRemapTable.ChannelFilter.Translation, offsets, (sourceId, targetId) => sourceId == hipsId && targetId == hipsId); } void OnDisable() { m_Rotations.Dispose(); m_HipsTranslation.Dispose(); // Dispose the sampler before the rig definition that it uses. if (m_Sampler.IsCreated) m_Sampler.Dispose(); if (m_SourceBoneTransforms.IsCreated) m_SourceBoneTransforms.Dispose(); if (m_TargetBoneTransforms.IsCreated) m_TargetBoneTransforms.Dispose(); m_SourceRig.Dispose(); m_TargetRig.Dispose(); } void Update() { m_Time += Time.deltaTime; ApplyPose(m_Time); } // Samples the clip on the source rig at a time, writes the pose to the source character, and writes the // remapped pose to this character. public void ApplyPose(float time) { if (!m_HipsTranslation.IsValid) return; AnimationStream source = AnimationStream.Create(ref m_SourceRig.Value, Allocator.Temp); m_Sampler.Sample(ref source, 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(source, m_SourceBoneTransforms); // A new stream holds the default pose of the rig. Remap writes the channels that the tables map, and the // other channels, such as the translations of the bones except the hips, keep their default values. AnimationStream target = AnimationStream.Create(ref m_TargetRig.Value, Allocator.Temp); // Remap takes the destination stream before the source stream. Core.Remap(ref m_Rotations.Value, ref target, ref source); Core.Remap(ref m_HipsTranslation.Value, ref target, ref source); WriteTransformsUtility.WriteTransforms(target, m_TargetBoneTransforms); // Dispose the streams explicitly, because a using declaration can't be passed by reference. target.Dispose(); source.Dispose(); } // Returns the height of a bone above the character, in the default pose of a rig. The default values of a rig // are the translations, rotations and scales of the transforms when you build it. static float DefaultHeight(ref RigDefinition rig, int boneIndex) { // Character space is the space of the character, and includes the parents of the bone, such as an Armature // transform above the hips. AnimationStream pose = AnimationStream.Create(ref rig, Allocator.Temp); float height = pose.GetLocalToCharacterTranslation(boneIndex).y; pose.Dispose(); return height; } }}

Fields

Value

Description

EntityIdMappingsEntity ID rig remap entries.
FloatMappingsFloat rig remap entries.
IntMappingsInteger rig remap entries.
LocalToParentTRCountNumber of translation and rotation remap entries that read in LocalToParent space.
RotationMappingsRotation rig remap entries.
RotationOffsetsOffsets referenced by rotation entries
ScaleMappingsScale rig remap entries.
SocketMappingsSocket rig remap entries.
SortedLocalToCharacterTREntriesSorted indices of translation and rotation channels that read in LocalToCharacter space.
TranslationMappingsTranslation rig remap entries.
TranslationOffsetsOffsets referenced by translation entries

Static Methods

Method

Description

CreateGiven source and destination rig definitions, this function creates a remap table based on matching bindings.
RemapEntityIdChannelRemaps an entity ID channel index associated to the RigRemapTable source rig definition to an entity ID channel index associated to the RigRemapTable target rig definition.
RemapFloatChannelRemaps a float channel index associated to the RigRemapTable source rig definition to a float channel index associated to the RigRemapTable target rig definition.
RemapIntChannelRemaps an integer channel index associated to the RigRemapTable source rig definition to an integer channel index associated to the RigRemapTable target rig definition.
RemapSocketChannelRemaps a socket channel index associated to the RigRemapTable source rig definition to a socket channel index associated to the RigRemapTable target rig definition.
RemapTransformChannelRemaps a transform channel index associated to the RigRemapTable source rig definition to a transform channel index associated to the RigRemapTable target rig definition.

Structs

Struct

Description

RigRemapTable.OffsetOverridesOffset overrides for translation and rotation channels in RigRemapTable.
RigRemapTable.RemapEntryEntry in RigRemapTable.
RigRemapTable.RotationOffsetOffset for rotation channel in RigRemapTable.
RigRemapTable.TranslationOffsetOffset for translation channel in RigRemapTable.

Enums

Enum

Description

RigRemapTable.ChannelFilterChannel filter to specify which types of channels should be used for matching.