Skip to content

Commit 7bca75a

Browse files
authored
Merge pull request #19996 from DefinitelyMaybe/helpers--move-to-es6-classes
helpers: move to es6 classes
2 parents 1efcd88 + 8b93bea commit 7bca75a

13 files changed

+605
-595
lines changed

src/helpers/ArrowHelper.js

Lines changed: 59 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -11,103 +11,104 @@ import { Vector3 } from '../math/Vector3.js';
1111
const _axis = new Vector3();
1212
let _lineGeometry, _coneGeometry;
1313

14-
function ArrowHelper( dir, origin, length, color, headLength, headWidth ) {
14+
class ArrowHelper extends Object3D {
1515

16-
// dir is assumed to be normalized
16+
constructor( dir, origin, length, color, headLength, headWidth ) {
1717

18-
Object3D.call( this );
18+
super();
19+
// dir is assumed to be normalized
1920

20-
this.type = 'ArrowHelper';
21+
this.type = 'ArrowHelper';
2122

22-
if ( dir === undefined ) dir = new Vector3( 0, 0, 1 );
23-
if ( origin === undefined ) origin = new Vector3( 0, 0, 0 );
24-
if ( length === undefined ) length = 1;
25-
if ( color === undefined ) color = 0xffff00;
26-
if ( headLength === undefined ) headLength = 0.2 * length;
27-
if ( headWidth === undefined ) headWidth = 0.2 * headLength;
23+
if ( dir === undefined ) dir = new Vector3( 0, 0, 1 );
24+
if ( origin === undefined ) origin = new Vector3( 0, 0, 0 );
25+
if ( length === undefined ) length = 1;
26+
if ( color === undefined ) color = 0xffff00;
27+
if ( headLength === undefined ) headLength = 0.2 * length;
28+
if ( headWidth === undefined ) headWidth = 0.2 * headLength;
2829

29-
if ( _lineGeometry === undefined ) {
30+
if ( _lineGeometry === undefined ) {
3031

31-
_lineGeometry = new BufferGeometry();
32-
_lineGeometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 0, 1, 0 ], 3 ) );
32+
_lineGeometry = new BufferGeometry();
33+
_lineGeometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 0, 1, 0 ], 3 ) );
3334

34-
_coneGeometry = new CylinderBufferGeometry( 0, 0.5, 1, 5, 1 );
35-
_coneGeometry.translate( 0, - 0.5, 0 );
35+
_coneGeometry = new CylinderBufferGeometry( 0, 0.5, 1, 5, 1 );
36+
_coneGeometry.translate( 0, - 0.5, 0 );
3637

37-
}
38+
}
3839

39-
this.position.copy( origin );
40+
this.position.copy( origin );
4041

41-
this.line = new Line( _lineGeometry, new LineBasicMaterial( { color: color, toneMapped: false } ) );
42-
this.line.matrixAutoUpdate = false;
43-
this.add( this.line );
42+
this.line = new Line( _lineGeometry, new LineBasicMaterial( { color: color, toneMapped: false } ) );
43+
this.line.matrixAutoUpdate = false;
44+
this.add( this.line );
4445

45-
this.cone = new Mesh( _coneGeometry, new MeshBasicMaterial( { color: color, toneMapped: false } ) );
46-
this.cone.matrixAutoUpdate = false;
47-
this.add( this.cone );
46+
this.cone = new Mesh( _coneGeometry, new MeshBasicMaterial( { color: color, toneMapped: false } ) );
47+
this.cone.matrixAutoUpdate = false;
48+
this.add( this.cone );
4849

49-
this.setDirection( dir );
50-
this.setLength( length, headLength, headWidth );
50+
this.setDirection( dir );
51+
this.setLength( length, headLength, headWidth );
5152

52-
}
53+
}
5354

