Skip to content

Commit 8bf6b22

Browse files
committed
Add support for value snapping
1 parent fecbd5a commit 8bf6b22

File tree

5 files changed

+85
-21
lines changed

5 files changed

+85
-21
lines changed

Runtime/Delegates.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
/// <summary>
2020
/// A function type that interpolates the value between a and b by t.
2121
/// </summary>
22-
public delegate T Interpolater<T>(T a, T b, float t);
22+
public delegate T Interpolater<T>(T a, T b, float t, bool snapping);
2323

2424
}

Runtime/Interpolation.cs

Lines changed: 70 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,47 +10,51 @@ public static class Interpolation
1010
/// <summary>
1111
/// Linearly interpolates between a and b by t.
1212
/// </summary>
13-
public static float Lerp(float a, float b, float t)
13+
public static float Lerp(float a, float b, float t, bool snapping = false)
1414
{
15-
return Mathf.Lerp(a, b, t);
15+
float value = Mathf.Lerp(a, b, t);
16+
return snapping ? (int)value : value;
1617
}
1718

1819
/// <summary>
1920
/// Linearly interpolates between a and b by t.
2021
/// </summary>
21-
public static double Lerp(double a, double b, float t)
22+
public static double Lerp(double a, double b, float t, bool snapping = false)
2223
{
23-
return Mathf.Lerp((float)a, (float)b, t);
24+
double value = Mathf.Lerp((float)a, (float)b, t);
25+
return snapping ? (int)value : value;
2426
}
2527

2628
/// <summary>
2729
/// Linearly interpolates between a and b by t.
2830
/// </summary>
29-
public static int Lerp(int a, int b, float t)
31+
public static int Lerp(int a, int b, float t, bool snapping = false)
3032
{
3133
return (int)Mathf.Lerp(a, b, t);
3234
}
3335

3436
/// <summary>
3537
/// Linearly interpolates between a and b by t.
3638
/// </summary>
37-
public static long Lerp(long a, long b, float t)
39+
public static long Lerp(long a, long b, float t, bool snapping = false)
3840
{
39-
return (long)Mathf.Lerp(a, b, t);
41+
long value = (long)Mathf.Lerp(a, b, t);
42+
return snapping ? (int)value : value;
4043
}
4144

4245
/// <summary>
4346
/// Linearly interpolates between a and b by t.
4447
/// </summary>
45-
public static Vector2 Lerp(Vector2 a, Vector2 b, float t)
48+
public static Vector2 Lerp(Vector2 a, Vector2 b, float t, bool snapping = false)
4649
{
47-
return Vector2.Lerp(a, b, t);
50+
Vector2 value = Vector2.Lerp(a, b, t);
51+
return snapping ? Snap(value) : value;
4852
}
4953

