123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- /**
- * @author mikael emtinger / http://gomo.se/
- * @author alteredq / http://alteredqualia.com/
- */
- import { Vector2 } from '../math/Vector2.js';
- import { Vector3 } from '../math/Vector3.js';
- import { Matrix4 } from '../math/Matrix4.js';
- import { Triangle } from '../math/Triangle.js';
- import { Object3D } from '../core/Object3D.js';
- import { BufferGeometry } from '../core/BufferGeometry.js';
- import { InterleavedBuffer } from '../core/InterleavedBuffer.js';
- import { InterleavedBufferAttribute } from '../core/InterleavedBufferAttribute.js';
- import { SpriteMaterial } from '../materials/SpriteMaterial.js';
- var _geometry;
- var _intersectPoint, _worldScale, _mvPosition;
- var _alignedPosition, _rotatedPosition, _viewWorldMatrix;
- var _vA, _vB, _vC;
- var _uvA, _uvB, _uvC;
- function Sprite( material ) {
- Object3D.call( this );
- this.type = 'Sprite';
- if ( _geometry === undefined ) {
- _geometry = new BufferGeometry();
- var float32Array = new Float32Array( [
- - 0.5, - 0.5, 0, 0, 0,
- 0.5, - 0.5, 0, 1, 0,
- 0.5, 0.5, 0, 1, 1,
- - 0.5, 0.5, 0, 0, 1
- ] );
- var interleavedBuffer = new InterleavedBuffer( float32Array, 5 );
- _geometry.setIndex( [ 0, 1, 2, 0, 2, 3 ] );
- _geometry.addAttribute( 'position', new InterleavedBufferAttribute( interleavedBuffer, 3, 0, false ) );
- _geometry.addAttribute( 'uv', new InterleavedBufferAttribute( interleavedBuffer, 2, 3, false ) );
- }
- this.geometry = _geometry;
- 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 ( raycaster, intersects ) {
- if ( raycaster.camera === null ) {
- console.error( 'THREE.Sprite: "Raycaster.camera" needs to be set in order to raycast against sprites.' );
- }
- if ( _uvC === undefined ) {
- _intersectPoint = new Vector3();
- _worldScale = new Vector3();
- _mvPosition = new Vector3();
- _alignedPosition = new Vector2();
- _rotatedPosition = new Vector2();
- _viewWorldMatrix = new Matrix4();
- _vA = new Vector3();
- _vB = new Vector3();
- _vC = new Vector3();
- _uvA = new Vector2();
- _uvB = new Vector2();
- _uvC = new Vector2();
- }
- _worldScale.setFromMatrixScale( this.matrixWorld );
- _viewWorldMatrix.copy( raycaster.camera.matrixWorld );
- this.modelViewMatrix.multiplyMatrices( raycaster.camera.matrixWorldInverse, this.matrixWorld );
- _mvPosition.setFromMatrixPosition( this.modelViewMatrix );
- if ( raycaster.camera.isPerspectiveCamera && this.material.sizeAttenuation === false ) {
- _worldScale.multiplyScalar( - _mvPosition.z );
- }
- 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.5, - 0.5, 0 ), _mvPosition, center, _worldScale, sin, cos );
- transformVertex( _vB.set( 0.5, - 0.5, 0 ), _mvPosition, center, _worldScale, sin, cos );
- transformVertex( _vC.set( 0.5, 0.5, 0 ), _mvPosition, center, _worldScale, sin, cos );
- _uvA.set( 0, 0 );
- _uvB.set( 1, 0 );
- _uvC.set( 1, 1 );
- // check first triangle
- var intersect = raycaster.ray.intersectTriangle( _vA, _vB, _vC, false, _intersectPoint );
- if ( intersect === null ) {
- // check second triangle
- transformVertex( _vB.set( - 0.5, 0.5, 0 ), _mvPosition, center, _worldScale, sin, cos );
- _uvB.set( 0, 1 );
- intersect = raycaster.ray.intersectTriangle( _vA, _vC, _vB, 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(),
- uv: Triangle.getUV( _intersectPoint, _vA, _vB, _vC, _uvA, _uvB, _uvC, new Vector2() ),
- 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;
- }
- } );
- function transformVertex( vertexPosition, mvPosition, center, scale, sin, cos ) {
- // compute position in camera space
- _alignedPosition.subVectors( vertexPosition, center ).addScalar( 0.5 ).multiply( scale );
- // to check if rotation is not zero
- 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 );
- }
- export { Sprite };
|