This repository has been archived by the owner on Jun 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.glsl
79 lines (69 loc) · 2.26 KB
/
math.glsl
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
#define FLT_EPSILON 1.192092896e-07
float ofMap(float value, float inputMin, float inputMax, float outputMin, float outputMax, bool bClamp)
{
if (abs(inputMin - inputMax) < FLT_EPSILON){
return outputMin;
} else {
float outVal = ((value - inputMin) / (inputMax - inputMin) * (outputMax - outputMin) + outputMin);
if (bClamp){
if (outputMax < outputMin){
if (outVal < outputMax) outVal = outputMax;
else if (outVal > outputMin) outVal = outputMin;
}else{
if (outVal > outputMax) outVal = outputMax;
else if (outVal < outputMin) outVal = outputMin;
}
}
return outVal;
}
}
vec2 ofMap(vec2 value, vec2 inputMin, vec2 inputMax, vec2 outputMin, vec2 outputMax, bool bClamp)
{
float x = ofMap(value.x, inputMin.x, inputMax.x, outputMin.x, outputMax.x, bClamp);
float y = ofMap(value.y, inputMin.y, inputMax.y, outputMin.y, outputMax.y, bClamp);
return vec2(x, y);
}
vec3 ofMap(vec3 value, vec3 inputMin, vec3 inputMax, vec3 outputMin, vec3 outputMax, bool bClamp)
{
float x = ofMap(value.x, inputMin.x, inputMax.x, outputMin.x, outputMax.x, bClamp);
float y = ofMap(value.y, inputMin.y, inputMax.y, outputMin.y, outputMax.y, bClamp);
float z = ofMap(value.z, inputMin.z, inputMax.z, outputMin.z, outputMax.z, bClamp);
return vec3(x, y, z);
}
float ofWrap(float value, float from, float to)
{
if (from > to){
float t = from;
from = to;
to = t;
}
float cycle = to - from;
if (cycle == 0){
return to;
}
return value - cycle * floor((value - from) / cycle);
}
float ofWrapRadians(float angle, float from, float to)
{
return ofWrap(angle, from, to);
}
float ofWrapDegrees(float angle, float from, float to)
{
return ofWrap(angle, from, to);
}
float ofAngleDifferenceDegrees(float currentAngle, float targetAngle)
{
return ofWrapDegrees(targetAngle - currentAngle, -180.0, 180.0);
}
float ofAngleDifferenceRadians(float currentAngle, float targetAngle)
{
return ofWrapRadians(targetAngle - currentAngle, -3.141592741, 3.141592741);
}
float ofLerpDegrees(float currentAngle, float targetAngle, float pct)
{
return currentAngle + ofAngleDifferenceDegrees(currentAngle,targetAngle) * pct;
}
float ofLerpRadians(float currentAngle, float targetAngle, float pct)
{
return currentAngle + ofAngleDifferenceRadians(currentAngle,targetAngle) * pct;
}