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: Mesh.copy and clone give the copy its own material array, as three.js does (#104)

Kevin Rajan24b3705f2ac8
diff --git a/packages/scene/src/core.ts b/packages/scene/src/core.ts
index 4f2f475..2678382 100644
--- a/packages/scene/src/core.ts
+++ b/packages/scene/src/core.ts
@@ -1308,6 +1308,12 @@ export class Mesh extends Object3D {
get materials(): MeshStandardMaterial[] {
return Array.isArray(this._material) ? this._material : [this._material];
}
+ /** As in three.js, copy the material array, but share its materials and geometry. */
+ override copy(source: Object3D, recursive = true): this {
+ super.copy(source, recursive);
+ if (source instanceof Mesh) this.material = source._material;
+ return this;
+ }
}
/**
diff --git a/packages/scene/test/three-compat.test.ts b/packages/scene/test/three-compat.test.ts
index c58bed2..f858648 100644
--- a/packages/scene/test/three-compat.test.ts
+++ b/packages/scene/test/three-compat.test.ts
@@ -126,6 +126,34 @@ test("Object3D: clone (geometry shared, own id), attach (world transform kept),
assert.ok(o.getWorldDirection(new THREE.Vector3()).distanceTo(new THREE.Vector3(1, 0, 0)) < 1e-9);
});
+test("Mesh.copy and clone: material arrays are independent, but geometry and materials are shared, as three.js does", () => {
+ const geometry = new THREE.BoxGeometry();
+ const red = new THREE.MeshStandardMaterial({ color: 0xff0000 });
+ const blue = new THREE.MeshStandardMaterial({ color: 0x0000ff });
+ const green = new THREE.MeshStandardMaterial({ color: 0x00ff00 });
+ const source = new THREE.Mesh(geometry, [red, blue]);
+
+ const clone = source.clone();
+ assert.equal(clone.geometry, geometry, "clone shares the source geometry");
+ assert.notEqual(clone.materials, source.materials, "clone has its own material array");
+ assert.equal(clone.materials[0], red, "first material instance is shared");
+ assert.equal(clone.materials[1], blue, "second material instance is shared");
+ clone.materials[0] = green;
+ assert.equal(source.materials[0], red, "changing the clone's slots does not mutate the source");
+
+ const copy = new THREE.Mesh(new THREE.BoxGeometry(), new THREE.MeshStandardMaterial());
+ assert.equal(copy.copy(source), copy);
+ assert.equal(copy.geometry, geometry, "copy shares the source geometry");
+ assert.notEqual(copy.materials, source.materials, "copy has its own material array");
+ assert.equal(copy.materials[0], red);
+ assert.equal(copy.materials[1], blue);
+ copy.materials[1] = green;
+ assert.equal(source.materials[1], blue, "changing copied slots does not mutate the source");
+
+ const single = new THREE.Mesh(geometry, red);
+ assert.equal(single.clone().material, red, "a single material remains shared");
+});
+
test("Raycaster: the nearest hit first, front faces of single-sided materials, instanceId, near / far", () => {
const scene = new THREE.Scene();
const near = new THREE.Mesh(new THREE.PlaneGeometry(2, 2), new THREE.MeshStandardMaterial());