Browse Source

Object3D: Clone material arrays (#26589)

* Object3D: Clone material arrays

* Revert unintentional lint fix

* Add tests

* Use shorthand if
Jesper van den Ende 2 years ago
parent
commit
03b6cc6390

+ 1 - 1
src/objects/Line.js

@@ -34,7 +34,7 @@ class Line extends Object3D {
 
 		super.copy( source, recursive );
 
-		this.material = source.material;
+		this.material = Array.isArray( source.material ) ? source.material.slice() : source.material;
 		this.geometry = source.geometry;
 
 		return this;

+ 1 - 1
src/objects/Mesh.js

@@ -65,7 +65,7 @@ class Mesh extends Object3D {
 
 		}
 
-		this.material = source.material;
+		this.material = Array.isArray( source.material ) ? source.material.slice() : source.material;
 		this.geometry = source.geometry;
 
 		return this;

+ 1 - 1
src/objects/Points.js

@@ -32,7 +32,7 @@ class Points extends Object3D {
 
 		super.copy( source, recursive );
 
-		this.material = source.material;
+		this.material = Array.isArray( source.material ) ? source.material.slice() : source.material;
 		this.geometry = source.geometry;
 
 		return this;

+ 18 - 0
test/unit/src/objects/Line.tests.js

@@ -3,6 +3,7 @@
 import { Line } from '../../../../src/objects/Line.js';
 
 import { Object3D } from '../../../../src/core/Object3D.js';
+import { Material } from '../../../../src/materials/Material.js';
 
 export default QUnit.module( 'Objects', () => {
 
@@ -67,6 +68,23 @@ export default QUnit.module( 'Objects', () => {
 
 		} );
 
+		QUnit.test( 'copy/material', ( assert ) => {
+
+			// Material arrays are cloned
+			const mesh1 = new Line();
+			mesh1.material = [ new Material() ];
+
+			const copy1 = mesh1.clone();
+			assert.notStrictEqual( mesh1.material, copy1.material );
+
+			// Non arrays are not cloned
+			const mesh2 = new Line();
+			mesh1.material = new Material();
+			const copy2 = mesh2.clone();
+			assert.strictEqual( mesh2.material, copy2.material );
+
+		} );
+
 		QUnit.todo( 'computeLineDistances', ( assert ) => {
 
 			assert.ok( false, 'everything\'s gonna be alright' );

+ 18 - 0
test/unit/src/objects/Mesh.tests.js

@@ -9,6 +9,7 @@ import { MeshBasicMaterial } from '../../../../src/materials/MeshBasicMaterial.j
 import { Vector2 } from '../../../../src/math/Vector2.js';
 import { Vector3 } from '../../../../src/math/Vector3.js';
 import { DoubleSide } from '../../../../src/constants.js';
+import { Material } from '../../../../src/materials/Material.js';
 
 export default QUnit.module( 'Objects', () => {
 
@@ -73,6 +74,23 @@ export default QUnit.module( 'Objects', () => {
 
 		} );
 
+		QUnit.test( 'copy/material', ( assert ) => {
+
+			// Material arrays are cloned
+			const mesh1 = new Mesh();
+			mesh1.material = [ new Material() ];
+
+			const copy1 = mesh1.clone();
+			assert.notStrictEqual( mesh1.material, copy1.material );
+
+			// Non arrays are not cloned
+			const mesh2 = new Mesh();
+			mesh1.material = new Material();
+			const copy2 = mesh2.clone();
+			assert.strictEqual( mesh2.material, copy2.material );
+
+		} );
+
 		QUnit.todo( 'updateMorphTargets', ( assert ) => {
 
 			assert.ok( false, 'everything\'s gonna be alright' );

+ 18 - 0
test/unit/src/objects/Points.tests.js

@@ -1,6 +1,7 @@
 /* global QUnit */
 
 import { Object3D } from '../../../../src/core/Object3D.js';
+import { Material } from '../../../../src/materials/Material.js';
 import { Points } from '../../../../src/objects/Points.js';
 
 export default QUnit.module( 'Objects', () => {
@@ -66,6 +67,23 @@ export default QUnit.module( 'Objects', () => {
 
 		} );
 
+		QUnit.test( 'copy/material', ( assert ) => {
+
+			// Material arrays are cloned
+			const mesh1 = new Points();
+			mesh1.material = [ new Material() ];
+
+			const copy1 = mesh1.clone();
+			assert.notStrictEqual( mesh1.material, copy1.material );
+
+			// Non arrays are not cloned
+			const mesh2 = new Points();
+			mesh1.material = new Material();
+			const copy2 = mesh2.clone();
+			assert.strictEqual( mesh2.material, copy2.material );
+
+		} );
+
 		QUnit.todo( 'raycast', ( assert ) => {
 
 			assert.ok( false, 'everything\'s gonna be alright' );