ArrowHelper.js 3.4 KB

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