| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- import { Vector2 } from '../math/Vector2.js';
- import { Vector3 } from '../math/Vector3.js';
- import { Matrix4 } from '../math/Matrix4.js';
- import { Object3D } from '../core/Object3D.js';
- import { SpriteMaterial } from '../materials/SpriteMaterial.js';
- /**
- * @author mikael emtinger / http://gomo.se/
- * @author alteredq / http://alteredqualia.com/
- */
- function Sprite( material ) {
- Object3D.call( this );
- this.type = 'Sprite';
- this.material = ( material !== undefined ) ? material : new SpriteMaterial();
- this.center = new Vector2( 0.5, 0.5 );
- }
- Sprite.prototype = Object.assign( Object.create( Object3D.prototype ), {
- constructor: Sprite,
- isSprite: true,
- raycast: ( function () {
- var intersectPoint = new Vector3();
- var worldScale = new Vector3();
- var mvPosition = new Vector3();
- var alignedPosition = new Vector2();
- var rotatedPosition = new Vector2();
- var viewWorldMatrix = new Matrix4();
- var vA = new Vector3();
- var vB = new Vector3();
- var vC = new Vector3();
- function transformVertex( vertexPosition, mvPosition, center, scale, sin, cos ) {
- // compute position in camera space
- alignedPosition.set( ( vertexPosition.x - center.x ) * scale.x, ( vertexPosition.y - center.y ) * scale.y );
- if ( sin !== undefined ) {
- rotatedPosition.x = ( cos * alignedPosition.x ) - ( sin * alignedPosition.y );
- rotatedPosition.y = ( sin * alignedPosition.x ) + ( cos * alignedPosition.y );
- } else {
- rotatedPosition.copy( alignedPosition );
- }
- vertexPosition.copy( mvPosition );
- vertexPosition.x += rotatedPosition.x;
- vertexPosition.y += rotatedPosition.y;
- // transform to world space
- vertexPosition.applyMatrix4( viewWorldMatrix );
- }
- return function raycast( raycaster, intersects ) {
- worldScale.setFromMatrixScale( this.matrixWorld );
- viewWorldMatrix.getInverse( this.modelViewMatrix ).premultiply( this.matrixWorld );
- mvPosition.setFromMatrixPosition( this.modelViewMatrix );
- var rotation = this.material.rotation;
- var sin, cos;
- if ( rotation !== 0 ) {
- cos = Math.cos( rotation );
- sin = Math.sin( rotation );
- }
- var center = this.center;
- transformVertex( vA.set( 0, 0, 1 ), mvPosition, center, worldScale, sin, cos );
- transformVertex( vB.set( 0, 1, 1 ), mvPosition, center, worldScale, sin, cos );
- transformVertex( vC.set( 1, 0, 1 ), mvPosition, center, worldScale, sin, cos );
- // check first triangle
- var intersect = raycaster.ray.intersectTriangle( vA, vB, vC, false, intersectPoint );
- if ( intersect === null ) {
- // check second triangle
- transformVertex( vA.set( 1, 1, 1 ), mvPosition, center, worldScale, sin, cos );
- intersect = raycaster.ray.intersectTriangle( vA, vB, vC, false, intersectPoint );
- if ( intersect === null ) {
- return;
- }
- }
- var distance = raycaster.ray.origin.distanceTo( intersectPoint );
- if ( distance < raycaster.near || distance > raycaster.far ) return;
- intersects.push( {
- distance: distance,
- point: intersectPoint.clone(),
- face: null,
- object: this
- } );
- };
- }() ),
- clone: function () {
- return new this.constructor( this.material ).copy( this );
- },
- copy: function ( source ) {
- Object3D.prototype.copy.call( this, source );
- if ( source.center !== undefined ) this.center.copy( source.center );
- return this;
- }
- } );
- export { Sprite };
|