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: Object3D.add skips arguments that are not an Object3D, as three.js does (#140)

Kevin Rajan814dee3ae797
diff --git a/packages/scene/src/core.ts b/packages/scene/src/core.ts
index 33ae4a8..f20a4a4 100644
--- a/packages/scene/src/core.ts
+++ b/packages/scene/src/core.ts
@@ -592,6 +592,10 @@ export class Object3D {
add(...objects: Object3D[]): this {
for (const o of objects) {
if (o === this) throw new Error("object can't be added as a child of itself");
+ if (!o?.isObject3D) {
+ console.error("Object3D.add: object not an instance of THREE.Object3D.", o);
+ continue;
+ }
// (An object the engine draws itself, a voxel body: it is in no scene.)
if ((o as { engineOwned?: boolean }).engineOwned) throw new TypeError(`${o.name || o.type}: the engine draws it; it is in no scene`);
o.parent?.remove(o);
diff --git a/packages/scene/test/three-compat-group-add.test.ts b/packages/scene/test/three-compat-group-add.test.ts
new file mode 100644
index 0000000..76529ee
--- /dev/null
+++ b/packages/scene/test/three-compat-group-add.test.ts
@@ -0,0 +1,19 @@
+// three.js r187 parity: Object3D.add rejects non-Object3D arguments without changing the scene graph.
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import * as THREE from "../src/index.ts";
+
+test("Object3D.add ignores a non-Object3D argument among valid children", () => {
+ const parent = new THREE.Group();
+ const first = new THREE.Object3D();
+ const last = new THREE.Object3D();
+ const invalid = { name: "not an Object3D", parent: null };
+
+ assert.equal(parent.add(first, invalid as unknown as THREE.Object3D, last), parent);
+ assert.equal(parent.children.length, 2, "the invalid argument must not become a child");
+ assert.equal(parent.children[0], first);
+ assert.equal(parent.children[1], last);
+ assert.equal(first.parent, parent);
+ assert.equal(last.parent, parent);
+ assert.equal(invalid.parent, null, "the invalid argument must not be reparented");
+});