< Summary

Class:GDX.Mathematics.Smooth
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/Mathematics/Smooth.cs
Covered lines:0
Uncovered lines:10
Coverable lines:10
Total lines:62
Line coverage:0% (0 of 10)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:2
Method coverage:0% (0 of 2)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Exponential(...)0%6200%
HalfLifeToSmoothingFactor(...)0%6200%

File(s)

./Packages/com.dotbunny.gdx/GDX/Mathematics/Smooth.cs

#LineLine coverage
 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
 5using System.Runtime.CompilerServices;
 6using Unity.Mathematics;
 7
 8namespace 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)
 037        {
 038            return halfLife <= 0f
 39                ? targetValue
 40                : math.lerp(previousValue, targetValue, HalfLifeToSmoothingFactor(halfLife, elapsedTime));
 041        }
 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)
 052        {
 053            if (halfLife <= 0f)
 054            {
 055                return float.NaN;
 56            }
 57
 058            float count = elapsedTime / halfLife;
 059            return 1f - math.pow(0.5f, count);
 060        }
 61    }
 62}