UNPKG

2.59 kBJavaScriptView Raw
1import { Float32BufferAttribute } from '../core/BufferAttribute.js';
2import { BufferGeometry } from '../core/BufferGeometry.js';
3import { Object3D } from '../core/Object3D.js';
4import { CylinderGeometry } from '../geometries/CylinderGeometry.js';
5import { MeshBasicMaterial } from '../materials/MeshBasicMaterial.js';
6import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
7import { Mesh } from '../objects/Mesh.js';
8import { Line } from '../objects/Line.js';
9import { Vector3 } from '../math/Vector3.js';
10
11const _axis = /*@__PURE__*/ new Vector3();
12let _lineGeometry, _coneGeometry;
13
14class ArrowHelper extends Object3D {
15
16 // dir is assumed to be normalized
17
18 constructor( dir = new Vector3( 0, 0, 1 ), origin = new Vector3( 0, 0, 0 ), length = 1, color = 0xffff00, headLength = length * 0.2, headWidth = headLength * 0.2 ) {
19
20 super();
21
22 this.type = 'ArrowHelper';
23
24 if ( _lineGeometry === undefined ) {
25
26 _lineGeometry = new BufferGeometry();
27 _lineGeometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 0, 1, 0 ], 3 ) );
28
29 _coneGeometry = new CylinderGeometry( 0, 0.5, 1, 5, 1 );
30 _coneGeometry.translate( 0, - 0.5, 0 );
31
32 }
33
34 this.position.copy( origin );
35
36 this.line = new Line( _lineGeometry, new LineBasicMaterial( { color: color, toneMapped: false } ) );
37 this.line.matrixAutoUpdate = false;
38 this.add( this.line );
39
40 this.cone = new Mesh( _coneGeometry, new MeshBasicMaterial( { color: color, toneMapped: false } ) );
41 this.cone.matrixAutoUpdate = false;
42 this.add( this.cone );
43
44 this.setDirection( dir );
45 this.setLength( length, headLength, headWidth );
46
47 }
48
49 setDirection( dir ) {
50
51 // dir is assumed to be normalized
52
53 if ( dir.y > 0.99999 ) {
54
55 this.quaternion.set( 0, 0, 0, 1 );
56
57 } else if ( dir.y < - 0.99999 ) {
58
59 this.quaternion.set( 1, 0, 0, 0 );
60
61 } else {
62
63 _axis.set( dir.z, 0, - dir.x ).normalize();
64
65 const radians = Math.acos( dir.y );
66
67 this.quaternion.setFromAxisAngle( _axis, radians );
68
69 }
70
71 }
72
73 setLength( length, headLength = length * 0.2, headWidth = headLength * 0.2 ) {
74
75 this.line.scale.set( 1, Math.max( 0.0001, length - headLength ), 1 ); // see #17458
76 this.line.updateMatrix();
77
78 this.cone.scale.set( headWidth, headLength, headWidth );
79 this.cone.position.y = length;
80 this.cone.updateMatrix();
81
82 }
83
84 setColor( color ) {
85
86 this.line.material.color.set( color );
87 this.cone.material.color.set( color );
88
89 }
90
91 copy( source ) {
92
93 super.copy( source, false );
94
95 this.line.copy( source.line );
96 this.cone.copy( source.cone );
97
98 return this;
99
100 }
101
102}
103
104
105export { ArrowHelper };