Documentation

Unity Engine

Download Unity

Unity Engine

FreeformBlendSpace2DData

The precomputed data that the freeform 2D blend space function of BlendSpaceUtility needs.
Read time 2 minutesLast updated 3 hours ago

Definition

public struct FreeformBlendSpace2DData : IDisposable

Remarks

The constructor precomputes the geometry of the blend space once for a set of animation positions and for one FreeformBlendSpace2DMode.
Reuse the same instance for every call of BlendSpaceUtility.ComputeBlendSpace2DFreeformWeights, as long as the animation positions don't change. The data holds unmanaged memory. Call FreeformBlendSpace2DData.Dispose when you no longer need it.
The weight function also writes temporary data inside the instance. Don't use the same instance from several threads or jobs at the same time.
The following example uses the
Unity.Collections
and
Unity.Mathematics
namespaces. It computes the weights of several streams for a 2D input, then mixes the streams with a Core.PoseMixer.

Examples

// This example blends several poses placed in a 2D blend space, for example the strafe animations of a// character, according to a 2D input such as the movement direction and speed.// The poses buffer holds the animated data of one pose after the other, in the same order as the positions,// each with the stream size of the rig of the output stream.public static void BlendStrafe( NativeArray<AnimatedData> poses, NativeArray<float2> positions, float2 input, ref AnimationStream output){ // Precompute the data of the blend space once, and keep it as long as the positions do not change. // The mode selects the algorithm that computes the weights. using FreeformBlendSpace2DData blendSpace = new FreeformBlendSpace2DData(positions, FreeformBlendSpace2DMode.Cartesian, Allocator.Temp); // Compute one weight per pose. using NativeArray<float> weights = new NativeArray<float>(positions.Length, Allocator.Temp); BlendSpaceUtility.ComputeBlendSpace2DFreeformWeights(input, blendSpace, weights); // Mix all the poses with their weights. The weights sum to 1, so the default pose has no influence. // A stream can't be stored in an array, so create a read-only stream on each pose when you mix it. ref RigDefinition rig = ref output.RigDefinition; int streamSize = rig.Bindings.StreamSize; Core.PoseMixer mixer = default; mixer.Begin(ref output); for (int i = 0; i < positions.Length; ++i) { AnimationStream pose = AnimationStream.CreateReadOnly(ref rig, poses.GetSubArray(i * streamSize, streamSize)); mixer.Add(ref output, pose, weights[i]); } mixer.End(ref output, default);}

Constructors

Properties

Property

Description

CountThe number of animations in the blend space.
IsCreatedWhether the data holds allocated memory.
ModeThe algorithm this data was precomputed for.

Methods

Method

Description

DisposeReleases the memory that the data holds.