ArrowHelper.js 2.3 KB
Newer Older
W
WestLangley 已提交
1
/**
W
WestLangley 已提交
2
 * @author WestLangley / http://github.com/WestLangley
W
WestLangley 已提交
3
 * @author zz85 / https://github.com/zz85
4
 * @author bhouston / https://exocortex.com
5 6 7 8 9 10 11 12
 *
 * Creates an arrow for visualizing directions
 *
 * Parameters:
 *  dir - Vector3
 *  origin - Vector3
 *  length - Number
 *  hex - color in hex value
W
WestLangley 已提交
13
 */
A
alteredq 已提交
14

15
THREE.ArrowHelper = function ( dir, origin, length, hex ) {
W
WestLangley 已提交
16

M
Mr.doob 已提交
17
	THREE.Object3D.call( this );
W
WestLangley 已提交
18

M
Mr.doob 已提交
19 20
	if ( hex === undefined ) hex = 0xffff00;
	if ( length === undefined ) length = 20;
21

M
Mr.doob 已提交
22 23 24
	var lineGeometry = new THREE.Geometry();
	lineGeometry.vertices.push( new THREE.Vector3( 0, 0, 0 ) );
	lineGeometry.vertices.push( new THREE.Vector3( 0, 1, 0 ) );
A
alteredq 已提交
25

M
Mr.doob 已提交
26 27
	this.line = new THREE.Line( lineGeometry, new THREE.LineBasicMaterial( { color: hex } ) );
	this.add( this.line );
A
alteredq 已提交
28

M
Mr.doob 已提交
29
	var coneGeometry = new THREE.CylinderGeometry( 0, 0.05, 0.25, 5, 1 );
A
alteredq 已提交
30

M
Mr.doob 已提交
31 32 33
	this.cone = new THREE.Mesh( coneGeometry, new THREE.MeshBasicMaterial( { color: hex } ) );
	this.cone.position.set( 0, 1, 0 );
	this.add( this.cone );
A
alteredq 已提交
34

M
Mr.doob 已提交
35
	if ( origin instanceof THREE.Vector3 ) this.position = origin;
A
alteredq 已提交
36

M
Mr.doob 已提交
37 38
	this.setDirection( dir );
	this.setLength( length );
W
WestLangley 已提交
39 40 41

};

42
THREE.ArrowHelper.prototype = Object.create( THREE.Object3D.prototype );
W
WestLangley 已提交
43

M
Mr.doob 已提交
44
THREE.ArrowHelper.prototype.setDirection = function ( dir ) {
W
WestLangley 已提交
45

46
    var d = dir.clone().normalize();
A
alteredq 已提交
47

48
    if ( d.y > 0.99999 ) {
A
alteredq 已提交
49

50 51 52
        this.rotation.set( 0, 0, 0 );
 
    }
A
alteredq 已提交
53

54
    else if ( d.y < - 0.99999 ) {
55

56 57
        this.rotation.set( Math.PI, 0, 0 );
        return;
58

59
    }
60

61
    else {
62

63
	    var axis = THREE.ArrowHelper.__v1.set( d.z, 0, - d.x );
64

65
	    var radians = Math.acos( d.y );
66

67 68 69 70 71
	    var matrix = THREE.ArrowHelper.__m1.makeRotationAxis( axis.normalize(), radians );

	    this.rotation.setEulerFromRotationMatrix( matrix, this.eulerOrder );

	}
A
alteredq 已提交
72

W
WestLangley 已提交
73 74
};

75 76
THREE.ArrowHelper.__v1 = new THREE.Vector3();
THREE.ArrowHelper.__m1 = new THREE.Matrix4();
M
Mr.doob 已提交
77
THREE.ArrowHelper.prototype.setLength = function ( length ) {
W
WestLangley 已提交
78

M
Mr.doob 已提交
79
	this.scale.set( length, length, length );
W
WestLangley 已提交
80 81 82

};

M
Mr.doob 已提交
83
THREE.ArrowHelper.prototype.setColor = function ( hex ) {
W
WestLangley 已提交
84

M
Mr.doob 已提交
85 86
	this.line.material.color.setHex( hex );
	this.cone.material.color.setHex( hex );
W
WestLangley 已提交
87 88

};
89 90 91 92 93

THREE.ArrowHelper.__m0 = new THREE.Matrix4();
THREE.ArrowHelper.__v0 = new THREE.Vector3();
THREE.ArrowHelper.__vYAxis = new THREE.Vector3( 0, 1, 0 );
THREE.ArrowHelper.__vYAxisNeg = new THREE.Vector3( 0, -1, 0 );