AnimationStreamBlendingExtensions
Extension methods that blend, add, weight, invert, and subtract animation streams.
Read time 2 minutesLast updated 3 hours ago
Definition
- Type: Class
- Namespace: Unity.Animations
- Assembly: UnityEngine.AnimationCoreModule
public static class AnimationStreamBlendingExtensions
Remarks
These methods let you call the stream operations on the stream that receives the result, for example . Each method writes its result into the stream that it's called on. All the streams that you pass to a method must use the same rig. Overloads that take an AnimationStreamMask scale the weight of each channel by the mask.
stream.Blend(input, weight)The following example uses the and namespaces. It computes the weights of walk, jog, and run streams for a speed, then blends the streams into one output stream.
Unity.AnimationsUnity.CollectionsAdditional Resources: BlendSpaceUtility, AnimationStream
Examples
// This example blends walk, jog, and run streams according to a movement speed.// The walk, jog, and run animations are placed at speeds 1, 3, and 6 in the blend space.public static void BlendLocomotion( in AnimationStream walk, in AnimationStream jog, in AnimationStream run, float speed, ref AnimationStream output){ // The movement speed that each stream represents, in the order walk, jog, and run. using NativeArray<float> animationSpeeds = new NativeArray<float>(new float[] { 1f, 3f, 6f }, Allocator.Temp); NativeArray<float> weights = new NativeArray<float>(3, Allocator.Temp); // Compute one weight per stream. At a speed of 2, walk and jog each get a weight of 0.5. // Speeds outside the range from 1 to 6 give all the weight to the nearest stream, because wrapping is off. // The two 0f arguments are the wrap limits, which the call ignores when wrapping is off. BlendSpaceUtility.ComputeBlendSpace1DWeights( speed, animationSpeeds, false, 0f, 0f, weights); // Start from the walk stream, then blend in each other stream with a weight // relative to the total weight accumulated so far. output.CopyFrom(walk); float accumulatedWeight = weights[0]; if (weights[1] > 0f) { accumulatedWeight += weights[1]; output.Blend(jog, weights[1] / accumulatedWeight); } if (weights[2] > 0f) { accumulatedWeight += weights[2]; output.Blend(run, weights[2] / accumulatedWeight); } // Dispose the weights explicitly, because a using declaration can't be passed by reference. weights.Dispose();}
Static Methods
Method | Description |
|---|---|
| Add | Adds stream values to the current stream. |
| Blend | Blends an input stream with the current stream using a blending value. |
| BlendAdditive | Blend stream with an additive stream using a blend weight value. |
| Delta | Computes the difference between input and a stream to subtract (delta = subtract^-1 * input) and stores the result in the stream. |
| Inverse | Inverts the stream. |
| Weight | Weights the stream using a stream weight mask. |