ArrowHelper.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. * color - color in hex value
  13. * headLength - Number
  14. * headWidth - Number
  15. */
  16. THREE.ArrowHelper = ( function () {
  17. var lineGeometry = new THREE.Geometry();
  18. lineGeometry.vertices.push( new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 1, 0 ) );
  19. var coneGeometry = new THREE.CylinderGeometry( 0, 0.5, 1, 5, 1 );
  20. coneGeometry.applyMatrix( new THREE.Matrix4().makeTranslation( 0, - 0.5, 0 ) );
  21. return function ( dir, origin, length, color, headLength, headWidth ) {
  22. // dir is assumed to be normalized
  23. THREE.Object3D.call( this );
  24. if ( color === undefined ) color = 0xffff00;
  25. if ( length === undefined ) length = 1;
  26. if ( headLength === undefined ) headLength = 0.2 * length;
  27. if ( headWidth === undefined ) headWidth = 0.2 * headLength;
  28. this.position.copy( origin );
  29. this.line = new THREE.Line( lineGeometry, new THREE.LineBasicMaterial( { color: color } ) );
  30. this.line.matrixAutoUpdate = false;
  31. this.add( this.line );
  32. this.cone = new THREE.Mesh( coneGeometry, new THREE.MeshBasicMaterial( { color: color } ) );
  33. this.cone.matrixAutoUpdate = false;
  34. this.add( this.cone );
  35. this.setDirection( dir );
  36. this.setLength( length, headLength, headWidth );
  37. }
  38. }() );
  39. THREE.ArrowHelper.prototype = Object.create( THREE.Object3D.prototype );
  40. THREE.ArrowHelper.prototype.constructor = THREE.ArrowHelper;
  41. THREE.ArrowHelper.prototype.setDirection = ( function () {
  42. var axis = new THREE.Vector3();
  43. var radians;
  44. return function ( dir ) {
  45. // dir is assumed to be normalized
  46. if ( dir.y > 0.99999 ) {
  47. this.quaternion.set( 0, 0, 0, 1 );
  48. } else if ( dir.y < - 0.99999 ) {
  49. this.quaternion.set( 1, 0, 0, 0 );
  50. } else {
  51. axis.set( dir.z, 0, - dir.x ).normalize();
  52. radians = Math.acos( dir.y );
  53. this.quaternion.setFromAxisAngle( axis, radians );
  54. }
  55. };
  56. }() );
  57. THREE.ArrowHelper.prototype.setLength = function ( length, headLength, headWidth ) {
  58. if ( headLength === undefined ) headLength = 0.2 * length;
  59. if ( headWidth === undefined ) headWidth = 0.2 * headLength;
  60. this.line.scale.set( 1, length - headLength, 1 );
  61. this.line.updateMatrix();
  62. this.cone.scale.set( headWidth, headLength, headWidth );
  63. this.cone.position.y = length;
  64. this.cone.updateMatrix();
  65. };
  66. THREE.ArrowHelper.prototype.setColor = function ( color ) {
  67. this.line.material.color.set( color );
  68. this.cone.material.color.set( color );
  69. };