瀏覽代碼

Add comments, cleanup

Garrett Johnson 5 年之前
父節點
當前提交
9941786a4b
共有 4 個文件被更改,包括 41 次插入42 次删除
  1. 20 25
      src/base/TilesRendererBase.js
  2. 0 2
      src/base/traverseFunctions.js
  3. 14 3
      src/three/DebugTilesRenderer.js
  4. 7 12
      src/three/raycastTraverse.js

+ 20 - 25
src/base/TilesRendererBase.js

@@ -4,11 +4,6 @@ import { PriorityQueue } from '../utilities/PriorityQueue.js';
 import { determineFrustumSet, toggleTiles, skipTraversal, markUsedSetLeaves, traverseSet } from './traverseFunctions.js';
 import { UNLOADED, LOADING, PARSING, LOADED, FAILED } from './constants.js';
 
-// TODO: Address the issue of too many promises, garbage collection
-// TODO: See if using classes improves performance
-// TODO: See if declaring function inline improves performance
-// TODO: Make sure active state works as expected
-
 // Function for sorting the evicted LRU items. We should evict the shallowest depth first.
 const priorityCallback = tile => 1 / ( tile.__depthFromRenderedParent + 1 );
 
@@ -117,7 +112,6 @@ export class TilesRendererBase {
 		skipTraversal( root, this );
 		toggleTiles( root, this );
 
-		// TODO: We may want to add this function in the requestTileContents function
 		lruCache.scheduleUnload();
 
 	}
