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

66 lines
1.6 KiB
C#

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;
static Dictionary<int,float> _framesToCoefficient = new Dictionary<int, float>();
protected override float _ComputeInterpolationAmount( float delta )
{
if ( frames <= 0 )
{
return 1;
}
var coefficient = GetCoefficientForFrames( frames );
return 1f - Mathf.Exp( -coefficient * delta );
}
public float GetCoefficientForFrames( int frames )
{
if ( ! _framesToCoefficient.ContainsKey( frames ) )
{
_framesToCoefficient[ frames ] = ComputeCoefficientForFrames( frames );
}
return _framesToCoefficient[ frames ];
}
static float ComputeCoefficientForFrames( int numFrames, float treshold = 1f/60f * 0.1f )
{
var framesDuration = numFrames * 1/60f;
var minMaxSearch = new MinMaxSearch<float>(
( a, b, t ) => Mathf.Lerp( a, b, t ),
( c ) => ExpSmoothing.GetDurationForCoefficient( c )
);
var lowDurationCoefficient = 100000f;
var highDurationCoefficient = 0.1f;
RJLog.Log( "Finding coefficient for frames", numFrames, ">>", framesDuration, "s" );
var coefficient = minMaxSearch.Find( framesDuration, lowDurationCoefficient, highDurationCoefficient, treshold );
RJLog.Log( "Found coefficient:", coefficient, ">> duration", ExpSmoothing.GetDurationForCoefficient( coefficient )._FF(), "s" );
return coefficient;
}
}
}