@@ -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 }
0 commit comments