Documentation

Unity Engine

Download Unity

Unity Engine

AnimationStreamBlendingExtensions

Extension methods that blend, add, weight, invert, and subtract animation streams.
Read time 2 minutesLast updated 3 hours ago

Definition

public static class AnimationStreamBlendingExtensions

Remarks

These methods let you call the stream operations on the stream that receives the result, for example
stream.Blend(input, weight)
. 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.
The following example uses the
Unity.Animations
and
Unity.Collections
namespaces. It computes the weights of walk, jog, and run streams for a speed, then blends the streams into one output stream.
Additional 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

AddAdds stream values to the current stream.
BlendBlends an input stream with the current stream using a blending value.
BlendAdditiveBlend stream with an additive stream using a blend weight value.
DeltaComputes the difference between input and a stream to subtract (delta = subtract^-1 * input) and stores the result in the stream.
InverseInverts the stream.
WeightWeights the stream using a stream weight mask.