@ac-kit/math-linear 0.2.1 → 0.4.0
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/CHANGELOG.md +337 -0
- package/README.md +232 -16
- package/dist/_brand.d.ts +14 -0
- package/dist/_brand.d.ts.map +1 -0
- package/dist/_brand.js +2 -0
- package/dist/_brand.js.map +1 -0
- package/dist/_invert.d.ts +214 -0
- package/dist/_invert.d.ts.map +1 -0
- package/dist/_invert.js +884 -0
- package/dist/_invert.js.map +1 -0
- package/dist/_slerp.d.ts +29 -0
- package/dist/_slerp.d.ts.map +1 -1
- package/dist/_slerp.js +103 -0
- package/dist/_slerp.js.map +1 -1
- package/dist/_strided-range.d.ts +23 -0
- package/dist/_strided-range.d.ts.map +1 -0
- package/dist/_strided-range.js +75 -0
- package/dist/_strided-range.js.map +1 -0
- package/dist/_trs.d.ts +38 -0
- package/dist/_trs.d.ts.map +1 -0
- package/dist/_trs.js +132 -0
- package/dist/_trs.js.map +1 -0
- package/dist/affine2-array.d.ts +53 -0
- package/dist/affine2-array.d.ts.map +1 -0
- package/dist/affine2-array.js +107 -0
- package/dist/affine2-array.js.map +1 -0
- package/dist/affine2-componentwise.d.ts +28 -0
- package/dist/affine2-componentwise.d.ts.map +1 -0
- package/dist/affine2-componentwise.js +66 -0
- package/dist/affine2-componentwise.js.map +1 -0
- package/dist/affine2.d.ts +156 -0
- package/dist/affine2.d.ts.map +1 -0
- package/dist/affine2.js +437 -0
- package/dist/affine2.js.map +1 -0
- package/dist/affine3-array.d.ts +53 -0
- package/dist/affine3-array.d.ts.map +1 -0
- package/dist/affine3-array.js +137 -0
- package/dist/affine3-array.js.map +1 -0
- package/dist/affine3-componentwise.d.ts +32 -0
- package/dist/affine3-componentwise.d.ts.map +1 -0
- package/dist/affine3-componentwise.js +100 -0
- package/dist/affine3-componentwise.js.map +1 -0
- package/dist/affine3.d.ts +205 -0
- package/dist/affine3.d.ts.map +1 -0
- package/dist/affine3.js +792 -0
- package/dist/affine3.js.map +1 -0
- package/dist/checked/index.d.ts +35 -0
- package/dist/checked/index.d.ts.map +1 -0
- package/dist/checked/index.js +58 -0
- package/dist/checked/index.js.map +1 -0
- package/dist/euler.d.ts +65 -0
- package/dist/euler.d.ts.map +1 -0
- package/dist/euler.js +217 -0
- package/dist/euler.js.map +1 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +29 -0
- package/dist/index.js.map +1 -1
- package/dist/kl-activations.d.ts.map +1 -1
- package/dist/kl-activations.js +4 -3
- package/dist/kl-activations.js.map +1 -1
- package/dist/mat2x2-array.d.ts +36 -0
- package/dist/mat2x2-array.d.ts.map +1 -0
- package/dist/mat2x2-array.js +71 -0
- package/dist/mat2x2-array.js.map +1 -0
- package/dist/mat2x2-componentwise.d.ts +86 -0
- package/dist/mat2x2-componentwise.d.ts.map +1 -0
- package/dist/mat2x2-componentwise.js +199 -0
- package/dist/mat2x2-componentwise.js.map +1 -0
- package/dist/mat2x2.d.ts +79 -25
- package/dist/mat2x2.d.ts.map +1 -1
- package/dist/mat2x2.js +192 -93
- package/dist/mat2x2.js.map +1 -1
- package/dist/mat3x3-array.d.ts +36 -0
- package/dist/mat3x3-array.d.ts.map +1 -0
- package/dist/mat3x3-array.js +91 -0
- package/dist/mat3x3-array.js.map +1 -0
- package/dist/mat3x3-componentwise.d.ts +86 -0
- package/dist/mat3x3-componentwise.d.ts.map +1 -0
- package/dist/mat3x3-componentwise.js +299 -0
- package/dist/mat3x3-componentwise.js.map +1 -0
- package/dist/mat3x3.d.ts +153 -48
- package/dist/mat3x3.d.ts.map +1 -1
- package/dist/mat3x3.js +470 -192
- package/dist/mat3x3.js.map +1 -1
- package/dist/mat4x4-array.d.ts +46 -0
- package/dist/mat4x4-array.d.ts.map +1 -0
- package/dist/mat4x4-array.js +133 -0
- package/dist/mat4x4-array.js.map +1 -0
- package/dist/mat4x4-componentwise.d.ts +87 -0
- package/dist/mat4x4-componentwise.d.ts.map +1 -0
- package/dist/mat4x4-componentwise.js +434 -0
- package/dist/mat4x4-componentwise.js.map +1 -0
- package/dist/mat4x4.d.ts +221 -67
- package/dist/mat4x4.d.ts.map +1 -1
- package/dist/mat4x4.js +972 -334
- package/dist/mat4x4.js.map +1 -1
- package/dist/projection.d.ts +113 -0
- package/dist/projection.d.ts.map +1 -0
- package/dist/projection.js +418 -0
- package/dist/projection.js.map +1 -0
- package/dist/quaternion-array.d.ts +47 -0
- package/dist/quaternion-array.d.ts.map +1 -0
- package/dist/quaternion-array.js +86 -0
- package/dist/quaternion-array.js.map +1 -0
- package/dist/quaternion.d.ts +106 -22
- package/dist/quaternion.d.ts.map +1 -1
- package/dist/quaternion.js +253 -182
- package/dist/quaternion.js.map +1 -1
- package/dist/transform.d.ts +220 -0
- package/dist/transform.d.ts.map +1 -0
- package/dist/transform.js +412 -0
- package/dist/transform.js.map +1 -0
- package/dist/tridiagonal.d.ts +52 -0
- package/dist/tridiagonal.d.ts.map +1 -0
- package/dist/tridiagonal.js +90 -0
- package/dist/tridiagonal.js.map +1 -0
- package/dist/upload.d.ts +55 -0
- package/dist/upload.d.ts.map +1 -0
- package/dist/upload.js +129 -0
- package/dist/upload.js.map +1 -0
- package/dist/vec2-array-componentwise.d.ts +207 -0
- package/dist/vec2-array-componentwise.d.ts.map +1 -0
- package/dist/vec2-array-componentwise.js +472 -0
- package/dist/vec2-array-componentwise.js.map +1 -0
- package/dist/vec2-array.d.ts +53 -0
- package/dist/vec2-array.d.ts.map +1 -0
- package/dist/vec2-array.js +123 -0
- package/dist/vec2-array.js.map +1 -0
- package/dist/vec2-componentwise.d.ts +311 -0
- package/dist/vec2-componentwise.d.ts.map +1 -0
- package/dist/vec2-componentwise.js +542 -0
- package/dist/vec2-componentwise.js.map +1 -0
- package/dist/vec2.d.ts +97 -82
- package/dist/vec2.d.ts.map +1 -1
- package/dist/vec2.js +121 -216
- package/dist/vec2.js.map +1 -1
- package/dist/vec3-array-componentwise.d.ts +207 -0
- package/dist/vec3-array-componentwise.d.ts.map +1 -0
- package/dist/vec3-array-componentwise.js +510 -0
- package/dist/vec3-array-componentwise.js.map +1 -0
- package/dist/vec3-array.d.ts +102 -0
- package/dist/vec3-array.d.ts.map +1 -0
- package/dist/vec3-array.js +299 -0
- package/dist/vec3-array.js.map +1 -0
- package/dist/vec3-componentwise.d.ts +310 -0
- package/dist/vec3-componentwise.d.ts.map +1 -0
- package/dist/vec3-componentwise.js +594 -0
- package/dist/vec3-componentwise.js.map +1 -0
- package/dist/vec3.d.ts +177 -87
- package/dist/vec3.d.ts.map +1 -1
- package/dist/vec3.js +391 -272
- package/dist/vec3.js.map +1 -1
- package/dist/vec4-array-componentwise.d.ts +198 -0
- package/dist/vec4-array-componentwise.d.ts.map +1 -0
- package/dist/vec4-array-componentwise.js +521 -0
- package/dist/vec4-array-componentwise.js.map +1 -0
- package/dist/vec4-array.d.ts +12 -0
- package/dist/vec4-array.d.ts.map +1 -0
- package/dist/vec4-array.js +43 -0
- package/dist/vec4-array.js.map +1 -0
- package/dist/vec4-componentwise.d.ts +299 -0
- package/dist/vec4-componentwise.d.ts.map +1 -0
- package/dist/vec4-componentwise.js +637 -0
- package/dist/vec4-componentwise.js.map +1 -0
- package/dist/vec4.d.ts +59 -53
- package/dist/vec4.d.ts.map +1 -1
- package/dist/vec4.js +74 -249
- package/dist/vec4.js.map +1 -1
- package/dist/view-basis.d.ts +15 -0
- package/dist/view-basis.d.ts.map +1 -0
- package/dist/view-basis.js +29 -0
- package/dist/view-basis.js.map +1 -0
- package/dist/viewport.d.ts +114 -0
- package/dist/viewport.d.ts.map +1 -0
- package/dist/viewport.js +250 -0
- package/dist/viewport.js.map +1 -0
- package/package.json +19 -9
package/dist/vec3.js
CHANGED
|
@@ -1,314 +1,433 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { diffOfProducts,
|
|
1
|
+
import { binaryExponent, scaleByPowerOfTwo } from "@ac-kit/core";
|
|
2
|
+
import { angleNormalizeRad, diffOfProducts, dotPrecise3, lerp, TWO_PI, } from "@ac-kit/math-scalar";
|
|
3
3
|
import { SLERP_LINEAR_FALLBACK_DOT_ } from "./_slerp.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
import { vec3DotPrecise, vec3LengthScaled, vec3NormalizeScaled, } from "./vec3-componentwise.js";
|
|
5
|
+
/**
|
|
6
|
+
* Writes the cross product `a × b` into `out`, in plain arithmetic: a vector
|
|
7
|
+
* perpendicular to both, of length `|a||b| sin θ`, right-handed (`x × y = z`).
|
|
8
|
+
* Where a component's two terms cancel use {@link vec3CrossPrecise}. `out` may
|
|
9
|
+
* be `a` or `b`.
|
|
10
|
+
*/
|
|
11
|
+
export function vec3Cross(out, a, b) {
|
|
12
|
+
const ax = a[0];
|
|
13
|
+
const ay = a[1];
|
|
14
|
+
const az = a[2];
|
|
15
|
+
const bx = b[0];
|
|
16
|
+
const by = b[1];
|
|
17
|
+
const bz = b[2];
|
|
18
|
+
out[0] = ay * bz - az * by;
|
|
19
|
+
out[1] = az * bx - ax * bz;
|
|
20
|
+
out[2] = ax * by - ay * bx;
|
|
21
|
+
return out;
|
|
8
22
|
}
|
|
9
|
-
|
|
10
|
-
|
|
23
|
+
/**
|
|
24
|
+
* {@link vec3Cross} with the rounding of every product recovered
|
|
25
|
+
* (`@ac-kit/math-scalar`'s `diffOfProducts`): each component within 1.5 ULP of
|
|
26
|
+
* the exact value however badly its terms cancel. About five times the cost.
|
|
27
|
+
* `out` may be `a` or `b`.
|
|
28
|
+
*/
|
|
29
|
+
export function vec3CrossPrecise(out, a, b) {
|
|
30
|
+
const ax = a[0];
|
|
31
|
+
const ay = a[1];
|
|
32
|
+
const az = a[2];
|
|
33
|
+
const bx = b[0];
|
|
34
|
+
const by = b[1];
|
|
35
|
+
const bz = b[2];
|
|
36
|
+
out[0] = diffOfProducts(ay, bz, az, by);
|
|
37
|
+
out[1] = diffOfProducts(az, bx, ax, bz);
|
|
38
|
+
out[2] = diffOfProducts(ax, by, ay, bx);
|
|
39
|
+
return out;
|
|
11
40
|
}
|
|
12
|
-
|
|
13
|
-
|
|
41
|
+
/**
|
|
42
|
+
* The sine of the angle between two vectors times their lengths, `|a × b|`,
|
|
43
|
+
* from the compensated cross product.
|
|
44
|
+
*/
|
|
45
|
+
function crossLengthPrecise_(a, b) {
|
|
46
|
+
const x = diffOfProducts(a[1], b[2], a[2], b[1]);
|
|
47
|
+
const y = diffOfProducts(a[2], b[0], a[0], b[2]);
|
|
48
|
+
const z = diffOfProducts(a[0], b[1], a[1], b[0]);
|
|
49
|
+
return Math.sqrt(x * x + y * y + z * z);
|
|
14
50
|
}
|
|
15
|
-
|
|
16
|
-
|
|
51
|
+
/**
|
|
52
|
+
* The signed angle from `a` to `b`, positive counter-clockwise when seen from
|
|
53
|
+
* the tip of `axis` (the right-hand rule about `axis`), in `(−π, π]`: `π` for
|
|
54
|
+
* opposite vectors. `axis` need not be a unit vector, and only its side of the
|
|
55
|
+
* plane of `a` and `b` matters. Computed from the compensated cross and dot
|
|
56
|
+
* products, so the angle of nearly parallel vectors keeps its digits. A zero
|
|
57
|
+
* vector gives `0`. Right while the product of the two lengths lies between
|
|
58
|
+
* about `1e−150` and `1e150`, which keeps the squares of the cross product in
|
|
59
|
+
* range (vectors of `1e100` give `Infinity` there and a wrong angle); an angle
|
|
60
|
+
* does not depend on the lengths, so beyond, normalise both vectors with
|
|
61
|
+
* {@link vec3NormalizeScaled} first.
|
|
62
|
+
*/
|
|
63
|
+
export function vec3AngleTo(a, b, axis) {
|
|
64
|
+
const cx = diffOfProducts(a[1], b[2], a[2], b[1]);
|
|
65
|
+
const cy = diffOfProducts(a[2], b[0], a[0], b[2]);
|
|
66
|
+
const cz = diffOfProducts(a[0], b[1], a[1], b[0]);
|
|
67
|
+
const angle = Math.atan2(Math.sqrt(cx * cx + cy * cy + cz * cz), vec3DotPrecise(a, b) + 0);
|
|
68
|
+
return angleNormalizeRad(cx * axis[0] + cy * axis[1] + cz * axis[2] < 0 ? -angle : angle);
|
|
17
69
|
}
|
|
18
70
|
/**
|
|
19
|
-
*
|
|
20
|
-
*
|
|
71
|
+
* The unsigned angle between two vectors, `atan2(|a × b|, a · b)`, in `[0, π]`,
|
|
72
|
+
* from the compensated cross and dot products. A zero vector gives `0`. Right
|
|
73
|
+
* while the product of the two lengths lies between about `1e−150` and `1e150`,
|
|
74
|
+
* which keeps the squares of the cross product in range (vectors of `1e100`
|
|
75
|
+
* give a wrong angle); an angle does not depend on the lengths, so beyond,
|
|
76
|
+
* normalise both vectors with {@link vec3NormalizeScaled} first.
|
|
21
77
|
*/
|
|
22
|
-
export function
|
|
23
|
-
|
|
24
|
-
throw new Error("Cannot compute angle to/from a zero-length vector.");
|
|
25
|
-
}
|
|
26
|
-
const cross = vec3Cross(v1, v2);
|
|
27
|
-
const angle = Math.atan2(vec3Length(cross), vec3Dot(v1, v2));
|
|
28
|
-
const sign = vec3Dot(cross, axis) < 0 ? -1 : 1;
|
|
29
|
-
return angle * sign;
|
|
78
|
+
export function vec3AngleBetween(a, b) {
|
|
79
|
+
return Math.atan2(crossLengthPrecise_(a, b), vec3DotPrecise(a, b) + 0);
|
|
30
80
|
}
|
|
31
81
|
/**
|
|
32
|
-
*
|
|
33
|
-
* range [0, π].
|
|
82
|
+
* Writes `[a.x, a.y, z]` into `out`: the plane vector lifted to space.
|
|
34
83
|
*
|
|
35
|
-
* @
|
|
84
|
+
* @param z The third component; defaults to `0`.
|
|
36
85
|
*/
|
|
37
|
-
export function
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
throw new Error("Cannot compute angle between zero-length vectors");
|
|
43
|
-
}
|
|
44
|
-
const cosTheta = clamp(dot / (len1 * len2), -1, 1);
|
|
45
|
-
return Math.acos(cosTheta);
|
|
46
|
-
}
|
|
47
|
-
export function vec3ApplyMat3(v, m) {
|
|
48
|
-
return [dotPrecise(v, m[0]), dotPrecise(v, m[1]), dotPrecise(v, m[2])];
|
|
86
|
+
export function vec3FromVec2(out, a, z = 0) {
|
|
87
|
+
out[0] = a[0];
|
|
88
|
+
out[1] = a[1];
|
|
89
|
+
out[2] = z;
|
|
90
|
+
return out;
|
|
49
91
|
}
|
|
50
92
|
/**
|
|
51
|
-
*
|
|
52
|
-
*
|
|
93
|
+
* Writes `[x, y, z] / w` into `out`: the Cartesian point of a homogeneous
|
|
94
|
+
* vector, the perspective divide. A `w` of zero (a point at infinity) gives
|
|
95
|
+
* non-finite components, `±Infinity` or `NaN`, not an exception.
|
|
53
96
|
*/
|
|
54
|
-
export function
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
dotPrecise(homogeneous, m[2]),
|
|
61
|
-
];
|
|
62
|
-
}
|
|
63
|
-
export function vec3ApplyQuaternion(v, q) {
|
|
64
|
-
const [qx, qy, qz, qw] = q;
|
|
65
|
-
// Calculate quaternion * vector
|
|
66
|
-
const ix = dotPrecise([qw, qy, -qz], [v[0], v[2], v[1]]);
|
|
67
|
-
const iy = dotPrecise([qw, qz, -qx], [v[1], v[0], v[2]]);
|
|
68
|
-
const iz = dotPrecise([qw, qx, -qy], [v[2], v[1], v[0]]);
|
|
69
|
-
const iw = dotPrecise([-qx, -qy, -qz], v);
|
|
70
|
-
// Calculate result * inverse quaternion
|
|
71
|
-
return [
|
|
72
|
-
dotPrecise([ix, -iw, -iy, iz], [qw, qx, qz, qy]),
|
|
73
|
-
dotPrecise([iy, -iw, -iz, ix], [qw, qy, qx, qz]),
|
|
74
|
-
dotPrecise([iz, -iw, -ix, iy], [qw, qz, qy, qx]),
|
|
75
|
-
];
|
|
97
|
+
export function vec3FromHomogeneous(out, a) {
|
|
98
|
+
const w = a[3];
|
|
99
|
+
out[0] = a[0] / w;
|
|
100
|
+
out[1] = a[1] / w;
|
|
101
|
+
out[2] = a[2] / w;
|
|
102
|
+
return out;
|
|
76
103
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
104
|
+
/**
|
|
105
|
+
* Writes `a` rotated by `angle` radians about the unit vector `axis`
|
|
106
|
+
* (Rodrigues' formula), counter-clockwise seen from the tip of `axis`, into
|
|
107
|
+
* `out`. `axis` is not normalised here: a vector that is not unit scales the
|
|
108
|
+
* result. `out` may be `a` or `axis`.
|
|
109
|
+
*/
|
|
110
|
+
export function vec3RotateAxisAngle(out, a, axis, angle) {
|
|
111
|
+
const x = a[0];
|
|
112
|
+
const y = a[1];
|
|
113
|
+
const z = a[2];
|
|
114
|
+
const kx = axis[0];
|
|
115
|
+
const ky = axis[1];
|
|
116
|
+
const kz = axis[2];
|
|
117
|
+
const cos = Math.cos(angle);
|
|
118
|
+
const sin = Math.sin(angle);
|
|
119
|
+
const along = (kx * x + ky * y + kz * z) * (1 - cos);
|
|
120
|
+
out[0] = x * cos + (ky * z - kz * y) * sin + kx * along;
|
|
121
|
+
out[1] = y * cos + (kz * x - kx * z) * sin + ky * along;
|
|
122
|
+
out[2] = z * cos + (kx * y - ky * x) * sin + kz * along;
|
|
123
|
+
return out;
|
|
83
124
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
125
|
+
/**
|
|
126
|
+
* Writes into `out` a unit vector perpendicular to the scalar components `[x,
|
|
127
|
+
* y, z]`: their cross product with the axis of the smallest component, which is
|
|
128
|
+
* as far from parallel as an axis can be, normalised at any magnitude.
|
|
129
|
+
*/
|
|
130
|
+
function perpendicular_(out, x, y, z) {
|
|
131
|
+
const absoluteX = Math.abs(x);
|
|
132
|
+
const absoluteY = Math.abs(y);
|
|
133
|
+
const absoluteZ = Math.abs(z);
|
|
134
|
+
if (absoluteX <= absoluteY && absoluteX <= absoluteZ) {
|
|
135
|
+
out[0] = 0;
|
|
136
|
+
out[1] = z;
|
|
137
|
+
out[2] = -y;
|
|
88
138
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
return [clamp(v[0], min, max), clamp(v[1], min, max), clamp(v[2], min, max)];
|
|
94
|
-
}
|
|
95
|
-
export function vec3Cross(a, b) {
|
|
96
|
-
return [
|
|
97
|
-
diffOfProducts(a[1], b[2], a[2], b[1]),
|
|
98
|
-
diffOfProducts(a[2], b[0], a[0], b[2]),
|
|
99
|
-
diffOfProducts(a[0], b[1], a[1], b[0]),
|
|
100
|
-
];
|
|
101
|
-
}
|
|
102
|
-
export function vec3DistanceTo(a, b, type = "euclidean") {
|
|
103
|
-
const dx = Math.abs(a[0] - b[0]);
|
|
104
|
-
const dy = Math.abs(a[1] - b[1]);
|
|
105
|
-
const dz = Math.abs(a[2] - b[2]);
|
|
106
|
-
switch (type) {
|
|
107
|
-
case "euclidean":
|
|
108
|
-
return Math.hypot(dx, dy, dz);
|
|
109
|
-
case "manhattan":
|
|
110
|
-
return dx + dy + dz;
|
|
111
|
-
case "chebyshev":
|
|
112
|
-
return Math.max(dx, dy, dz);
|
|
139
|
+
else if (absoluteY <= absoluteZ) {
|
|
140
|
+
out[0] = -z;
|
|
141
|
+
out[1] = 0;
|
|
142
|
+
out[2] = x;
|
|
113
143
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
return
|
|
120
|
-
}
|
|
121
|
-
export function vec3Divide(a, b) {
|
|
122
|
-
return [a[0] / b[0], a[1] / b[1], a[2] / b[2]];
|
|
123
|
-
}
|
|
124
|
-
export function vec3DivideScalar(v, s) {
|
|
125
|
-
return [v[0] / s, v[1] / s, v[2] / s];
|
|
126
|
-
}
|
|
127
|
-
export function vec3Dot(a, b) {
|
|
128
|
-
return dotPrecise(a, b);
|
|
129
|
-
}
|
|
130
|
-
/** Exact component-wise equality. See {@link vec3IsClose} for a tolerant test. */
|
|
131
|
-
export function vec3Equals(a, b) {
|
|
132
|
-
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2];
|
|
144
|
+
else {
|
|
145
|
+
out[0] = y;
|
|
146
|
+
out[1] = -x;
|
|
147
|
+
out[2] = 0;
|
|
148
|
+
}
|
|
149
|
+
return vec3NormalizeScaled(out, out);
|
|
133
150
|
}
|
|
134
151
|
/**
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
* required: a vector carries no units of its own, so only the caller knows what
|
|
140
|
-
* counts as close.
|
|
152
|
+
* Writes a unit vector perpendicular to `a` into `out`: `a` crossed with the
|
|
153
|
+
* axis of its smallest component, normalised at any magnitude. The result is
|
|
154
|
+
* not unique; this one is stable, so a continuous input gives it a few jumps
|
|
155
|
+
* only. The zero vector stays zero. `out` may be `a`.
|
|
141
156
|
*/
|
|
142
|
-
export function
|
|
143
|
-
return (
|
|
144
|
-
isCloseTo(a[1], b[1], tolerance) &&
|
|
145
|
-
isCloseTo(a[2], b[2], tolerance));
|
|
157
|
+
export function vec3Perpendicular(out, a) {
|
|
158
|
+
return perpendicular_(out, a[0], a[1], a[2]);
|
|
146
159
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
160
|
+
/**
|
|
161
|
+
* Writes the tangent and the bitangent that complete the unit vector `normal`
|
|
162
|
+
* to a right-handed orthonormal basis (`tangent × bitangent = normal`), by
|
|
163
|
+
* Duff, Burgess, Christensen, Hery, Kensler, Liani and Villemin's branch-light
|
|
164
|
+
* construction ("Building an Orthonormal Basis, Revisited", 2017): no
|
|
165
|
+
* normalisation and no singularity, whatever the direction of `normal`.
|
|
166
|
+
* `normal` is not normalised here. The outputs may be `normal`.
|
|
167
|
+
*/
|
|
168
|
+
export function vec3OrthonormalBasis(outTangent, outBitangent, normal) {
|
|
169
|
+
const x = normal[0];
|
|
170
|
+
const y = normal[1];
|
|
171
|
+
const z = normal[2];
|
|
172
|
+
const sign = z < 0 ? -1 : 1;
|
|
173
|
+
const a = -1 / (sign + z);
|
|
174
|
+
const b = x * y * a;
|
|
175
|
+
outTangent[0] = 1 + sign * x * x * a;
|
|
176
|
+
outTangent[1] = sign * b;
|
|
177
|
+
outTangent[2] = -sign * x;
|
|
178
|
+
outBitangent[0] = b;
|
|
179
|
+
outBitangent[1] = sign + y * y * a;
|
|
180
|
+
outBitangent[2] = -y;
|
|
159
181
|
}
|
|
160
|
-
|
|
161
|
-
|
|
182
|
+
/**
|
|
183
|
+
* Writes `a` minus its component along `normal` into `out`: `a` flattened onto
|
|
184
|
+
* the plane through the origin whose normal is `normal`. `normal` need not be a
|
|
185
|
+
* unit vector; a zero `normal` gives `NaN`. Right when the largest component of
|
|
186
|
+
* `normal` lies between about `1e−150` and `1e150`, where its squared length
|
|
187
|
+
* stays in range: above about `1.3e154` the result is `a` unflattened, and
|
|
188
|
+
* below about `1.5e−154` it is not finite. Beyond, normalise `normal` with
|
|
189
|
+
* {@link vec3NormalizeScaled} first. `out` may be `a` or `normal`.
|
|
190
|
+
*/
|
|
191
|
+
export function vec3ProjectOntoPlane(out, a, normal) {
|
|
192
|
+
const x = a[0];
|
|
193
|
+
const y = a[1];
|
|
194
|
+
const z = a[2];
|
|
195
|
+
const nx = normal[0];
|
|
196
|
+
const ny = normal[1];
|
|
197
|
+
const nz = normal[2];
|
|
198
|
+
const scale = (x * nx + y * ny + z * nz) / (nx * nx + ny * ny + nz * nz);
|
|
199
|
+
out[0] = x - nx * scale;
|
|
200
|
+
out[1] = y - ny * scale;
|
|
201
|
+
out[2] = z - nz * scale;
|
|
202
|
+
return out;
|
|
162
203
|
}
|
|
163
|
-
|
|
164
|
-
|
|
204
|
+
/**
|
|
205
|
+
* Writes `m · a` into `out`, the matrix applied to the column vector. `out` may
|
|
206
|
+
* be `a`.
|
|
207
|
+
*/
|
|
208
|
+
export function vec3TransformMat3x3(out, a, m) {
|
|
209
|
+
const x = a[0];
|
|
210
|
+
const y = a[1];
|
|
211
|
+
const z = a[2];
|
|
212
|
+
out[0] = m[0] * x + m[3] * y + m[6] * z;
|
|
213
|
+
out[1] = m[1] * x + m[4] * y + m[7] * z;
|
|
214
|
+
out[2] = m[2] * x + m[5] * y + m[8] * z;
|
|
215
|
+
return out;
|
|
165
216
|
}
|
|
166
|
-
|
|
167
|
-
|
|
217
|
+
/**
|
|
218
|
+
* {@link vec3TransformMat3x3} with each component a compensated dot product, as
|
|
219
|
+
* accurate as one in doubled precision rounded once. `out` may be `a`.
|
|
220
|
+
*/
|
|
221
|
+
export function vec3TransformMat3x3Precise(out, a, m) {
|
|
222
|
+
const x = a[0];
|
|
223
|
+
const y = a[1];
|
|
224
|
+
const z = a[2];
|
|
225
|
+
out[0] = dotPrecise3(m[0], x, m[3], y, m[6], z);
|
|
226
|
+
out[1] = dotPrecise3(m[1], x, m[4], y, m[7], z);
|
|
227
|
+
out[2] = dotPrecise3(m[2], x, m[5], y, m[8], z);
|
|
228
|
+
return out;
|
|
168
229
|
}
|
|
169
|
-
|
|
170
|
-
|
|
230
|
+
/**
|
|
231
|
+
* Writes the point `a` (as `[x, y, z, 1]`) transformed by `m` into `out`,
|
|
232
|
+
* divided by the resulting `w`, as gl-matrix and three.js do: for an affine
|
|
233
|
+
* matrix `w` is exactly `1` and nothing changes, for a projection this is the
|
|
234
|
+
* perspective divide. A `w` of zero gives non-finite components, not an
|
|
235
|
+
* exception. `out` may be `a`.
|
|
236
|
+
*/
|
|
237
|
+
export function vec3TransformMat4x4(out, a, m) {
|
|
238
|
+
const x = a[0];
|
|
239
|
+
const y = a[1];
|
|
240
|
+
const z = a[2];
|
|
241
|
+
const w = m[3] * x + m[7] * y + m[11] * z + m[15];
|
|
242
|
+
out[0] = (m[0] * x + m[4] * y + m[8] * z + m[12]) / w;
|
|
243
|
+
out[1] = (m[1] * x + m[5] * y + m[9] * z + m[13]) / w;
|
|
244
|
+
out[2] = (m[2] * x + m[6] * y + m[10] * z + m[14]) / w;
|
|
245
|
+
return out;
|
|
171
246
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
if (v[1] > maxY)
|
|
185
|
-
maxY = v[1];
|
|
186
|
-
if (v[2] > maxZ)
|
|
187
|
-
maxZ = v[2];
|
|
188
|
-
}
|
|
189
|
-
return { min: [minX, minY, minZ], max: [maxX, maxY, maxZ] };
|
|
247
|
+
/**
|
|
248
|
+
* Writes the direction `a` transformed by the upper-left 3×3 part of `m`, with
|
|
249
|
+
* no translation and no divide, into `out`. `out` may be `a`.
|
|
250
|
+
*/
|
|
251
|
+
export function vec3TransformDirectionMat4x4(out, a, m) {
|
|
252
|
+
const x = a[0];
|
|
253
|
+
const y = a[1];
|
|
254
|
+
const z = a[2];
|
|
255
|
+
out[0] = m[0] * x + m[4] * y + m[8] * z;
|
|
256
|
+
out[1] = m[1] * x + m[5] * y + m[9] * z;
|
|
257
|
+
out[2] = m[2] * x + m[6] * y + m[10] * z;
|
|
258
|
+
return out;
|
|
190
259
|
}
|
|
191
|
-
|
|
192
|
-
|
|
260
|
+
/**
|
|
261
|
+
* Writes the point `a` transformed by the affine transform (the linear part
|
|
262
|
+
* applied, then the translation added) into `out`. `out` may be `a`.
|
|
263
|
+
*/
|
|
264
|
+
export function vec3TransformAffine3(out, a, transform) {
|
|
265
|
+
const x = a[0];
|
|
266
|
+
const y = a[1];
|
|
267
|
+
const z = a[2];
|
|
268
|
+
out[0] =
|
|
269
|
+
transform[0] * x + transform[3] * y + transform[6] * z + transform[9];
|
|
270
|
+
out[1] =
|
|
271
|
+
transform[1] * x + transform[4] * y + transform[7] * z + transform[10];
|
|
272
|
+
out[2] =
|
|
273
|
+
transform[2] * x + transform[5] * y + transform[8] * z + transform[11];
|
|
274
|
+
return out;
|
|
193
275
|
}
|
|
194
|
-
|
|
195
|
-
|
|
276
|
+
/**
|
|
277
|
+
* Writes the direction `a` transformed by the linear part of the affine
|
|
278
|
+
* transform, with no translation, into `out`. `out` may be `a`.
|
|
279
|
+
*/
|
|
280
|
+
export function vec3TransformDirectionAffine3(out, a, transform) {
|
|
281
|
+
const x = a[0];
|
|
282
|
+
const y = a[1];
|
|
283
|
+
const z = a[2];
|
|
284
|
+
out[0] = transform[0] * x + transform[3] * y + transform[6] * z;
|
|
285
|
+
out[1] = transform[1] * x + transform[4] * y + transform[7] * z;
|
|
286
|
+
out[2] = transform[2] * x + transform[5] * y + transform[8] * z;
|
|
287
|
+
return out;
|
|
196
288
|
}
|
|
197
|
-
|
|
198
|
-
|
|
289
|
+
/**
|
|
290
|
+
* Writes `a` rotated by the unit quaternion `q` into `out`: `q · a · q⁻¹` in 18
|
|
291
|
+
* multiplications, no matrix. `q` must be unit length; it is not normalised
|
|
292
|
+
* here. `out` may be `a`.
|
|
293
|
+
*/
|
|
294
|
+
export function vec3TransformQuaternion(out, a, q) {
|
|
295
|
+
const x = a[0];
|
|
296
|
+
const y = a[1];
|
|
297
|
+
const z = a[2];
|
|
298
|
+
const qx = q[0];
|
|
299
|
+
const qy = q[1];
|
|
300
|
+
const qz = q[2];
|
|
301
|
+
const qw = q[3];
|
|
302
|
+
const tx = 2 * (qy * z - qz * y);
|
|
303
|
+
const ty = 2 * (qz * x - qx * z);
|
|
304
|
+
const tz = 2 * (qx * y - qy * x);
|
|
305
|
+
out[0] = x + qw * tx + (qy * tz - qz * ty);
|
|
306
|
+
out[1] = y + qw * ty + (qz * tx - qx * tz);
|
|
307
|
+
out[2] = z + qw * tz + (qx * ty - qy * tx);
|
|
308
|
+
return out;
|
|
199
309
|
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
310
|
+
/**
|
|
311
|
+
* The binary exponent of the largest component of `v`; `0` when there is none
|
|
312
|
+
* to scale by (a zero vector, a non-finite component), which the slerp then
|
|
313
|
+
* carries to `NaN` as it always did.
|
|
314
|
+
*/
|
|
315
|
+
function peakExponent_(v) {
|
|
316
|
+
const exponent = binaryExponent(Math.max(Math.abs(v[0]), Math.abs(v[1]), Math.abs(v[2])));
|
|
317
|
+
return Number.isFinite(exponent) ? exponent : 0;
|
|
206
318
|
}
|
|
207
319
|
/**
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
*
|
|
320
|
+
* Writes the interpolation of `a` and `b` along the great circle into `out`:
|
|
321
|
+
* the direction turns at constant angular speed along the shorter arc, and the
|
|
322
|
+
* length is interpolated linearly. The inputs need not be unit vectors. For `t`
|
|
323
|
+
* in `[0, 1]` and nearly parallel directions (about 1.8° apart or less) the
|
|
324
|
+
* directions are interpolated linearly and normalised, which traces the same
|
|
325
|
+
* arc. The arc lies in the plane of the two vectors, however close to opposite
|
|
326
|
+
* they are (`(1, 0, 0)` to `(−1, 0, 10⁻⁹)` passes through `+z`); for exactly
|
|
327
|
+
* opposite directions, where every great circle is as short, it turns about the
|
|
328
|
+
* axis of {@link vec3Perpendicular} of `a`. The length is interpolated all the
|
|
329
|
+
* same, so `t = 0.5` of opposite vectors has the mean length, never the origin
|
|
330
|
+
* that a straight line between them would give. `t` outside `[0, 1]`
|
|
331
|
+
* extrapolates along the same circle at the same angular speed. A zero vector
|
|
332
|
+
* has no direction: the result is `NaN`. The axis is the cross product of the
|
|
333
|
+
* vectors themselves, accurately rounded, so a pair that is a rounding away
|
|
334
|
+
* from opposite still turns in its own plane; lengths are taken at any
|
|
335
|
+
* magnitude, so vectors of `1e±200` slerp as well. `out` may be `a` or `b`.
|
|
214
336
|
*/
|
|
215
|
-
export function
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
337
|
+
export function vec3Slerp(out, a, b, t) {
|
|
338
|
+
const lengthA = vec3LengthScaled(a);
|
|
339
|
+
const lengthB = vec3LengthScaled(b);
|
|
340
|
+
const ax = a[0] / lengthA;
|
|
341
|
+
const ay = a[1] / lengthA;
|
|
342
|
+
const az = a[2] / lengthA;
|
|
343
|
+
const bx = b[0] / lengthB;
|
|
344
|
+
const by = b[1] / lengthB;
|
|
345
|
+
const bz = b[2] / lengthB;
|
|
346
|
+
const length = lerp(lengthA, lengthB, t);
|
|
347
|
+
const dot = ax * bx + ay * by + az * bz;
|
|
348
|
+
if (dot > SLERP_LINEAR_FALLBACK_DOT_ && t >= 0 && t <= 1) {
|
|
349
|
+
const x = lerp(ax, bx, t);
|
|
350
|
+
const y = lerp(ay, by, t);
|
|
351
|
+
const z = lerp(az, bz, t);
|
|
352
|
+
const scale = length / Math.sqrt(x * x + y * y + z * z);
|
|
353
|
+
out[0] = x * scale;
|
|
354
|
+
out[1] = y * scale;
|
|
355
|
+
out[2] = z * scale;
|
|
356
|
+
return out;
|
|
220
357
|
}
|
|
221
|
-
|
|
222
|
-
|
|
358
|
+
// The axis of the turn is `a × b`, formed from the vectors themselves (the
|
|
359
|
+
// unit directions of two exact antipodes of different lengths leave a
|
|
360
|
+
// rounding residue in theirs, and with it a spurious axis), each scaled by a
|
|
361
|
+
// power of two to between 1 and 2 so that the products neither overflow nor
|
|
362
|
+
// underflow. It is normalised after dividing by its largest component so that
|
|
363
|
+
// a tiny one (nearly opposite directions) keeps its digits.
|
|
364
|
+
const exponentA = peakExponent_(a);
|
|
365
|
+
const exponentB = peakExponent_(b);
|
|
366
|
+
const scaledAx = scaleByPowerOfTwo(a[0], -exponentA);
|
|
367
|
+
const scaledAy = scaleByPowerOfTwo(a[1], -exponentA);
|
|
368
|
+
const scaledAz = scaleByPowerOfTwo(a[2], -exponentA);
|
|
369
|
+
const scaledBx = scaleByPowerOfTwo(b[0], -exponentB);
|
|
370
|
+
const scaledBy = scaleByPowerOfTwo(b[1], -exponentB);
|
|
371
|
+
const scaledBz = scaleByPowerOfTwo(b[2], -exponentB);
|
|
372
|
+
const cx = diffOfProducts(scaledAy, scaledBz, scaledAz, scaledBy);
|
|
373
|
+
const cy = diffOfProducts(scaledAz, scaledBx, scaledAx, scaledBz);
|
|
374
|
+
const cz = diffOfProducts(scaledAx, scaledBy, scaledAy, scaledBx);
|
|
375
|
+
const peak = Math.max(Math.abs(cx), Math.abs(cy), Math.abs(cz));
|
|
376
|
+
// The unit vector, perpendicular to `a`, toward `b` in their plane.
|
|
377
|
+
let rx;
|
|
378
|
+
let ry;
|
|
379
|
+
let rz;
|
|
380
|
+
let sine = 0;
|
|
381
|
+
if (peak > 0) {
|
|
382
|
+
const ux = cx / peak;
|
|
383
|
+
const uy = cy / peak;
|
|
384
|
+
const uz = cz / peak;
|
|
385
|
+
const norm = Math.sqrt(ux * ux + uy * uy + uz * uz);
|
|
386
|
+
const nx = ux / norm;
|
|
387
|
+
const ny = uy / norm;
|
|
388
|
+
const nz = uz / norm;
|
|
389
|
+
rx = ny * az - nz * ay;
|
|
390
|
+
ry = nz * ax - nx * az;
|
|
391
|
+
rz = nx * ay - ny * ax;
|
|
392
|
+
// |a × b| over |a||b|, the lengths scaled as the vectors were.
|
|
393
|
+
sine =
|
|
394
|
+
(peak * norm) /
|
|
395
|
+
(scaleByPowerOfTwo(lengthA, -exponentA) *
|
|
396
|
+
scaleByPowerOfTwo(lengthB, -exponentB));
|
|
223
397
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
398
|
+
else {
|
|
399
|
+
// Parallel or opposite, exactly (or `NaN`, which `perpendicular_` carries
|
|
400
|
+
// through): any plane holds both.
|
|
401
|
+
perpendicular_(out, ax, ay, az);
|
|
402
|
+
const px = out[0];
|
|
403
|
+
const py = out[1];
|
|
404
|
+
const pz = out[2];
|
|
405
|
+
rx = py * az - pz * ay;
|
|
406
|
+
ry = pz * ax - px * az;
|
|
407
|
+
rz = px * ay - py * ax;
|
|
233
408
|
}
|
|
234
|
-
const
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
const random = randFn ?? Math.random;
|
|
242
|
-
const azimuth = random() * TWO_PI;
|
|
243
|
-
const inclination = Math.acos(2 * random() - 1); // uniform surface distribution — not random() * π
|
|
244
|
-
const sinI = Math.sin(inclination);
|
|
245
|
-
return [
|
|
246
|
-
sinI * Math.cos(azimuth),
|
|
247
|
-
Math.cos(inclination),
|
|
248
|
-
sinI * Math.sin(azimuth),
|
|
249
|
-
];
|
|
250
|
-
}
|
|
251
|
-
export function vec3Reflect(v, normal) {
|
|
252
|
-
const dot = vec3Dot(v, normal);
|
|
253
|
-
return vec3Sub(v, vec3MultiplyScalar(normal, 2 * dot));
|
|
254
|
-
}
|
|
255
|
-
export function vec3RotateAroundAxis(v, axis, angle) {
|
|
256
|
-
const cosA = Math.cos(angle);
|
|
257
|
-
const sinA = Math.sin(angle);
|
|
258
|
-
const dot = vec3Dot(axis, v);
|
|
259
|
-
const cross = vec3Cross(axis, v);
|
|
260
|
-
const weights = [cosA, sinA, dot * (1 - cosA)];
|
|
261
|
-
return [
|
|
262
|
-
dotPrecise(weights, [v[0], cross[0], axis[0]]),
|
|
263
|
-
dotPrecise(weights, [v[1], cross[1], axis[1]]),
|
|
264
|
-
dotPrecise(weights, [v[2], cross[2], axis[2]]),
|
|
265
|
-
];
|
|
266
|
-
}
|
|
267
|
-
export function vec3Round(v, options) {
|
|
268
|
-
return [round(v[0], options), round(v[1], options), round(v[2], options)];
|
|
269
|
-
}
|
|
270
|
-
export function vec3Sub(a, b) {
|
|
271
|
-
return [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
|
|
272
|
-
}
|
|
273
|
-
export function vec3SubScalar(v, s) {
|
|
274
|
-
return [v[0] - s, v[1] - s, v[2] - s];
|
|
275
|
-
}
|
|
276
|
-
export function vec3SubScaled(a, b, s) {
|
|
277
|
-
return [a[0] - b[0] * s, a[1] - b[1] * s, a[2] - b[2] * s];
|
|
409
|
+
const theta = Math.atan2(sine, dot) * t;
|
|
410
|
+
const cos = Math.cos(theta) * length;
|
|
411
|
+
const sin = Math.sin(theta) * length;
|
|
412
|
+
out[0] = ax * cos + rx * sin;
|
|
413
|
+
out[1] = ay * cos + ry * sin;
|
|
414
|
+
out[2] = az * cos + rz * sin;
|
|
415
|
+
return out;
|
|
278
416
|
}
|
|
279
417
|
/**
|
|
280
|
-
*
|
|
281
|
-
*
|
|
282
|
-
*
|
|
418
|
+
* Writes a unit vector in a uniformly random direction (uniform on the sphere:
|
|
419
|
+
* `z` uniform in `[−1, 1]`, the azimuth uniform in `[0, 2π)`) into `out`.
|
|
420
|
+
*
|
|
421
|
+
* @param out The vector to fill.
|
|
422
|
+
* @param random A source of numbers in `[0, 1)`; defaults to `Math.random`.
|
|
283
423
|
*/
|
|
284
|
-
export function
|
|
285
|
-
const
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
// t·π around an arbitrary axis perpendicular to it
|
|
293
|
-
return vec3RotateAroundAxis(a, vec3Normalize(vec3Perpendicular(a)), Math.PI * t);
|
|
294
|
-
}
|
|
295
|
-
const theta = Math.acos(dot) * t;
|
|
296
|
-
const relB = vec3Normalize(vec3Sub(b, vec3MultiplyScalar(a, dot)));
|
|
297
|
-
return vec3Add(vec3MultiplyScalar(a, Math.cos(theta)), vec3MultiplyScalar(relB, Math.sin(theta)));
|
|
298
|
-
}
|
|
299
|
-
export function vec3WeightedMean(items, getWeightedValue) {
|
|
300
|
-
const x = new PreciseSum();
|
|
301
|
-
const y = new PreciseSum();
|
|
302
|
-
const z = new PreciseSum();
|
|
303
|
-
const totalWeight = new PreciseSum();
|
|
304
|
-
for (const item of items) {
|
|
305
|
-
const [vec, w] = getWeightedValue(item);
|
|
306
|
-
x.add(vec[0] * w);
|
|
307
|
-
y.add(vec[1] * w);
|
|
308
|
-
z.add(vec[2] * w);
|
|
309
|
-
totalWeight.add(w);
|
|
310
|
-
}
|
|
311
|
-
const weight = totalWeight.value;
|
|
312
|
-
return [x.value / weight, y.value / weight, z.value / weight];
|
|
424
|
+
export function vec3Random(out, random = Math.random) {
|
|
425
|
+
const azimuth = random() * TWO_PI;
|
|
426
|
+
const z = 2 * random() - 1;
|
|
427
|
+
const radius = Math.sqrt(1 - z * z);
|
|
428
|
+
out[0] = radius * Math.cos(azimuth);
|
|
429
|
+
out[1] = radius * Math.sin(azimuth);
|
|
430
|
+
out[2] = z;
|
|
431
|
+
return out;
|
|
313
432
|
}
|
|
314
433
|
//# sourceMappingURL=vec3.js.map
|