iTranslated by AI
Tween Library with Clear License and Easing Functions
Why Reinvent the Wheel?
When it comes to easing functions, resources like this one[1] are well-known. However, it is licensed under GPL-3.0.
While mathematical algorithms themselves are not subject to copyright, copying the source code and converting it to C# is a gray area at best; in the worst case, you may be required to publicly release the entire source code of your application, including the incorporated code.
It's easy to copy with the attitude of "Don't claim copyright on this sort of thing," but if you have some level of pride, you need to erase your memory of the website above, or rewrite everything from scratch without copy-pasting the source code.
With that in mind, I've clarified the precision for float and double, applied various optimizations, and organized everything under the MIT License.
Fundamentally, all you need is to apply easing to a Lerp function; I didn't like having to use the incantations specific to tween libraries, so I've consolidated everything into simple, Burst-compatible methods.
Mathf.LerpUnclamped(from, to, t);
// All I need is the easing function itself to plug into t!
Mathf.LerpUnclamped(from, to, easing(t));
// Using animation as a device to save/apply state while controlling easing via script
anim.SampleAnimation(this.gameObject, easing(t));
- Constant-fold where possible
- Eliminate divisions entirely
- Replace
Mathfunctions with lighter alternatives wherever possible - Since Sin is used within -90° to 90°, an approximation is good enough
- Only for the Sine series
- For Elastic, approximations fail to maintain
floataccuracy - It's ironic that the least appealing easing function requires the heaviest computation
- Just use
Quad
- Use the Doom (John Carmack) trick[2] for Sqrt (Circ series)
- AI never suggested it for some reason
- When I pointed to pointers, it even derived the magic number for the
longversion - It's on Wikipedia, so I'm puzzled why I didn't know (maybe because I didn't ask?)
- Keep expressions like
t * t * t * tas is, trusting the compiler will optimize them- Does
float t2 = t * t; return t2 * t2;really make a difference?
- Does
- Support for Unity Burst
- Reference Unity.Burst and Unity.Mathematics libraries
- Define symbols in
.asmdefetc.STMG_BURST_EXISTSSTMG_UNITYMATH_EXISTS- Note: Falls back to
System.Math/UnityEngine.Mathfif not defined
- Note: Not shown in Burst inspector because
IJobis not implemented
For Expo, I felt that t ^ 8 looks almost identical visually in animation results? However, considering the visible difference (staying near zero up to about 0.5) even in a small curve preview, I settled on the standard algorithm.
If you want an ultra-fast approximation (though not really similar), use:
float ExpoApprox(float t)
{
float t2 = t * t, t4 = t2 * t2, t8 = t4 * t4;
return t8;
}
It also gives a sense of action. You can also combine Quad * Cubic * Quart * Quint arbitrarily to create any level of "swoosh!" effect.
How to Use
Pass a normalized time between 0 and 1, and the eased value is returned. It's also convenient when you just want easing, not necessarily tweening—e.g., accelerating to maximum speed in one second.
There are EasingDouble and EasingFloat; the single-precision version is Unity-specific.
// For animation over a specified duration in an async method
float invDuration = 1.0f / duration;
// Accumulate Time.deltaTime until the specified duration elapses
float t = (elapsedTime += Time.deltaTime) * invDuration;
// Easing functions return 0..1, so use Lerp (some return values outside range but reach 1 at t=1)
// If multiple animations share the same duration, reuse the ease value and Lerp for slight efficiency
var value = Mathf.LerpUnclamped(from, to, EasingFloat.Quart(t));
👇 Loop so that execution starts at t=0, never exceeds 1, and always ends with t=1:
float elapsedTime = 0f;
float t = 0f;
do
{
await FooAsync(EasingFloat.Quart(t));
// Check for CT cancellation; break to skip to t=1, or return to maintain state
}
while ((t = (elapsedTime += Time.deltaTime) * invDuration) < 1f);
await FooAsync(EasingFloat.Quart(1f));
.NET's Lerp clamps forcefully, making things a bit cumbersome. Still, the fundamentals don't change.
// Simulate Mathf.LerpUnclamped
double value = from + ((to - from) * t);
Burst Details
Thanks, AI
-
[BurstCompile(FloatPrecision = FloatPrecision.Standard, FloatMode = FloatMode.Fast, OptimizeFor = OptimizeFor.Performance)]- FloatPrecision.Standard: Fast with sufficient precision
- FloatMode.Fast: Speeds up via SIMD optimizations and instruction reordering
- OptimizeFor.Performance: Speed over debugging for pure functions
- Using the
mathnamespace can further improve speed (e.g.,math.pow,math.sqrt) - For most game applications,
floatis sufficient
-
[BurstCompile(FloatPrecision = FloatPrecision.High, FloatMode = FloatMode.Fast, OptimizeFor = OptimizeFor.Performance)]- FloatPrecision.High: Recommended for
doubleas precision is important - FloatMode.Fast: Instructions can be sped up, but SIMD effect is smaller than for
float -
doublemay be SIMD vectorized on some CPUs but can be slower than single precision depending on the CPU - Note:
doubleprioritizes precision; speed won't matchfloat
- FloatPrecision.High: Recommended for
Optimizations for Unity Burst Compiler
According to AI, creating too many intermediate variables might actually negate the benefits of Burst.
Intermediate variables are likely effective in .NET environments, but for the Burst compiler, it might be better to let it decide whether to create them. Since we don't implement IJob, the Burst inspector shows nothing, leaving us blind—so we can't really tell.
(The .dll is output, so compilation does occur.)
FastSqrt is fast in the editor, but since it's JIT-compiled (unlike runtime), actual results in the execution environment are unknown. In any case, the AI only says things like "For SIMD, math.sqrt is better..." so it's not helpful. The caller isn't using Burst.
👇 Note: The following list targets files as of version 1.7.1
| Function | SIMD-Friendly | Comment | Optimization Example |
|---|---|---|---|
| QuadIn | ◎ | Only x*x, independent mul | return x * x; |
| QuadOut | ◎ | t=1-x, 1-(t*t) | return 1.0 - ((1.0-x)*(1.0-x)); |
| QuadInOut | △ | Has if branch, dependent on condition | Branchless: return (x<0.5)? 2*x*x : 1 - pow(-2*x+2,2)*0.5;
|
| CubicIn | ◎ | x³ | return x*x*x; |
| CubicOut | ◎ | t³ | return 1.0 - (1.0-x)*(1.0-x)*(1.0-x); |
| CubicInOut | △ | if branch | Can be made branchless |
| QuartIn | ◎ | x⁴ | return x*x*x*x; |
| QuartOut | ◎ | t⁴ | return 1.0 - pow(1.0-x,4); |
| QuartInOut | △ | if branch, t²*t² | return (x<0.5)? 8*x*x*x*x : 1 - pow(-2*x+2,2)*pow(-2*x+2,2)*0.5; |
| QuintIn | ◎ | x⁵ | return x*x*x*x*x; |
| QuintOut | ◎ | t⁵ | return 1.0 - pow(1.0-x,5); |
| QuintInOut | △ | if branch, t²t²t | Minimize dependencies via branchless |
| Sin5 | ◎ | t - t³/6 + t⁵/120 | return t - (t*t*t*inv6) + (t*t*t*t*t*inv120); |
| SineIn/Out/InOut | ◎ | Only calls Sin5 | Uses Sin5 above |
| ExpoIn/Out/InOut | × | Calls exp | Not SIMD-friendly (not only independent multiplications) |
| CircIn/Out/InOut | △ | FastSqrt has dependencies | Some multiplication dependency, but t² is SIMD-friendly |
| BackIn/Out/InOut | ◎ | Only polynomials | Removing intermediate variables makes SIMD easier |
| ElasticIn/Out/InOut | × | Calls exp + sin | Not SIMD-friendly |
🔹 Optimization Strategy
-
Reduce intermediate variables
Eliminate variables liket2, t3, t5and write expressions directly. -
Branchless coding
Replace InOut series with conditional operators. -
Use Horner's method or sequential multiplication for power operations
Streamline dependencies to make SIMD instructions easier to apply.
💡 Based on this table, Quad–Quint series, Sin5, Back series are SIMD-friendly even with a single input, and Burst is likely to automatically vectorize them.
If you wish, I can generate fully optimized code rewritten for SIMD based on this table.
Conclusion
When benchmarked on the latest .NET environment, the approximate Sine formula was 5x faster. I haven't benchmarked it on an actual application, but it shouldn't be slower, at least.
Attach EasingFloat_DebugGUI.cs to an empty GameObject and enter Play Mode to preview.
(You need to select whether to use float or double beforehand.)
That's all. Thank you for your time.
References
-
Easing Functions Cheat Sheet — https://easings.net/ ↩︎
-
Fast Inverse Square Root and Magic Numbers #C++ - Qiita — https://qiita.com/metaphysical_bard/items/e04378b16d6173127435 ↩︎
Discussion