ArrowHelper.js 3.4 KB

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