54-
ArrowHelper.prototype = Object.create( Object3D.prototype );
55-
ArrowHelper.prototype.constructor = ArrowHelper;
55+
setDirection( dir ) {
5656

57-
ArrowHelper.prototype.setDirection = function ( dir ) {
57+
// dir is assumed to be normalized
5858

59-
// dir is assumed to be normalized
59+
if ( dir.y > 0.99999 ) {
6060

61-
if ( dir.y > 0.99999 ) {
61+
this.quaternion.set( 0, 0, 0, 1 );
6262

63-
this.quaternion.set( 0, 0, 0, 1 );
63+
} else if ( dir.y < - 0.99999 ) {
6464

65-
} else if ( dir.y < - 0.99999 ) {
65+
this.quaternion.set( 1, 0, 0, 0 );
6666

67-
this.quaternion.set( 1, 0, 0, 0 );
67+
} else {
6868

69-
} else {
69+
_axis.set( dir.z, 0, - dir.x ).normalize();
7070

71-
_axis.set( dir.z, 0, - dir.x ).normalize();
71+
const radians = Math.acos( dir.y );
7272

73-
const radians = Math.acos( dir.y );
73+
this.quaternion.setFromAxisAngle( _axis, radians );
7474

75-
this.quaternion.setFromAxisAngle( _axis, radians );
75+
}
7676

7777
}
7878

79-
};
79+
setLength( length, headLength, headWidth ) {
80+
81+
if ( headLength === undefined ) headLength = 0.2 * length;
82+
if ( headWidth === undefined ) headWidth = 0.2 * headLength;
8083

81-
ArrowHelper.prototype.setLength = function ( length, headLength, headWidth ) {
84+
this.line.scale.set( 1, Math.max( 0.0001, length - headLength ), 1 ); // see #17458
85+
this.line.updateMatrix();
8286

83-
if ( headLength === undefined ) headLength = 0.2 * length;
84-
if ( headWidth === undefined ) headWidth = 0.2 * headLength;
87+
this.cone.scale.set( headWidth, headLength, headWidth );
88+
this.cone.position.y = length;
89+
this.cone.updateMatrix();
8590

86-
this.line.scale.set( 1, Math.max( 0.0001, length - headLength ), 1 ); // see #17458
87-
this.line.updateMatrix();
91+
}
8892

89-
this.cone.scale.set( headWidth, headLength, headWidth );
90-
this.cone.position.y = length;
91-
this.cone.updateMatrix();
93+
setColor( color ) {
9294

93-
};
95+
this.line.material.color.set( color );
96+
this.cone.material.color.set( color );
9497

95-
ArrowHelper.prototype.setColor = function ( color ) {
98+
}
9699

97-
this.line.material.color.set( color );
98-
this.cone.material.color.set( color );
100+
copy( source ) {
99101

100-
};
102+
super.copy( source, false );
101103

102-
ArrowHelper.prototype.copy = function ( source ) {
104+
this.line.copy( source.line );
105+
this.cone.copy( source.cone );
103106

104-
Object3D.prototype.copy.call( this, source, false );
107+
return this;
105108

106-
this.line.copy( source.line );
107-
this.cone.copy( source.cone );
109+
}
108110

109-
return this;
111+
}
110112

111-
};
112113

113114
export { ArrowHelper };

src/helpers/AxesHelper.js

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,37 @@ import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
33
import { Float32BufferAttribute } from '../core/BufferAttribute.js';
44
import { BufferGeometry } from '../core/BufferGeometry.js';
55

