-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec3.lua
281 lines (234 loc) · 7.07 KB
/
vec3.lua
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
-- vec2.lua: A simple 3D vector library
--[[
Copyright (c) 2022 Nelson Lopez
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
]]
local vector = { x = 0, y = 0, z = 0 }
vector.__index = vector
vector.__type = "vec3"
vector.name = vector.__type
local function is_vector(a)
return getmetatable(a) == vector
end
vector.is_vector = is_vector
vector.new = function (x, y, z)
return setmetatable(
{ x = x, y = y, z = z }, vector
)
end
vector.copy = function (self)
return vector.new(self.x, self.y, self.z)
end
vector.rotate = function (self, phi, axis)
local u = axis:normalize()
local c = math.cos(phi)
local s = math.sin(phi)
return vector.new(
self:dot(vector.new(
(c + u.x * u.x * (1 - c)),
(u.x * u.y * (1 - c) - u.z * s),
(u.x * u.z * (1 - c) + u.y * s)
)),
self:dot(vector.new(
(u.y * u.x * (1 - c) + u.z * s),
(c + u.y * u.y * (1 - c)),
(u.y * u.z * (1 - c) - u.x * s)
)),
self:dot(vector.new(
(u.z * u.x * (1 - c) - u.y * s),
(u.z * u.y * (1 - c) + u.x * s),
(c + u.z * u.z * (1 - c))
))
)
end
vector.from_table = function (t)
return vector.new(
tonumber(t.x) or 0,
tonumber(t.y) or 0,
tonumber(t.z) or 0
)
end
vector.from_array = function (array)
return vector.new(array[1], array[2], array[3])
end
vector.to_array = function (self)
return {self.x, self.y, self.z}
end
vector.unpack = function (self)
return self.x, self.y, self.z
end
vector.magnitude2 = function (self)
return self.x*self.x + self.y*self.y + self.z*self.z
end
vector.magnitude = function (self)
return math.sqrt(self:magnitude2())
end
vector.normalize = function (self)
local m = self:magnitude()
return m ~= 0 and (self / m) or vector(0, 0, 0)
end
vector.dist = function (a, b)
local dx = a.x - b.x
local dy = a.y - b.y
local dz = a.z - b.z
return math.sqrt(dx * dx + dy * dy + dz * dz)
end
vector.dot = function (a, b)
return a.x * b.x + a.y * b.y + a.z * b.z
end
vector.reflect = function (a, b)
return a - (b * vector.dot(a, b) * 2)
end
vector.cross = function (a, b)
return vector.new(
a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x
)
end
vector.sign = function (a)
return vector.new(
(a.x == 0) and 0 or (a.x > 0) and 1 or -1,
(a.y == 0) and 0 or (a.y > 0) and 1 or -1,
(a.z == 0) and 0 or (a.z > 0) and 1 or -1
)
end
local clamp = function (x, min, max)
return math.min(math.max(min, x), max)
end
local lerp = function (a, b, t)
return a * (1-t) + b * t
end
local decay = function (value, target, rate, delta)
return lerp(target, value, math.exp(-math.exp(rate)*delta))
end
vector.clamp = function (a, min, max)
local minx, miny, minz = min, min, min
if is_vector(min) then
minx, miny, minz = min:unpack()
end
local maxx, maxy, maxz = max, max, max
if is_vector(max) then
maxx, maxy, maxz = max:unpack()
end
return vector.new(
clamp(a.x, minx, maxx),
clamp(a.y, miny, maxy),
clamp(a.z, minz, maxz)
)
end
vector.lerp = function (a, b, t)
return is_vector(b) and
vector.new(
lerp(a.x, b.x, t),
lerp(a.y, b.y, t),
lerp(a.z, b.z, t)
)
or
vector.new(
lerp(a.x, b, t),
lerp(a.y, b, t),
lerp(a.z, b, t)
)
end
vector.decay = function (a, b, rate, delay)
return is_vector(b) and
vector.new(
decay(a.x, b.x, rate, delay),
decay(a.y, b.y, rate, delay),
decay(a.z, b.z, rate, delay)
)
or
vector.new(
decay(a.x, b, rate, delay),
decay(a.y, b, rate, delay),
decay(a.z, b, rate, delay)
)
end
vector.round = function(a, b)
b = b or 1
return is_vector(b) and
vector.new(
math.floor((a.x / b.x) + .5) * b.x,
math.floor((a.y / b.y) + .5) * b.y,
math.floor((a.z / b.z) + .5) * b.z
)
or
vector.new(
math.floor((a.x / b) + .5) * b,
math.floor((a.y / b) + .5) * b,
math.floor((a.z / b) + .5) * b
)
end
vector.__call = function (self, ...)
return self:copy()
end
vector.__add = function (a, b)
return is_vector(b) and vector.new(a.x+b.x, a.y+b.y, a.z+b.z)
or vector.new(a.x+b, a.y+b , a.z+b)
end
vector.add = vector.__add
vector.__sub = function (a, b)
return is_vector(b) and vector.new(a.x-b.x, a.y-b.y, a.z-b.z)
or vector.new(a.x-b, a.y-b , a.z-b)
end
vector.sub = vector.__sub
vector.__mul = function (a, b)
return is_vector(b) and vector.new(a.x*b.x, a.y*b.y, a.z*b.z)
or vector.new(a.x*b, a.y*b , a.z*b)
end
vector.mul = vector.__mul
vector.__div = function (a, b)
return is_vector(b) and vector.new(a.x/b.x, a.y/b.y, a.z/b.z)
or vector.new(a.x/b, a.y/b , a.z/b)
end
vector.div = vector.__div
vector.__mod = function (a, b)
return is_vector(b) and vector.new(a.x%b.x, a.y%b.y, a.z%b.z)
or vector.new(a.x%b, a.y%b , a.z%b)
end
vector.modulo = vector.__mod
vector.__pow = function (a, b)
return is_vector(b) and vector.new(a.x^b.x, a.y^b.y, a.z^b.z)
or vector.new(a.x^b, a.y^b , a.z^b)
end
vector.pow = vector.__pow
vector.__unm = function (a)
a.x = -a.x
a.y = -a.y
a.z = -a.z
return a
end
vector.__eq = function (a, b)
return is_vector(b) and (a.x==b.x and a.y==b.y and a.z==b.z)
or (a.x==b and a.y==b and a.z==b)
end
vector.__lt = function (a, b)
return is_vector(b) and (a.x>b.x and a.y>b.y and a.z>b.z)
or (a.x>b and a.y>b and a.z>b)
end
vector.__le = function (a, b)
return is_vector(b) and (a.x>=b.x and a.y>=b.y and a.z>=b.z)
or (a.x>=b and a.y>=b and a.z>=b)
end
vector.__tostring = function (a)
return ("vec3(%s, %s, %s)"):format(a.x, a.y, a.z)
end
vector.zero = vector.new(0, 0, 0)
vector.one = vector.new(1, 1, 1)
return setmetatable(vector, {
__call = function (self, ...)
return vector.new(...)
end
})