-
Notifications
You must be signed in to change notification settings - Fork 2
/
vec4.js
165 lines (150 loc) · 3.45 KB
/
vec4.js
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
/** @module vec4 */
/**
* Returns a new vec4 at 0, 0, 0, 1.
* @returns {import("./types.js").vec4}
*/
export function create() {
return [0, 0, 0, 1];
}
/**
* Returns a copy of a vector.
* @param {import("./types.js").vec4} a
* @returns {import("./types.js").vec4}
*/
export function copy(a) {
return a.slice();
}
/**
* Sets a vector to another vector.
* @param {import("./types.js").vec4} a
* @param {import("./types.js").vec4} b
* @returns {import("./types.js").vec4}
*/
export function set(a, b) {
a[0] = b[0];
a[1] = b[1];
a[2] = b[2];
a[3] = b[3];
return a;
}
/**
* Compares two vectors.
* @param {import("./types.js").vec4} a
* @param {import("./types.js").vec4} b
* @returns {boolean}
*/
export function equals(a, b) {
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3];
}
/**
* Adds a vector to another.
* @param {import("./types.js").vec4} a
* @param {import("./types.js").vec4} b
* @returns {import("./types.js").vec4}
*/
export function add(a, b) {
a[0] += b[0];
a[1] += b[1];
a[2] += b[2];
a[3] += b[3];
return a;
}
/**
* Subtracts a vector from another.
* @param {import("./types.js").vec4} a
* @param {import("./types.js").vec4} b
* @returns {import("./types.js").vec4}
*/
export function sub(a, b) {
a[0] -= b[0];
a[1] -= b[1];
a[2] -= b[2];
a[3] -= b[3];
return a;
}
/**
* Scales a vector by a number.
* @param {import("./types.js").vec4} a
* @param {number} s
* @returns {import("./types.js").vec4}
*/
export function scale(a, s) {
a[0] *= s;
a[1] *= s;
a[2] *= s;
a[3] *= s;
return a;
}
/**
* Adds two vectors after scaling the second one.
* @param {import("./types.js").vec4} a
* @param {import("./types.js").vec4} b
* @param {number} s
* @returns {import("./types.js").vec4}
*/
export function addScaled(a, b, s) {
a[0] += b[0] * s;
a[1] += b[1] * s;
a[2] += b[2] * s;
a[3] += b[3] * s;
return a;
}
/**
* Create a vec4 from vec3.
* @param {import("./types.js").vec4} a
* @param {import("./types.js").vec3} b
* @returns {import("./types.js").vec4}
*/
export function fromVec3(a, b) {
a[0] = b[0];
a[1] = b[1];
a[2] = b[2];
a[3] = 1;
return a;
}
/**
* Multiplies a vector with a matrix.
* @param {import("./types.js").vec4} a
* @param {import("./types.js").mat4} m
* @returns {import("./types.js").vec4}
*/
export function multMat4(a, m) {
const x = a[0];
const y = a[1];
const z = a[2];
const w = a[3];
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;
a[3] = m[3] * x + m[7] * y + m[11] * z + m[15] * w;
return a;
}
/**
* Linearly interpolates between two vectors.
* @param {import("./types.js").vec4} a
* @param {import("./types.js").vec4} b
* @param {number} t
* @returns {import("./types.js").vec4}
*/
export function lerp(a, b, t) {
const x = a[0];
const y = a[1];
const z = a[2];
const w = a[3];
a[0] = x + (b[0] - x) * t;
a[1] = y + (b[1] - y) * t;
a[2] = z + (b[2] - z) * t;
a[3] = w + (b[3] - w) * t;
return a;
}
/**
* Prints a vector to a string.
* @param {import("./types.js").vec4} a
* @param {number} [precision=4]
* @returns {string}
*/
export function toString(a, precision = 4) {
const scale = 10 ** precision;
// prettier-ignore
return `[${Math.floor(a[0] * scale) / scale}, ${Math.floor(a[1] * scale) / scale}, ${Math.floor(a[2] * scale) / scale}, ${Math.floor(a[3] * scale) / scale}]`;
}