@bluemoonfoundry/daz-ts 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +377 -2
- package/dist/animation.d.ts +129 -0
- package/dist/animation.js +276 -0
- package/dist/batch.d.ts +118 -0
- package/dist/batch.js +186 -0
- package/dist/bone.d.ts +50 -0
- package/dist/bone.js +66 -0
- package/dist/camera.d.ts +61 -0
- package/dist/camera.js +120 -0
- package/dist/cinematics.d.ts +188 -0
- package/dist/cinematics.js +165 -0
- package/dist/client.d.ts +399 -0
- package/dist/client.js +769 -0
- package/dist/dforce.d.ts +15 -0
- package/dist/dforce.js +36 -0
- package/dist/element.d.ts +51 -0
- package/dist/element.js +141 -0
- package/dist/exceptions.d.ts +124 -0
- package/dist/exceptions.js +173 -0
- package/dist/geometry.d.ts +81 -0
- package/dist/geometry.js +433 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.js +36 -0
- package/dist/light.d.ts +24 -0
- package/dist/light.js +40 -0
- package/dist/lighting.d.ts +120 -0
- package/dist/lighting.js +107 -0
- package/dist/material.d.ts +27 -0
- package/dist/material.js +63 -0
- package/dist/materials.d.ts +107 -0
- package/dist/materials.js +165 -0
- package/dist/math3.d.ts +204 -0
- package/dist/math3.js +502 -0
- package/dist/modifier.d.ts +14 -0
- package/dist/modifier.js +24 -0
- package/dist/morph.d.ts +11 -0
- package/dist/morph.js +24 -0
- package/dist/node.d.ts +196 -0
- package/dist/node.js +447 -0
- package/dist/polling.d.ts +24 -0
- package/dist/polling.js +50 -0
- package/dist/pose.d.ts +82 -0
- package/dist/pose.js +295 -0
- package/dist/poses.d.ts +54 -0
- package/dist/poses.js +40 -0
- package/dist/property.d.ts +64 -0
- package/dist/property.js +125 -0
- package/dist/render.d.ts +242 -0
- package/dist/render.js +811 -0
- package/dist/renderApi.d.ts +124 -0
- package/dist/renderApi.js +235 -0
- package/dist/result.d.ts +15 -0
- package/dist/result.js +1 -0
- package/dist/scene.d.ts +154 -0
- package/dist/scene.js +584 -0
- package/dist/sceneEvents.d.ts +63 -0
- package/dist/sceneEvents.js +102 -0
- package/dist/sceneState.d.ts +167 -0
- package/dist/sceneState.js +430 -0
- package/dist/scriptBuilder.d.ts +41 -0
- package/dist/scriptBuilder.js +72 -0
- package/dist/shotGeometry.d.ts +49 -0
- package/dist/shotGeometry.js +76 -0
- package/dist/skeleton.d.ts +120 -0
- package/dist/skeleton.js +561 -0
- package/dist/timeline.d.ts +24 -0
- package/dist/timeline.js +39 -0
- package/dist/undo.d.ts +24 -0
- package/dist/undo.js +45 -0
- package/dist/version.d.ts +31 -0
- package/dist/version.js +36 -0
- package/dist/viewport.d.ts +58 -0
- package/dist/viewport.js +239 -0
- package/package.json +52 -4
package/dist/math3.d.ts
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure TypeScript 3D math for DAZ Studio workflows.
|
|
3
|
+
*
|
|
4
|
+
* No external dependencies. Mirrors dazpy's `math3.py` exactly, providing
|
|
5
|
+
* {@link Vec3}, {@link Quat}, {@link BoundingBox}, and {@link AxisRemap}
|
|
6
|
+
* with direct support for the dict shapes returned by the daz-ts API
|
|
7
|
+
* (once Phase 2 proxy classes exist).
|
|
8
|
+
*/
|
|
9
|
+
/** Immutable 3-component vector. All methods return new {@link Vec3} instances. */
|
|
10
|
+
export declare class Vec3 {
|
|
11
|
+
readonly x: number;
|
|
12
|
+
readonly y: number;
|
|
13
|
+
readonly z: number;
|
|
14
|
+
constructor(x: number, y: number, z: number);
|
|
15
|
+
static fromDict(d: {
|
|
16
|
+
x: number;
|
|
17
|
+
y: number;
|
|
18
|
+
z: number;
|
|
19
|
+
}): Vec3;
|
|
20
|
+
/**
|
|
21
|
+
* Create from a `[x, y, z]` list (e.g. a vertex from `DazGeometry.vertexPositionsAll()`).
|
|
22
|
+
*
|
|
23
|
+
* @throws {RangeError} If `v` doesn't have exactly three elements.
|
|
24
|
+
*/
|
|
25
|
+
static fromList(v: readonly number[]): Vec3;
|
|
26
|
+
static zero(): Vec3;
|
|
27
|
+
toDict(): {
|
|
28
|
+
x: number;
|
|
29
|
+
y: number;
|
|
30
|
+
z: number;
|
|
31
|
+
};
|
|
32
|
+
toList(): number[];
|
|
33
|
+
add(other: Vec3): Vec3;
|
|
34
|
+
sub(other: Vec3): Vec3;
|
|
35
|
+
mul(scalar: number): Vec3;
|
|
36
|
+
div(scalar: number): Vec3;
|
|
37
|
+
neg(): Vec3;
|
|
38
|
+
equals(other: Vec3): boolean;
|
|
39
|
+
dot(other: Vec3): number;
|
|
40
|
+
cross(other: Vec3): Vec3;
|
|
41
|
+
lengthSq(): number;
|
|
42
|
+
length(): number;
|
|
43
|
+
normalize(): Vec3;
|
|
44
|
+
distanceSq(other: Vec3): number;
|
|
45
|
+
distance(other: Vec3): number;
|
|
46
|
+
lerp(other: Vec3, t: number): Vec3;
|
|
47
|
+
/** Reflect this vector about `normal` (must be unit length). */
|
|
48
|
+
reflect(normal: Vec3): Vec3;
|
|
49
|
+
}
|
|
50
|
+
type RotationOrder = "XYZ" | "XZY" | "YXZ" | "YZX" | "ZXY" | "ZYX";
|
|
51
|
+
/**
|
|
52
|
+
* Unit quaternion representing a 3D rotation.
|
|
53
|
+
*
|
|
54
|
+
* Stored as `(x, y, z, w)` — imaginary components first, scalar last.
|
|
55
|
+
* Studio returns the same dict shape in its DzQuat convention, which is the
|
|
56
|
+
* conjugate: convert with {@link Quat.fromDz} / {@link Quat.toDz}.
|
|
57
|
+
* All methods return new {@link Quat} instances.
|
|
58
|
+
*/
|
|
59
|
+
export declare class Quat {
|
|
60
|
+
readonly x: number;
|
|
61
|
+
readonly y: number;
|
|
62
|
+
readonly z: number;
|
|
63
|
+
readonly w: number;
|
|
64
|
+
constructor(x: number, y: number, z: number, w: number);
|
|
65
|
+
static identity(): Quat;
|
|
66
|
+
/**
|
|
67
|
+
* Create from an `{x, y, z, w}` dict, components taken as-is. For a
|
|
68
|
+
* quaternion read from Studio (`getLocalRot`, `getWSRot`,
|
|
69
|
+
* `getOrientation`) use {@link fromDz}: a DzQuat stores the conjugate.
|
|
70
|
+
*/
|
|
71
|
+
static fromDict(d: {
|
|
72
|
+
x: number;
|
|
73
|
+
y: number;
|
|
74
|
+
z: number;
|
|
75
|
+
w: number;
|
|
76
|
+
}): Quat;
|
|
77
|
+
/**
|
|
78
|
+
* Create from a DAZ Studio DzQuat dict. A DzQuat stores the conjugate
|
|
79
|
+
* `(-x, -y, -z, w)` of the math3 quaternion for the same rotation
|
|
80
|
+
* (live-verified; `DzQuat.multVec` rotates like {@link rotate} on the
|
|
81
|
+
* converted value).
|
|
82
|
+
*/
|
|
83
|
+
static fromDz(d: {
|
|
84
|
+
x: number;
|
|
85
|
+
y: number;
|
|
86
|
+
z: number;
|
|
87
|
+
w: number;
|
|
88
|
+
}): Quat;
|
|
89
|
+
/** The DzQuat dict for this rotation (inverse of {@link fromDz}). */
|
|
90
|
+
toDz(): {
|
|
91
|
+
x: number;
|
|
92
|
+
y: number;
|
|
93
|
+
z: number;
|
|
94
|
+
w: number;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* The local rotation Studio produces from rotation channel values (degrees,
|
|
98
|
+
* as applied after limits). Studio's rotation order string (e.g. `"YZX"`)
|
|
99
|
+
* is the reverse of math3's `order`, and channels rotate about the bone's
|
|
100
|
+
* oriented axes: `local = O * fromEuler(x, y, z, reversed) * O⁻¹`.
|
|
101
|
+
* `orientation` is `Quat.fromDz(getOrientation())`; omit for identity.
|
|
102
|
+
* Returns the value equal to `Quat.fromDz(getLocalRot())`.
|
|
103
|
+
*/
|
|
104
|
+
static fromDzEuler(x: number, y: number, z: number, rotationOrder?: string, orientation?: Quat): Quat;
|
|
105
|
+
/** Channel values `[x, y, z]` reproducing this local rotation. Inverse of {@link fromDzEuler}; ignores limits. */
|
|
106
|
+
toDzEuler(rotationOrder?: string, orientation?: Quat): [number, number, number];
|
|
107
|
+
/**
|
|
108
|
+
* Create from Euler angles in degrees using intrinsic rotations. `order`
|
|
109
|
+
* is the reverse of Studio's rotation order string; to convert Studio
|
|
110
|
+
* channel values use {@link fromDzEuler}.
|
|
111
|
+
*/
|
|
112
|
+
static fromEuler(x: number, y: number, z: number, order?: RotationOrder): Quat;
|
|
113
|
+
static fromAxisAngle(axis: Vec3, angleDeg: number): Quat;
|
|
114
|
+
toDict(): {
|
|
115
|
+
x: number;
|
|
116
|
+
y: number;
|
|
117
|
+
z: number;
|
|
118
|
+
w: number;
|
|
119
|
+
};
|
|
120
|
+
/** Return the equivalent 3x3 rotation matrix (row-major, right-handed). */
|
|
121
|
+
toMatrix(): number[][];
|
|
122
|
+
/** Extract Euler angles in degrees. Inverse of {@link fromEuler} for the same `order`. */
|
|
123
|
+
toEuler(order?: RotationOrder): [number, number, number];
|
|
124
|
+
/** Hamilton product `this * other`. */
|
|
125
|
+
multiply(other: Quat): Quat;
|
|
126
|
+
conjugate(): Quat;
|
|
127
|
+
length(): number;
|
|
128
|
+
normalize(): Quat;
|
|
129
|
+
dot(other: Quat): number;
|
|
130
|
+
/** Rotate vector `v` by this quaternion (sandwich product `q * v * q⁻¹`). */
|
|
131
|
+
rotate(v: Vec3): Vec3;
|
|
132
|
+
/** Spherical linear interpolation. Always takes the shortest arc. */
|
|
133
|
+
slerp(other: Quat, t: number): Quat;
|
|
134
|
+
equals(other: Quat): boolean;
|
|
135
|
+
}
|
|
136
|
+
/** Axis-aligned bounding box. */
|
|
137
|
+
export declare class BoundingBox {
|
|
138
|
+
readonly min: Vec3;
|
|
139
|
+
readonly max: Vec3;
|
|
140
|
+
constructor(min: Vec3, max: Vec3);
|
|
141
|
+
static fromDict(d: {
|
|
142
|
+
min: {
|
|
143
|
+
x: number;
|
|
144
|
+
y: number;
|
|
145
|
+
z: number;
|
|
146
|
+
};
|
|
147
|
+
max: {
|
|
148
|
+
x: number;
|
|
149
|
+
y: number;
|
|
150
|
+
z: number;
|
|
151
|
+
};
|
|
152
|
+
}): BoundingBox;
|
|
153
|
+
static fromPoints(points: Vec3[]): BoundingBox;
|
|
154
|
+
toDict(): {
|
|
155
|
+
min: {
|
|
156
|
+
x: number;
|
|
157
|
+
y: number;
|
|
158
|
+
z: number;
|
|
159
|
+
};
|
|
160
|
+
max: {
|
|
161
|
+
x: number;
|
|
162
|
+
y: number;
|
|
163
|
+
z: number;
|
|
164
|
+
};
|
|
165
|
+
};
|
|
166
|
+
get center(): Vec3;
|
|
167
|
+
get size(): Vec3;
|
|
168
|
+
get volume(): number;
|
|
169
|
+
contains(point: Vec3): boolean;
|
|
170
|
+
overlaps(other: BoundingBox): boolean;
|
|
171
|
+
expand(amount: number): BoundingBox;
|
|
172
|
+
union(other: BoundingBox): BoundingBox;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Converts {@link Vec3}, {@link Quat}, and {@link BoundingBox} values
|
|
176
|
+
* between axis conventions (e.g. Y-up to Z-up).
|
|
177
|
+
*
|
|
178
|
+
* A generic signed-axis-permutation remap. Each of `x`, `y`, `z` names
|
|
179
|
+
* which source axis (optionally signed) the corresponding output axis is
|
|
180
|
+
* derived from. All three source axes must be referenced exactly once
|
|
181
|
+
* (signs aside).
|
|
182
|
+
*/
|
|
183
|
+
export declare class AxisRemap {
|
|
184
|
+
private readonly specs;
|
|
185
|
+
private readonly rotationQuat;
|
|
186
|
+
constructor(x: string, y: string, z: string);
|
|
187
|
+
/** Remap a vector or point. Works for any remap, proper or reflective. */
|
|
188
|
+
applyVec3(v: Vec3): Vec3;
|
|
189
|
+
/**
|
|
190
|
+
* Remap a rotation.
|
|
191
|
+
* @throws {Error} If this remap is a reflection (determinant -1). Use
|
|
192
|
+
* {@link applyVec3} for vectors/points instead.
|
|
193
|
+
*/
|
|
194
|
+
applyQuat(q: Quat): Quat;
|
|
195
|
+
/** Remap a bounding box. Both corners are remapped and re-sorted per axis. */
|
|
196
|
+
applyBbox(b: BoundingBox): BoundingBox;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Converts DAZ Studio's Y-up convention to a Z-up convention (e.g. Blender,
|
|
200
|
+
* glTF-consuming tools converted to Z-up). Up (+Y) becomes +Z; DAZ's
|
|
201
|
+
* forward (+Z) becomes -Y.
|
|
202
|
+
*/
|
|
203
|
+
export declare const Y_UP_TO_Z_UP: AxisRemap;
|
|
204
|
+
export {};
|
package/dist/math3.js
ADDED
|
@@ -0,0 +1,502 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure TypeScript 3D math for DAZ Studio workflows.
|
|
3
|
+
*
|
|
4
|
+
* No external dependencies. Mirrors dazpy's `math3.py` exactly, providing
|
|
5
|
+
* {@link Vec3}, {@link Quat}, {@link BoundingBox}, and {@link AxisRemap}
|
|
6
|
+
* with direct support for the dict shapes returned by the daz-ts API
|
|
7
|
+
* (once Phase 2 proxy classes exist).
|
|
8
|
+
*/
|
|
9
|
+
// ── Vec3 ──────────────────────────────────────────────────────────────────
|
|
10
|
+
/** Immutable 3-component vector. All methods return new {@link Vec3} instances. */
|
|
11
|
+
export class Vec3 {
|
|
12
|
+
x;
|
|
13
|
+
y;
|
|
14
|
+
z;
|
|
15
|
+
constructor(x, y, z) {
|
|
16
|
+
this.x = x;
|
|
17
|
+
this.y = y;
|
|
18
|
+
this.z = z;
|
|
19
|
+
}
|
|
20
|
+
static fromDict(d) {
|
|
21
|
+
return new Vec3(d.x, d.y, d.z);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Create from a `[x, y, z]` list (e.g. a vertex from `DazGeometry.vertexPositionsAll()`).
|
|
25
|
+
*
|
|
26
|
+
* @throws {RangeError} If `v` doesn't have exactly three elements.
|
|
27
|
+
*/
|
|
28
|
+
static fromList(v) {
|
|
29
|
+
if (v.length !== 3) {
|
|
30
|
+
throw new RangeError(`Vec3.fromList expects 3 elements, got ${v.length}`);
|
|
31
|
+
}
|
|
32
|
+
return new Vec3(v[0], v[1], v[2]);
|
|
33
|
+
}
|
|
34
|
+
static zero() {
|
|
35
|
+
return new Vec3(0, 0, 0);
|
|
36
|
+
}
|
|
37
|
+
toDict() {
|
|
38
|
+
return { x: this.x, y: this.y, z: this.z };
|
|
39
|
+
}
|
|
40
|
+
toList() {
|
|
41
|
+
return [this.x, this.y, this.z];
|
|
42
|
+
}
|
|
43
|
+
add(other) {
|
|
44
|
+
return new Vec3(this.x + other.x, this.y + other.y, this.z + other.z);
|
|
45
|
+
}
|
|
46
|
+
sub(other) {
|
|
47
|
+
return new Vec3(this.x - other.x, this.y - other.y, this.z - other.z);
|
|
48
|
+
}
|
|
49
|
+
mul(scalar) {
|
|
50
|
+
return new Vec3(this.x * scalar, this.y * scalar, this.z * scalar);
|
|
51
|
+
}
|
|
52
|
+
div(scalar) {
|
|
53
|
+
return new Vec3(this.x / scalar, this.y / scalar, this.z / scalar);
|
|
54
|
+
}
|
|
55
|
+
neg() {
|
|
56
|
+
return new Vec3(-this.x, -this.y, -this.z);
|
|
57
|
+
}
|
|
58
|
+
equals(other) {
|
|
59
|
+
return this.x === other.x && this.y === other.y && this.z === other.z;
|
|
60
|
+
}
|
|
61
|
+
dot(other) {
|
|
62
|
+
return this.x * other.x + this.y * other.y + this.z * other.z;
|
|
63
|
+
}
|
|
64
|
+
cross(other) {
|
|
65
|
+
return new Vec3(this.y * other.z - this.z * other.y, this.z * other.x - this.x * other.z, this.x * other.y - this.y * other.x);
|
|
66
|
+
}
|
|
67
|
+
lengthSq() {
|
|
68
|
+
return this.x * this.x + this.y * this.y + this.z * this.z;
|
|
69
|
+
}
|
|
70
|
+
length() {
|
|
71
|
+
return Math.sqrt(this.lengthSq());
|
|
72
|
+
}
|
|
73
|
+
normalize() {
|
|
74
|
+
const mag = this.length();
|
|
75
|
+
if (mag < 1e-10) {
|
|
76
|
+
return new Vec3(0, 0, 0);
|
|
77
|
+
}
|
|
78
|
+
return this.div(mag);
|
|
79
|
+
}
|
|
80
|
+
distanceSq(other) {
|
|
81
|
+
return this.sub(other).lengthSq();
|
|
82
|
+
}
|
|
83
|
+
distance(other) {
|
|
84
|
+
return Math.sqrt(this.distanceSq(other));
|
|
85
|
+
}
|
|
86
|
+
lerp(other, t) {
|
|
87
|
+
return new Vec3(this.x + (other.x - this.x) * t, this.y + (other.y - this.y) * t, this.z + (other.z - this.z) * t);
|
|
88
|
+
}
|
|
89
|
+
/** Reflect this vector about `normal` (must be unit length). */
|
|
90
|
+
reflect(normal) {
|
|
91
|
+
return this.sub(normal.mul(2 * this.dot(normal)));
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const ORDERS = ["XYZ", "XZY", "YXZ", "YZX", "ZXY", "ZYX"];
|
|
95
|
+
/** math3 order for a Studio rotation order string (Studio's is reversed). */
|
|
96
|
+
function dzOrder(rotationOrder) {
|
|
97
|
+
const o = String(rotationOrder).toUpperCase();
|
|
98
|
+
if (!ORDERS.includes(o)) {
|
|
99
|
+
throw new RangeError(`Unknown rotation order: ${String(rotationOrder)}. Expected one of ${ORDERS.join(" ")}.`);
|
|
100
|
+
}
|
|
101
|
+
return o.split("").reverse().join("");
|
|
102
|
+
}
|
|
103
|
+
function eulerToQuat(hx, hy, hz, order) {
|
|
104
|
+
const cx = Math.cos(hx), sx = Math.sin(hx);
|
|
105
|
+
const cy = Math.cos(hy), sy = Math.sin(hy);
|
|
106
|
+
const cz = Math.cos(hz), sz = Math.sin(hz);
|
|
107
|
+
switch (order) {
|
|
108
|
+
case "XYZ":
|
|
109
|
+
return [sx * cy * cz + cx * sy * sz, cx * sy * cz - sx * cy * sz, cx * cy * sz + sx * sy * cz, cx * cy * cz - sx * sy * sz];
|
|
110
|
+
case "XZY":
|
|
111
|
+
return [sx * cy * cz - cx * sy * sz, cx * sy * cz - sx * cy * sz, cx * cy * sz + sx * sy * cz, cx * cy * cz + sx * sy * sz];
|
|
112
|
+
case "YXZ":
|
|
113
|
+
return [sx * cy * cz + cx * sy * sz, cx * sy * cz - sx * cy * sz, cx * cy * sz - sx * sy * cz, cx * cy * cz + sx * sy * sz];
|
|
114
|
+
case "YZX":
|
|
115
|
+
return [sx * cy * cz + cx * sy * sz, cx * sy * cz + sx * cy * sz, cx * cy * sz - sx * sy * cz, cx * cy * cz - sx * sy * sz];
|
|
116
|
+
case "ZXY":
|
|
117
|
+
return [sx * cy * cz - cx * sy * sz, cx * sy * cz + sx * cy * sz, cx * cy * sz + sx * sy * cz, cx * cy * cz - sx * sy * sz];
|
|
118
|
+
case "ZYX":
|
|
119
|
+
// dazpy's math3.py has a sign bug here; verified correct via Hamilton-product
|
|
120
|
+
// derivation — see daz-script-server-z0c1. w should be `+ sx*sy*sz`, not `-`.
|
|
121
|
+
return [sx * cy * cz - cx * sy * sz, cx * sy * cz + sx * cy * sz, cx * cy * sz - sx * sy * cz, cx * cy * cz + sx * sy * sz];
|
|
122
|
+
default:
|
|
123
|
+
throw new Error(`Unknown rotation order: ${String(order)}. Expected one of XYZ XZY YXZ YZX ZXY ZYX.`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function quatToEuler(m, order) {
|
|
127
|
+
const SAFE = 1.0 - 1e-6;
|
|
128
|
+
const clamp = (v) => Math.max(-1, Math.min(1, v));
|
|
129
|
+
const deg = (r) => (r * 180) / Math.PI;
|
|
130
|
+
switch (order) {
|
|
131
|
+
case "XYZ": {
|
|
132
|
+
const sy = clamp(m[0][2]);
|
|
133
|
+
const y = deg(Math.asin(sy));
|
|
134
|
+
if (Math.abs(sy) < SAFE) {
|
|
135
|
+
return [deg(Math.atan2(-m[1][2], m[2][2])), y, deg(Math.atan2(-m[0][1], m[0][0]))];
|
|
136
|
+
}
|
|
137
|
+
return [deg(Math.atan2(m[2][1], m[1][1])), y, 0];
|
|
138
|
+
}
|
|
139
|
+
case "XZY": {
|
|
140
|
+
const sz = clamp(-m[0][1]);
|
|
141
|
+
const z = deg(Math.asin(sz));
|
|
142
|
+
if (Math.abs(sz) < SAFE) {
|
|
143
|
+
return [deg(Math.atan2(m[2][1], m[1][1])), deg(Math.atan2(m[0][2], m[0][0])), z];
|
|
144
|
+
}
|
|
145
|
+
return [deg(Math.atan2(-m[1][2], m[2][2])), 0, z];
|
|
146
|
+
}
|
|
147
|
+
case "YXZ": {
|
|
148
|
+
const sx = clamp(-m[1][2]);
|
|
149
|
+
const x = deg(Math.asin(sx));
|
|
150
|
+
if (Math.abs(sx) < SAFE) {
|
|
151
|
+
return [x, deg(Math.atan2(m[0][2], m[2][2])), deg(Math.atan2(m[1][0], m[1][1]))];
|
|
152
|
+
}
|
|
153
|
+
return [x, deg(Math.atan2(-m[0][1], m[0][0])), 0];
|
|
154
|
+
}
|
|
155
|
+
case "YZX": {
|
|
156
|
+
const sz = clamp(m[1][0]);
|
|
157
|
+
const z = deg(Math.asin(sz));
|
|
158
|
+
if (Math.abs(sz) < SAFE) {
|
|
159
|
+
return [deg(Math.atan2(-m[1][2], m[1][1])), deg(Math.atan2(-m[2][0], m[0][0])), z];
|
|
160
|
+
}
|
|
161
|
+
return [deg(Math.atan2(m[0][1], m[2][1])), 0, z];
|
|
162
|
+
}
|
|
163
|
+
case "ZXY": {
|
|
164
|
+
const sx = clamp(m[2][1]);
|
|
165
|
+
const x = deg(Math.asin(sx));
|
|
166
|
+
if (Math.abs(sx) < SAFE) {
|
|
167
|
+
return [x, deg(Math.atan2(-m[2][0], m[2][2])), deg(Math.atan2(-m[0][1], m[1][1]))];
|
|
168
|
+
}
|
|
169
|
+
return [x, deg(Math.atan2(m[0][2], m[0][0])), 0];
|
|
170
|
+
}
|
|
171
|
+
case "ZYX": {
|
|
172
|
+
const sy = clamp(-m[2][0]);
|
|
173
|
+
const y = deg(Math.asin(sy));
|
|
174
|
+
if (Math.abs(sy) < SAFE) {
|
|
175
|
+
return [deg(Math.atan2(m[2][1], m[2][2])), y, deg(Math.atan2(m[1][0], m[0][0]))];
|
|
176
|
+
}
|
|
177
|
+
return [deg(Math.atan2(-m[0][1], m[1][1])), y, 0];
|
|
178
|
+
}
|
|
179
|
+
default:
|
|
180
|
+
throw new Error(`Unknown rotation order: ${String(order)}. Expected one of XYZ XZY YXZ YZX ZXY ZYX.`);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Unit quaternion representing a 3D rotation.
|
|
185
|
+
*
|
|
186
|
+
* Stored as `(x, y, z, w)` — imaginary components first, scalar last.
|
|
187
|
+
* Studio returns the same dict shape in its DzQuat convention, which is the
|
|
188
|
+
* conjugate: convert with {@link Quat.fromDz} / {@link Quat.toDz}.
|
|
189
|
+
* All methods return new {@link Quat} instances.
|
|
190
|
+
*/
|
|
191
|
+
export class Quat {
|
|
192
|
+
x;
|
|
193
|
+
y;
|
|
194
|
+
z;
|
|
195
|
+
w;
|
|
196
|
+
constructor(x, y, z, w) {
|
|
197
|
+
this.x = x;
|
|
198
|
+
this.y = y;
|
|
199
|
+
this.z = z;
|
|
200
|
+
this.w = w;
|
|
201
|
+
}
|
|
202
|
+
static identity() {
|
|
203
|
+
return new Quat(0, 0, 0, 1);
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Create from an `{x, y, z, w}` dict, components taken as-is. For a
|
|
207
|
+
* quaternion read from Studio (`getLocalRot`, `getWSRot`,
|
|
208
|
+
* `getOrientation`) use {@link fromDz}: a DzQuat stores the conjugate.
|
|
209
|
+
*/
|
|
210
|
+
static fromDict(d) {
|
|
211
|
+
return new Quat(d.x, d.y, d.z, d.w);
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Create from a DAZ Studio DzQuat dict. A DzQuat stores the conjugate
|
|
215
|
+
* `(-x, -y, -z, w)` of the math3 quaternion for the same rotation
|
|
216
|
+
* (live-verified; `DzQuat.multVec` rotates like {@link rotate} on the
|
|
217
|
+
* converted value).
|
|
218
|
+
*/
|
|
219
|
+
static fromDz(d) {
|
|
220
|
+
return new Quat(-d.x, -d.y, -d.z, d.w);
|
|
221
|
+
}
|
|
222
|
+
/** The DzQuat dict for this rotation (inverse of {@link fromDz}). */
|
|
223
|
+
toDz() {
|
|
224
|
+
return { x: -this.x, y: -this.y, z: -this.z, w: this.w };
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* The local rotation Studio produces from rotation channel values (degrees,
|
|
228
|
+
* as applied after limits). Studio's rotation order string (e.g. `"YZX"`)
|
|
229
|
+
* is the reverse of math3's `order`, and channels rotate about the bone's
|
|
230
|
+
* oriented axes: `local = O * fromEuler(x, y, z, reversed) * O⁻¹`.
|
|
231
|
+
* `orientation` is `Quat.fromDz(getOrientation())`; omit for identity.
|
|
232
|
+
* Returns the value equal to `Quat.fromDz(getLocalRot())`.
|
|
233
|
+
*/
|
|
234
|
+
static fromDzEuler(x, y, z, rotationOrder = "XYZ", orientation) {
|
|
235
|
+
const e = Quat.fromEuler(x, y, z, dzOrder(rotationOrder));
|
|
236
|
+
return orientation ? orientation.multiply(e).multiply(orientation.conjugate()) : e;
|
|
237
|
+
}
|
|
238
|
+
/** Channel values `[x, y, z]` reproducing this local rotation. Inverse of {@link fromDzEuler}; ignores limits. */
|
|
239
|
+
toDzEuler(rotationOrder = "XYZ", orientation) {
|
|
240
|
+
const q = orientation ? orientation.conjugate().multiply(this).multiply(orientation) : this;
|
|
241
|
+
return q.toEuler(dzOrder(rotationOrder));
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Create from Euler angles in degrees using intrinsic rotations. `order`
|
|
245
|
+
* is the reverse of Studio's rotation order string; to convert Studio
|
|
246
|
+
* channel values use {@link fromDzEuler}.
|
|
247
|
+
*/
|
|
248
|
+
static fromEuler(x, y, z, order = "XYZ") {
|
|
249
|
+
const hx = ((x * Math.PI) / 180) * 0.5;
|
|
250
|
+
const hy = ((y * Math.PI) / 180) * 0.5;
|
|
251
|
+
const hz = ((z * Math.PI) / 180) * 0.5;
|
|
252
|
+
const [qx, qy, qz, qw] = eulerToQuat(hx, hy, hz, order);
|
|
253
|
+
return new Quat(qx, qy, qz, qw);
|
|
254
|
+
}
|
|
255
|
+
static fromAxisAngle(axis, angleDeg) {
|
|
256
|
+
const n = axis.normalize();
|
|
257
|
+
const half = ((angleDeg * Math.PI) / 180) * 0.5;
|
|
258
|
+
const s = Math.sin(half);
|
|
259
|
+
return new Quat(n.x * s, n.y * s, n.z * s, Math.cos(half));
|
|
260
|
+
}
|
|
261
|
+
toDict() {
|
|
262
|
+
return { x: this.x, y: this.y, z: this.z, w: this.w };
|
|
263
|
+
}
|
|
264
|
+
/** Return the equivalent 3x3 rotation matrix (row-major, right-handed). */
|
|
265
|
+
toMatrix() {
|
|
266
|
+
const { x, y, z, w } = this;
|
|
267
|
+
const x2 = 2 * x * x, y2 = 2 * y * y, z2 = 2 * z * z;
|
|
268
|
+
const xy = 2 * x * y, xz = 2 * x * z, yz = 2 * y * z;
|
|
269
|
+
const wx = 2 * w * x, wy = 2 * w * y, wz = 2 * w * z;
|
|
270
|
+
return [
|
|
271
|
+
[1 - y2 - z2, xy - wz, xz + wy],
|
|
272
|
+
[xy + wz, 1 - x2 - z2, yz - wx],
|
|
273
|
+
[xz - wy, yz + wx, 1 - x2 - y2],
|
|
274
|
+
];
|
|
275
|
+
}
|
|
276
|
+
/** Extract Euler angles in degrees. Inverse of {@link fromEuler} for the same `order`. */
|
|
277
|
+
toEuler(order = "XYZ") {
|
|
278
|
+
return quatToEuler(this.toMatrix(), order);
|
|
279
|
+
}
|
|
280
|
+
/** Hamilton product `this * other`. */
|
|
281
|
+
multiply(other) {
|
|
282
|
+
const { x: ax, y: ay, z: az, w: aw } = this;
|
|
283
|
+
const { x: bx, y: by, z: bz, w: bw } = other;
|
|
284
|
+
return new Quat(aw * bx + ax * bw + ay * bz - az * by, aw * by - ax * bz + ay * bw + az * bx, aw * bz + ax * by - ay * bx + az * bw, aw * bw - ax * bx - ay * by - az * bz);
|
|
285
|
+
}
|
|
286
|
+
conjugate() {
|
|
287
|
+
return new Quat(-this.x, -this.y, -this.z, this.w);
|
|
288
|
+
}
|
|
289
|
+
length() {
|
|
290
|
+
return Math.sqrt(this.x ** 2 + this.y ** 2 + this.z ** 2 + this.w ** 2);
|
|
291
|
+
}
|
|
292
|
+
normalize() {
|
|
293
|
+
const mag = this.length();
|
|
294
|
+
if (mag < 1e-10) {
|
|
295
|
+
return Quat.identity();
|
|
296
|
+
}
|
|
297
|
+
return new Quat(this.x / mag, this.y / mag, this.z / mag, this.w / mag);
|
|
298
|
+
}
|
|
299
|
+
dot(other) {
|
|
300
|
+
return this.x * other.x + this.y * other.y + this.z * other.z + this.w * other.w;
|
|
301
|
+
}
|
|
302
|
+
/** Rotate vector `v` by this quaternion (sandwich product `q * v * q⁻¹`). */
|
|
303
|
+
rotate(v) {
|
|
304
|
+
const { x: qx, y: qy, z: qz, w: qw } = this;
|
|
305
|
+
const { x: vx, y: vy, z: vz } = v;
|
|
306
|
+
const tx = 2 * (qy * vz - qz * vy);
|
|
307
|
+
const ty = 2 * (qz * vx - qx * vz);
|
|
308
|
+
const tz = 2 * (qx * vy - qy * vx);
|
|
309
|
+
return new Vec3(vx + qw * tx + qy * tz - qz * ty, vy + qw * ty + qz * tx - qx * tz, vz + qw * tz + qx * ty - qy * tx);
|
|
310
|
+
}
|
|
311
|
+
/** Spherical linear interpolation. Always takes the shortest arc. */
|
|
312
|
+
slerp(other, t) {
|
|
313
|
+
let o = other;
|
|
314
|
+
let d = this.dot(o);
|
|
315
|
+
if (d < 0) {
|
|
316
|
+
o = new Quat(-o.x, -o.y, -o.z, -o.w);
|
|
317
|
+
d = -d;
|
|
318
|
+
}
|
|
319
|
+
d = Math.min(1, d);
|
|
320
|
+
if (d > 0.9995) {
|
|
321
|
+
const result = new Quat(this.x + t * (o.x - this.x), this.y + t * (o.y - this.y), this.z + t * (o.z - this.z), this.w + t * (o.w - this.w));
|
|
322
|
+
return result.normalize();
|
|
323
|
+
}
|
|
324
|
+
const theta0 = Math.acos(d);
|
|
325
|
+
const sinTheta0 = Math.sin(theta0);
|
|
326
|
+
const theta = theta0 * t;
|
|
327
|
+
const sinTheta = Math.sin(theta);
|
|
328
|
+
const cosTheta = Math.cos(theta);
|
|
329
|
+
const s0 = cosTheta - (d * sinTheta) / sinTheta0;
|
|
330
|
+
const s1 = sinTheta / sinTheta0;
|
|
331
|
+
return new Quat(s0 * this.x + s1 * o.x, s0 * this.y + s1 * o.y, s0 * this.z + s1 * o.z, s0 * this.w + s1 * o.w);
|
|
332
|
+
}
|
|
333
|
+
equals(other) {
|
|
334
|
+
return this.x === other.x && this.y === other.y && this.z === other.z && this.w === other.w;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
// ── BoundingBox ───────────────────────────────────────────────────────────
|
|
338
|
+
/** Axis-aligned bounding box. */
|
|
339
|
+
export class BoundingBox {
|
|
340
|
+
min;
|
|
341
|
+
max;
|
|
342
|
+
constructor(min, max) {
|
|
343
|
+
this.min = min;
|
|
344
|
+
this.max = max;
|
|
345
|
+
}
|
|
346
|
+
static fromDict(d) {
|
|
347
|
+
return new BoundingBox(Vec3.fromDict(d.min), Vec3.fromDict(d.max));
|
|
348
|
+
}
|
|
349
|
+
static fromPoints(points) {
|
|
350
|
+
if (points.length === 0) {
|
|
351
|
+
throw new Error("fromPoints requires at least one point");
|
|
352
|
+
}
|
|
353
|
+
const xs = points.map((p) => p.x);
|
|
354
|
+
const ys = points.map((p) => p.y);
|
|
355
|
+
const zs = points.map((p) => p.z);
|
|
356
|
+
return new BoundingBox(new Vec3(Math.min(...xs), Math.min(...ys), Math.min(...zs)), new Vec3(Math.max(...xs), Math.max(...ys), Math.max(...zs)));
|
|
357
|
+
}
|
|
358
|
+
toDict() {
|
|
359
|
+
return { min: this.min.toDict(), max: this.max.toDict() };
|
|
360
|
+
}
|
|
361
|
+
get center() {
|
|
362
|
+
return new Vec3((this.min.x + this.max.x) * 0.5, (this.min.y + this.max.y) * 0.5, (this.min.z + this.max.z) * 0.5);
|
|
363
|
+
}
|
|
364
|
+
get size() {
|
|
365
|
+
return this.max.sub(this.min);
|
|
366
|
+
}
|
|
367
|
+
get volume() {
|
|
368
|
+
const s = this.size;
|
|
369
|
+
return s.x * s.y * s.z;
|
|
370
|
+
}
|
|
371
|
+
contains(point) {
|
|
372
|
+
return (this.min.x <= point.x && point.x <= this.max.x &&
|
|
373
|
+
this.min.y <= point.y && point.y <= this.max.y &&
|
|
374
|
+
this.min.z <= point.z && point.z <= this.max.z);
|
|
375
|
+
}
|
|
376
|
+
overlaps(other) {
|
|
377
|
+
return (this.min.x <= other.max.x && this.max.x >= other.min.x &&
|
|
378
|
+
this.min.y <= other.max.y && this.max.y >= other.min.y &&
|
|
379
|
+
this.min.z <= other.max.z && this.max.z >= other.min.z);
|
|
380
|
+
}
|
|
381
|
+
expand(amount) {
|
|
382
|
+
const v = new Vec3(amount, amount, amount);
|
|
383
|
+
return new BoundingBox(this.min.sub(v), this.max.add(v));
|
|
384
|
+
}
|
|
385
|
+
union(other) {
|
|
386
|
+
return new BoundingBox(new Vec3(Math.min(this.min.x, other.min.x), Math.min(this.min.y, other.min.y), Math.min(this.min.z, other.min.z)), new Vec3(Math.max(this.max.x, other.max.x), Math.max(this.max.y, other.max.y), Math.max(this.max.z, other.max.z)));
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
// ── AxisRemap ─────────────────────────────────────────────────────────────
|
|
390
|
+
const AXIS_INDEX = { x: 0, y: 1, z: 2 };
|
|
391
|
+
function parseAxisSpec(spec) {
|
|
392
|
+
if (!spec) {
|
|
393
|
+
throw new Error(`Invalid axis spec ${JSON.stringify(spec)}; expected one of x, y, z, -x, -y, -z`);
|
|
394
|
+
}
|
|
395
|
+
let sign = 1;
|
|
396
|
+
let name = spec;
|
|
397
|
+
if (name[0] === "+" || name[0] === "-") {
|
|
398
|
+
sign = name[0] === "-" ? -1 : 1;
|
|
399
|
+
name = name.slice(1);
|
|
400
|
+
}
|
|
401
|
+
if (!(name in AXIS_INDEX)) {
|
|
402
|
+
throw new Error(`Invalid axis spec ${JSON.stringify(spec)}; expected one of x, y, z, -x, -y, -z`);
|
|
403
|
+
}
|
|
404
|
+
return [AXIS_INDEX[name], sign];
|
|
405
|
+
}
|
|
406
|
+
function mat3Det(m) {
|
|
407
|
+
return (m[0][0] * (m[1][1] * m[2][2] - m[1][2] * m[2][1]) -
|
|
408
|
+
m[0][1] * (m[1][0] * m[2][2] - m[1][2] * m[2][0]) +
|
|
409
|
+
m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0]));
|
|
410
|
+
}
|
|
411
|
+
function mat3ToQuat(m) {
|
|
412
|
+
const trace = m[0][0] + m[1][1] + m[2][2];
|
|
413
|
+
let x, y, z, w;
|
|
414
|
+
if (trace > 0) {
|
|
415
|
+
const s = Math.sqrt(trace + 1.0) * 2.0;
|
|
416
|
+
w = 0.25 * s;
|
|
417
|
+
x = (m[2][1] - m[1][2]) / s;
|
|
418
|
+
y = (m[0][2] - m[2][0]) / s;
|
|
419
|
+
z = (m[1][0] - m[0][1]) / s;
|
|
420
|
+
}
|
|
421
|
+
else if (m[0][0] > m[1][1] && m[0][0] > m[2][2]) {
|
|
422
|
+
const s = Math.sqrt(1.0 + m[0][0] - m[1][1] - m[2][2]) * 2.0;
|
|
423
|
+
w = (m[2][1] - m[1][2]) / s;
|
|
424
|
+
x = 0.25 * s;
|
|
425
|
+
y = (m[0][1] + m[1][0]) / s;
|
|
426
|
+
z = (m[0][2] + m[2][0]) / s;
|
|
427
|
+
}
|
|
428
|
+
else if (m[1][1] > m[2][2]) {
|
|
429
|
+
const s = Math.sqrt(1.0 + m[1][1] - m[0][0] - m[2][2]) * 2.0;
|
|
430
|
+
w = (m[0][2] - m[2][0]) / s;
|
|
431
|
+
x = (m[0][1] + m[1][0]) / s;
|
|
432
|
+
y = 0.25 * s;
|
|
433
|
+
z = (m[1][2] + m[2][1]) / s;
|
|
434
|
+
}
|
|
435
|
+
else {
|
|
436
|
+
const s = Math.sqrt(1.0 + m[2][2] - m[0][0] - m[1][1]) * 2.0;
|
|
437
|
+
w = (m[1][0] - m[0][1]) / s;
|
|
438
|
+
x = (m[0][2] + m[2][0]) / s;
|
|
439
|
+
y = (m[1][2] + m[2][1]) / s;
|
|
440
|
+
z = 0.25 * s;
|
|
441
|
+
}
|
|
442
|
+
return new Quat(x, y, z, w);
|
|
443
|
+
}
|
|
444
|
+
/**
|
|
445
|
+
* Converts {@link Vec3}, {@link Quat}, and {@link BoundingBox} values
|
|
446
|
+
* between axis conventions (e.g. Y-up to Z-up).
|
|
447
|
+
*
|
|
448
|
+
* A generic signed-axis-permutation remap. Each of `x`, `y`, `z` names
|
|
449
|
+
* which source axis (optionally signed) the corresponding output axis is
|
|
450
|
+
* derived from. All three source axes must be referenced exactly once
|
|
451
|
+
* (signs aside).
|
|
452
|
+
*/
|
|
453
|
+
export class AxisRemap {
|
|
454
|
+
specs;
|
|
455
|
+
rotationQuat;
|
|
456
|
+
constructor(x, y, z) {
|
|
457
|
+
const specs = [parseAxisSpec(x), parseAxisSpec(y), parseAxisSpec(z)];
|
|
458
|
+
const used = specs.map(([idx]) => idx).sort();
|
|
459
|
+
if (used.join(",") !== "0,1,2") {
|
|
460
|
+
throw new Error(`AxisRemap must reference each of x, y, z exactly once (got x=${x}, y=${y}, z=${z})`);
|
|
461
|
+
}
|
|
462
|
+
const matrix = [[0, 0, 0], [0, 0, 0], [0, 0, 0]];
|
|
463
|
+
specs.forEach(([idx, sign], row) => {
|
|
464
|
+
matrix[row][idx] = sign;
|
|
465
|
+
});
|
|
466
|
+
const det = mat3Det(matrix);
|
|
467
|
+
this.specs = specs;
|
|
468
|
+
this.rotationQuat = det > 0 ? mat3ToQuat(matrix) : null;
|
|
469
|
+
}
|
|
470
|
+
/** Remap a vector or point. Works for any remap, proper or reflective. */
|
|
471
|
+
applyVec3(v) {
|
|
472
|
+
const comps = [v.x, v.y, v.z];
|
|
473
|
+
const out = this.specs.map(([idx, sign]) => comps[idx] * sign);
|
|
474
|
+
return new Vec3(out[0], out[1], out[2]);
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* Remap a rotation.
|
|
478
|
+
* @throws {Error} If this remap is a reflection (determinant -1). Use
|
|
479
|
+
* {@link applyVec3} for vectors/points instead.
|
|
480
|
+
*/
|
|
481
|
+
applyQuat(q) {
|
|
482
|
+
if (this.rotationQuat === null) {
|
|
483
|
+
throw new Error("AxisRemap represents a reflection (determinant -1); cannot remap a Quat. Use applyVec3 for vectors/points instead.");
|
|
484
|
+
}
|
|
485
|
+
const r = this.rotationQuat;
|
|
486
|
+
return r.multiply(q).multiply(r.conjugate());
|
|
487
|
+
}
|
|
488
|
+
/** Remap a bounding box. Both corners are remapped and re-sorted per axis. */
|
|
489
|
+
applyBbox(b) {
|
|
490
|
+
const p1 = this.applyVec3(b.min);
|
|
491
|
+
const p2 = this.applyVec3(b.max);
|
|
492
|
+
const lo = new Vec3(Math.min(p1.x, p2.x), Math.min(p1.y, p2.y), Math.min(p1.z, p2.z));
|
|
493
|
+
const hi = new Vec3(Math.max(p1.x, p2.x), Math.max(p1.y, p2.y), Math.max(p1.z, p2.z));
|
|
494
|
+
return new BoundingBox(lo, hi);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* Converts DAZ Studio's Y-up convention to a Z-up convention (e.g. Blender,
|
|
499
|
+
* glTF-consuming tools converted to Z-up). Up (+Y) becomes +Z; DAZ's
|
|
500
|
+
* forward (+Z) becomes -Y.
|
|
501
|
+
*/
|
|
502
|
+
export const Y_UP_TO_Z_UP = new AxisRemap("x", "-z", "y");
|