| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System.Runtime.CompilerServices; |
| | 6 | | using Unity.Mathematics; |
| | 7 | |
|
| | 8 | | namespace GDX.Mathematics |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Some helpful interpolation functionality. |
| | 12 | | /// </summary> |
| | 13 | | [VisualScriptingCompatible(8)] |
| | 14 | | public static class Smooth |
| | 15 | | { |
| | 16 | | // ReSharper disable CommentTypo |
| | 17 | | /// <summary> |
| | 18 | | /// Smooths between <paramref name="previousValue" /> and <paramref name="targetValue" /> based on time sinc |
| | 19 | | /// the last sample and a given half-life. |
| | 20 | | /// </summary> |
| | 21 | | /// <remarks>Assumes wibbly wobbly, timey wimey.</remarks> |
| | 22 | | /// <param name="previousValue">Ideally, the previous output value.</param> |
| | 23 | | /// <param name="targetValue">The target value.</param> |
| | 24 | | /// <param name="halfLife"> |
| | 25 | | /// Half of the time it would take to go from <paramref name="previousValue" /> to |
| | 26 | | /// <paramref name="targetValue" /> if time were constant. |
| | 27 | | /// </param> |
| | 28 | | /// <param name="elapsedTime"> |
| | 29 | | /// The amount of time that has transpired since the <paramref name="previousValue" /> was |
| | 30 | | /// generated. |
| | 31 | | /// </param> |
| | 32 | | /// <returns>A smoothed value.</returns> |
| | 33 | | // ReSharper restore CommentTypo |
| | 34 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 35 | | public static float Exponential(float previousValue, float targetValue, float halfLife, |
| | 36 | | float elapsedTime = float.NaN) |
| 0 | 37 | | { |
| 0 | 38 | | return halfLife <= 0f |
| | 39 | | ? targetValue |
| | 40 | | : math.lerp(previousValue, targetValue, HalfLifeToSmoothingFactor(halfLife, elapsedTime)); |
| 0 | 41 | | } |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Takes a <paramref name="halfLife" /> value, and outputs a factor based on <paramref name="elapsedTime" / |
| | 45 | | /// </summary> |
| | 46 | | /// <remarks>Not providing a value for <paramref name="elapsedTime" /> will result in using <c>Time.deltaTime</c |
| | 47 | | /// <param name="halfLife">The desired half-life.</param> |
| | 48 | | /// <param name="elapsedTime">The time since the last sample.</param> |
| | 49 | | /// <returns>The coefficient value applied to the weight(t) of a lerp.</returns> |
| | 50 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 51 | | public static float HalfLifeToSmoothingFactor(float halfLife, float elapsedTime) |
| 0 | 52 | | { |
| 0 | 53 | | if (halfLife <= 0f) |
| 0 | 54 | | { |
| 0 | 55 | | return float.NaN; |
| | 56 | | } |
| | 57 | |
|
| 0 | 58 | | float count = elapsedTime / halfLife; |
| 0 | 59 | | return 1f - math.pow(0.5f, count); |
| 0 | 60 | | } |
| | 61 | | } |
| | 62 | | } |