ArrowHelper.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * @author WestLangley / http://github.com/WestLangley
  3. * @author zz85 / https://github.com/zz85
  4. * @author bhouston / https://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. */
  14. THREE.ArrowHelper = function ( dir, origin, length, hex ) {
  15. THREE.Object3D.call( this );
  16. if ( hex === undefined ) hex = 0xffff00;
  17. if ( length === undefined ) length = 20;
  18. var lineGeometry = new THREE.Geometry();
  19. lineGeometry.vertices.push( new THREE.Vector3( 0, 0, 0 ) );
  20. lineGeometry.vertices.push( new THREE.Vector3( 0, 1, 0 ) );
  21. this.line = new THREE.Line( lineGeometry, new THREE.LineBasicMaterial( { color: hex } ) );
  22. this.add( this.line );
  23. var coneGeometry = new THREE.CylinderGeometry( 0, 0.05, 0.25, 5, 1 );
  24. this.cone = new THREE.Mesh( coneGeometry, new THREE.MeshBasicMaterial( { color: hex } ) );
  25. this.cone.position.set( 0, 1, 0 );
  26. this.add( this.cone );
  27. if ( origin instanceof THREE.Vector3 ) this.position = origin;
  28. this.setDirection( dir );
  29. this.setLength( length );
  30. };
  31. THREE.ArrowHelper.prototype = Object.create( THREE.Object3D.prototype );
  32. THREE.ArrowHelper.prototype.setDirection = function ( dir ) {
  33. var d = THREE.ArrowHelper.__v1.copy( dir ).normalize();
  34. if ( d.y > 0.99999 ) {
  35. this.rotation.set( 0, 0, 0 );
  36. }
  37. else if ( d.y < - 0.99999 ) {
  38. this.rotation.set( Math.PI, 0, 0 );
  39. return;
  40. }
  41. else {
  42. var axis = THREE.ArrowHelper.__v2.set( d.z, 0, - d.x );
  43. var radians = Math.acos( d.y );
  44. var quat = THREE.ArrowHelper.__q1.setFromAxisAngle( axis.normalize(), radians );
  45. this.rotation.setEulerFromQuaternion( quat, this.eulerOrder );
  46. }
  47. };
  48. THREE.ArrowHelper.prototype.setLength = function ( length ) {
  49. this.scale.set( length, length, length );
  50. };
  51. THREE.ArrowHelper.prototype.setColor = function ( hex ) {
  52. this.line.material.color.setHex( hex );
  53. this.cone.material.color.setHex( hex );
  54. };
  55. THREE.ArrowHelper.__v1 = new THREE.Vector3();
  56. THREE.ArrowHelper.__v2 = new THREE.Vector3();
  57. THREE.ArrowHelper.__q1 = new THREE.Quaternion();