5054
/// <summary>
5155
/// Linearly interpolates between a and b by t.
5256
/// </summary>
53-
public static Vector2Int Lerp(Vector2Int a, Vector2Int b, float t)
57+
public static Vector2Int Lerp(Vector2Int a, Vector2Int b, float t, bool snapping = false)
5458
{
5559
return new Vector2Int(
5660
(int)Mathf.Lerp(a.x, b.x, t),
@@ -60,15 +64,16 @@ public static Vector2Int Lerp(Vector2Int a, Vector2Int b, float t)
6064
/// <summary>
6165
/// Linearly interpolates between a and b by t.
6266
/// </summary>
63-
public static Vector3 Lerp(Vector3 a, Vector3 b, float t)
67+
public static Vector3 Lerp(Vector3 a, Vector3 b, float t, bool snapping = false)
6468
{
65-
return Vector3.Lerp(a, b, t);
69+
Vector3 value = Vector3.Lerp(a, b, t);
70+
return snapping ? Snap(value) : value;
6671
}
6772

6873
/// <summary>
6974
/// Linearly interpolates between a and b by t.
7075
/// </summary>
71-
public static Vector3Int Lerp(Vector3Int a, Vector3Int b, float t)
76+
public static Vector3Int Lerp(Vector3Int a, Vector3Int b, float t, bool snapping = false)
7277
{
7378
return new Vector3Int(
7479
(int)Mathf.Lerp(a.x, b.x, t),
@@ -79,25 +84,71 @@ public static Vector3Int Lerp(Vector3Int a, Vector3Int b, float t)
7984
/// <summary>
8085
/// Linearly interpolates between a and b by t.
8186
/// </summary>
82-
public static Vector4 Lerp(Vector4 a, Vector4 b, float t)
87+
public static Vector4 Lerp(Vector4 a, Vector4 b, float t, bool snapping = false)
8388
{
84-
return Vector4.Lerp(a, b, t);
89+
Vector4 value = Vector4.Lerp(a, b, t);
90+
return snapping ? Snap(value) : value;
8591
}
8692

8793
/// <summary>
8894
/// Linearly interpolates between a and b by t.
8995
/// </summary>
90-
public static Quaternion Lerp(Quaternion a, Quaternion b, float t)
96+
public static Quaternion Lerp(Quaternion a, Quaternion b, float t, bool snapping = false)
9197
{
92-
return Quaternion.Lerp(a, b, t);
98+
Quaternion value = Quaternion.Lerp(a, b, t);
99+
return snapping ? Snap(value) : value;
93100
}
94101

95102
/// <summary>
96103
/// Linearly interpolates between a and b by t.
97104
/// </summary>
98-
public static Color Lerp(Color a, Color b, float t)
105+
public static Color Lerp(Color a, Color b, float t, bool snapping = false)
99106
{
100-
return Color.Lerp(a, b, t);
107+
Color value = Color.Lerp(a, b, t);
108+
return snapping ? Snap(value) : value;
109+
}
110+
111+
/// <summary>
112+
/// Smoothly snaps the values to integers.
113+
/// </summary>
114+
internal static Vector2 Snap(Vector2 value)
115+
{
116+
return new Vector2((int)value.x, (int)value.y);
117+
}
118+
119+
/// <summary>
120+
/// Smoothly snaps the values to integers.
121+
/// </summary>
122+
internal static Vector3 Snap(Vector3 value)
123+
{
124+
return new Vector3((int)value.x, (int)value.y, (int)value.z);
125+
}
126+
127+
/// <summary>
128+
/// Smoothly snaps the values to integers.
129+
/// </summary>
130+
internal static Vector4 Snap(Vector4 value)
131+
{
132+
return new Vector4((int)value.x, (int)value.y, (int)value.z, (int)value.w);
133+
}
134+
135+
/// <summary>
136+
/// Smoothly snaps the values to integers.
137+
/// </summary>
138+
internal static Quaternion Snap(Quaternion value)
139+
{
140+
return Quaternion.Euler(Snap(value.eulerAngles));
141+
}
142+
143+
/// <summary>
144+
/// Smoothly snaps the values to integers.
145+
/// </summary>
146+
internal static Color Snap(Color value)
147+
{
148+
int r = Mathf.RoundToInt(value.r * 255.0f);
149+
int g = Mathf.RoundToInt(value.g * 255.0f);
150+
int b = Mathf.RoundToInt(value.b * 255.0f);
151+
return new Color(r / 255.0f, g / 255.0f, b / 255.0f, value.a);
101152
}
102153

103154
}

Runtime/PropertyChaining.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ public static T SetReversed<T>(this T tween, bool reversed = true) where T: Twee
5454
return tween;
5555
}
5656

57+
public static T SetSnapping<T>(this T tween, bool snapping = true) where T: Tween
58+
{
59+
tween.snapping = snapping;
60+
return tween;
61+
}
62+
5763
public static T SetRecyclable<T>(this T tween, bool recyclable) where T: Tween
5864
{
5965
tween.recyclable = recyclable;

Runtime/Tween.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ public abstract class Tween
117117
/// </summary>
118118
public bool reversed = false;
119119

120+
/// <summary>
121+
/// Smoothly snaps all interpolated values to integers.
122+
/// </summary>
123+
public bool snapping = false;
124+
120125
/// <summary>
121126
/// Keeps the tween in memory to be re-used after being killed.
122127
/// </summary>
@@ -408,6 +413,8 @@ internal void Reset()
408413
this.iterations = 0;
409414

410415
this.reversed = false;
416+
this.snapping = false;
417+
411418
this.autoStart = Tweening.defaultAutoStart;
412419
this.autoKill = Tweening.defaultAutoKill;
413420
this.recyclable = Tweening.defaultRecyclable;

Runtime/Tweener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public override void Animate()
5555
}
5656

5757
float time = EaseFunction.lookup[this.ease](percent);
58-
setter(interpolater(this.startValue, this.endValue, time));
58+
setter(interpolater(this.startValue, this.endValue, time, this.snapping));
5959
}
6060

6161
protected override void OnStart()

0 commit comments

Comments
 (0)