123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- /**
- * @author mrdoob / http://mrdoob.com/
- * @author bhouston / http://exocortex.com/
- * @author stephomi / http://stephaneginier.com/
- */
- ( function ( THREE ) {
- THREE.Raycaster = function ( origin, direction, near, far ) {
- this.ray = new THREE.Ray( origin, direction );
- // direction is assumed to be normalized (for accurate distance calculations)
- this.near = near || 0;
- this.far = far || Infinity;
- this.params = {
- Sprite: {},
- Mesh: {},
- PointCloud: { threshold: 1 },
- LOD: {},
- Line: {}
- };
- };
- var descSort = function ( a, b ) {
- return a.distance - b.distance;
- };
- var intersectObject = function ( object, raycaster, intersects, recursive ) {
- if ( object.visible === false ) return;
- object.raycast( raycaster, intersects );
- if ( recursive === true ) {
- var children = object.children;
- for ( var i = 0, l = children.length; i < l; i ++ ) {
- intersectObject( children[ i ], raycaster, intersects, true );
- }
- }
- };
- //
- THREE.Raycaster.prototype = {
- constructor: THREE.Raycaster,
- linePrecision: 1,
- set: function ( origin, direction ) {
- // direction is assumed to be normalized (for accurate distance calculations)
- this.ray.set( origin, direction );
- },
- setFromCamera: function ( coords, camera ) {
- if ( camera instanceof THREE.PerspectiveCamera ) {
- this.ray.origin.setFromMatrixPosition( camera.matrixWorld );
- this.ray.direction.set( coords.x, coords.y, 0.5 ).unproject( camera ).sub( this.ray.origin ).normalize();
- } else if ( camera instanceof THREE.OrthographicCamera ) {
- this.ray.origin.set( coords.x, coords.y, - 1 ).unproject( camera );
- this.ray.direction.set( 0, 0, - 1 ).transformDirection( camera.matrixWorld );
- } else {
- console.error( 'THREE.Raycaster: Unsupported camera type.' );
- }
- },
- intersectObject: function ( object, recursive ) {
- var intersects = [];
- intersectObject( object, this, intersects, recursive );
- intersects.sort( descSort );
- return intersects;
- },
- intersectObjects: function ( objects, recursive ) {
- var intersects = [];
- if ( Array.isArray( objects ) === false ) {
- console.warn( 'THREE.Raycaster.intersectObjects: objects is not an Array.' );
- return intersects;
- }
- for ( var i = 0, l = objects.length; i < l; i ++ ) {
- intersectObject( objects[ i ], this, intersects, recursive );
- }
- intersects.sort( descSort );
- return intersects;
- }
- };
- }( THREE ) );
|