瀏覽代碼

Fix Sprite raycast intersection distance

The existing code used the distance between the sprite and the closest point on the ray. However, this is inconsistent with both the documentation and other objects, which return the distance between the intersection and the ray origin.

The new code is similar to the Point raycast implementation. The closest point on the ray is saved to `intersectionPoint`. An intersection object is created if the following conditions are met:
1. The (squared) distance between `intersectionPoint` and the sprite position is less than the (squared) sprite size.
2. The distance between `intersectionPoint` and the ray origin is within the `near` and `far` bounds.
Jay Weisskopf 8 年之前
父節點
當前提交
9d72e97f9d
共有 1 個文件被更改,包括 7 次插入7 次删除
  1. 7 7
      src/objects/Sprite.js

+ 7 - 7
src/objects/Sprite.js

@@ -25,25 +25,25 @@ Sprite.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
 	raycast: ( function () {
 
+		var intersectPoint = new Vector3();
 		var matrixPosition = new Vector3();
 
 		return function raycast( raycaster, intersects ) {
 
 			matrixPosition.setFromMatrixPosition( this.matrixWorld );
-
-			var distanceSq = raycaster.ray.distanceSqToPoint( matrixPosition );
+			raycaster.ray.closestPointToPoint( matrixPosition, intersectPoint );
 			var guessSizeSq = this.scale.x * this.scale.y / 4;
 
-			if ( distanceSq > guessSizeSq ) {
+			if ( matrixPosition.distanceToSquared( intersectPoint ) > guessSizeSq ) return;
 
-				return;
+			var distance = raycaster.ray.origin.distanceTo( intersectPoint );
 
-			}
+			if ( distance < raycaster.near || distance > raycaster.far ) return;
 
 			intersects.push( {
 
-				distance: Math.sqrt( distanceSq ),
-				point: this.position,
+				distance: distance,
+				point: intersectPoint.clone(),
 				face: null,
 				object: this