-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMathfx.cs
124 lines (103 loc) · 4.35 KB
/
Mathfx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using UnityEngine;
using System;
public class Mathfx {
public static float Hermite(float start, float end, float value) {
return Mathf.Lerp(start, end, value * value * (3.0f - 2.0f * value));
}
public static float Sinerp(float start, float end, float value) {
return Mathf.Lerp(start, end, Mathf.Sin(value * Mathf.PI * 0.5f));
}
public static float Coserp(float start, float end, float value) {
return Mathf.Lerp(start, end, 1.0f - Mathf.Cos(value * Mathf.PI * 0.5f));
}
public static float Berp(float start, float end, float value) {
value = Mathf.Clamp01(value);
value = (Mathf.Sin(value * Mathf.PI * (0.2f + 2.5f * value * value * value)) * Mathf.Pow(1f - value, 2.2f) + value) * (1f + (1.2f * (1f - value)));
return start + (end - start) * value;
}
public static float SmoothStep(float x, float min, float max) {
x = Mathf.Clamp(x, min, max);
float v1 = (x - min) / (max - min);
float v2 = (x - min) / (max - min);
return -2 * v1 * v1 * v1 + 3 * v2 * v2;
}
public static float Lerp(float start, float end, float value) {
return ((1.0f - value) * start) + (value * end);
}
public static Vector3 NearestPoint(Vector3 lineStart, Vector3 lineEnd, Vector3 point) {
Vector3 lineDirection = Vector3.Normalize(lineEnd - lineStart);
float closestPoint = Vector3.Dot((point - lineStart), lineDirection) / Vector3.Dot(lineDirection, lineDirection);
return lineStart + (closestPoint * lineDirection);
}
public static Vector3 NearestPointStrict(Vector3 lineStart, Vector3 lineEnd, Vector3 point) {
Vector3 fullDirection = lineEnd - lineStart;
Vector3 lineDirection = Vector3.Normalize(fullDirection);
float closestPoint = Vector3.Dot((point - lineStart), lineDirection) / Vector3.Dot(lineDirection, lineDirection);
return lineStart + (Mathf.Clamp(closestPoint, 0.0f, Vector3.Magnitude(fullDirection)) * lineDirection);
}
public static float Bounce(float x) {
return Mathf.Abs(Mathf.Sin(6.28f * (x + 1f) * (x + 1f)) * (1f - x));
}
// test for value that is near specified float (due to floating point inprecision)
// all thanks to Opless for this!
public static bool Approx(float val, float about, float range) {
return ((Mathf.Abs(val - about) < range));
}
// test if a Vector3 is close to another Vector3 (due to floating point inprecision)
// compares the square of the distance to the square of the range as this
// avoids calculating a square root which is much slower than squaring the range
public static bool Approx(Vector3 val, Vector3 about, float range) {
return ((val - about).sqrMagnitude < range * range);
}
/*
* CLerp - Circular Lerp - is like lerp but handles the wraparound from 0 to 360.
* This is useful when interpolating eulerAngles and the object
* crosses the 0/360 boundary. The standard Lerp function causes the object
* to rotate in the wrong direction and looks stupid. Clerp fixes that.
*/
public static float Clerp(float start, float end, float value) {
float min = 0.0f;
float max = 360.0f;
float half = Mathf.Abs((max - min) / 2.0f);//half the distance between min and max
float retval = 0.0f;
float diff = 0.0f;
if ((end - start) < -half) {
diff = ((max - start) + end) * value;
retval = start + diff;
} else if ((end - start) > half) {
diff = -((max - end) + start) * value;
retval = start + diff;
} else
retval = start + (end - start) * value;
// Debug.Log("Start: " + start + " End: " + end + " Value: " + value + " Half: " + half + " Diff: " + diff + " Retval: " + retval);
return retval;
}
public static Vector3 Coserp(Vector3 start, Vector3 end, float value) {
return new Vector3(
Mathfx.Coserp(start.x, end.x, value),
Mathfx.Coserp(start.y, end.y, value),
Mathfx.Coserp(start.z, end.z, value)
);
}
public static Vector3 Sinerp(Vector3 start, Vector3 end, float value) {
return new Vector3(
Mathfx.Sinerp(start.x, end.x, value),
Mathfx.Sinerp(start.y, end.y, value),
Mathfx.Sinerp(start.z, end.z, value)
);
}
public static Vector3 Hermite(Vector3 start, Vector3 end, float value) {
return new Vector3(
Mathfx.Hermite(start.x, end.x, value),
Mathfx.Hermite(start.y, end.y, value),
Mathfx.Hermite(start.z, end.z, value)
);
}
public static Vector3 Berp(Vector3 start, Vector3 end, float value) {
return new Vector3(
Mathfx.Berp(start.x, end.x, value),
Mathfx.Berp(start.y, end.y, value),
Mathfx.Berp(start.z, end.z, value)
);
}
}