Browse Source

Moved Raycaster PointCloud.

Mr.doob 11 years ago
parent
commit
47928d1bc5
2 changed files with 167 additions and 151 deletions
  1. 1 151
      src/core/Raycaster.js
  2. 166 0
      src/objects/PointCloud.js

+ 1 - 151
src/core/Raycaster.js

@@ -50,157 +50,7 @@
 
 		} else if ( object instanceof THREE.PointCloud ) {
 		
-			var geometry = object.geometry;
-			var threshold = raycaster.params.PointCloud.threshold;
-
-			inverseMatrix.getInverse( object.matrixWorld );
-			localRay.copy( raycaster.ray ).applyMatrix4( inverseMatrix );
-			
-			if ( geometry.boundingBox !== null ) {
-			
-				if ( localRay.isIntersectionBox( geometry.boundingBox ) === false )  {
-				
-					return;
-					
-				}
-				
-			}
-			
-			var localThreshold = threshold / ( ( object.scale.x + object.scale.y + object.scale.z ) / 3 );
-			var pos = new THREE.Vector3();
-
-			if ( geometry instanceof THREE.BufferGeometry ) {
-			
-				var attributes = geometry.attributes;
-				var positions = attributes.position.array;
-				
-				if ( attributes.index !== undefined ) {
-				
-					var indices = attributes.index.array;
-					var offsets = geometry.offsets;
-					
-					if ( offsets.length === 0 ) {
-
-						var offset = {
-							start: 0,
-							count: indices.length,
-							index: 0
-						};
-						
-						offsets = [ offset ];
-
-					}
-					
-					for ( var oi = 0, ol = offsets.length; oi < ol; ++oi ) {
-
-						var start = offsets[ oi ].start;
-						var count = offsets[ oi ].count;
-						var index = offsets[ oi ].index;
-
-						for ( var i = start, il = start + count; i < il; i++ ) {
-						
-							var a = index + indices[ i ];
-						
-							pos.set(
-								positions[ a * 3 ],
-								positions[ a * 3 + 1 ],
-								positions[ a * 3 + 2 ]
-							);
-							
-							var rayPointDistance = localRay.distanceToPoint( pos );
-
-							if ( rayPointDistance < localThreshold ) {
-								
-								var intersectPoint = localRay.closestPointToPoint( pos );
-								intersectPoint.applyMatrix4( object.matrixWorld );
-								var distance = raycaster.ray.origin.distanceTo( intersectPoint );
-							
-								intersects.push( {
-							
-									distance: distance,
-									distanceToRay: rayPointDistance,
-									point: intersectPoint.clone(),
-									index: a,
-									face: null,
-									object: object
-									
-								} );
-							
-							}
-						
-						}
-						
-					}
-				
-				} else {
-				
-					var pointCount = positions.length / 3;
-
-					for (var i = 0; i < pointCount; i++ ) {
-					
-						pos.set(
-							positions[ 3 * i ],
-							positions[ 3 * i + 1 ],
-							positions[ 3 * i + 2 ]
-						);
-
-						var rayPointDistance = localRay.distanceToPoint( pos );
-
-						if ( rayPointDistance < localThreshold ) {
-							
-							var intersectPoint = localRay.closestPointToPoint( pos );
-							intersectPoint.applyMatrix4( object.matrixWorld );
-							var distance = raycaster.ray.origin.distanceTo( intersectPoint );
-							
-							intersects.push( {
-							
-								distance: distance,
-								distanceToRay: rayPointDistance,
-								point: intersectPoint,
-								index: i,
-								face: null,
-								object: object
-								
-							} );
-							
-						}
-						
-					}
-					
-				}
-				
-			} else {
-			
-				var vertices = object.geometry.vertices;
-
-				for ( var i = 0; i < vertices.length; i ++ ) {
-
-					pos = vertices[ i ];
-					
-					var rayPointDistance = localRay.distanceToPoint( pos );
-
-					if ( rayPointDistance < localThreshold ) {
-						
-						var intersectPoint = localRay.closestPointToPoint( pos );
-						intersectPoint.applyMatrix4( object.matrixWorld );
-						var distance = raycaster.ray.origin.distanceTo( intersectPoint );
-						
-						intersects.push( {
-						
-							distance: distance,
-							distanceToRay: rayPointDistance,
-							point: intersectPoint.clone(),
-							index: i,
-							face: null,
-							object: object
-							
-						} );
-						
-					}
-
-				}
-				
-			}
+			object.raycast( raycaster, intersects );
 
 		} else if ( object instanceof THREE.LOD ) {
 

+ 166 - 0
src/objects/PointCloud.js

@@ -15,6 +15,172 @@ THREE.PointCloud = function ( geometry, material ) {
 
 THREE.PointCloud.prototype = Object.create( THREE.Object3D.prototype );
 
+THREE.PointCloud.prototype.raycast = ( function () {
+
+	var ray = new THREE.Ray();
+	var inverseMatrix = new THREE.Matrix4();
+
+	return function ( raycaster, intersects ) {
+
+		var geometry = this.geometry;
+		var threshold = raycaster.params.PointCloud.threshold;
+
+		inverseMatrix.getInverse( this.matrixWorld );
+		ray.copy( raycaster.ray ).applyMatrix4( inverseMatrix );
+
+		if ( geometry.boundingBox !== null ) {
+	
+			if ( ray.isIntersectionBox( geometry.boundingBox ) === false ) {
+
+				return;
+
+			}
+
+		}
+	
+		var localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 );
+		var pos = new THREE.Vector3();
+
+		if ( geometry instanceof THREE.BufferGeometry ) {
+	
+			var attributes = geometry.attributes;
+			var positions = attributes.position.array;
+
+			if ( attributes.index !== undefined ) {
+
+				var indices = attributes.index.array;
+				var offsets = geometry.offsets;
+
+				if ( offsets.length === 0 ) {
+
+					var offset = {
+						start: 0,
+						count: indices.length,
+						index: 0
+					};
+
+					offsets = [ offset ];
+
+				}
+
+				for ( var oi = 0, ol = offsets.length; oi < ol; ++oi ) {
+
+					var start = offsets[ oi ].start;
+					var count = offsets[ oi ].count;
+					var index = offsets[ oi ].index;
+
+					for ( var i = start, il = start + count; i < il; i++ ) {
+
+						var a = index + indices[ i ];
+
+						pos.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
+
+							} );
+
+						}
+
+					}
+
+				}
+
+			} else {
+
+				var pointCount = positions.length / 3;
+
+				for ( var i = 0; i < pointCount; i ++ ) {
+
+					pos.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,
+							index: i,
+							face: null,
+							object: this
+
+						} );
+
+					}
+
+				}
+
+			}
+
+		} else {
+
+			var vertices = this.geometry.vertices;
+
+			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
+
+					} );
+
+				}
+
+			}
+
+		}
+
+	};
+
+}() );
+
 THREE.PointCloud.prototype.clone = function ( object ) {
 
 	if ( object === undefined ) object = new THREE.PointCloud( this.geometry, this.material );