@@ -232,33 +226,32 @@ export class TilesRendererBase {
 		if ( ! ( url in tileSets ) ) {
 
 			const pr =
-                fetch( url, this.fetchOptions )
-                	.then( res => {
+				fetch( url, this.fetchOptions )
+					.then( res => {
 
-                		if ( res.ok ) {
+						if ( res.ok ) {
 
-                			return res.json();
+							return res.json();
 
-                		} else {
+						} else {
 
-                			throw new Error( `Status ${ res.status } (${ res.statusText })` );
+							throw new Error( `Status ${ res.status } (${ res.statusText })` );
 
-                		}
+						}
 
-                	} )
-                	.then( json => {
+					} )
+					.then( json => {
 
-                		// TODO: Add version query?
-                		const version = json.asset.version;
-                		console.assert( version === '1.0' || version === '0.0' );
+						const version = json.asset.version;
+						console.assert( version === '1.0' || version === '0.0' );
 
-                		const basePath = path.dirname( url );
+						const basePath = path.dirname( url );
 
-                		traverseSet( json.root, ( node, parent ) => this.preprocessNode( node, parent, basePath ) );
+						traverseSet( json.root, ( node, parent ) => this.preprocessNode( node, parent, basePath ) );
 
-                		tileSets[ url ] = json;
+						tileSets[ url ] = json;
 
-                	} );
+					} );
 
 			pr.catch( e => {
 
@@ -286,12 +279,13 @@ export class TilesRendererBase {
 
 		}
 
-		// TODO: reuse the functions created here?
+		const stats = this.stats;
 		const lruCache = this.lruCache;
 		const downloadQueue = this.downloadQueue;
 		const parseQueue = this.parseQueue;
 		lruCache.add( tile, t => {
 
+			// Stop the load if it's started
 			if ( t.__loadingState === LOADING ) {
 
 				t.__loadAbort.abort();
@@ -303,6 +297,7 @@ export class TilesRendererBase {
 
 			}
 
+			// Decrement stats
 			if ( t.__loadingState === LOADING ) {
 
 				stats.downloading --;
@@ -322,10 +317,10 @@ export class TilesRendererBase {
 
 		} );
 
+		// Track a new load index so we avoid the condition where this load is stopped and
+		// another begins soon after so we don't parse twice.
 		tile.__loadIndex ++;
 		const loadIndex = tile.__loadIndex;
-		const stats = this.stats;
-
 		const controller = new AbortController();
 		const signal = controller.signal;
 

+ 0 - 2
src/base/traverseFunctions.js

@@ -184,8 +184,6 @@ export function markUsedSetLeaves( tile, renderer ) {
 		// considered to be in the used set then we shouldn't set ourselves to a leaf here.
 		tile.__isLeaf = true;
 
-		// TODO: stats
-
 	} else {
 
 		let childrenWereVisible = false;

+ 14 - 3
src/three/DebugTilesRenderer.js

@@ -3,6 +3,7 @@ import { TilesRenderer } from './TilesRenderer.js';
 import { SphereHelper } from './SphereHelper.js';
 
 const ORIGINAL_MATERIAL = Symbol( 'ORIGINAL_MATERIAL' );
+const HAS_RANDOM_COLOR = Symbol( 'HAS_RANDOM_COLOR' );
 
 function emptyRaycast() {}
 
@@ -44,6 +45,7 @@ export class DebugTilesRenderer extends TilesRenderer {
 
 	initExtremes() {
 
+		// initialize the extreme values of the hierarchy
 		let maxDepth = - 1;
 		this.traverse( tile => {
 
@@ -74,6 +76,8 @@ export class DebugTilesRenderer extends TilesRenderer {
 
 	getTileInformationFromActiveObject( object ) {
 
+		// Find which tile this scene is associated with. This is slow and
+		// intended for debug purposes only.
 		let targetTile = null;
 		const activeTiles = this.activeTiles;
 		activeTiles.forEach( tile => {
@@ -131,9 +135,11 @@ export class DebugTilesRenderer extends TilesRenderer {
 
 		}
 
+		// set box or sphere visibility
 		this.boxGroup.visible = this.displayBoxBounds;
 		this.sphereGroup.visible = this.displaySphereBounds;
 
+		// get max values to use for materials
 		let maxDepth = - 1;
 		if ( this.maxDebugDepth === - 1 ) {
 
@@ -179,6 +185,7 @@ export class DebugTilesRenderer extends TilesRenderer {
 				const currMaterial = c.material;
 				if ( currMaterial ) {
 
+					// Reset the material if needed
 					const originalMaterial = c[ ORIGINAL_MATERIAL ];
 					if ( colorMode === NONE && currMaterial !== originalMaterial ) {
 
@@ -193,10 +200,11 @@ export class DebugTilesRenderer extends TilesRenderer {
 
 					if ( colorMode !== RANDOM_COLOR ) {
 
-						delete c.material.__randomColor;
+						delete c.material[ HAS_RANDOM_COLOR ];
 
 					}
 
+					// Set the color on the basic material
 					switch ( colorMode ) {
 
 						case DEPTH: {
@@ -260,13 +268,13 @@ export class DebugTilesRenderer extends TilesRenderer {
 						}
 						case RANDOM_COLOR: {
 
-							if ( ! c.material.__randomColor ) {
+							if ( ! c.material[ HAS_RANDOM_COLOR ] ) {
 
 								const h = Math.random();
 								const s = 0.5 + Math.random() * 0.5;
 								const l = 0.375 + Math.random() * 0.25;
 								c.material.color.setHSL( h, s, l );
-								c.material.__randomColor = true;
+								c.material[ HAS_RANDOM_COLOR ] = true;
 
 							}
 							break;
@@ -323,6 +331,7 @@ export class DebugTilesRenderer extends TilesRenderer {
 					const cachedBox = cached.box;
 					const cachedBoxMat = cached.boxTransform;
 
+					// Create debug bounding box
 					const boxHelperGroup = new Group();
 					boxHelperGroup.matrix.copy( cachedBoxMat );
 					boxHelperGroup.matrix.decompose( boxHelperGroup.position, boxHelperGroup.quaternion, boxHelperGroup.scale );
@@ -340,6 +349,7 @@ export class DebugTilesRenderer extends TilesRenderer {
 
 					}
 
+					// Create debugbounding sphere
 					const cachedSphere = cached.sphere;
 					const sphereHelper = new SphereHelper( cachedSphere );
 					sphereHelper.raycast = emptyRaycast;
@@ -352,6 +362,7 @@ export class DebugTilesRenderer extends TilesRenderer {
 
 					}
 
+					// Cache the original materials
 					scene.traverse( c => {
 
 						const material = c.material;

+ 7 - 12
src/three/raycastTraverse.js

@@ -20,6 +20,7 @@ function intersectTileScene( scene, raycaster, intersects ) {
 
 		if ( ! ( c instanceof Box3Helper ) ) {
 
+			// 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 );
 
 		}
@@ -81,7 +82,7 @@ export function raycastTraverseFirstHit( root, group, activeTiles, raycaster ) {
 
 		}
 
-		// TODO: check region
+		// TODO: check region?
 
 		const boundingBox = cached.box;
 		const obbMat = cached.boxTransform;
@@ -89,7 +90,8 @@ export function raycastTraverseFirstHit( root, group, activeTiles, raycaster ) {
 
 			_mat.multiply( obbMat );
 			_mat.getInverse( _mat );
-			_ray.copy( raycaster.ray ).applyMatrix4( _mat );
+			_ray.copy( raycaster.ray );
+			_ray.applyMatrix4( _mat );
 			if ( _ray.intersectBox( boundingBox, _vec ) ) {
 
 				// account for tile scale
@@ -144,7 +146,6 @@ export function raycastTraverseFirstHit( root, group, activeTiles, raycaster ) {
 			const scene = tile.cached.scene;
 
 			let hit = null;
-
 			if ( activeTiles.has( tile ) ) {
 
 				// save the hit if it's closer
@@ -195,6 +196,7 @@ export function raycastTraverse( tile, group, activeTiles, raycaster, intersects
 
 	_mat.copy( groupMatrixWorld );
 
+	// Early out if we don't hit this tile sphere
 	const sphere = cached.sphere;
 	if ( sphere ) {
 
@@ -208,6 +210,7 @@ export function raycastTraverse( tile, group, activeTiles, raycaster, intersects
 
 	}
 
+	// Early out if we don't this this tile box
 	const boundingBox = cached.box;
 	const obbMat = cached.boxTransform;
 	if ( boundingBox ) {
@@ -228,15 +231,7 @@ export function raycastTraverse( tile, group, activeTiles, raycaster, intersects
 	const scene = cached.scene;
 	if ( activeTiles.has( tile ) ) {
 
-		scene.traverse( c => {
-
-			if ( ! ( c instanceof Box3Helper ) ) {
-
-				Object.getPrototypeOf( c ).raycast.call( c, raycaster, intersects );
-
-			}
-
-		} );
+		intersectTileScene( scene, raycaster, intersects );
 		return;
 
 	}