Sprite.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { Vector2 } from '../math/Vector2.js';
  2. import { Vector3 } from '../math/Vector3.js';
  3. import { Matrix4 } from '../math/Matrix4.js';
  4. import { Object3D } from '../core/Object3D.js';
  5. import { SpriteMaterial } from '../materials/SpriteMaterial.js';
  6. /**
  7. * @author mikael emtinger / http://gomo.se/
  8. * @author alteredq / http://alteredqualia.com/
  9. */
  10. function Sprite( material ) {
  11. Object3D.call( this );
  12. this.type = 'Sprite';
  13. this.material = ( material !== undefined ) ? material : new SpriteMaterial();
  14. this.center = new Vector2( 0.5, 0.5 );
  15. }
  16. Sprite.prototype = Object.assign( Object.create( Object3D.prototype ), {
  17. constructor: Sprite,
  18. isSprite: true,
  19. raycast: ( function () {
  20. var intersectPoint = new Vector3();
  21. var worldScale = new Vector3();
  22. var mvPosition = new Vector3();
  23. var alignedPosition = new Vector2();
  24. var rotatedPosition = new Vector2();
  25. var viewWorldMatrix = new Matrix4();
  26. var vA = new Vector3();
  27. var vB = new Vector3();
  28. var vC = new Vector3();
  29. function transformVertex( vertexPosition, mvPosition, center, scale, sin, cos ) {
  30. // compute position in camera space
  31. alignedPosition.set( ( vertexPosition.x - center.x ) * scale.x, ( vertexPosition.y - center.y ) * scale.y );
  32. if ( sin !== undefined ) {
  33. rotatedPosition.x = ( cos * alignedPosition.x ) - ( sin * alignedPosition.y );
  34. rotatedPosition.y = ( sin * alignedPosition.x ) + ( cos * alignedPosition.y );
  35. } else {
  36. rotatedPosition.copy( alignedPosition );
  37. }
  38. vertexPosition.copy( mvPosition );
  39. vertexPosition.x += rotatedPosition.x;
  40. vertexPosition.y += rotatedPosition.y;
  41. // transform to world space
  42. vertexPosition.applyMatrix4( viewWorldMatrix );
  43. }
  44. return function raycast( raycaster, intersects ) {
  45. worldScale.setFromMatrixScale( this.matrixWorld );
  46. viewWorldMatrix.getInverse( this.modelViewMatrix ).premultiply( this.matrixWorld );
  47. mvPosition.setFromMatrixPosition( this.modelViewMatrix );
  48. var rotation = this.material.rotation;
  49. var sin, cos;
  50. if ( rotation !== 0 ) {
  51. cos = Math.cos( rotation );
  52. sin = Math.sin( rotation );
  53. }
  54. var center = this.center;
  55. transformVertex( vA.set( 0, 0, 1 ), mvPosition, center, worldScale, sin, cos );
  56. transformVertex( vB.set( 0, 1, 1 ), mvPosition, center, worldScale, sin, cos );
  57. transformVertex( vC.set( 1, 0, 1 ), mvPosition, center, worldScale, sin, cos );
  58. // check first triangle
  59. var intersect = raycaster.ray.intersectTriangle( vA, vB, vC, false, intersectPoint );
  60. if ( intersect === null ) {
  61. // check second triangle
  62. transformVertex( vA.set( 1, 1, 1 ), mvPosition, center, worldScale, sin, cos );
  63. intersect = raycaster.ray.intersectTriangle( vA, vB, vC, false, intersectPoint );
  64. if ( intersect === null ) {
  65. return;
  66. }
  67. }
  68. var distance = raycaster.ray.origin.distanceTo( intersectPoint );
  69. if ( distance < raycaster.near || distance > raycaster.far ) return;
  70. intersects.push( {
  71. distance: distance,
  72. point: intersectPoint.clone(),
  73. face: null,
  74. object: this
  75. } );
  76. };
  77. }() ),
  78. clone: function () {
  79. return new this.constructor( this.material ).copy( this );
  80. },
  81. copy: function ( source ) {
  82. Object3D.prototype.copy.call( this, source );
  83. if ( source.center !== undefined ) this.center.copy( source.center );
  84. return this;
  85. }
  86. } );
  87. export { Sprite };
粤ICP备19079148号