Browse Source

Simplified PointCloud.raycast.

Mr.doob 11 years ago
parent
commit
a41512c723
1 changed files with 35 additions and 70 deletions
  1. 35 70
      src/objects/PointCloud.js

+ 35 - 70
src/objects/PointCloud.js

@@ -22,7 +22,8 @@ THREE.PointCloud.prototype.raycast = ( function () {
 
 	return function ( raycaster, intersects ) {
 
-		var geometry = this.geometry;
+		var object = this;
+		var geometry = object.geometry;
 		var threshold = raycaster.params.PointCloud.threshold;
 
 		inverseMatrix.getInverse( this.matrixWorld );
@@ -39,7 +40,33 @@ THREE.PointCloud.prototype.raycast = ( function () {
 		}
 	
 		var localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 );
-		var pos = new THREE.Vector3();
+		var position = new THREE.Vector3();
+
+		var testPoint = function ( point, index ) {
+
+			var rayPointDistance = ray.distanceToPoint( point );
+
+			if ( rayPointDistance < localThreshold ) {
+
+				var intersectPoint = ray.closestPointToPoint( point );
+				intersectPoint.applyMatrix4( object.matrixWorld );
+
+				var distance = raycaster.ray.origin.distanceTo( intersectPoint );
+
+				intersects.push( {
+
+					distance: distance,
+					distanceToRay: rayPointDistance,
+					point: intersectPoint.clone(),
+					index: index,
+					face: null,
+					object: object
+
+				} );
+
+			}
+
+		};
 
 		if ( geometry instanceof THREE.BufferGeometry ) {
 	
@@ -73,33 +100,13 @@ THREE.PointCloud.prototype.raycast = ( function () {
 
 						var a = index + indices[ i ];
 
-						pos.set(
+						position.set(
 							positions[ a * 3 ],
 							positions[ a * 3 + 1 ],
 							positions[ a * 3 + 2 ]
 						);
-
-						var rayPointDistance = ray.distanceToPoint( pos );
-
-						if ( rayPointDistance < localThreshold ) {
-
-							var intersectPoint = ray.closestPointToPoint( pos );
-							intersectPoint.applyMatrix4( this.matrixWorld );
-
-							var distance = raycaster.ray.origin.distanceTo( intersectPoint );
-
-							intersects.push( {
-
-								distance: distance,
-								distanceToRay: rayPointDistance,
-								point: intersectPoint.clone(),
-								index: a,
-								face: null,
-								object: this
-
-							} );
-
-						}
+						
+						testPoint( position, a );
 
 					}
 
@@ -111,33 +118,13 @@ THREE.PointCloud.prototype.raycast = ( function () {
 
 				for ( var i = 0; i < pointCount; i ++ ) {
 
-					pos.set(
+					position.set(
 						positions[ 3 * i ],
 						positions[ 3 * i + 1 ],
 						positions[ 3 * i + 2 ]
 					);
 
-					var rayPointDistance = ray.distanceToPoint( pos );
-
-					if ( rayPointDistance < localThreshold ) {
-
-						var intersectPoint = ray.closestPointToPoint( pos );
-						intersectPoint.applyMatrix4( this.matrixWorld );
-
-						var distance = raycaster.ray.origin.distanceTo( intersectPoint );
-
-						intersects.push( {
-
-							distance: distance,
-							distanceToRay: rayPointDistance,
-							point: intersectPoint.clone(),
-							index: i,
-							face: null,
-							object: this
-
-						} );
-
-					}
+					testPoint( position, i );
 
 				}
 
@@ -149,29 +136,7 @@ THREE.PointCloud.prototype.raycast = ( function () {
 
 			for ( var i = 0; i < vertices.length; i ++ ) {
 
-				pos = vertices[ i ];
-
-				var rayPointDistance = ray.distanceToPoint( pos );
-
-				if ( rayPointDistance < localThreshold ) {
-
-					var intersectPoint = ray.closestPointToPoint( pos );
-					intersectPoint.applyMatrix4( this.matrixWorld );
-
-					var distance = raycaster.ray.origin.distanceTo( intersectPoint );
-
-					intersects.push( {
-
-						distance: distance,
-						distanceToRay: rayPointDistance,
-						point: intersectPoint.clone(),
-						index: i,
-						face: null,
-						object: this
-
-					} );
-
-				}
+				testPoint( vertices[ i ], i );
 
 			}