Interpolate#
Math / Interpolate Positions
Can Read
Cannot Write
Description#
Interpolates a position between A and B given a ratio number
Code#
using System;
using UnityEngine;
using VisualScript.Icons;
namespace VisualScript.Runtime
{
[Get]
[Title("Interpolate")]
[Category("Math/Interpolate", "Positions")]
[Description("Interpolates a position between A and B given a ratio number")]
[Image(typeof(IconInterpolate), ColorTheme.Type.Yellow)]
[Keywords("Lerp", "Slerp", "Linear", "Smooth", "Damp", "Step")]
[Serializable]
public class TypeVector2MathInterpolatePositions : TypeVector2
{
[SerializeField] private GetVector2 m_PointA = new GetVector2();
[SerializeField] private GetVector2 m_PointB = new GetVector2();
[SerializeField] private GetNumber m_Ratio = new GetNumber();
[SerializeField] private EaseMode m_Easing = EaseMode.Linear;
[SerializeField] private GetBool m_Clamp = new GetBool();
public override Vector2 GetPreview(GameObject source)
{
float ratio = Ease.Get(this.m_Easing, (float) this.m_Ratio.GetPreview(source));
return Interpolate(this.m_PointA.GetPreview(source), this.m_PointB.GetPreview(source), ratio, this.m_Clamp.GetPreview(source));
}
public override Vector2 Get(Args args)
{
Vector2 a = this.m_PointA.Get(args);
Vector2 b = this.m_PointB.Get(args);
double ratio = Ease.Get(this.m_Easing, (float) this.m_Ratio.Get(args));
bool clamp = this.m_Clamp.Get(args);
return Interpolate(a, b, ratio, clamp);
}
private static Vector2 Interpolate(Vector2 a, Vector2 b, double ratio, bool clamp)
{
if (clamp) ratio = Math.Clamp(ratio, 0.0, 1.0);
return new Vector2(
(float) (a.x + (b.x - a.x) * ratio),
(float) (a.y + (b.y - a.y) * ratio)
);
}
public override string ToString()
{
return $"Interpolate {this.m_PointA} to {this.m_PointB} by {this.m_Ratio}";
}
protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
}
}