6-
function AxesHelper( size ) {
6+
class AxesHelper extends LineSegments {
77

8-
size = size || 1;
8+
constructor( size ) {
99

10-
const vertices = [
11-
0, 0, 0, size, 0, 0,
12-
0, 0, 0, 0, size, 0,
13-
0, 0, 0, 0, 0, size
14-
];
10+
size = size || 1;
1511

16-
const colors = [
17-
1, 0, 0, 1, 0.6, 0,
18-
0, 1, 0, 0.6, 1, 0,
19-
0, 0, 1, 0, 0.6, 1
20-
];
12+
const vertices = [
13+
0, 0, 0, size, 0, 0,
14+
0, 0, 0, 0, size, 0,
15+
0, 0, 0, 0, 0, size
16+
];
2117

22-
const geometry = new BufferGeometry();
23-
geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
24-
geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
18+
const colors = [
19+
1, 0, 0, 1, 0.6, 0,
20+
0, 1, 0, 0.6, 1, 0,
21+
0, 0, 1, 0, 0.6, 1
22+
];
2523

26-
const material = new LineBasicMaterial( { vertexColors: true, toneMapped: false } );
24+
const geometry = new BufferGeometry();
25+
geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
26+
geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
2727

28-
LineSegments.call( this, geometry, material );
28+
const material = new LineBasicMaterial( { vertexColors: true, toneMapped: false } );
2929

30-
this.type = 'AxesHelper';
30+
super( geometry, material );
3131

32-
}
32+
this.type = 'AxesHelper';
33+
34+
}
3335

34-
AxesHelper.prototype = Object.create( LineSegments.prototype );
35-
AxesHelper.prototype.constructor = AxesHelper;
36+
}
3637

3738

3839
export { AxesHelper };

src/helpers/Box3Helper.js

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,49 @@ import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
33
import { BufferAttribute } from '../core/BufferAttribute.js';
44
import { Float32BufferAttribute } from '../core/BufferAttribute.js';
55
import { BufferGeometry } from '../core/BufferGeometry.js';
6-
import { Object3D } from '../core/Object3D.js';
76

8-
function Box3Helper( box, color ) {
7+
class Box3Helper extends LineSegments {
98

10-
this.type = 'Box3Helper';
9+
constructor( box, color ) {
1110

12-
this.box = box;
11+
if ( color === undefined ) color = 0xffff00;
1312

14-
if ( color === undefined ) color = 0xffff00;
13+
const indices = new Uint16Array( [ 0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7 ] );
1514

16-
const indices = new Uint16Array( [ 0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7 ] );
15+
const positions = [ 1, 1, 1, - 1, 1, 1, - 1, - 1, 1, 1, - 1, 1, 1, 1, - 1, - 1, 1, - 1, - 1, - 1, - 1, 1, - 1, - 1 ];
1716

18-
const positions = [ 1, 1, 1, - 1, 1, 1, - 1, - 1, 1, 1, - 1, 1, 1, 1, - 1, - 1, 1, - 1, - 1, - 1, - 1, 1, - 1, - 1 ];
17+
const geometry = new BufferGeometry();
1918

20-
const geometry = new BufferGeometry();
19+
geometry.setIndex( new BufferAttribute( indices, 1 ) );
2120

22-
geometry.setIndex( new BufferAttribute( indices, 1 ) );
21+
geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
2322

24-
geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
23+
super( geometry, new LineBasicMaterial( { color: color, toneMapped: false } ) );
2524

26-
LineSegments.call( this, geometry, new LineBasicMaterial( { color: color, toneMapped: false } ) );
25+
this.box = box;
2726

28-
this.type = 'Box3Helper';
27+
this.type = 'Box3Helper';
2928

30-
this.geometry.computeBoundingSphere();
29+
this.geometry.computeBoundingSphere();
3130

32-
}
31+
}
3332

34-
Box3Helper.prototype = Object.create( LineSegments.prototype );
35-
Box3Helper.prototype.constructor = Box3Helper;
33+
updateMatrixWorld( force ) {
3634

37-
Box3Helper.prototype.updateMatrixWorld = function ( force ) {
35+
const box = this.box;
3836

39-
const box = this.box;
37+
if ( box.isEmpty() ) return;
4038

41-
if ( box.isEmpty() ) return;
39+
box.getCenter( this.position );
4240

43-
box.getCenter( this.position );
41+
box.getSize( this.scale );
4442

45-
box.getSize( this.scale );
43+
this.scale.multiplyScalar( 0.5 );
4644

47-
this.scale.multiplyScalar( 0.5 );
45+
super.updateMatrixWorld( force );
4846

49-
Object3D.prototype.updateMatrixWorld.call( this, force );
47+
}
5048

51-
};
49+
}
5250

5351
export { Box3Helper };

0 commit comments

Comments
 (0)