ArrowHelper.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * @author WestLangley / https://github.com/WestLangley
  3. * @author zz85 / https://github.com/zz85
  4. */
  5. THREE.ArrowHelper = function ( origin, dir, length, hex ) {
  6. THREE.Object3D.call( this );
  7. var lineGeometry = new THREE.Geometry();
  8. lineGeometry.vertices.push( new THREE.Vertex() );
  9. lineGeometry.vertices.push( new THREE.Vertex( new THREE.Vector3( 0, 1, 0 ) ) );
  10. this.line = new THREE.Line( lineGeometry, new THREE.LineBasicMaterial( { color : hex } ) );
  11. this.add( this.line );
  12. var coneGeometry = new THREE.CylinderGeometry( 0, 0.05, 0.25, 5, 1 );
  13. this.cone = new THREE.Mesh( coneGeometry, new THREE.MeshBasicMaterial( { color : hex } ) );
  14. this.cone.position.set( 0, 1, 0 );
  15. this.add( this.cone );
  16. this.position = origin;
  17. this.setDirection( dir );
  18. this.setLength( length );
  19. };
  20. THREE.ArrowHelper.prototype = new THREE.Object3D();
  21. THREE.ArrowHelper.prototype.constructor = THREE.ArrowHelper;
  22. THREE.ArrowHelper.prototype.setDirection = function( dir ) {
  23. var axis = new THREE.Vector3( 0, 1, 0 ).crossSelf( dir );
  24. var radians = Math.acos( new THREE.Vector3( 0, 1, 0 ).dot( dir.clone().normalize() ) );
  25. this.matrix = new THREE.Matrix4().setRotationAxis( axis.normalize(), radians );
  26. this.rotation.getRotationFromMatrix( this.matrix, this.scale );
  27. };
  28. THREE.ArrowHelper.prototype.setLength = function( length ) {
  29. this.scale.set( length, length, length );
  30. };
  31. THREE.ArrowHelper.prototype.setColor = function( hex ) {
  32. this.line.material.color.setHex( hex );
  33. this.cone.material.color.setHex( hex );
  34. };