rj-action-library/Runtime/Animation/Smoothing/FrameSmoothing.cs

57 lines
1.3 KiB
C#
Raw Normal View History

2025-01-13 18:11:12 +00:00
using System.Collections;
using System.Collections.Generic;
using Godot;
namespace Rokojori
{
[Tool]
[GlobalClass]
public partial class FrameSmoothing: Smoothing
{
[Export( PropertyHint.Range, "0,600")]
public int frames = 10;
2025-01-16 07:20:32 +00:00
2025-01-13 18:11:12 +00:00
protected override float _ComputeInterpolationAmount( float delta )
{
2025-01-16 07:20:32 +00:00
frames = Mathf.Clamp( frames, 0, 600 );
2025-01-13 18:11:12 +00:00
if ( frames <= 0 )
{
return 1;
2025-01-16 07:20:32 +00:00
}
2025-01-13 18:11:12 +00:00
var coefficient = GetCoefficientForFrames( frames );
return 1f - Mathf.Exp( -coefficient * delta );
}
2025-04-25 08:13:22 +00:00
public static float ComputeCoefficient( float delta, float frames )
{
int floored = Mathf.FloorToInt( frames );
float low = ComputeCoefficientInt( delta, floored );
float high = ComputeCoefficientInt( delta, floored + 1 );
return Mathf.Lerp( low, high, frames - floored );
}
public static float ComputeCoefficientInt( float delta, int frames )
{
frames = Mathf.Clamp( frames, 0, 600 );
if ( frames <= 0 )
{
return 1;
}
var coefficient = GetCoefficientForFrames( frames );
return 1f - Mathf.Exp( -coefficient * delta );
}
2025-01-16 07:20:32 +00:00
public static float GetCoefficientForFrames( int frames )
2025-01-13 18:11:12 +00:00
{
2025-01-16 07:20:32 +00:00
return FrameSmoothingTable.Get( frames );
2025-01-13 18:11:12 +00:00
}
}
}