Sprite.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { Vector2 } from '../math/Vector2.js';
  2. import { Vector3 } from '../math/Vector3.js';
  3. import { Object3D } from '../core/Object3D.js';
  4. import { SpriteMaterial } from '../materials/SpriteMaterial.js';
  5. /**
  6. * @author mikael emtinger / http://gomo.se/
  7. * @author alteredq / http://alteredqualia.com/
  8. */
  9. function Sprite( material ) {
  10. Object3D.call( this );
  11. this.type = 'Sprite';
  12. this.material = ( material !== undefined ) ? material : new SpriteMaterial();
  13. this.center = new Vector2( 0.5, 0.5 );
  14. }
  15. Sprite.prototype = Object.assign( Object.create( Object3D.prototype ), {
  16. constructor: Sprite,
  17. isSprite: true,
  18. raycast: ( function () {
  19. var intersectPoint = new Vector3();
  20. var worldPosition = new Vector3();
  21. var worldScale = new Vector3();
  22. return function raycast( raycaster, intersects ) {
  23. worldPosition.setFromMatrixPosition( this.matrixWorld );
  24. raycaster.ray.closestPointToPoint( worldPosition, intersectPoint );
  25. worldScale.setFromMatrixScale( this.matrixWorld );
  26. var guessSizeSq = worldScale.x * worldScale.y / 4;
  27. if ( worldPosition.distanceToSquared( intersectPoint ) > guessSizeSq ) return;
  28. var distance = raycaster.ray.origin.distanceTo( intersectPoint );
  29. if ( distance < raycaster.near || distance > raycaster.far ) return;
  30. intersects.push( {
  31. distance: distance,
  32. point: intersectPoint.clone(),
  33. face: null,
  34. object: this
  35. } );
  36. };
  37. }() ),
  38. clone: function () {
  39. return new this.constructor( this.material ).copy( this );
  40. },
  41. copy: function ( source ) {
  42. Object3D.prototype.copy.call( this, source );
  43. if ( source.center !== undefined ) this.center.copy( source.center );
  44. return this;
  45. }
  46. } );
  47. export { Sprite };