Skip to content

The native runtime for three.js: three.js, down to the metal. 45× smaller than Electron, on a Sony PS3, and path-traced at 60 fps on ray-tracing GPUs.

Scene: assigning Euler.order updates the object's quaternion, as three.js does (#209, supersedes #122)

Evan (Yifeng) Wang6e37e8a08b07
diff --git a/packages/scene/src/math.ts b/packages/scene/src/math.ts
index 28a10f6..affe7a7 100644
--- a/packages/scene/src/math.ts
+++ b/packages/scene/src/math.ts
@@ -303,7 +303,7 @@ export class Euler {
private _x: number;
private _y: number;
private _z: number;
- order: EulerOrder;
+ private _order: EulerOrder;
/** Called after a change (an object's rotation / quaternion keep each other and the object in step). */
_onChange(): void {}
/** @internal The object whose rotation this is, if any (weakly: no cycle). */
@@ -312,8 +312,11 @@ export class Euler {
this._x = x;
this._y = y;
this._z = z;
- this.order = order;
+ this._order = order;
}
+ /** Assigning the order recomposes the object's quaternion, as three.js does. */
+ get order() { return this._order; }
+ set order(value: EulerOrder) { this._order = value; this._onChange(); }
get x() { return this._x; }
set x(v) { this._x = v; this._onChange(); }
get y() { return this._y; }
@@ -324,7 +327,7 @@ export class Euler {
this._x = x;
this._y = y;
this._z = z;
- this.order = order;
+ this._order = order;
this._onChange();
return this;
}
@@ -344,7 +347,7 @@ export class Euler {
this._x = Math.atan2(m32, m22);
this._z = 0;
}
- this.order = "XYZ";
+ this._order = "XYZ";
if (notify) this._onChange();
return this;
}
diff --git a/packages/scene/test/three-compat-euler.test.ts b/packages/scene/test/three-compat-euler.test.ts
new file mode 100644
index 0000000..7a10731
--- /dev/null
+++ b/packages/scene/test/three-compat-euler.test.ts
@@ -0,0 +1,15 @@
+// three.js compatibility: Euler.order notifies, as three.js r187 has it (the snippet-level suite is three-compat.test.ts).
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import * as THREE from "../src/index.ts";
+
+test("Euler.order: assigning the order keeps Object3D.quaternion in sync, as three.js does", () => {
+ const object = new THREE.Object3D();
+ object.rotation.set(Math.PI / 2, Math.PI / 2, 0, "XYZ");
+ assert.ok(Math.abs(object.quaternion.z - 0.5) < 1e-12, "XYZ begins with +0.5 z");
+ object.rotation.order = "YXZ";
+ const expected = [0.5, 0.5, -0.5, 0.5];
+ assert.ok(object.quaternion.toArray().every((value, i) => Math.abs(value - expected[i]) < 1e-12),
+ "YXZ recomposes the quaternion to (0.5, 0.5, -0.5, 0.5)");
+ assert.equal(object.rotation.order, "YXZ", "setting the order preserves the chosen order");
+});