@carbonenginejs/core-math 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -2
- package/src/mat3.js +1 -1
- package/src/mat4.js +51 -45
- package/src/mesh.js +72 -54
- package/src/quat.js +5 -5
- package/src/tangent.js +18 -10
- package/src/vec2.js +1 -1
- package/src/vec3.js +36 -34
- package/src/vec4.js +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@carbonenginejs/core-math",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Browser-friendly gl-matrix based math containers and CarbonEngineJS mesh/tangent helpers.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
|
@@ -71,4 +71,3 @@
|
|
|
71
71
|
"access": "public"
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
|
-
|
package/src/mat3.js
CHANGED
package/src/mat4.js
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as glMat4 from "gl-matrix/esm/mat4.js";
|
|
2
|
+
import {
|
|
3
|
+
copy as copyVec3,
|
|
4
|
+
cross as crossVec3,
|
|
5
|
+
dot as dotVec3,
|
|
6
|
+
normalize as normalizeVec3,
|
|
7
|
+
squaredLength as squaredLengthVec3,
|
|
8
|
+
subtract as subtractVec3
|
|
9
|
+
} from "gl-matrix/esm/vec3.js";
|
|
2
10
|
import { pool } from "./pool.js";
|
|
3
11
|
|
|
4
|
-
const
|
|
5
|
-
mat4 = { ...glMat4 },
|
|
6
|
-
vec3 = { ...glVec3 };
|
|
12
|
+
const mat4 = { ...glMat4 };
|
|
7
13
|
|
|
8
14
|
export { mat4 };
|
|
9
15
|
|
|
@@ -81,9 +87,9 @@ mat4.fromJointMatIndex = function (out, jointMat, index)
|
|
|
81
87
|
* @param {vec3} v
|
|
82
88
|
* @return {mat4} out
|
|
83
89
|
*/
|
|
84
|
-
mat4.arcFromForward = function (out, v)
|
|
85
|
-
{
|
|
86
|
-
const norm =
|
|
90
|
+
mat4.arcFromForward = function (out, v)
|
|
91
|
+
{
|
|
92
|
+
const norm = normalizeVec3(pool.allocF32(3), v);
|
|
87
93
|
|
|
88
94
|
mat4.identity(out);
|
|
89
95
|
|
|
@@ -193,32 +199,32 @@ mat4.lookAtD3D = function (out, eye, center, up)
|
|
|
193
199
|
const z = pool.allocF32(3);
|
|
194
200
|
|
|
195
201
|
// z = forward = normalize(center - eye) (LH)
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
if (
|
|
202
|
+
subtractVec3(z, center, eye);
|
|
203
|
+
|
|
204
|
+
if (squaredLengthVec3(z) === 0)
|
|
199
205
|
{
|
|
200
206
|
z[2] = 1;
|
|
201
207
|
}
|
|
202
208
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
// x = normalize(cross(up, z))
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
if (
|
|
209
|
+
normalizeVec3(z, z);
|
|
210
|
+
|
|
211
|
+
// x = normalize(cross(up, z))
|
|
212
|
+
crossVec3(x, up, z);
|
|
213
|
+
|
|
214
|
+
if (squaredLengthVec3(x) === 0)
|
|
209
215
|
{
|
|
210
216
|
// nudge z slightly if up is parallel
|
|
211
217
|
if (Math.abs(up[2]) === 1) z[0] += 0.0001;
|
|
212
218
|
else z[2] += 0.0001;
|
|
213
219
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
// y = cross(z, x)
|
|
221
|
-
|
|
220
|
+
normalizeVec3(z, z);
|
|
221
|
+
crossVec3(x, up, z);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
normalizeVec3(x, x);
|
|
225
|
+
|
|
226
|
+
// y = cross(z, x)
|
|
227
|
+
crossVec3(y, z, x);
|
|
222
228
|
|
|
223
229
|
// Rotation (axes in columns)
|
|
224
230
|
out[0] = x[0]; out[1] = x[1]; out[2] = x[2]; out[3] = 0;
|
|
@@ -226,9 +232,9 @@ mat4.lookAtD3D = function (out, eye, center, up)
|
|
|
226
232
|
out[8] = z[0]; out[9] = z[1]; out[10] = z[2]; out[11] = 0;
|
|
227
233
|
|
|
228
234
|
// Translation
|
|
229
|
-
out[12] = -
|
|
230
|
-
out[13] = -
|
|
231
|
-
out[14] = -
|
|
235
|
+
out[12] = -dotVec3(x, eye);
|
|
236
|
+
out[13] = -dotVec3(y, eye);
|
|
237
|
+
out[14] = -dotVec3(z, eye);
|
|
232
238
|
out[15] = 1;
|
|
233
239
|
|
|
234
240
|
pool.freeType(x);
|
|
@@ -266,17 +272,17 @@ mat4.setLookRotation = function (out, m, eye, center, up)
|
|
|
266
272
|
u = pool.allocF32(3); // safeUp
|
|
267
273
|
|
|
268
274
|
// z axis = eye - center (camera backward); -z is forward
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
if (
|
|
275
|
+
subtractVec3(z, eye, center);
|
|
276
|
+
|
|
277
|
+
if (squaredLengthVec3(z) === 0)
|
|
272
278
|
{
|
|
273
279
|
// arbitrary (back)
|
|
274
280
|
z[2] = 1;
|
|
275
281
|
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
// Pick a stable up if the provided up is too aligned with z
|
|
279
|
-
|
|
282
|
+
normalizeVec3(z, z);
|
|
283
|
+
|
|
284
|
+
// Pick a stable up if the provided up is too aligned with z
|
|
285
|
+
copyVec3(u, up);
|
|
280
286
|
|
|
281
287
|
// if |dot(up, z)| is ~1 then up × z is unstable
|
|
282
288
|
const dz = Math.abs(u[0] * z[0] + u[1] * z[1] + u[2] * z[2]);
|
|
@@ -293,10 +299,10 @@ mat4.setLookRotation = function (out, m, eye, center, up)
|
|
|
293
299
|
}
|
|
294
300
|
|
|
295
301
|
// x = up × z
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
// Still degenerate? (can happen if 'up' was zero-length etc.)
|
|
299
|
-
if (
|
|
302
|
+
crossVec3(x, u, z);
|
|
303
|
+
|
|
304
|
+
// Still degenerate? (can happen if 'up' was zero-length etc.)
|
|
305
|
+
if (squaredLengthVec3(x) === 0)
|
|
300
306
|
{
|
|
301
307
|
// fall back to a guaranteed-not-parallel up using z's dominant axis
|
|
302
308
|
if (Math.abs(z[1]) < 0.999)
|
|
@@ -307,13 +313,13 @@ mat4.setLookRotation = function (out, m, eye, center, up)
|
|
|
307
313
|
{
|
|
308
314
|
u[0] = 1; u[1] = 0; u[2] = 0;
|
|
309
315
|
}
|
|
310
|
-
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
// y = z × x
|
|
316
|
-
|
|
316
|
+
crossVec3(x, u, z);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
normalizeVec3(x, x);
|
|
320
|
+
|
|
321
|
+
// y = z × x
|
|
322
|
+
crossVec3(y, z, x);
|
|
317
323
|
|
|
318
324
|
// write rotation (columns)
|
|
319
325
|
out[0] = x[0]; out[1] = x[1]; out[2] = x[2];
|
package/src/mesh.js
CHANGED
|
@@ -5,8 +5,11 @@
|
|
|
5
5
|
* plain arrays or typed arrays and return plain arrays unless otherwise noted.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import {
|
|
9
|
-
|
|
8
|
+
import {
|
|
9
|
+
cross,
|
|
10
|
+
length as vec3Length,
|
|
11
|
+
normalize
|
|
12
|
+
} from "gl-matrix/esm/vec3.js";
|
|
10
13
|
|
|
11
14
|
/**
|
|
12
15
|
* Calculate the unit face normal for a triangle.
|
|
@@ -19,12 +22,12 @@ import { vec3 } from "./vec3.js";
|
|
|
19
22
|
export function triangleNormal(a, b, c)
|
|
20
23
|
{
|
|
21
24
|
const normal = [ 0, 0, 0 ];
|
|
22
|
-
|
|
25
|
+
cross(
|
|
23
26
|
normal,
|
|
24
27
|
[ b[0] - a[0], b[1] - a[1], b[2] - a[2] ],
|
|
25
28
|
[ c[0] - a[0], c[1] - a[1], c[2] - a[2] ]
|
|
26
29
|
);
|
|
27
|
-
return
|
|
30
|
+
return normalize(normal, normal);
|
|
28
31
|
}
|
|
29
32
|
|
|
30
33
|
/**
|
|
@@ -38,12 +41,12 @@ export function triangleNormal(a, b, c)
|
|
|
38
41
|
export function triangleArea2(a, b, c)
|
|
39
42
|
{
|
|
40
43
|
const normal = [ 0, 0, 0 ];
|
|
41
|
-
|
|
44
|
+
cross(
|
|
42
45
|
normal,
|
|
43
46
|
[ b[0] - a[0], b[1] - a[1], b[2] - a[2] ],
|
|
44
47
|
[ c[0] - a[0], c[1] - a[1], c[2] - a[2] ]
|
|
45
48
|
);
|
|
46
|
-
return
|
|
49
|
+
return vec3Length(normal);
|
|
47
50
|
}
|
|
48
51
|
|
|
49
52
|
/**
|
|
@@ -127,24 +130,26 @@ export function computeBoundsFromTriangles(triangles)
|
|
|
127
130
|
*
|
|
128
131
|
* @param {ArrayLike<number>} positions Flat xyz positions.
|
|
129
132
|
* @param {ArrayLike<number>} indices Flat triangle indices.
|
|
130
|
-
* @returns {
|
|
133
|
+
* @returns {Float32Array} Flat xyz normals.
|
|
131
134
|
*/
|
|
132
135
|
export function generateNormals(positions, indices)
|
|
133
136
|
{
|
|
134
|
-
const
|
|
137
|
+
const
|
|
138
|
+
vertexCount = positions.length / 3,
|
|
139
|
+
normals = new Float32Array(positions.length);
|
|
135
140
|
|
|
136
|
-
for (let
|
|
141
|
+
for (let t = 0; t < indices.length; t += 3)
|
|
137
142
|
{
|
|
138
143
|
const
|
|
139
|
-
ia = indices[
|
|
140
|
-
ib = indices[
|
|
141
|
-
ic = indices[
|
|
144
|
+
ia = indices[t] * 3,
|
|
145
|
+
ib = indices[t + 1] * 3,
|
|
146
|
+
ic = indices[t + 2] * 3,
|
|
142
147
|
ax = positions[ia],
|
|
143
148
|
ay = positions[ia + 1],
|
|
144
149
|
az = positions[ia + 2],
|
|
145
150
|
faceNormal = [ 0, 0, 0 ];
|
|
146
151
|
|
|
147
|
-
|
|
152
|
+
cross(
|
|
148
153
|
faceNormal,
|
|
149
154
|
[ positions[ib] - ax, positions[ib + 1] - ay, positions[ib + 2] - az ],
|
|
150
155
|
[ positions[ic] - ax, positions[ic + 1] - ay, positions[ic + 2] - az ]
|
|
@@ -158,12 +163,15 @@ export function generateNormals(positions, indices)
|
|
|
158
163
|
}
|
|
159
164
|
}
|
|
160
165
|
|
|
161
|
-
for (let i = 0; i <
|
|
166
|
+
for (let i = 0; i < vertexCount; i++)
|
|
162
167
|
{
|
|
163
|
-
const
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
168
|
+
const
|
|
169
|
+
offset = i * 3,
|
|
170
|
+
length = Math.hypot(normals[offset], normals[offset + 1], normals[offset + 2]) || 1;
|
|
171
|
+
|
|
172
|
+
normals[offset] /= length;
|
|
173
|
+
normals[offset + 1] /= length;
|
|
174
|
+
normals[offset + 2] /= length;
|
|
167
175
|
}
|
|
168
176
|
|
|
169
177
|
return normals;
|
|
@@ -176,20 +184,21 @@ export function generateNormals(positions, indices)
|
|
|
176
184
|
* @param {ArrayLike<number>} normals Flat xyz normals.
|
|
177
185
|
* @param {ArrayLike<number>} uvs Flat uv coordinates.
|
|
178
186
|
* @param {ArrayLike<number>} indices Flat triangle indices.
|
|
179
|
-
* @returns {
|
|
187
|
+
* @returns {Float32Array} Flat xyz tangents.
|
|
180
188
|
*/
|
|
181
189
|
export function generateTangents(positions, normals, uvs, indices)
|
|
182
190
|
{
|
|
183
191
|
const
|
|
184
192
|
vertexCount = positions.length / 3,
|
|
185
|
-
|
|
193
|
+
tan1 = new Float32Array(vertexCount * 3),
|
|
194
|
+
tan2 = new Float32Array(vertexCount * 3);
|
|
186
195
|
|
|
187
|
-
for (let
|
|
196
|
+
for (let t = 0; t < indices.length; t += 3)
|
|
188
197
|
{
|
|
189
198
|
const
|
|
190
|
-
i0 = indices[
|
|
191
|
-
i1 = indices[
|
|
192
|
-
i2 = indices[
|
|
199
|
+
i0 = indices[t],
|
|
200
|
+
i1 = indices[t + 1],
|
|
201
|
+
i2 = indices[t + 2],
|
|
193
202
|
p0 = i0 * 3,
|
|
194
203
|
p1 = i1 * 3,
|
|
195
204
|
p2 = i2 * 3,
|
|
@@ -206,43 +215,52 @@ export function generateTangents(positions, normals, uvs, indices)
|
|
|
206
215
|
v1 = uvs[t1 + 1] - uvs[t0 + 1],
|
|
207
216
|
s2 = uvs[t2] - uvs[t0],
|
|
208
217
|
v2 = uvs[t2 + 1] - uvs[t0 + 1],
|
|
209
|
-
divisor = s1 * v2 - s2 * v1
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
tz = (v2 * z1 - v1 * z2) * scale;
|
|
218
|
+
divisor = s1 * v2 - s2 * v1,
|
|
219
|
+
scale = divisor ? 1 / divisor : 0,
|
|
220
|
+
sx = (v2 * x1 - v1 * x2) * scale,
|
|
221
|
+
sy = (v2 * y1 - v1 * y2) * scale,
|
|
222
|
+
sz = (v2 * z1 - v1 * z2) * scale,
|
|
223
|
+
tx = (s1 * x2 - s2 * x1) * scale,
|
|
224
|
+
ty = (s1 * y2 - s2 * y1) * scale,
|
|
225
|
+
tz = (s1 * z2 - s2 * z1) * scale;
|
|
218
226
|
|
|
219
227
|
for (const offset of [ p0, p1, p2 ])
|
|
220
228
|
{
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
229
|
+
tan1[offset] += sx;
|
|
230
|
+
tan1[offset + 1] += sy;
|
|
231
|
+
tan1[offset + 2] += sz;
|
|
232
|
+
tan2[offset] += tx;
|
|
233
|
+
tan2[offset + 1] += ty;
|
|
234
|
+
tan2[offset + 2] += tz;
|
|
224
235
|
}
|
|
225
236
|
}
|
|
226
237
|
|
|
227
|
-
|
|
238
|
+
const tangents = new Float32Array(vertexCount * 3);
|
|
239
|
+
for (let i = 0; i < vertexCount; i++)
|
|
228
240
|
{
|
|
229
241
|
const
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
242
|
+
offset = i * 3,
|
|
243
|
+
nx = normals[offset],
|
|
244
|
+
ny = normals[offset + 1],
|
|
245
|
+
nz = normals[offset + 2],
|
|
246
|
+
tx = tan1[offset],
|
|
247
|
+
ty = tan1[offset + 1],
|
|
248
|
+
tz = tan1[offset + 2],
|
|
249
|
+
normalDotTangent = nx * tx + ny * ty + nz * tz;
|
|
250
|
+
|
|
251
|
+
let ox = tx - nx * normalDotTangent,
|
|
252
|
+
oy = ty - ny * normalDotTangent,
|
|
253
|
+
oz = tz - nz * normalDotTangent;
|
|
254
|
+
|
|
255
|
+
const length = Math.hypot(ox, oy, oz) || 1;
|
|
256
|
+
|
|
257
|
+
ox /= length;
|
|
258
|
+
oy /= length;
|
|
259
|
+
oz /= length;
|
|
260
|
+
|
|
261
|
+
tangents[offset] = ox;
|
|
262
|
+
tangents[offset + 1] = oy;
|
|
263
|
+
tangents[offset + 2] = oz;
|
|
246
264
|
}
|
|
247
265
|
|
|
248
266
|
return tangents;
|
|
@@ -270,7 +288,7 @@ export function generateBiNormals(normals, tangents, { uvHandedness = "right" }
|
|
|
270
288
|
|
|
271
289
|
for (let i = 0; i < normals.length; i += 3)
|
|
272
290
|
{
|
|
273
|
-
const b =
|
|
291
|
+
const b = normalize(
|
|
274
292
|
[ 0, 0, 0 ],
|
|
275
293
|
[
|
|
276
294
|
normals[i + 1] * tangents[i + 2] - normals[i + 2] * tangents[i + 1],
|
package/src/quat.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import * as glQuat from "gl-matrix/esm/quat.js";
|
|
2
|
+
import { dot as vec3Dot } from "gl-matrix/esm/vec3.js";
|
|
3
|
+
import { EPSILON } from "./num.js";
|
|
4
4
|
import { pool } from "./pool.js";
|
|
5
5
|
|
|
6
6
|
const quat = { ...glQuat };
|
|
@@ -35,8 +35,8 @@ quat.unalloc = function(a)
|
|
|
35
35
|
*/
|
|
36
36
|
quat.fromUnitVectors = function(out, from, to)
|
|
37
37
|
{
|
|
38
|
-
let r =
|
|
39
|
-
if (r <
|
|
38
|
+
let r = vec3Dot(from, to) + 1;
|
|
39
|
+
if (r < EPSILON)
|
|
40
40
|
{
|
|
41
41
|
r = 0;
|
|
42
42
|
if (Math.abs(from[0]) > Math.abs(from[2]))
|
package/src/tangent.js
CHANGED
|
@@ -11,8 +11,16 @@ import {
|
|
|
11
11
|
generateNormals,
|
|
12
12
|
generateTangents
|
|
13
13
|
} from "./mesh.js";
|
|
14
|
-
import {
|
|
15
|
-
|
|
14
|
+
import {
|
|
15
|
+
EPSILON,
|
|
16
|
+
clamp
|
|
17
|
+
} from "./num.js";
|
|
18
|
+
import {
|
|
19
|
+
cross,
|
|
20
|
+
dot,
|
|
21
|
+
length as vec3Length,
|
|
22
|
+
normalize
|
|
23
|
+
} from "gl-matrix/esm/vec3.js";
|
|
16
24
|
|
|
17
25
|
/** Full-turn float32 constant used by the CCP tangent-frame shader. */
|
|
18
26
|
export const TANGENT_TAU = 6.28318548;
|
|
@@ -100,18 +108,18 @@ export function decodeTangentFrame(u)
|
|
|
100
108
|
export function encodeTangentFrame(T, B, N)
|
|
101
109
|
{
|
|
102
110
|
let a0 = Math.atan2(T[1], T[0]),
|
|
103
|
-
a1 = Math.acos(
|
|
111
|
+
a1 = Math.acos(clamp(T[2], -1, 1));
|
|
104
112
|
|
|
105
113
|
const
|
|
106
114
|
a2 = Math.atan2(B[1], B[0]),
|
|
107
|
-
a3 = Math.acos(
|
|
115
|
+
a3 = Math.acos(clamp(B[2], -1, 1));
|
|
108
116
|
|
|
109
|
-
if (N &&
|
|
117
|
+
if (N && dot(N, cross([ 0, 0, 0 ], T, B)) < 0)
|
|
110
118
|
{
|
|
111
119
|
a1 = -a1;
|
|
112
120
|
}
|
|
113
121
|
|
|
114
|
-
const enc = angle =>
|
|
122
|
+
const enc = angle => clamp((angle + PI) / TAU, 0, 1);
|
|
115
123
|
return [ enc(a0), enc(a1), enc(a2), enc(a3) ];
|
|
116
124
|
}
|
|
117
125
|
|
|
@@ -212,11 +220,11 @@ export function packTangentFrames(normals, tangents, binormals)
|
|
|
212
220
|
for (let i = 0, o = 0; i < normals.length; i += 3, o += 4)
|
|
213
221
|
{
|
|
214
222
|
const
|
|
215
|
-
normal =
|
|
216
|
-
tangent =
|
|
217
|
-
binormal =
|
|
223
|
+
normal = normalize([ 0, 0, 0 ], [ normals[i], normals[i + 1], normals[i + 2] ]),
|
|
224
|
+
tangent = normalize([ 0, 0, 0 ], [ tangents[i], tangents[i + 1], tangents[i + 2] ]),
|
|
225
|
+
binormal = normalize([ 0, 0, 0 ], [ binormals[i], binormals[i + 1], binormals[i + 2] ]);
|
|
218
226
|
|
|
219
|
-
if (
|
|
227
|
+
if (vec3Length(normal) <= EPSILON || vec3Length(tangent) <= EPSILON || vec3Length(binormal) <= EPSILON)
|
|
220
228
|
{
|
|
221
229
|
packed[o] = NULL_TANGENT_UNORM[0];
|
|
222
230
|
packed[o + 1] = NULL_TANGENT_UNORM[1];
|
package/src/vec2.js
CHANGED
package/src/vec3.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as glVec3 from "gl-matrix/esm/vec3.js";
|
|
2
|
+
import {
|
|
3
|
+
fromQuat as mat4FromQuat,
|
|
4
|
+
getRotation as getMat4Rotation
|
|
5
|
+
} from "gl-matrix/esm/mat4.js";
|
|
6
|
+
import { transformMat4 as transformVec4Mat4 } from "gl-matrix/esm/vec4.js";
|
|
2
7
|
import { num } from "./num.js";
|
|
3
8
|
import { pool } from "./pool.js";
|
|
4
|
-
import { vec4 } from "./vec4.js";
|
|
5
9
|
|
|
6
|
-
const
|
|
7
|
-
vec3 = { ...glVec3 },
|
|
8
|
-
mat4 = { ...glMat4 };
|
|
10
|
+
const vec3 = { ...glVec3 };
|
|
9
11
|
|
|
10
12
|
/**
|
|
11
13
|
* Vector 3
|
|
@@ -136,13 +138,13 @@ vec3.directionFromQuat = function (out, axis, q)
|
|
|
136
138
|
* @param {mat4} m
|
|
137
139
|
* @returns {vec3} out
|
|
138
140
|
*/
|
|
139
|
-
vec3.directionFromMat4 = function (out, axis, m)
|
|
140
|
-
{
|
|
141
|
-
const quat_0 =
|
|
142
|
-
vec3.transformQuat(out, axis, quat_0);
|
|
143
|
-
|
|
144
|
-
return out;
|
|
145
|
-
};
|
|
141
|
+
vec3.directionFromMat4 = function (out, axis, m)
|
|
142
|
+
{
|
|
143
|
+
const quat_0 = getMat4Rotation(pool.allocF32(4), m);
|
|
144
|
+
vec3.transformQuat(out, axis, quat_0);
|
|
145
|
+
pool.freeType(quat_0);
|
|
146
|
+
return out;
|
|
147
|
+
};
|
|
146
148
|
|
|
147
149
|
/**
|
|
148
150
|
* Divides a vec3 by a scalar
|
|
@@ -178,13 +180,13 @@ vec3.euler.DEFAULT_ORDER = "XYZ";
|
|
|
178
180
|
* @param {string} [order=vec3.euler.DEFAULT_ORDER]
|
|
179
181
|
* @returns {vec3} out
|
|
180
182
|
*/
|
|
181
|
-
vec3.euler.fromQuat = function (out, q, order = vec3.euler.DEFAULT_ORDER)
|
|
182
|
-
{
|
|
183
|
-
// mat4.alloc
|
|
184
|
-
const mat4_0 =
|
|
185
|
-
vec3.euler.fromMat4(out, mat4_0, order);
|
|
186
|
-
pool.unalloc(mat4_0);
|
|
187
|
-
return out;
|
|
183
|
+
vec3.euler.fromQuat = function (out, q, order = vec3.euler.DEFAULT_ORDER)
|
|
184
|
+
{
|
|
185
|
+
// mat4.alloc
|
|
186
|
+
const mat4_0 = mat4FromQuat(pool.allocF32(16), q);
|
|
187
|
+
vec3.euler.fromMat4(out, mat4_0, order);
|
|
188
|
+
pool.unalloc(mat4_0);
|
|
189
|
+
return out;
|
|
188
190
|
};
|
|
189
191
|
|
|
190
192
|
/**
|
|
@@ -687,9 +689,9 @@ vec3.subtractScalar = function (out, a, s)
|
|
|
687
689
|
* @returns {vec3} out
|
|
688
690
|
* @throw On perspective divide error
|
|
689
691
|
*/
|
|
690
|
-
vec3.unproject = function (out, a, invViewProj, viewport)
|
|
691
|
-
{
|
|
692
|
-
const vec4_0 =
|
|
692
|
+
vec3.unproject = function (out, a, invViewProj, viewport)
|
|
693
|
+
{
|
|
694
|
+
const vec4_0 = pool.allocF32(4);
|
|
693
695
|
|
|
694
696
|
let x = a[0],
|
|
695
697
|
y = a[1],
|
|
@@ -700,14 +702,14 @@ vec3.unproject = function (out, a, invViewProj, viewport)
|
|
|
700
702
|
vec4_0[2] = 2.0 * z - 1.0;
|
|
701
703
|
vec4_0[3] = 1.0;
|
|
702
704
|
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
if (vec4_0[3] === 0.0)
|
|
706
|
-
{
|
|
707
|
-
|
|
708
|
-
out[0] = 0;
|
|
709
|
-
out[1] = 0;
|
|
710
|
-
out[2] = 0;
|
|
705
|
+
transformVec4Mat4(vec4_0, vec4_0, invViewProj);
|
|
706
|
+
|
|
707
|
+
if (vec4_0[3] === 0.0)
|
|
708
|
+
{
|
|
709
|
+
pool.freeType(vec4_0);
|
|
710
|
+
out[0] = 0;
|
|
711
|
+
out[1] = 0;
|
|
712
|
+
out[2] = 0;
|
|
711
713
|
throw new Error("Perspective divide error");
|
|
712
714
|
}
|
|
713
715
|
|
|
@@ -715,10 +717,10 @@ vec3.unproject = function (out, a, invViewProj, viewport)
|
|
|
715
717
|
out[1] = vec4_0[1] / vec4_0[3];
|
|
716
718
|
out[2] = vec4_0[2] / vec4_0[3];
|
|
717
719
|
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
return out;
|
|
721
|
-
};
|
|
720
|
+
pool.freeType(vec4_0);
|
|
721
|
+
|
|
722
|
+
return out;
|
|
723
|
+
};
|
|
722
724
|
|
|
723
725
|
/**
|
|
724
726
|
* Unwraps degrees
|
package/src/vec4.js
CHANGED