Skip to content

Commit 03b6cc6

Browse files
authored
Object3D: Clone material arrays (#26589)
* Object3D: Clone material arrays * Revert unintentional lint fix * Add tests * Use shorthand if
1 parent 8d9f8d5 commit 03b6cc6

File tree

6 files changed

+57
-3
lines changed

6 files changed

+57
-3
lines changed

src/objects/Line.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Line extends Object3D {
3434

3535
super.copy( source, recursive );
3636

37-
this.material = source.material;
37+
this.material = Array.isArray( source.material ) ? source.material.slice() : source.material;
3838
this.geometry = source.geometry;
3939

4040
return this;

src/objects/Mesh.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class Mesh extends Object3D {
6565

6666
}
6767

68-
this.material = source.material;
68+
this.material = Array.isArray( source.material ) ? source.material.slice() : source.material;
6969
this.geometry = source.geometry;
7070

7171
return this;

src/objects/Points.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Points extends Object3D {
3232

3333
super.copy( source, recursive );
3434

35-
this.material = source.material;
35+
this.material = Array.isArray( source.material ) ? source.material.slice() : source.material;
3636
this.geometry = source.geometry;
3737

3838
return this;

test/unit/src/objects/Line.tests.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { Line } from '../../../../src/objects/Line.js';
44

55
import { Object3D } from '../../../../src/core/Object3D.js';
6+
import { Material } from '../../../../src/materials/Material.js';
67

78
export default QUnit.module( 'Objects', () => {
89

@@ -67,6 +68,23 @@ export default QUnit.module( 'Objects', () => {
6768

6869
} );
6970

71+
QUnit.test( 'copy/material', ( assert ) => {
72+
73+
// Material arrays are cloned
74+
const mesh1 = new Line();
75+
mesh1.material = [ new Material() ];
76+
77+
const copy1 = mesh1.clone();
78+
assert.notStrictEqual( mesh1.material, copy1.material );
79+
80+
// Non arrays are not cloned
81+
const mesh2 = new Line();
82+
mesh1.material = new Material();
83+
const copy2 = mesh2.clone();
84+
assert.strictEqual( mesh2.material, copy2.material );
85+
86+
} );
87+
7088
QUnit.todo( 'computeLineDistances', ( assert ) => {
7189

7290
assert.ok( false, 'everything\'s gonna be alright' );

test/unit/src/objects/Mesh.tests.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { MeshBasicMaterial } from '../../../../src/materials/MeshBasicMaterial.j
99
import { Vector2 } from '../../../../src/math/Vector2.js';
1010
import { Vector3 } from '../../../../src/math/Vector3.js';
1111
import { DoubleSide } from '../../../../src/constants.js';
12+
import { Material } from '../../../../src/materials/Material.js';
1213

1314
export default QUnit.module( 'Objects', () => {
1415

@@ -73,6 +74,23 @@ export default QUnit.module( 'Objects', () => {
7374

7475
} );
7576

77+
QUnit.test( 'copy/material', ( assert ) => {
78+
79+
// Material arrays are cloned
80+
const mesh1 = new Mesh();
81+
mesh1.material = [ new Material() ];
82+
83+
const copy1 = mesh1.clone();
84+
assert.notStrictEqual( mesh1.material, copy1.material );
85+
86+
// Non arrays are not cloned
87+
const mesh2 = new Mesh();
88+
mesh1.material = new Material();
89+
const copy2 = mesh2.clone();
90+
assert.strictEqual( mesh2.material, copy2.material );
91+
92+
} );
93+
7694
QUnit.todo( 'updateMorphTargets', ( assert ) => {
7795

7896
assert.ok( false, 'everything\'s gonna be alright' );

test/unit/src/objects/Points.tests.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* global QUnit */
22

33
import { Object3D } from '../../../../src/core/Object3D.js';
4+
import { Material } from '../../../../src/materials/Material.js';
45
import { Points } from '../../../../src/objects/Points.js';
56

67
export default QUnit.module( 'Objects', () => {
@@ -66,6 +67,23 @@ export default QUnit.module( 'Objects', () => {
6667

6768
} );
6869

70+
QUnit.test( 'copy/material', ( assert ) => {
71+
72+
// Material arrays are cloned
73+
const mesh1 = new Points();
74+
mesh1.material = [ new Material() ];
75+
76+
const copy1 = mesh1.clone();
77+
assert.notStrictEqual( mesh1.material, copy1.material );
78+
79+
// Non arrays are not cloned
80+
const mesh2 = new Points();
81+
mesh1.material = new Material();
82+
const copy2 = mesh2.clone();
83+
assert.strictEqual( mesh2.material, copy2.material );
84+
85+
} );
86+
6987
QUnit.todo( 'raycast', ( assert ) => {
7088

7189
assert.ok( false, 'everything\'s gonna be alright' );

0 commit comments

Comments
 (0)