Explorar el Código

Make optimizeRaycast option work through the renderers state

Instead of replacing raycast() with emptyRaycast, we now call the
overridenRaycast() function which checks the state of the tilesRenderer
and decides whether to call the prototype's raycast() function or not.
Stelios Vitalis hace 4 años
padre
commit
3a02e2f9e9
Se han modificado 2 ficheros con 32 adiciones y 89 borrados
  1. 3 2
      example/index.js
  2. 29 87
      src/three/TilesRenderer.js

+ 3 - 2
example/index.js

@@ -51,7 +51,7 @@ let params = {
 
 	'enableUpdate': true,
 	'raycast': NONE,
-	'overrideRaycast': true,
+	'optimizeRaycast': true,
 	'enableCacheDisplay': false,
 	'enableRendererStats': false,
 	'orthographic': false,
@@ -274,7 +274,7 @@ function init() {
 
 	} );
 	exampleOptions.add( params, 'raycast', { NONE, ALL_HITS, FIRST_HIT_ONLY } );
-	exampleOptions.add( params, 'overrideRaycast', );
+	exampleOptions.add( params, 'optimizeRaycast', );
 	exampleOptions.add( params, 'enableCacheDisplay' );
 	exampleOptions.add( params, 'enableRendererStats' );
 	exampleOptions.open();
@@ -444,6 +444,7 @@ function animate() {
 	tiles.errorTarget = params.errorTarget;
 	tiles.errorThreshold = params.errorThreshold;
 	tiles.loadSiblings = params.loadSiblings;
+	tiles.optimizeRaycast = params.optimizeRaycast;
 	tiles.stopAtEmptyTiles = params.stopAtEmptyTiles;
 	tiles.displayActiveTiles = params.displayActiveTiles;
 	tiles.maxDepth = params.maxDepth;

+ 29 - 87
src/three/TilesRenderer.js

@@ -12,8 +12,7 @@ import {
 	Vector2,
 	Math as MathUtils,
 	Frustum,
-	LoadingManager,
-	Group,
+	LoadingManager
 } from 'three';
 import { raycastTraverse, raycastTraverseFirstHit } from './raycastTraverse.js';
 
@@ -29,7 +28,15 @@ const vecZ = new Vector3();
 const X_AXIS = new Vector3( 1, 0, 0 );
 const Y_AXIS = new Vector3( 0, 1, 0 );
 
-function emptyRaycast() {}
+function overridenRaycast( raycaster, intersects ) {
+
+	if ( ! this.userData.tilesRenderer.optimizeRaycast ) {
+
+		Object.getPrototypeOf( this ).raycast.call( this, raycaster, intersects );
+
+	}
+
+}
 
 function updateFrustumCulled( object, toInitialValue ) {
 
@@ -68,67 +75,6 @@ export class TilesRenderer extends TilesRendererBase {
 
 	}
 
-	get optimizeRaycast() {
-
-		return this._optimizeRaycast;
-
-	}
-
-	set optimizeRaycast( value ) {
-
-		if ( this._optimizeRaycast != value ) {
-
-			this._optimizeRaycast = value;
-
-			if ( this._optimizeRaycast ) {
-
-				this.traverse( tile => {
-
-					if ( tile instanceof TilesGroup ) {
-
-						this.raycast = TilesGroup.prototype.raycast;
-
-					} else {
-
-						this.raycast = emptyRaycast;
-
-					}
-
-				} );
-
-			} else {
-
-				this.traverse( tile => {
-
-					if ( tile instanceof TilesGroup ) {
-
-						console.log( "Replace to group's raycast" );
-						tile.raycast = Group.prototype.raycast;
-
-					} else {
-
-						tile.raycast = Object.getPrototypeOf( tile ).raycast;
-
-					}
-
-				} );
-
-			}
-
-			this.traverse( tile => {
-
-				if ( tile.scene ) {
-
-					updateFrustumCulled( tile.scene, value );
-
-				}
-
-			} );
-
-		}
-
-	}
-
 	constructor( ...args ) {
 
 		super( ...args );
@@ -139,7 +85,7 @@ export class TilesRenderer extends TilesRendererBase {
 		this.activeTiles = new Set();
 		this.visibleTiles = new Set();
 		this._autoDisableRendererCulling = true;
-		this._optimizeRaycast = true;
+		this.optimizeRaycast = true;
 
 		this.onLoadTileSet = null;
 		this.onLoadModel = null;
@@ -235,30 +181,28 @@ export class TilesRenderer extends TilesRendererBase {
 
 	raycast( raycaster, intersects ) {
 
-		if ( ! this._optimizeRaycast ) {
-
-			return;
+		if ( this.optimizeRaycast ) {
 
-		}
+			if ( ! this.root ) {
 
-		if ( ! this.root ) {
+				return;
 
-			return;
+			}
 
-		}
+			if ( raycaster.firstHitOnly ) {
 
-		if ( raycaster.firstHitOnly ) {
+				const hit = raycastTraverseFirstHit( this.root, this.group, this.activeTiles, raycaster );
+				if ( hit ) {
 
-			const hit = raycastTraverseFirstHit( this.root, this.group, this.activeTiles, raycaster );
-			if ( hit ) {
+					intersects.push( hit );
 
-				intersects.push( hit );
+				}
 
-			}
+			} else {
 
-		} else {
+				raycastTraverse( this.root, this.group, this.activeTiles, raycaster, intersects );
 
-			raycastTraverse( this.root, this.group, this.activeTiles, raycaster, intersects );
+			}
 
 		}
 
@@ -683,16 +627,14 @@ export class TilesRenderer extends TilesRendererBase {
 
 			cached.scene = scene;
 
-			if ( this._optimizeRaycast ) {
-
-				// We handle raycasting in a custom way so remove it from here
-				scene.traverse( c => {
-
-					c.raycast = emptyRaycast;
+			let renderer = this;
+			// We handle raycasting in a custom way so remove it from here
+			scene.traverse( c => {
 
-				} );
+				c.userData.tilesRenderer = renderer;
+				c.raycast = overridenRaycast;
 
-			}
+			} );
 
 			const materials = [];
 			const geometry = [];