diff --git a/src/three/TilesGroup.js b/src/three/TilesGroup.js index 86e32ba2f..11ffcd33c 100644 --- a/src/three/TilesGroup.js +++ b/src/three/TilesGroup.js @@ -16,12 +16,16 @@ export class TilesGroup extends Group { raycast( raycaster, intersects ) { + // returning "true" ends raycast traversal if ( this.tilesRenderer.optimizeRaycast ) { this.tilesRenderer.raycast( raycaster, intersects ); + return true; } + return false; + } updateMatrixWorld( force ) { diff --git a/src/three/TilesRenderer.js b/src/three/TilesRenderer.js index 95d3ab910..ca3450b10 100644 --- a/src/three/TilesRenderer.js +++ b/src/three/TilesRenderer.js @@ -11,12 +11,15 @@ import { Vector2, LoadingManager, EventDispatcher, + REVISION, } from 'three'; import { raycastTraverse, raycastTraverseFirstHit } from './raycastTraverse.js'; import { readMagicBytes } from '../utilities/readMagicBytes.js'; import { TileBoundingVolume } from './math/TileBoundingVolume.js'; import { ExtendedFrustum } from './math/ExtendedFrustum.js'; +// In three.js r165 and higher raycast traversal can be ended early +const REVISION_165 = parseInt( REVISION ) < 165; const INITIAL_FRUSTUM_CULLED = Symbol( 'INITIAL_FRUSTUM_CULLED' ); const tempMat = new Matrix4(); const tempMat2 = new Matrix4(); @@ -92,18 +95,22 @@ export class TilesRenderer extends TilesRendererBase { } ); this.manager = manager; - // Setting up the override raycasting function to be used by - // 3D objects created by this renderer - const tilesRenderer = this; - this._overridenRaycast = function ( raycaster, intersects ) { + if ( REVISION_165 ) { - if ( ! tilesRenderer.optimizeRaycast ) { + // Setting up the override raycasting function to be used by + // 3D objects created by this renderer + const tilesRenderer = this; + this._overridenRaycast = function ( raycaster, intersects ) { - Object.getPrototypeOf( this ).raycast.call( this, raycaster, intersects ); + if ( ! tilesRenderer.optimizeRaycast ) { - } + Object.getPrototypeOf( this ).raycast.call( this, raycaster, intersects ); - }; + } + + }; + + } } @@ -678,12 +685,16 @@ export class TilesRenderer extends TilesRendererBase { } ); updateFrustumCulled( scene, ! this.autoDisableRendererCulling ); - // We handle raycasting in a custom way so remove it from here - scene.traverse( c => { + if ( REVISION_165 ) { - c.raycast = this._overridenRaycast; + // We handle raycasting in a custom way so remove it from here + scene.traverse( c => { - } ); + c.raycast = this._overridenRaycast; + + } ); + + } const materials = []; const geometry = []; diff --git a/src/three/gltf/MeshFeatures.js b/src/three/gltf/MeshFeatures.js index 7db274ea8..e4fa55d13 100644 --- a/src/three/gltf/MeshFeatures.js +++ b/src/three/gltf/MeshFeatures.js @@ -1,6 +1,8 @@ import { ShaderMaterial, Vector2, Vector4, WebGLRenderTarget, WebGLRenderer, REVISION, Box2 } from 'three'; import { FullScreenQuad } from 'three/examples/jsm/postprocessing/Pass.js'; +const REVISION_165 = parseInt( REVISION ) >= 165; + // renderer and quad for rendering a single pixel const _renderer = new WebGLRenderer(); const _quad = new FullScreenQuad( new ShaderMaterial( { @@ -58,7 +60,7 @@ function getTextureCoordAttribute( geometry, index ) { // render target function renderPixelToTarget( texture, pixel, dstPixel, target ) { - if ( REVISION >= 165 ) { + if ( REVISION_165 ) { _box.min.copy( pixel ); _box.max.copy( pixel ); @@ -134,7 +136,7 @@ export class MeshFeatures { // performs texture data read back asynchronously getFeaturesAsync( ...args ) { - if ( REVISION >= 165 ) { + if ( REVISION_165 ) { this._asyncRead = true; const result = this.getFeatures( ...args ); diff --git a/src/three/raycastTraverse.js b/src/three/raycastTraverse.js index be5936486..ed6393597 100644 --- a/src/three/raycastTraverse.js +++ b/src/three/raycastTraverse.js @@ -1,5 +1,7 @@ -import { Matrix4, Ray, Vector3 } from 'three'; +import { Matrix4, Ray, Vector3, REVISION } from 'three'; +// In three.js r165 and higher raycast traversal can be ended early +const REVISION_165 = parseInt( REVISION ) < 165; const _mat = new Matrix4(); const _localRay = new Ray(); const _vec = new Vector3(); @@ -13,20 +15,28 @@ function distanceSort( a, b ) { function intersectTileScene( scene, raycaster, intersects ) { - // Don't intersect the box3 helpers because those are used for debugging - scene.traverse( c => { + if ( REVISION_165 ) { - // We set the default raycast function to empty so three.js doesn't automatically cast against it - Object.getPrototypeOf( c ).raycast.call( c, raycaster, intersects ); + // Don't intersect the box3 helpers because those are used for debugging + scene.traverse( c => { - } ); + // We set the default raycast function to empty so three.js doesn't automatically cast against it + Object.getPrototypeOf( c ).raycast.call( c, raycaster, intersects ); + + } ); + _hitArray.sort( distanceSort ); + + } else { + + raycaster.intersectObject( scene, true, intersects ); + + } } function intersectTileSceneFirstHist( scene, raycaster ) { intersectTileScene( scene, raycaster, _hitArray ); - _hitArray.sort( distanceSort ); const hit = _hitArray[ 0 ] || null; _hitArray.length = 0;