@@ -11,103 +11,104 @@ import { Vector3 } from '../math/Vector3.js';
1111const _axis = new Vector3 ( ) ;
1212let _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
113114export { ArrowHelper } ;
0 commit comments