提交 53876c8d 编写于 作者: R Ricardo Cabello

Merge pull request #6768 from dubejf/raysq

Ray: Add distanceSqToPoint method
...@@ -104,6 +104,14 @@ ...@@ -104,6 +104,14 @@
Get the distance of the closest approach between the [page:Ray] and the [page:Vector3]. Get the distance of the closest approach between the [page:Ray] and the [page:Vector3].
</div> </div>
<h3>[method:Float distanceSqToPoint]([page:Vector3 point])</h3>
<div>
point -- [page:Vector3] The [page:Vector3] to compute a distance to.
</div>
<div>
Get the squared distance of the closest approach between the [page:Ray] and the [page:Vector3].
</div>
<h3>[method:Boolean equals]([page:Ray ray])</h3> <h3>[method:Boolean equals]([page:Ray ray])</h3>
<div> <div>
ray -- [page:Ray] The [page:Ray] to compare to. ray -- [page:Ray] The [page:Ray] to compare to.
......
...@@ -69,7 +69,13 @@ THREE.Ray.prototype = { ...@@ -69,7 +69,13 @@ THREE.Ray.prototype = {
}, },
distanceToPoint: function () { distanceToPoint: function ( point ) {
return Math.sqrt( this.distanceSqToPoint( point ) );
},
distanceSqToPoint: function () {
var v1 = new THREE.Vector3(); var v1 = new THREE.Vector3();
...@@ -81,13 +87,13 @@ THREE.Ray.prototype = { ...@@ -81,13 +87,13 @@ THREE.Ray.prototype = {
if ( directionDistance < 0 ) { if ( directionDistance < 0 ) {
return this.origin.distanceTo( point ); return this.origin.distanceToSquared( point );
} }
v1.copy( this.direction ).multiplyScalar( directionDistance ).add( this.origin ); v1.copy( this.direction ).multiplyScalar( directionDistance ).add( this.origin );
return v1.distanceTo( point ); return v1.distanceToSquared( point );
}; };
......
...@@ -41,13 +41,14 @@ THREE.PointCloud.prototype.raycast = ( function () { ...@@ -41,13 +41,14 @@ THREE.PointCloud.prototype.raycast = ( function () {
} }
var localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 ); var localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 );
var localThresholdSq = localThreshold * localThreshold;
var position = new THREE.Vector3(); var position = new THREE.Vector3();
var testPoint = function ( point, index ) { var testPoint = function ( point, index ) {
var rayPointDistance = ray.distanceToPoint( point ); var rayPointDistanceSq = ray.distanceSqToPoint( point );
if ( rayPointDistance < localThreshold ) { if ( rayPointDistanceSq < localThresholdSq ) {
var intersectPoint = ray.closestPointToPoint( point ); var intersectPoint = ray.closestPointToPoint( point );
intersectPoint.applyMatrix4( object.matrixWorld ); intersectPoint.applyMatrix4( object.matrixWorld );
...@@ -59,7 +60,7 @@ THREE.PointCloud.prototype.raycast = ( function () { ...@@ -59,7 +60,7 @@ THREE.PointCloud.prototype.raycast = ( function () {
intersects.push( { intersects.push( {
distance: distance, distance: distance,
distanceToRay: rayPointDistance, distanceToRay: Math.sqrt( rayPointDistanceSq ),
point: intersectPoint.clone(), point: intersectPoint.clone(),
index: index, index: index,
face: null, face: null,
......
...@@ -38,9 +38,10 @@ THREE.Sprite.prototype.raycast = ( function () { ...@@ -38,9 +38,10 @@ THREE.Sprite.prototype.raycast = ( function () {
matrixPosition.setFromMatrixPosition( this.matrixWorld ); matrixPosition.setFromMatrixPosition( this.matrixWorld );
var distance = raycaster.ray.distanceToPoint( matrixPosition ); var distanceSq = raycaster.ray.distanceSqToPoint( matrixPosition );
var guessSizeSq = this.scale.x * this.scale.y;
if ( distance > this.scale.x ) {
if ( distanceSq > guessSizeSq ) {
return; return;
...@@ -48,7 +49,7 @@ THREE.Sprite.prototype.raycast = ( function () { ...@@ -48,7 +49,7 @@ THREE.Sprite.prototype.raycast = ( function () {
intersects.push( { intersects.push( {
distance: distance, distance: Math.sqrt( distanceSq ),
point: this.position, point: this.position,
face: null, face: null,
object: this object: this
......
...@@ -93,6 +93,22 @@ test( "distanceToPoint", function() { ...@@ -93,6 +93,22 @@ test( "distanceToPoint", function() {
ok( d === 0, "Passed!" ); ok( d === 0, "Passed!" );
}); });
test( "distanceSqToPoint", function() {
var a = new THREE.Ray( one3.clone(), new THREE.Vector3( 0, 0, 1 ) );
// behind the ray
var b = a.distanceSqToPoint( zero3 );
ok( b === 3, "Passed!" );
// front of the ray
var c = a.distanceSqToPoint( new THREE.Vector3( 0, 0, 50 ) );
ok( c === 2, "Passed!" );
// exactly on the ray
var d = a.distanceSqToPoint( one3 );
ok( d === 0, "Passed!" );
});
test( "isIntersectionSphere", function() { test( "isIntersectionSphere", function() {
var a = new THREE.Ray( one3.clone(), new THREE.Vector3( 0, 0, 1 ) ); var a = new THREE.Ray( one3.clone(), new THREE.Vector3( 0, 0, 1 ) );
var b = new THREE.Sphere( zero3, 0.5 ); var b = new THREE.Sphere( zero3, 0.5 );
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册