Ease缓动函数封装

 

什么是Ease缓动函数:

Ease缓动函数是一种常见的动画效果函数,使用Ease缓动函数可以让动画效果看起来更加真实,时而加速时而减速,让动画不至于看上去太过“平凡”

具体的缓动效果可以参考 https://www.xuanfengge.com/easeing/easeing/

算是比较简单的一个功能,就不墨迹了

using UnityEngine;

public static class EaseUtils
{
    public enum EaseType
    {
        Linear,
        InQuad,
        OutQuad,
        InOutQuad,
        InCubic,
        OutCubic,
        InOutCubic,
        InQuart,
        OutQuart,
        InOutQuart,
        InQuint,
        OutQuint,
        InOutQuint,
        InSine,
        OutSine,
        InOutSine,
        InExpo,
        OutExpo,
        InOutExpo,
        InCirc,
        OutCirc,
        InOutCirc
    }

    public static float Ease(float t, EaseType easeType)
    {
        switch (easeType)
        {
            case EaseType.Linear:
                return Linear(t);
            case EaseType.InQuad:
                return InQuad(t);
            case EaseType.OutQuad:
                return OutQuad(t);
            case EaseType.InOutQuad:
                return InOutQuad(t);
            case EaseType.InCubic:
                return InCubic(t);
            case EaseType.OutCubic:
                return OutCubic(t);
            case EaseType.InOutCubic:
                return InOutCubic(t);
            case EaseType.InQuart:
                return InQuart(t);
            case EaseType.OutQuart:
                return OutQuart(t);
            case EaseType.InOutQuart:
                return InOutQuart(t);
            case EaseType.InQuint:
                return InQuint(t);
            case EaseType.OutQuint:
                return OutQuint(t);
            case EaseType.InOutQuint:
                return InOutQuint(t);
            case EaseType.InSine:
                return InSine(t);
            case EaseType.OutSine:
                return OutSine(t);
            case EaseType.InOutSine:
                return InOutSine(t);
            case EaseType.InExpo:
                return InExpo(t);
            case EaseType.OutExpo:
                return OutExpo(t);
            case EaseType.InOutExpo:
                return InOutExpo(t);
            case EaseType.InCirc:
                return InCirc(t);
            case EaseType.OutCirc:
                return OutCirc(t);
            case EaseType.InOutCirc:
                return InOutCirc(t);
        }

        return t; // 默认为Linear类型
    }

    private static float Linear(float t)
    {
        return t;
    }

    private static float InQuad(float t)
    {
        return t * t;
    }

    private static float OutQuad(float t)
    {
        return 1f - (1f - t) * (1f - t);
    }

    private static float InOutQuad(float t)
    {
        if (t < 0.5f)
            return 2f * t * t;
        else
            return 1f - 2f * (1f - t) * (1f - t);
    }

    private static float InCubic(float t)
    {
        return t * t * t;
    }

    private static float OutCubic(float t)
    {
        float f = t - 1f;
        return f * f * f + 1f;
    }

    private static float InOutCubic(float t)
    {
        if (t < 0.5f)
            return 4f * t * t * t;
        else
        {
            float f = 2f * t - 2f;
            return 0.5f * f * f * f + 1f;
        }
    }

    private static float InQuart(float t)
    {
        return t * t * t * t;
    }

    private static float OutQuart(float t)
    {
        float f = t - 1f;
        return 1f - f * f * f * f;
    }

    private static float InOutQuart(float t)
    {
        if (t < 0.5f)
            return 8f * t * t * t * t;
        else
        {
            float f = t - 1f;
            return 1f - 8f * f * f * f * f;
        }
    }

    private static float InQuint(float t)
    {
        return t * t * t * t * t;
    }

    private static float OutQuint(float t)
    {
        float f = t - 1f;
        return 1f + f * f * f * f * f;
    }

    private static float InOutQuint(float t)
    {
        if (t < 0.5f)
            return 16f * t * t * t * t * t;
        else
        {
            float f = 2f * t - 2f;
            return 0.5f * (1f + f * f * f * f * f);
        }
    }

    private static float InSine(float t)
    {
        return 1f - Mathf.Cos(t * Mathf.PI * 0.5f);
    }

    private static float OutSine(float t)
    {
        return Mathf.Sin(t * Mathf.PI * 0.5f);
    }

    private static float InOutSine(float t)
    {
        return 0.5f * (1f - Mathf.Cos(t * Mathf.PI));
    }

    private static float InExpo(float t)
    {
        return t == 0f ? 0f : Mathf.Pow(2f, 10f * (t - 1f));
    }

    private static float OutExpo(float t)
    {
        return t == 1f ? 1f : 1f - Mathf.Pow(2f, -10f * t);
    }

    private static float InOutExpo(float t)
    {
        if (t == 0f || t == 1f)
            return t;
        else if (t < 0.5f)
            return 0.5f * Mathf.Pow(2f, 20f * t - 10f);
        else
            return -0.5f * Mathf.Pow(2f, -20f * t + 10f) + 1f;
    }

    private static float InCirc(float t)
    {
        return 1f - Mathf.Sqrt(1f - t * t);
    }

    private static float OutCirc(float t)
    {
        float f = t - 1f;
        return Mathf.Sqrt(1f - f * f);
    }

    private static float InOutCirc(float t)
    {
        if (t < 0.5f)
            return 0.5f * (1f - Mathf.Sqrt(1f - 4f * t * t));
        else
            return 0.5f * (Mathf.Sqrt(1f - (2f * t - 2f) * (2f * t - 2f)) + 1f);
    }

}

 

posted @ 2023-06-13 17:41  狐狸爱看书  阅读(124)  评论(0编辑  收藏  举报