ArrowHelper.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { Float32BufferAttribute } from '../core/BufferAttribute.js';
  2. import { BufferGeometry } from '../core/BufferGeometry.js';
  3. import { Object3D } from '../core/Object3D.js';
  4. import { CylinderBufferGeometry } from '../geometries/CylinderGeometry.js';
  5. import { MeshBasicMaterial } from '../materials/MeshBasicMaterial.js';
  6. import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
  7. import { Mesh } from '../objects/Mesh.js';
  8. import { Line } from '../objects/Line.js';
  9. import { Vector3 } from '../math/Vector3.js';
  10. const _axis = new Vector3();
  11. let _lineGeometry, _coneGeometry;
  12. class ArrowHelper extends Object3D {
  13. constructor( dir, origin, length, color, headLength, headWidth ) {
  14. super();
  15. // dir is assumed to be normalized
  16. this.type = 'ArrowHelper';
  17. if ( dir === undefined ) dir = new Vector3( 0, 0, 1 );
  18. if ( origin === undefined ) origin = new Vector3( 0, 0, 0 );
  19. if ( length === undefined ) length = 1;
  20. if ( color === undefined ) color = 0xffff00;
  21. if ( headLength === undefined ) headLength = 0.2 * length;
  22. if ( headWidth === undefined ) headWidth = 0.2 * headLength;
  23. if ( _lineGeometry === undefined ) {
  24. _lineGeometry = new BufferGeometry();
  25. _lineGeometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 0, 1, 0 ], 3 ) );
  26. _coneGeometry = new CylinderBufferGeometry( 0, 0.5, 1, 5, 1 );
  27. _coneGeometry.translate( 0, - 0.5, 0 );
  28. }
  29. this.position.copy( origin );
  30. this.line = new Line( _lineGeometry, new LineBasicMaterial( { color: color, toneMapped: false } ) );
  31. this.line.matrixAutoUpdate = false;
  32. this.add( this.line );
  33. this.cone = new Mesh( _coneGeometry, new MeshBasicMaterial( { color: color, toneMapped: false } ) );
  34. this.cone.matrixAutoUpdate = false;
  35. this.add( this.cone );
  36. this.setDirection( dir );
  37. this.setLength( length, headLength, headWidth );
  38. }
  39. setDirection( dir ) {
  40. // dir is assumed to be normalized
  41. if ( dir.y > 0.99999 ) {
  42. this.quaternion.set( 0, 0, 0, 1 );
  43. } else if ( dir.y < - 0.99999 ) {
  44. this.quaternion.set( 1, 0, 0, 0 );
  45. } else {
  46. _axis.set( dir.z, 0, - dir.x ).normalize();
  47. const radians = Math.acos( dir.y );
  48. this.quaternion.setFromAxisAngle( _axis, radians );
  49. }
  50. }
  51. setLength( length, headLength, headWidth ) {
  52. if ( headLength === undefined ) headLength = 0.2 * length;
  53. if ( headWidth === undefined ) headWidth = 0.2 * headLength;
  54. this.line.scale.set( 1, Math.max( 0.0001, length - headLength ), 1 ); // see #17458
  55. this.line.updateMatrix();
  56. this.cone.scale.set( headWidth, headLength, headWidth );
  57. this.cone.position.y = length;
  58. this.cone.updateMatrix();
  59. }
  60. setColor( color ) {
  61. this.line.material.color.set( color );
  62. this.cone.material.color.set( color );
  63. }
  64. copy( source ) {
  65. super.copy( source, false );
  66. this.line.copy( source.line );
  67. this.cone.copy( source.cone );
  68. return this;
  69. }
  70. }
  71. export { ArrowHelper };
粤ICP备19079148号