-
-
Notifications
You must be signed in to change notification settings - Fork 35.6k
/
Copy pathSprite.js
235 lines (166 loc) · 5.98 KB
/
Sprite.js
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
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';
let _geometry;
const _intersectPoint = /*@__PURE__*/ new Vector3();
const _worldScale = /*@__PURE__*/ new Vector3();
const _mvPosition = /*@__PURE__*/ new Vector3();
const _alignedPosition = /*@__PURE__*/ new Vector2();
const _rotatedPosition = /*@__PURE__*/ new Vector2();
const _viewWorldMatrix = /*@__PURE__*/ new Matrix4();
const _vA = /*@__PURE__*/ new Vector3();
const _vB = /*@__PURE__*/ new Vector3();
const _vC = /*@__PURE__*/ new Vector3();
const _uvA = /*@__PURE__*/ new Vector2();
const _uvB = /*@__PURE__*/ new Vector2();
const _uvC = /*@__PURE__*/ new Vector2();
/**
* A sprite is a plane that always faces towards the camera, generally with a
* partially transparent texture applied.
*
* Sprites do not cast shadows, setting {@link Object3D#castShadow} to `true` will
* have no effect.
*
* ```js
* const map = new THREE.TextureLoader().load( 'sprite.png' );
* const material = new THREE.SpriteMaterial( { map: map } );
*
* const sprite = new THREE.Sprite( material );
* scene.add( sprite );
* ```
*
* @augments Object3D
*/
class Sprite extends Object3D {
/**
* Constructs a new sprite.
*
* @param {SpriteMaterial} [material] - The sprite material.
*/
constructor( material = new SpriteMaterial() ) {
super();
/**
* This flag can be used for type testing.
*
* @type {boolean}
* @readonly
* @default true
*/
this.isSprite = true;
this.type = 'Sprite';
if ( _geometry === undefined ) {
_geometry = new BufferGeometry();
const 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
] );
const interleavedBuffer = new InterleavedBuffer( float32Array, 5 );
_geometry.setIndex( [ 0, 1, 2, 0, 2, 3 ] );
_geometry.setAttribute( 'position', new InterleavedBufferAttribute( interleavedBuffer, 3, 0, false ) );
_geometry.setAttribute( 'uv', new InterleavedBufferAttribute( interleavedBuffer, 2, 3, false ) );
}
/**
* The sprite geometry.
*
* @type {BufferGeometry}
*/
this.geometry = _geometry;
/**
* The sprite material.
*
* @type {SpriteMaterial}
*/
this.material = material;
/**
* The sprite's anchor point, and the point around which the sprite rotates.
* A value of `(0.5, 0.5)` corresponds to the midpoint of the sprite. A value
* of `(0, 0)` corresponds to the lower left corner of the sprite.
*
* @type {Vector2}
* @default (0.5,0.5)
*/
this.center = new Vector2( 0.5, 0.5 );
}
/**
* Computes intersection points between a casted ray and this sprite.
*
* @param {Raycaster} raycaster - The raycaster.
* @param {Array<Object>} intersects - The target array that holds the intersection points.
*/
raycast( raycaster, intersects ) {
if ( raycaster.camera === null ) {
console.error( 'THREE.Sprite: "Raycaster.camera" needs to be set in order to raycast against sprites.' );
}
_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 );
}
const rotation = this.material.rotation;
let sin, cos;
if ( rotation !== 0 ) {
cos = Math.cos( rotation );
sin = Math.sin( rotation );
}
const 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
let 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;
}
}
const distance = raycaster.ray.origin.distanceTo( _intersectPoint );
if ( distance < raycaster.near || distance > raycaster.far ) return;
intersects.push( {
distance: distance,
point: _intersectPoint.clone(),
uv: Triangle.getInterpolation( _intersectPoint, _vA, _vB, _vC, _uvA, _uvB, _uvC, new Vector2() ),
face: null,
object: this
} );
}
copy( source, recursive ) {
super.copy( source, recursive );
if ( source.center !== undefined ) this.center.copy( source.center );
this.material = source.material;
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 };