Skip to content

Commit

Permalink
fix EaseMovementPlanTimings with inverse easing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cryy22 committed Apr 7, 2024
1 parent f0d2521 commit 884cc13
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 21 deletions.
57 changes: 43 additions & 14 deletions Runtime/Presentation/Arrangements/ArrangementMovementScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,14 @@ public static void ScheduleSerialTiming(
bool consistentSpeed
)
{
if (plans.Length == 0) return;

if (consistentSpeed)
{
var spacingAwareDistance = 0f;
for (var i = 0; i < plans.Length; i++)
spacingAwareDistance += plans[i].Distance * (1 + (i > 0 ? spacingPct : 0));
float spacingAwareDistance = plans[0].Distance;

for (var i = 1; i < plans.Length; i++)
spacingAwareDistance += plans[i].Distance * (1 + spacingPct);
spacingAwareDistance = Mathf.Max(a: spacingAwareDistance, b: Mathf.Epsilon);

var startTime = 0f;
Expand All @@ -185,14 +188,13 @@ bool consistentSpeed
{
float pDuration = duration / (1 + (plans.Length - 1) * (1 + spacingPct));
var startTime = 0f;
var endTime = 0f;

for (var i = 0; i < plans.Length; i++)
{
Plan plan = plans[i];
startTime = Mathf.Max(a: endTime + spacingPct * pDuration, b: startTime);
endTime = startTime + pDuration;
plans[i] = plan.Copy(startTime: startTime, endTime: startTime + pDuration);

plans[i] = plan.Copy(startTime: startTime, endTime: endTime);
startTime += Mathf.Max(a: (1 + spacingPct) * pDuration, b: 0);
}
}
}
Expand Down Expand Up @@ -269,17 +271,44 @@ public static Arrangement MussMovementPlans(
public static Arrangement EaseMovementPlanTimings(this Arrangement arrangement, Easings.Enum easing)
{
Plan[] plans = arrangement.ElementsMovementPlans.Values.ToArray();
float startDuration = plans.Max(p => p.StartTime);

EaseMovementPlanTimings(plans: plans, easing: easing);
foreach (Plan plan in plans)
arrangement.SetMovementPlan(plan);

return arrangement;
}

public static void EaseMovementPlanTimings(IEnumerable<Arrangement> arrangements, Easings.Enum easing)
{
int arrangementsCount = arrangements.SelectMany(a => a.ElementsMovementPlans).Count();
var plans = new Plan[arrangementsCount];
var arrangementsForPlans = new Arrangement[arrangementsCount];

var index = 0;
foreach (Arrangement arrangement in arrangements)
foreach (ElementMovementPlan plan in arrangement.ElementsMovementPlans.Values)
{
float startTime = Easings.Ease(t: plan.StartTime / startDuration, easing: easing) * startDuration;
arrangement.SetMovementPlan(
plan.Copy(startTime: startTime, endTime: startTime + plan.Duration)
);
plans[index] = plan;
arrangementsForPlans[index] = arrangement;
index++;
}

return arrangement;
EaseMovementPlanTimings(plans: plans, easing: easing);

for (var i = 0; i < arrangementsCount; i++)
arrangementsForPlans[i].SetMovementPlan(plans[i]);
}

public static void EaseMovementPlanTimings(Plan[] plans, Easings.Enum easing)
{
float startDuration = plans.Max(p => p.StartTime);

for (var i = 0; i < plans.Length; i++)
{
Plan plan = plans[i];
float startTime = Easings.Unease(x: plan.StartTime / startDuration, easing: easing) * startDuration;
plans[i] = plan.Copy(startTime: startTime, endTime: startTime + plan.Duration);
}
}

public static Plan CreateMovementPlan(
Expand Down
55 changes: 48 additions & 7 deletions Runtime/Presentation/Arrangements/Easings.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using UnityEngine;

namespace Crysc.Presentation.Arrangements
Expand All @@ -7,7 +8,9 @@ public abstract class Easings
public enum Enum
{
Linear,
EaseInOutSine,
EaseOutCubic,
EaseInOutCubic,
EaseOutElastic,
EaseInBack,
EaseOutBack,
Expand All @@ -23,20 +26,58 @@ public static float Ease(float t, Enum easing)
{
return easing switch
{
Enum.Linear => Linear(t),
Enum.EaseInOutSine => EaseInOutSine(t),
Enum.EaseOutCubic => EaseOutCubic(t),
Enum.EaseInOutCubic => EaseInOutCubic(t),
Enum.EaseOutElastic => EaseOutElastic(t),
Enum.EaseInBack => EaseInBack(t),
Enum.EaseOutBack => EaseOutBack(t),
Enum.EaseInOutBack => EaseInOutBack(t),
Enum.Linear => Linear(t),
_ => Linear(t),
_ => throw new ArgumentOutOfRangeException(
paramName: nameof(easing),
actualValue: easing,
message: "no ease function for this easing"
),
};
}

public static float Unease(float x, Enum easing)
{
return easing switch
{
Enum.EaseInOutSine => UneaseInOutSine(x),
Enum.EaseInOutCubic => UneaseInOutCubic(x),
_ => throw new ArgumentOutOfRangeException(
paramName: nameof(easing),
actualValue: easing,
message: "no unease function for this easing"
),
};
}

private static float Linear(float t) { return t; }

private static float EaseInOutSine(float t) { return -(Mathf.Cos(Mathf.PI * t) - 1) / 2; }

private static float UneaseInOutSine(float x) { return Mathf.Acos(-2 * x + 1) / Mathf.PI; }

private static float EaseOutCubic(float t) { return 1 - Mathf.Pow(f: 1 - t, p: 3); }

private static float EaseInOutCubic(float t)
{
return t < 0.5f
? 4f * t * t * t
: 1f - Mathf.Pow(f: -2f * t + 2f, p: 3f) / 2f;
}

private static float UneaseInOutCubic(float x)
{
return x < 0.5f
? Mathf.Pow(f: x / 4, p: 1f / 3f)
: -(Mathf.Pow(f: -2 * x + 2, p: 1f / 3f) - 2) / 2f;
}

private static float EaseOutElastic(float t)
{
return t switch
Expand All @@ -49,16 +90,16 @@ private static float EaseOutElastic(float t)

private static float EaseInBack(float t) { return _c3 * t * t * t - _c1 * t * t; }

private static float EaseOutBack(float t)
{
return 1 + _c3 * Mathf.Pow(f: t - 1, p: 3) + _c1 * Mathf.Pow(f: t - 1, p: 2);
}

private static float EaseInOutBack(float t)
{
return t < 0.5f
? Mathf.Pow(f: 2 * t, p: 2) * ((_c2 + 1) * 2 * t - _c2) / 2
: (Mathf.Pow(f: 2 * t - 2, p: 2) * ((_c2 + 1) * (t * 2 - 2) + _c2) + 2) / 2;
}

private static float EaseOutBack(float t)
{
return 1 + _c3 * Mathf.Pow(f: t - 1, p: 3) + _c1 * Mathf.Pow(f: t - 1, p: 2);
}
}
}

0 comments on commit 884cc13

Please sign in to comment.