-
Notifications
You must be signed in to change notification settings - Fork 2
/
vec3.go
121 lines (96 loc) · 2.93 KB
/
vec3.go
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
package gm
import (
"math"
)
// Vec3 an Float array of size 3 with methods for vector operations.
type Vec3 [3]Float
func V3(v ...Float) Vec3 {
switch len(v) {
case 0:
return Vec3{}
case 1:
return Vec3{v[0], v[0], v[0]}
case 2:
return Vec3{v[0], v[1], v[0]}
default:
return Vec3{v[0], v[1], v[2]}
}
}
// Add returns a new Vec3 based on the sum of the param.
func (v Vec3) Add(v2 Vec3) Vec3 {
return Vec3{v[0] + v2[0], v[1] + v2[1], v[2] + v2[2]}
}
// Sub returns a new Vec3 with subtracting each element of v2.
func (v Vec3) Sub(v2 Vec3) Vec3 {
return Vec3{v[0] - v2[0], v[1] - v2[1], v[2] - v2[2]}
}
// Mul returns a new vec3 based on the multiplication of vec3*scalar.
func (v Vec3) Mul(c Float) Vec3 {
return Vec3{v[0] * c, v[1] * c, v[2] * c}
}
// MulVec3 multiplies each element by the corresponding element of v2.
func (v Vec3) MulVec3(v2 Vec3) Vec3 {
return Vec3{v[0] * v2[0], v[1] * v2[1], v[2] * v2[2]}
}
// Len returns the vec3 len.
func (v Vec3) Len() Float {
return Float(math.Sqrt(float64(v[0]*v[0] + v[1]*v[1] + v[2]*v[2])))
}
// Normalize returns a new normalized vec3.
func (v Vec3) Normalize() Vec3 {
l := 1.0 / v.Len()
return Vec3{v[0] * l, v[1] * l, v[2] * l}
}
// Cross returns a new vec3 with the cross product of v with v2.
func (v Vec3) Cross(v2 Vec3) Vec3 {
return Vec3{v[1]*v2[2] - v[2]*v2[1], v[2]*v2[0] - v[0]*v2[2], v[0]*v2[1] - v[1]*v2[0]}
}
// Dot returns the dot product of v with v2
func (v Vec3) Dot(v2 Vec3) Float {
return v[0]*v2[0] + v[1]*v2[1] + v[2]*v2[2]
}
// Reflect returns a reflected vec3 with n
func (v Vec3) Reflect(n Vec3) Vec3 {
return v.Sub(n.Mul(2 * v.Dot(n)))
}
// Vec2 returns a Vec2 with the first 2 elements of v.
func (v Vec3) Vec2() Vec2 {
return Vec2{v[0], v[1]}
}
// Vec4 returns a vec4 with the new element w.
func (v Vec3) Vec4(w Float) Vec4 {
return Vec4{v[0], v[1], v[2], w}
}
// Clamp returns a vec3 with the v elements clamped to min and max.
func (v Vec3) Clamp(min, max Vec3) Vec3 {
return Vec3{
Clamp(v[0], min[0], max[0]),
Clamp(v[1], min[1], max[1]),
Clamp(v[2], min[2], max[2]),
}
}
// Lerp Linear interpolation between 2 vecs
func (v Vec3) Lerp(b Vec3, t Float) Vec3 {
return Vec3{
v[0] + t*(b[0]-v[0]),
v[1] + t*(b[1]-v[1]),
v[2] + t*(b[2]-v[2]),
}
}
func (v Vec3) X() Float { return v[0] }
func (v Vec3) Y() Float { return v[1] }
func (v Vec3) Z() Float { return v[2] }
func (v Vec3) XY() Vec2 { return Vec2{v[0], v[1]} }
func (v Vec3) YZ() Vec2 { return Vec2{v[1], v[2]} }
// Up returns a up vector
func Up() Vec3 { return Vec3{0, 1, 0} }
// Down returns a down vector
func Down() Vec3 { return Vec3{0, -1, 0} }
// Forward returns a vector facing forward
func Forward() Vec3 { return Vec3{0, 0, -1} }
// Backward returns a vector facing backward
func Backward() Vec3 { return Vec3{0, 0, 1} }
// Left returns a vector pointing left
func Left() Vec3 { return Vec3{-1, 0, 0} }
// Right returns a vector pointing left
func Right() Vec3 { return Vec3{1, 0, 0} }