2
0

ArrowHelper.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * @author WestLangley / http://github.com/WestLangley
  3. * @author zz85 / https://github.com/zz85
  4. *
  5. * Creates an arrow for visualizing directions
  6. *
  7. * Parameters:
  8. * dir - Vector3
  9. * origin - Vector3
  10. * length - Number
  11. * hex - color in hex value
  12. */
  13. THREE.ArrowHelper = function ( dir, origin, length, hex ) {
  14. THREE.Object3D.call( this );
  15. if ( hex === undefined ) hex = 0xffff00;
  16. if ( length === undefined ) length = 20;
  17. var lineGeometry = new THREE.Geometry();
  18. lineGeometry.vertices.push( new THREE.Vector3( 0, 0, 0 ) );
  19. lineGeometry.vertices.push( new THREE.Vector3( 0, 1, 0 ) );
  20. this.line = new THREE.Line( lineGeometry, new THREE.LineBasicMaterial( { color: hex } ) );
  21. this.add( this.line );
  22. var coneGeometry = new THREE.CylinderGeometry( 0, 0.05, 0.25, 5, 1 );
  23. this.cone = new THREE.Mesh( coneGeometry, new THREE.MeshBasicMaterial( { color: hex } ) );
  24. this.cone.position.set( 0, 1, 0 );
  25. this.add( this.cone );
  26. if ( origin instanceof THREE.Vector3 ) this.position = origin;
  27. this.setDirection( dir );
  28. this.setLength( length );
  29. };
  30. THREE.ArrowHelper.prototype = Object.create( THREE.Object3D.prototype );
  31. THREE.ArrowHelper.prototype.setDirection = function ( dir ) {
  32. var axis = new THREE.Vector3( 0, 1, 0 ).crossSelf( dir );
  33. var radians = Math.acos( new THREE.Vector3( 0, 1, 0 ).dot( dir.clone().normalize() ) );
  34. this.matrix = new THREE.Matrix4().makeRotationAxis( axis.normalize(), radians );
  35. this.rotation.setEulerFromRotationMatrix( this.matrix, this.eulerOrder );
  36. };
  37. THREE.ArrowHelper.prototype.setLength = function ( length ) {
  38. this.scale.set( length, length, length );
  39. };
  40. THREE.ArrowHelper.prototype.setColor = function ( hex ) {
  41. this.line.material.color.setHex( hex );
  42. this.cone.material.color.setHex( hex );
  43. };