ArrowHelper.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * @author WestLangley / http://github.com/WestLangley
  3. * @author zz85 / http://github.com/zz85
  4. * @author bhouston / http://clara.io
  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. import { Vector3 } from '../math/Vector3';
  17. import { Object3D } from '../core/Object3D';
  18. import { CylinderBufferGeometry } from '../geometries/CylinderBufferGeometry';
  19. import { Float32BufferAttribute } from '../core/BufferAttribute';
  20. import { BufferGeometry } from '../core/BufferGeometry';
  21. import { MeshBasicMaterial } from '../materials/MeshBasicMaterial';
  22. import { Mesh } from '../objects/Mesh';
  23. import { LineBasicMaterial } from '../materials/LineBasicMaterial';
  24. import { Line } from '../objects/Line';
  25. var lineGeometry = new BufferGeometry();
  26. lineGeometry.addAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 0, 1, 0 ], 3 ) );
  27. var coneGeometry = new CylinderBufferGeometry( 0, 0.5, 1, 5, 1 );
  28. coneGeometry.translate( 0, - 0.5, 0 );
  29. function ArrowHelper( dir, origin, length, color, headLength, headWidth ) {
  30. // dir is assumed to be normalized
  31. Object3D.call( this );
  32. if ( color === undefined ) color = 0xffff00;
  33. if ( length === undefined ) length = 1;
  34. if ( headLength === undefined ) headLength = 0.2 * length;
  35. if ( headWidth === undefined ) headWidth = 0.2 * headLength;
  36. this.position.copy( origin );
  37. this.line = new Line( lineGeometry, new LineBasicMaterial( { color: color } ) );
  38. this.line.matrixAutoUpdate = false;
  39. this.add( this.line );
  40. this.cone = new Mesh( coneGeometry, new MeshBasicMaterial( { color: color } ) );
  41. this.cone.matrixAutoUpdate = false;
  42. this.add( this.cone );
  43. this.setDirection( dir );
  44. this.setLength( length, headLength, headWidth );
  45. }
  46. ArrowHelper.prototype = Object.create( Object3D.prototype );
  47. ArrowHelper.prototype.constructor = ArrowHelper;
  48. ArrowHelper.prototype.setDirection = ( function () {
  49. var axis = new Vector3();
  50. var radians;
  51. return function setDirection( dir ) {
  52. // dir is assumed to be normalized
  53. if ( dir.y > 0.99999 ) {
  54. this.quaternion.set( 0, 0, 0, 1 );
  55. } else if ( dir.y < - 0.99999 ) {
  56. this.quaternion.set( 1, 0, 0, 0 );
  57. } else {
  58. axis.set( dir.z, 0, - dir.x ).normalize();
  59. radians = Math.acos( dir.y );
  60. this.quaternion.setFromAxisAngle( axis, radians );
  61. }
  62. };
  63. }() );
  64. ArrowHelper.prototype.setLength = function ( length, headLength, headWidth ) {
  65. if ( headLength === undefined ) headLength = 0.2 * length;
  66. if ( headWidth === undefined ) headWidth = 0.2 * headLength;
  67. this.line.scale.set( 1, Math.max( 0, length - headLength ), 1 );
  68. this.line.updateMatrix();
  69. this.cone.scale.set( headWidth, headLength, headWidth );
  70. this.cone.position.y = length;
  71. this.cone.updateMatrix();
  72. };
  73. ArrowHelper.prototype.setColor = function ( color ) {
  74. this.line.material.color.copy( color );
  75. this.cone.material.color.copy( color );
  76. };
  77. export { ArrowHelper };