Documentation

Unity Engine

Download Unity

Unity Engine

Remap(ref RigRemapTable, ref AnimationStream, ref AnimationStream)

Copies the channel values of a source stream to a destination stream, using a rig remap table.
Read time 6 minutesLast updated 4 hours ago

Definition

Remap(RigRemapTable, AnimationStream, AnimationStream)

public static void Remap(ref RigRemapTable remapTable, ref AnimationStream destinationStream, ref AnimationStream sourceStream)

Parameters

remapTable

Remap table from the rig of the source stream to the rig of the destination stream.

destinationStream

Stream that receives the remapped values. It must be a created stream.

sourceStream

Stream that provides the values. It must be a created stream.

Remarks

The function copies the translation, rotation, scale, float, integer, and entity ID channels that the remap table lists. It applies the offsets of the remap table to the translation and rotation values. Translation and rotation channels whose offset uses RigRemapSpace.LocalToCharacter space are read and written in that space. All other channels are read and written in RigRemapSpace.LocalToParent space. Channels that the remap table doesn't list keep their current value in the destination stream.
The function doesn't copy sockets, because sockets belong to the rig, not to the stream. To map sockets, use RigRemapTable.SocketMappings.
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; } }}