From eb4d6efecd36c66c75bd2afe76d19d9534f1a261 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Tue, 23 Feb 2021 00:43:58 +1000 Subject: [PATCH] Common: Add easing.h --- src/common/common.vcxproj | 1 + src/common/common.vcxproj.filters | 1 + src/common/easing.h | 257 ++++++++++++++++++++++++++++++ 3 files changed, 259 insertions(+) create mode 100644 src/common/easing.h diff --git a/src/common/common.vcxproj b/src/common/common.vcxproj index 3f281daef..2aa989d8e 100644 --- a/src/common/common.vcxproj +++ b/src/common/common.vcxproj @@ -67,6 +67,7 @@ + diff --git a/src/common/common.vcxproj.filters b/src/common/common.vcxproj.filters index 5dd50afb0..74a1322f1 100644 --- a/src/common/common.vcxproj.filters +++ b/src/common/common.vcxproj.filters @@ -109,6 +109,7 @@ + diff --git a/src/common/easing.h b/src/common/easing.h new file mode 100644 index 000000000..b5d5c1d54 --- /dev/null +++ b/src/common/easing.h @@ -0,0 +1,257 @@ +#pragma once +#include "types.h" +#include + +// From https://github.com/nicolausYes/easing-functions/blob/master/src/easing.cpp + +namespace Easing { +static constexpr float pi = 3.1415926545f; + +template +ALWAYS_INLINE_RELEASE static T InSine(T t) +{ + return std::sin(1.5707963f * t); +} + +template +ALWAYS_INLINE_RELEASE static T OutSine(T t) +{ + return 1 + std::sin(1.5707963f * (--t)); +} + +template +ALWAYS_INLINE_RELEASE static T InOutSine(T t) +{ + return 0.5f * (1 + std::sin(3.1415926f * (t - 0.5f))); +} + +template +ALWAYS_INLINE_RELEASE static T InQuad(T t) +{ + return t * t; +} + +template +ALWAYS_INLINE_RELEASE static T OutQuad(T t) +{ + return t * (2 - t); +} + +template +ALWAYS_INLINE_RELEASE static T InOutQuad(T t) +{ + return t < 0.5f ? 2 * t * t : t * (4 - 2 * t) - 1; +} + +template +ALWAYS_INLINE_RELEASE static T InCubic(T t) +{ + return t * t * t; +} + +template +ALWAYS_INLINE_RELEASE static T OutCubic(T t) +{ + return 1 + (--t) * t * t; +} + +template +ALWAYS_INLINE_RELEASE static T InOutCubic(T t) +{ + return t < 0.5f ? 4 * t * t * t : 1 + (--t) * (2 * (--t)) * (2 * t); +} + +template +ALWAYS_INLINE_RELEASE static T InQuart(T t) +{ + t *= t; + return t * t; +} + +template +ALWAYS_INLINE_RELEASE static T OutQuart(T t) +{ + t = (--t) * t; + return 1 - t * t; +} + +template +ALWAYS_INLINE_RELEASE static T InOutQuart(T t) +{ + if (t < 0.5) + { + t *= t; + return 8 * t * t; + } + else + { + t = (--t) * t; + return 1 - 8 * t * t; + } +} + +template +ALWAYS_INLINE_RELEASE static T InQuint(T t) +{ + T t2 = t * t; + return t * t2 * t2; +} + +template +ALWAYS_INLINE_RELEASE static T OutQuint(T t) +{ + T t2 = (--t) * t; + return 1 + t * t2 * t2; +} + +template +ALWAYS_INLINE_RELEASE static T InOutQuint(T t) +{ + T t2; + if (t < 0.5) + { + t2 = t * t; + return 16 * t * t2 * t2; + } + else + { + t2 = (--t) * t; + return 1 + 16 * t * t2 * t2; + } +} + +template +ALWAYS_INLINE_RELEASE static T InExpo(T t) +{ + return (std::pow(2, 8 * t) - 1) / 255; +} + +template +ALWAYS_INLINE_RELEASE static T OutExpo(T t) +{ + return 1 - std::pow(2, -8 * t); +} + +template +ALWAYS_INLINE_RELEASE static T InOutExpo(T t) +{ + if (t < 0.5f) + { + return (std::pow(2, 16 * t) - 1) / 510; + } + else + { + return 1 - 0.5f * std::pow(2, -16 * (t - 0.5f)); + } +} + +template +ALWAYS_INLINE_RELEASE static T InCirc(T t) +{ + return 1 - std::sqrt(1 - t); +} + +template +ALWAYS_INLINE_RELEASE static T OutCirc(T t) +{ + return std::sqrt(t); +} + +template +ALWAYS_INLINE_RELEASE static T InOutCirc(T t) +{ + if (t < 0.5f) + { + return (1 - std::sqrt(1 - 2 * t)) * 0.5f; + } + else + { + return (1 + std::sqrt(2 * t - 1)) * 0.5f; + } +} + +template +ALWAYS_INLINE_RELEASE static T InBack(T t) +{ + return t * t * (2.70158f * t - 1.70158f); +} + +template +ALWAYS_INLINE_RELEASE static T OutBack(T t) +{ + return 1 + (--t) * t * (2.70158f * t + 1.70158f); +} + +template +ALWAYS_INLINE_RELEASE static T InOutBack(T t) +{ + if (t < 0.5f) + { + return t * t * (7 * t - 2.5f) * 2; + } + else + { + return 1 + (--t) * t * 2 * (7 * t + 2.5f); + } +} + +template +ALWAYS_INLINE_RELEASE static T InElastic(T t) +{ + T t2 = t * t; + return t2 * t2 * std::sin(t * pi * 4.5f); +} + +template +ALWAYS_INLINE_RELEASE static T OutElastic(T t) +{ + T t2 = (t - 1) * (t - 1); + return 1 - t2 * t2 * std::cos(t * pi * 4.5f); +} + +template +ALWAYS_INLINE_RELEASE static T InOutElastic(T t) +{ + T t2; + if (t < 0.45f) + { + t2 = t * t; + return 8 * t2 * t2 * std::sin(t * pi * 9); + } + else if (t < 0.55f) + { + return 0.5f + 0.75f * std::sin(t * pi * 4); + } + else + { + t2 = (t - 1) * (t - 1); + return 1 - 8 * t2 * t2 * std::sin(t * pi * 9); + } +} + +template +ALWAYS_INLINE_RELEASE static T InBounce(T t) +{ + return std::pow(2, 6 * (t - 1)) * std::abs(sin(t * pi * 3.5f)); +} + +template +ALWAYS_INLINE_RELEASE static T OutBounce(T t) +{ + return 1 - std::pow(2, -6 * t) * std::abs(std::cos(t * pi * 3.5f)); +} + +template +ALWAYS_INLINE_RELEASE static T InOutBounce(T t) +{ + if (t < 0.5f) + { + return 8 * std::pow(2, 8 * (t - 1)) * std::abs(std::sin(t * pi * 7)); + } + else + { + return 1 - 8 * std::pow(2, -8 * t) * std::abs(std::sin(t * pi * 7)); + } +} + +} // namespace Easing \ No newline at end of file