| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- /**
- * @author mrdoob / http://mrdoob.com/
- * @author bhouston / http://exocortex.com/
- */
- ( function ( THREE ) {
- THREE.Raycaster = function ( origin, direction, near, far ) {
- this.ray = new THREE.Ray( origin, direction );
-
- // normalized ray.direction required for accurate distance calculations
- if( this.ray.direction.length() > 0 ) {
- this.ray.direction.normalize();
- }
- this.near = near || 0;
- this.far = far || Infinity;
- };
- var sphere = new THREE.Sphere();
- var localRay = new THREE.Ray();
- var facePlane = new THREE.Plane();
- var intersectPoint = new THREE.Vector3();
- var inverseMatrix = new THREE.Matrix4();
- var descSort = function ( a, b ) {
- return a.distance - b.distance;
- };
- var v0 = new THREE.Vector3(), v1 = new THREE.Vector3(), v2 = new THREE.Vector3();
- // http://www.blackpawn.com/texts/pointinpoly/default.html
- var intersectObject = function ( object, raycaster, intersects ) {
- if ( object instanceof THREE.Particle ) {
- var distance = raycaster.ray.distanceToPoint( object.matrixWorld.getPosition() );
- if ( distance > object.scale.x ) {
- return intersects;
- }
- intersects.push( {
- distance: distance,
- point: object.position,
- face: null,
- object: object
- } );
- } else if ( object instanceof THREE.Mesh ) {
- // Checking boundingSphere distance to ray
- sphere.set(
- object.matrixWorld.getPosition(),
- object.geometry.boundingSphere.radius* object.matrixWorld.getMaxScaleOnAxis() );
- if ( ! raycaster.ray.isIntersectionSphere( sphere ) ) {
- return intersects;
- }
- // Checking faces
- var geometry = object.geometry;
- var vertices = geometry.vertices;
- var isFaceMaterial = object.material instanceof THREE.MeshFaceMaterial;
- var objectMaterials = isFaceMaterial === true ? object.material.materials : null;
- var side = object.material.side;
- var a, b, c, d;
- var precision = raycaster.precision;
- object.matrixRotationWorld.extractRotation( object.matrixWorld );
- inverseMatrix.getInverse( object.matrixWorld );
- localRay.copy( raycaster.ray ).transformSelf( inverseMatrix );
-
- for ( var f = 0, fl = geometry.faces.length; f < fl; f ++ ) {
- var face = geometry.faces[ f ];
- var material = isFaceMaterial === true ? objectMaterials[ face.materialIndex ] : object.material;
- if ( material === undefined ) continue;
-
- facePlane.setFromNormalAndCoplanarPoint( face.normal, vertices[face.a] );
- var planeDistance = localRay.distanceToPlane( facePlane );
-
- // bail if raycaster and plane are parallel
- if ( Math.abs( planeDistance ) < precision ) continue;
-
- // if negative distance, then plane is behind raycaster
- if ( planeDistance < 0 ) continue;
- // check if we hit the wrong side of a single sided face
- side = material.side;
- if( side !== THREE.DoubleSide ) {
- var planeSign = localRay.direction.dot( facePlane.normal );
- if( ! ( side === THREE.FrontSide ? planeSign < 0 : planeSign > 0 ) ) continue;
- }
- // this can be done using the planeDistance from localRay because localRay wasn't normalized, but ray was
- if ( planeDistance < raycaster.near || planeDistance > raycaster.far ) continue;
-
- intersectPoint = localRay.at( planeDistance, intersectPoint ); // passing in intersectPoint avoids a copy
- if ( face instanceof THREE.Face3 ) {
- a = vertices[ face.a ];
- b = vertices[ face.b ];
- c = vertices[ face.c ];
- if ( ! THREE.Triangle3.containsPoint( intersectPoint, a, b, c ) ) continue;
- } else if ( face instanceof THREE.Face4 ) {
- a = vertices[ face.a ];
- b = vertices[ face.b ];
- c = vertices[ face.c ];
- d = vertices[ face.d ];
- if ( ( ! THREE.Triangle3.containsPoint( intersectPoint, a, b, d ) ) &&
- ( ! THREE.Triangle3.containsPoint( intersectPoint, b, c, d ) ) ) continue;
- } else {
- // This is added because if we call out of this if/else group when none of the cases
- // match it will add a point to the intersection list erroneously.
- throw Error( "face type not supported" );
- }
- intersects.push( {
- distance: planeDistance, // this works because the original ray was normalized, and the transformed localRay wasn't
- point: raycaster.ray.at( planeDistance ),
- face: face,
- faceIndex: f,
- object: object
- } );
- }
- }
- };
- var intersectDescendants = function ( object, raycaster, intersects ) {
- var descendants = object.getDescendants();
- for ( var i = 0, l = descendants.length; i < l; i ++ ) {
- intersectObject( descendants[ i ], raycaster, intersects );
- }
- };
- //
- THREE.Raycaster.prototype.precision = 0.0001;
- THREE.Raycaster.prototype.set = function ( origin, direction ) {
- this.ray.set( origin, direction );
- // normalized ray.direction required for accurate distance calculations
- if( this.ray.direction.length() > 0 ) {
- this.ray.direction.normalize();
- }
- };
- THREE.Raycaster.prototype.intersectObject = function ( object, recursive ) {
- var intersects = [];
- if ( recursive === true ) {
- intersectDescendants( object, this, intersects );
- }
- intersectObject( object, this, intersects );
- intersects.sort( descSort );
- return intersects;
- };
- THREE.Raycaster.prototype.intersectObjects = function ( objects, recursive ) {
- var intersects = [];
- for ( var i = 0, l = objects.length; i < l; i ++ ) {
- intersectObject( objects[ i ], this, intersects );
- if ( recursive === true ) {
- intersectDescendants( objects[ i ], this, intersects );
- }
- }
- intersects.sort( descSort );
- return intersects;
- };
- }( THREE ) );
|