Skip to content

Commit 5c600d9

Browse files
authored
Merge pull request #16423 from WestLangley/dev-sprite_raycast
Fix sprite raycasting bugs
2 parents f420e67 + c572f96 commit 5c600d9

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

examples/webgl_raycast_sprite.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@
5858
sprite.scale.set( 2, 5, 1 );
5959
group.add( sprite );
6060

61-
var sprite = new THREE.Sprite( new THREE.SpriteMaterial( { color: '#69f' } ) );
61+
var sprite = new THREE.Sprite( new THREE.SpriteMaterial( { color: '#69f', sizeAttenuation: false } ) );
6262
sprite.material.rotation = Math.PI / 3 * 4;
6363
sprite.position.set( 8, - 2, 2 );
6464
sprite.center.set( 0.5, 0 );
65-
sprite.scale.set( 1, - 5, 1 );
65+
sprite.scale.set( .1, .5, .1 );
6666
group.add( sprite );
6767

6868
var group2 = new THREE.Object3D();

src/core/Raycaster.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,13 @@ Object.assign( Raycaster.prototype, {
7979

8080
this.ray.origin.setFromMatrixPosition( camera.matrixWorld );
8181
this.ray.direction.set( coords.x, coords.y, 0.5 ).unproject( camera ).sub( this.ray.origin ).normalize();
82+
this._camera = camera;
8283

8384
} else if ( ( camera && camera.isOrthographicCamera ) ) {
8485

8586
this.ray.origin.set( coords.x, coords.y, ( camera.near + camera.far ) / ( camera.near - camera.far ) ).unproject( camera ); // set origin in plane of camera
8687
this.ray.direction.set( 0, 0, - 1 ).transformDirection( camera.matrixWorld );
88+
this._camera = camera;
8789

8890
} else {
8991

src/objects/Sprite.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,18 @@ Sprite.prototype = Object.assign( Object.create( Object3D.prototype ), {
101101
return function raycast( raycaster, intersects ) {
102102

103103
worldScale.setFromMatrixScale( this.matrixWorld );
104-
viewWorldMatrix.getInverse( this.modelViewMatrix ).premultiply( this.matrixWorld );
104+
105+
viewWorldMatrix.copy( raycaster._camera.matrixWorld );
106+
this.modelViewMatrix.multiplyMatrices( raycaster._camera.matrixWorldInverse, this.matrixWorld );
107+
105108
mvPosition.setFromMatrixPosition( this.modelViewMatrix );
106109

110+
if ( raycaster._camera.isPerspectiveCamera && this.material.sizeAttenuation === false ) {
111+
112+
worldScale.multiplyScalar( - mvPosition.z );
113+
114+
}
115+
107116
var rotation = this.material.rotation;
108117
var sin, cos;
109118
if ( rotation !== 0 ) {

0 commit comments

Comments
 (0)