namespace Zinnia.Data.Type.Transformation { using System; using UnityEngine; using UnityEngine.Events; using Zinnia.Extension; /// /// Transforms a by remapping from a range to a new range. /// /// /// 2f -> From(0f, 10f), To(0f, 1f), Mode(Lerp) = 0.2f /// 2f -> From(0f, 10f), To(1f, 0f), Mode(Lerp) = 0.8f /// 2f -> From(10f, 0f), To(0f, 1f), Mode(Lerp) = 0.8f /// 2f -> From(10f, 0f), To(1f, 0f), Mode(Lerp) = 0.2f /// 2f -> From(0f, 10f), To(0f, 1f), Mode(SmoothStep) = 0.104f /// public class FloatRangeValueRemapper : Transformer { /// /// Defines the event with the remapped value. /// [Serializable] public class UnityEvent : UnityEvent { } [Tooltip("The range of the value from.")] [SerializeField] private FloatRange from = new FloatRange(0f, 1f); /// /// The range of the value from. /// public FloatRange From { get { return from; } set { from = value; } } [Tooltip("The range of the value remaps to.")] [SerializeField] private FloatRange to = new FloatRange(0f, 1f); /// /// The range of the value remaps to. /// public FloatRange To { get { return to; } set { to = value; } } /// /// The mode to use when remapping. /// public enum OutputMode { /// /// Linearly interpolates. /// Lerp, /// /// Interpolates with smoothing at the limits /// SmoothStep } [Tooltip("The mode to use when remapping.")] [SerializeField] private OutputMode mode = OutputMode.Lerp; /// /// The mode to use when remapping. /// public OutputMode Mode { get { return mode; } set { mode = value; } } /// /// Sets the . /// /// The index of the . public virtual void SetMode(int index) { Mode = EnumExtensions.GetByIndex(index); } /// /// Transforms the given by remapping to a new range. /// /// The value to remap. /// A new remapped. protected override float Process(float input) { float t = Mathf.InverseLerp(From.minimum, From.maximum, input); return Mode == OutputMode.Lerp ? Mathf.Lerp(To.minimum, To.maximum, t) : Mathf.SmoothStep(To.minimum, To.maximum, t); } } }