Documentation

Unity Engine

Download Unity

Unity Engine

Delta(ref AnimationStream, in AnimationStream, in AnimationStream)

Computes the difference between input and a stream to subtract (delta = subtract^-1 * input) and stores the result in the stream.
Read time 5 minutesLast updated 4 hours ago

Definition

Delta(AnimationStream, AnimationStream, AnimationStream)

public static void Delta(this ref AnimationStream stream, in AnimationStream input, in AnimationStream subtract)

Parameters

The stream that receives the result. The method overwrites its previous contents.

The input stream.

The stream to subtract.

Remarks

Don't pass the stream itself as
input
or
subtract
. The method uses the stream for intermediate operations.
The result is an additive pose. Apply it with AnimationStreamBlendingExtensions.BlendAdditive, or weight it with AnimationStreamBlendingExtensions.Weight and apply it with AnimationStreamBlendingExtensions.Add.
The following example is a
MonoBehaviour
that makes an additive pose from two poses and adds it to a base clip with a weight for each channel.
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 UnityEngine;namespace Unity.Animations.DocumentationExamples{ // This example makes an additive pose from two poses at runtime, for example the difference between a leaning pose // and a neutral pose, and adds it to the pose of a base clip with a weight. Assign three clips that animate the // transforms under the GameObject of this component, with paths relative to this GameObject. Assign the skeleton // root, the top bone of the skeleton, a direct child of this GameObject. The pose clip and the reference pose clip // give the poses at their first frame. The mask selects the bone at the mask root path and the bones below it. public class AdditivePoseExample : MonoBehaviour { public AnimationClip baseClip; public AnimationClip poseClip; public AnimationClip referencePoseClip; public Transform skeletonRootTransform; public string maskRootPath = "Hips/Spine"; [Range(0f, 1f)] public float poseWeight = 1f; BlobObjectReference<RigDefinition> m_RigDefinition; BlobObjectReference<WeightMask> m_WeightMask; WeightMaskSampler m_WeightMaskSampler; NativeArray<TransformHandle> m_BoneTransforms; ClipSampler m_BaseSampler; NativeArray<AnimatedData> m_Additive; float m_Time; void OnEnable() { if (baseClip == null || poseClip == null || referencePoseClip == null || skeletonRootTransform == null) return; // Build one rig for the three clips, so that the clips sample into streams of the same rig. The reference // keeps the rig definition alive until you dispose it. using RigBuilder rigBuilder = RigBuilder.Create( transform, skeletonRootTransform, new[] { baseClip, poseClip, referencePoseClip }, Allocator.Temp); m_RigDefinition = rigBuilder.ToRigDefinition(); RigDefinitionUtility.BindTransforms( ref m_RigDefinition.Value, transform, Allocator.Persistent, out m_BoneTransforms); m_BaseSampler = baseClip.ToClipSampler(ref m_RigDefinition.Value, Allocator.Persistent); // Sample the two poses at the first frame of their clips. ClipSampler poseSampler = poseClip.ToClipSampler(ref m_RigDefinition.Value, Allocator.Persistent); ClipSampler referenceSampler = referencePoseClip.ToClipSampler( ref m_RigDefinition.Value, Allocator.Persistent); AnimationStream pose = AnimationStream.Create(ref m_RigDefinition.Value, Allocator.Temp); AnimationStream reference = AnimationStream.Create(ref m_RigDefinition.Value, Allocator.Temp); poseSampler.Sample(ref pose, new DiscreteTime(0f)); referenceSampler.Sample(ref reference, new DiscreteTime(0f)); referenceSampler.Dispose(); poseSampler.Dispose(); // Delta writes reference^-1 * pose to the additive stream. Applied to the reference pose, it gives the pose // back. The poses don't change, so compute the additive pose once and keep its buffer. A stream can't be a // field, so create a view of the buffer when you need the stream. m_Additive = new NativeArray<AnimatedData>(m_RigDefinition.Value.Bindings.StreamSize, Allocator.Persistent); AnimationStream additive = AnimationStream.Create(ref m_RigDefinition.Value, m_Additive); additive.Delta(pose, reference); reference.Dispose(); pose.Dispose(); // The bones that the mask doesn't list get a weight of 0, so only the bones below the mask root get the // additive pose. Because propagateWeight is true, the weight of the mask root also applies to its children. using WeightMaskBuilder maskBuilder = new WeightMaskBuilder( Allocator.Temp, defaultTransformWeight: 0f, defaultGenericWeight: 1f); maskBuilder.SetTransformMask(new StringHash(maskRootPath), 1f, propagateWeight: true); m_WeightMask = maskBuilder.ToWeightMask(); m_WeightMaskSampler = m_WeightMask.Value.ToWeightMaskSampler(ref m_RigDefinition.Value, Allocator.Persistent); m_Time = 0f; } void OnDisable() { // Dispose the samplers and the buffer before the rig definition that they use. if (m_BaseSampler.IsCreated) m_BaseSampler.Dispose(); if (m_Additive.IsCreated) m_Additive.Dispose(); if (m_WeightMaskSampler.IsCreated) m_WeightMaskSampler.Dispose(); m_WeightMask.Dispose(); if (m_BoneTransforms.IsCreated) m_BoneTransforms.Dispose(); m_RigDefinition.Dispose(); } void Update() { m_Time += Time.deltaTime; ApplyPose(m_Time); } // Samples the base clip at a time, adds the weighted pose difference, and writes the pose to the transforms. public void ApplyPose(float time) { if (!m_Additive.IsCreated) return; AnimationStream output = AnimationStream.Create(ref m_RigDefinition.Value, Allocator.Temp); m_BaseSampler.Sample(ref output, new DiscreteTime(time)); // The weight of each channel is poseWeight multiplied by its weight in the mask. Weight scales the additive // values towards zero and the rotations towards the identity rotation, for each channel. Use it on an // additive stream only: on a pose, it would also scale the bone lengths and scales. AnimationStreamMask mask = AnimationStreamMask.CreateReadOnly(ref m_RigDefinition.Value, m_WeightMaskSampler); AnimationStream weighted = AnimationStream.Create(ref m_RigDefinition.Value, Allocator.Temp); AnimationStream additive = AnimationStream.Create(ref m_RigDefinition.Value, m_Additive); weighted.Weight(additive, poseWeight, mask); // Add applies the weighted additive pose to the output pose. To blend several additive layers, weight each // one, then add them in turn. output.Add(weighted); // 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(output, m_BoneTransforms); // Dispose the streams explicitly, because a using declaration can't be passed by reference. weighted.Dispose(); output.Dispose(); } }}