-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvec3.js
More file actions
150 lines (110 loc) · 2.82 KB
/
Copy pathvec3.js
File metadata and controls
150 lines (110 loc) · 2.82 KB
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
/**
vec3 utility functions, using (array, index).
*/
const vec3 = {
add: function(out, k, arr0, i, arr1, j) {
out[k] = arr0[i] + arr1[j];
out[k+1] = arr0[i+1] + arr1[j+1];
out[k+2] = arr0[i+2] + arr1[j+2];
return out;
},
sub: function(out, k, a, i, b, j) {
out[k] = a[i] - b[j];
out[k+1] = a[i+1] - b[j+1];
out[k+2] = a[i+2] - b[j+2];
return out;
},
copy: function(dest, i, source, j) {
dest[i] = source[j];
dest[i+1] = source[j+1];
dest[i+2] = source[j+2];
return dest;
},
lerp: function(out, k, arr0, i, arr1, j, u) {
out[k] = arr0[i] + u * (arr1[j] - arr0[i]);
out[k+1] = arr0[i+1] + u * (arr1[j+1] - arr0[i+1]);
out[k+2] = arr0[i+2] + u * (arr1[j+2] - arr0[i+2]);
return out;
},
scale: function(out, k, arr, i, u) {
out[k] = arr[i] * u;
out[k+1] = arr[i+1] * u;
out[k+2] = arr[i+2] * u;
return out;
},
scaleAndAdd: function(out, k, arr0, i, arr1, j, u) {
out[k] = arr0[i] + arr1[j] * u;
out[k+1] = arr0[i+1] + arr1[j+1] * u;
out[k+2] = arr0[i+2] + arr1[j+2] * u;
return out;
},
cross: function(out, k, a, i, b, j) {
const ax = a[i], ay = a[i+1], az = a[i+2];
const bx = b[j], by = b[j+1], bz = b[j+2];
out[k] = ay * bz - az * by;
out[k+1] = az * bx - ax * bz;
out[k+2] = ax * by - ay * bx;
return out;
},
transformMat4: function(out, a, m) {
const x = a[0], y = a[1], z = a[2];
let w = m[3] * x + m[7] * y + m[11] * z + m[15];
w = w || 1.0;
out[0] = (m[0] * x + m[4] * y + m[8] * z + m[12]) / w;
out[1] = (m[1] * x + m[5] * y + m[9] * z + m[13]) / w;
out[2] = (m[2] * x + m[6] * y + m[10] * z + m[14]) / w;
return out;
},
}
const vec3a = {
add: function(dest, i, source, j) {
dest[i] += source[j];
dest[i+1] += source[j+1];
dest[i+2] += source[j+2];
return dest;
},
copy: function(dest, i, source, j) {
dest[i] = source[j];
dest[i+1] = source[j+1];
dest[i+2] = source[j+2];
return dest;
},
scale: function(dest, i, x) {
dest[i] *= x;
dest[i+1] *= x;
dest[i+2] *= x;
return dest;
},
scaleAndAdd: function(dest, i, source, j, x) {
dest[i] += source[j] * x;
dest[i+1] += source[j+1] * x;
dest[i+2] += source[j+2] * x;
return dest;
},
normalize: function(dest, i) {
let x = dest[i];
let y = dest[i+1];
let z = dest[i+2];
let len = x * x + y * y + z * z;
if (len > 0) {
len = 1 / Math.sqrt(len);
}
dest[i] *= len;
dest[i+1] *= len;
dest[i+2] *= len;
return dest;
},
transformMat4: function(a, m) {
const x = a[0], y = a[1], z = a[2];
let w = m[3] * x + m[7] * y + m[11] * z + m[15];
w = w || 1.0;
a[0] = (m[0] * x + m[4] * y + m[8] * z + m[12]) / w;
a[1] = (m[1] * x + m[5] * y + m[9] * z + m[13]) / w;
a[2] = (m[2] * x + m[6] * y + m[10] * z + m[14]) / w;
return a;
},
}
export {
vec3,
vec3a,
}