1 | /**
|
---|
2 | * @author WestLangley / http://github.com/WestLangley
|
---|
3 | * @author zz85 / http://github.com/zz85
|
---|
4 | * @author bhouston / http://exocortex.com
|
---|
5 | *
|
---|
6 | * Creates an arrow for visualizing directions
|
---|
7 | *
|
---|
8 | * Parameters:
|
---|
9 | * dir - Vector3
|
---|
10 | * origin - Vector3
|
---|
11 | * length - Number
|
---|
12 | * hex - color in hex value
|
---|
13 | * headLength - Number
|
---|
14 | * headWidth - Number
|
---|
15 | */
|
---|
16 |
|
---|
17 | THREE.ArrowHelper = function ( dir, origin, length, hex, headLength, headWidth ) {
|
---|
18 |
|
---|
19 | // dir is assumed to be normalized
|
---|
20 |
|
---|
21 | THREE.Object3D.call( this );
|
---|
22 |
|
---|
23 | if ( hex === undefined ) hex = 0xffff00;
|
---|
24 | if ( length === undefined ) length = 1;
|
---|
25 | if ( headLength === undefined ) headLength = 0.2 * length;
|
---|
26 | if ( headWidth === undefined ) headWidth = 0.2 * headLength;
|
---|
27 |
|
---|
28 | this.position = origin;
|
---|
29 |
|
---|
30 | var lineGeometry = new THREE.Geometry();
|
---|
31 | lineGeometry.vertices.push( new THREE.Vector3( 0, 0, 0 ) );
|
---|
32 | lineGeometry.vertices.push( new THREE.Vector3( 0, 1, 0 ) );
|
---|
33 |
|
---|
34 | this.line = new THREE.Line( lineGeometry, new THREE.LineBasicMaterial( { color: hex } ) );
|
---|
35 | this.line.matrixAutoUpdate = false;
|
---|
36 | this.add( this.line );
|
---|
37 |
|
---|
38 | var coneGeometry = new THREE.CylinderGeometry( 0, 0.5, 1, 5, 1 );
|
---|
39 | coneGeometry.applyMatrix( new THREE.Matrix4().makeTranslation( 0, - 0.5, 0 ) );
|
---|
40 |
|
---|
41 | this.cone = new THREE.Mesh( coneGeometry, new THREE.MeshBasicMaterial( { color: hex } ) );
|
---|
42 | this.cone.matrixAutoUpdate = false;
|
---|
43 | this.add( this.cone );
|
---|
44 |
|
---|
45 | this.setDirection( dir );
|
---|
46 | this.setLength( length, headLength, headWidth );
|
---|
47 |
|
---|
48 | };
|
---|
49 |
|
---|
50 | THREE.ArrowHelper.prototype = Object.create( THREE.Object3D.prototype );
|
---|
51 |
|
---|
52 | THREE.ArrowHelper.prototype.setDirection = function () {
|
---|
53 |
|
---|
54 | var axis = new THREE.Vector3();
|
---|
55 | var radians;
|
---|
56 |
|
---|
57 | return function ( dir ) {
|
---|
58 |
|
---|
59 | // dir is assumed to be normalized
|
---|
60 |
|
---|
61 | if ( dir.y > 0.99999 ) {
|
---|
62 |
|
---|
63 | this.quaternion.set( 0, 0, 0, 1 );
|
---|
64 |
|
---|
65 | } else if ( dir.y < - 0.99999 ) {
|
---|
66 |
|
---|
67 | this.quaternion.set( 1, 0, 0, 0 );
|
---|
68 |
|
---|
69 | } else {
|
---|
70 |
|
---|
71 | axis.set( dir.z, 0, - dir.x ).normalize();
|
---|
72 |
|
---|
73 | radians = Math.acos( dir.y );
|
---|
74 |
|
---|
75 | this.quaternion.setFromAxisAngle( axis, radians );
|
---|
76 |
|
---|
77 | }
|
---|
78 |
|
---|
79 | };
|
---|
80 |
|
---|
81 | }();
|
---|
82 |
|
---|
83 | THREE.ArrowHelper.prototype.setLength = function ( length, headLength, headWidth ) {
|
---|
84 |
|
---|
85 | if ( headLength === undefined ) headLength = 0.2 * length;
|
---|
86 | if ( headWidth === undefined ) headWidth = 0.2 * headLength;
|
---|
87 |
|
---|
88 | this.line.scale.set( 1, length, 1 );
|
---|
89 | this.line.updateMatrix();
|
---|
90 |
|
---|
91 | this.cone.scale.set( headWidth, headLength, headWidth );
|
---|
92 | this.cone.position.y = length;
|
---|
93 | this.cone.updateMatrix();
|
---|
94 |
|
---|
95 | };
|
---|
96 |
|
---|
97 | THREE.ArrowHelper.prototype.setColor = function ( hex ) {
|
---|
98 |
|
---|
99 | this.line.material.color.setHex( hex );
|
---|
100 | this.cone.material.color.setHex( hex );
|
---|
101 |
|
---|
102 | };
|
---|