Spaces:
Running
Running
File size: 4,519 Bytes
6cd9596 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
/**
* @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;
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 () {
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();
var uvA = new Vector2();
var uvB = new Vector2();
var uvC = new Vector2();
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 );
}
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.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;
}
} );
export { Sprite };
|