Documentation

Unity Engine

Download Unity

Unity Engine

Evaluate(FunctionType, float)

Interpolates value of x based on specified interpolation function type.
Read time 4 minutesLast updated 3 hours ago

Definition

[BurstCompile]public static float Evaluate(Interpolator.FunctionType func, float x)

Parameters

Interpolation function type.

Value to interpolate. If x is outside of [0, 1] range, it will be clamped to [0, 1].

Returns

Type

Description

floatInterpolated value.

Remarks

The following example is a
MonoBehaviour
that cross-fades from one clip to another with an easing curve.
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 cross-fades from one clip to another with an easing curve. A linear fade changes the pose at a // constant rate, which can look abrupt where it starts and ends. SmoothStep starts and ends slowly. Assign two // 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. Set when // the transition starts and how long it lasts, in seconds. public class EaseCrossFadeExample : MonoBehaviour { public AnimationClip fromClip; public AnimationClip toClip; public Transform skeletonRootTransform; public float transitionStartTime; public float transitionDuration = 0.3f; BlobObjectReference<RigDefinition> m_RigDefinition; NativeArray<TransformHandle> m_BoneTransforms; ClipSampler m_FromSampler; ClipSampler m_ToSampler; float m_Time; void OnEnable() { if (fromClip == null || toClip == null || skeletonRootTransform == null) return; // Build one rig from both clips, so that both samplers write to streams of the same rig. // The reference keeps the rig definition alive until you dispose it. using RigBuilder rigBuilder = RigBuilder.Create( transform, skeletonRootTransform, new[] { fromClip, toClip }, Allocator.Temp); m_RigDefinition = rigBuilder.ToRigDefinition(); RigDefinitionUtility.BindTransforms( ref m_RigDefinition.Value, transform, Allocator.Persistent, out m_BoneTransforms); m_FromSampler = fromClip.ToClipSampler(ref m_RigDefinition.Value, Allocator.Persistent); m_ToSampler = toClip.ToClipSampler(ref m_RigDefinition.Value, Allocator.Persistent); m_Time = 0f; } void OnDisable() { // Dispose the samplers before the rig definition that they use. if (m_FromSampler.IsCreated) m_FromSampler.Dispose(); if (m_ToSampler.IsCreated) m_ToSampler.Dispose(); if (m_BoneTransforms.IsCreated) m_BoneTransforms.Dispose(); m_RigDefinition.Dispose(); } void Update() { m_Time += Time.deltaTime; ApplyPose(m_Time); } // Samples both clips at a time, blends them with the easing curve, and writes the pose to the transforms. public void ApplyPose(float time) { if (!m_FromSampler.IsCreated) return; AnimationStream fromPose = AnimationStream.Create(ref m_RigDefinition.Value, Allocator.Temp); AnimationStream toPose = AnimationStream.Create(ref m_RigDefinition.Value, Allocator.Temp); AnimationStream output = AnimationStream.Create(ref m_RigDefinition.Value, Allocator.Temp); m_FromSampler.Sample(ref fromPose, new DiscreteTime(time)); m_ToSampler.Sample(ref toPose, new DiscreteTime(time)); // Evaluate clamps its input to the range 0 to 1, so the fade holds the first pose before the transition // and the second pose after it. A transition without a duration switches at its start time. float progress = transitionDuration > 0f ? (time - transitionStartTime) / transitionDuration : (time >= transitionStartTime ? 1f : 0f); float weight = Interpolator.Evaluate(Interpolator.FunctionType.SmoothStep, progress); // A weight of 0 gives fromPose, and a weight of 1 gives toPose. output.Blend(fromPose, toPose, weight); // 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. output.Dispose(); toPose.Dispose(); fromPose.Dispose(); } }}