ArrowHelper.js 1.7 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 5 6 7 8 9 10 11
 *
 * Creates an arrow for visualizing directions
 *
 * Parameters:
 *  dir - Vector3
 *  origin - Vector3
 *  length - Number
 *  hex - color in hex value
W
WestLangley 已提交
12
 */
A
alteredq 已提交
13

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

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

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

M
Mr.doob 已提交
21 22 23
	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 已提交
24

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

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

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

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

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

};

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

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

M
Mr.doob 已提交
45
	var axis = new THREE.Vector3( 0, 1, 0 ).crossSelf( dir );
A
alteredq 已提交
46

M
Mr.doob 已提交
47
	var radians = Math.acos( new THREE.Vector3( 0, 1, 0 ).dot( dir.clone().normalize() ) );
A
alteredq 已提交
48

M
Mr.doob 已提交
49
	this.matrix = new THREE.Matrix4().makeRotationAxis( axis.normalize(), radians );
A
alteredq 已提交
50

W
WestLangley 已提交
51
	this.rotation.setEulerFromRotationMatrix( this.matrix, this.eulerOrder );
A
alteredq 已提交
52

W
WestLangley 已提交
53 54
};

M
Mr.doob 已提交
55
THREE.ArrowHelper.prototype.setLength = function ( length ) {
W
WestLangley 已提交
56

M
Mr.doob 已提交
57
	this.scale.set( length, length, length );
W
WestLangley 已提交
58 59 60

};

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

M
Mr.doob 已提交
63 64
	this.line.material.color.setHex( hex );
	this.cone.material.color.setHex( hex );
W
WestLangley 已提交
65 66

};