// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) VRMADA, All rights reserved.
//
// --------------------------------------------------------------------------------------------------------------------
using UnityEngine;
namespace UltimateXR.Animation.Interpolation
{
///
/// Generic base for interpolator classes.
///
/// The type the class will interpolate
public abstract class UxrVarInterpolator : UxrVarInterpolator
{
#region Constructors & Finalizer
///
/// Constructor.
///
/// Smooth damp value [0.0, 1.0]
/// Whether to use step interpolation, where the interpolation will always return the start value
protected UxrVarInterpolator(float smoothDamp, bool useStep) : base(smoothDamp, useStep)
{
}
#endregion
#region Public Overrides UxrVarInterpolator
///
public override object Interpolate(object a, object b, float t)
{
if (UseStep)
{
return a;
}
if (a is not T ta)
{
return default(T);
}
if (b is not T tb)
{
return default(T);
}
return Interpolate(ta, tb, t);
}
#endregion
#region Public Methods
///
/// Interpolates between 2 values.
///
/// Start value
/// End value
/// Interpolation factor [0.0, 1.0]
/// Interpolated value
///
/// The interpolated value will be affected by smoothing if the object was initialized with a smoothDamp value
/// greater than 0
///
public T Interpolate(T a, T b, float t)
{
T result = GetInterpolatedValue(a, b, t);
if (!RequiresSmoothDampRestart && SmoothDamp > 0.0f)
{
result = GetInterpolatedValue(_lastValue, result, UxrInterpolator.GetSmoothInterpolationValue(SmoothDamp, Time.deltaTime));
}
ClearSmoothDampRestart();
_lastValue = result;
return result;
}
#endregion
#region Protected Methods
///
/// Interpolates between 2 values. To be interpolated in child classes.
///
/// Start value
/// End value
/// Interpolation factor [0.0, 1.0]
/// Interpolated value
protected abstract T GetInterpolatedValue(T a, T b, float t);
#endregion
#region Private Types & Data
private T _lastValue;
#endregion
}
}