Jelajahi Sumber

Add fetch tile function

Garrett Johnson 4 tahun lalu
induk
melakukan
e75ee603a0
1 mengubah file dengan 126 tambahan dan 120 penghapusan
  1. 126 120
      src/base/TilesRendererBase.js

+ 126 - 120
src/base/TilesRendererBase.js

@@ -239,41 +239,58 @@ export class TilesRendererBase {
 	}
 
 	// Private Functions
-	loadTileSet( url ) {
+	fetchTileSet( url, fetchOptions, parent = null ) {
 
-		const tileSets = this.tileSets;
-		if ( ! ( url in tileSets ) ) {
+		return fetch( url, fetchOptions )
+			.then( res => {
 
-			const pr =
-				fetch( url, this.fetchOptions )
-					.then( res => {
+				if ( res.ok ) {
 
-						if ( res.ok ) {
+					return res.json();
 
-							return res.json();
+				} else {
 
-						} else {
+					throw new Error( `TilesRenderer: Failed to load tileset "${ url }" with status ${ res.status } : ${ res.statusText }` );
 
-							throw new Error( `TilesRenderer: Failed to load tileset "${ url }" with status ${ res.status } : ${ res.statusText }` );
+				}
 
-						}
+			} )
+			.then( json => {
 
-					} )
-					.then( json => {
+				const version = json.asset.version;
+				console.assert(
+					version === '1.0' || version === '0.0',
+					'asset.version is expected to be a string of "1.0" or "0.0"'
+				);
 
-						const version = json.asset.version;
-						console.assert(
-							version === '1.0' || version === '0.0',
-							'asset.version is expected to be a string of "1.0" or "0.0"'
-						);
+				const basePath = path.dirname( url );
 
-						const basePath = path.dirname( url );
+				traverseSet(
+					json.root,
+					( node, parent ) => this.preprocessNode( node, parent, basePath ),
+					null,
+					parent,
+					parent ? parent.__depth : 0,
+				);
 
-						traverseSet( json.root, ( node, parent ) => this.preprocessNode( node, parent, basePath ) );
+				return json;
 
-						tileSets[ url ] = json;
+			} );
 
-					} );
+	}
+
+	loadTileSet( url ) {
+
+		const tileSets = this.tileSets;
+		if ( ! ( url in tileSets ) ) {
+
+			const pr = this
+				.fetchTileSet( url, this.fetchOptions )
+				.then( json => {
+
+					tileSets[ url ] = json;
+
+				} );
 
 			pr.catch( err => {
 
@@ -361,177 +378,166 @@ export class TilesRendererBase {
 		tile.__loadAbort = controller;
 		tile.__loadingState = LOADING;
 
-		downloadQueue.add( tile, tile => {
+		const errorCallback = e => {
 
+			// if it has been unloaded then the tile has been disposed
 			if ( tile.__loadIndex !== loadIndex ) {
 
-				return Promise.resolve();
+				return;
 
 			}
 
-			return fetch( tile.content.uri, Object.assign( { signal }, this.fetchOptions ) );
+			if ( e.name !== 'AbortError' ) {
 
-		} )
-			.then( res => {
+				parseQueue.remove( tile );
+				downloadQueue.remove( tile );
 
-				if ( tile.__loadIndex !== loadIndex ) {
+				if ( tile.__loadingState === PARSING ) {
 
-					return;
+					stats.parsing --;
 
-				}
+				} else if ( tile.__loadingState === LOADING ) {
 
-				if ( isExternalTileSet ) {
+					stats.downloading --;
 
-					if ( res.ok ) {
+				}
 
-						return res.json();
+				stats.failed ++;
 
-					} else {
+				console.error( 'TilesRenderer : Failed to load tile.' );
+				console.error( e );
+				tile.__loadingState = FAILED;
 
-						throw new Error( `Failed to external tileset with error code ${res.status}` );
+			} else {
 
-					}
+				lruCache.remove( tile );
 
-				} else {
+			}
 
-					if ( res.ok ) {
+		};
 
-						return res.arrayBuffer();
+		if ( isExternalTileSet ) {
 
-					} else {
+			downloadQueue.add( tile, tile => {
 
-						throw new Error( `Failed to load model with error code ${res.status}` );
+				if ( tile.__loadIndex !== loadIndex ) {
 
-					}
+					return Promise.resolve();
 
 				}
 
+				return this.fetchTileSet( tile.content.uri, Object.assign( { signal }, this.fetchOptions ) );
+
 			} )
-			.then( data => {
+				.then( json => {
 
-				// if it has been unloaded then the tile has been disposed
-				if ( tile.__loadIndex !== loadIndex ) {
+					if ( tile.__loadIndex !== loadIndex ) {
 
-					return;
+						return;
 
-				}
+					}
 
-				stats.downloading --;
-				stats.parsing ++;
-				tile.__loadAbort = null;
-				tile.__loadingState = PARSING;
-
-				if ( isExternalTileSet ) {
-
-					const json = data;
-					const version = json.asset.version;
-					console.assert(
-						version === '1.0' || version === '0.0',
-						'asset.version is expected to be a string of "1.0" or "0.0"'
-					);
-
-					const basePath = path.dirname( tile.content.uri );
-					traverseSet(
-						json.root,
-						( node, parent ) => this.preprocessNode( node, parent, basePath ),
-						null,
-						tile,
-						tile.__depth,
-					);
+					stats.downloading --;
+					tile.__loadAbort = null;
+					tile.__loadingState = LOADED;
 
 					tile.children.push( json.root );
-					console.log( 'LOADED' );
-					console.log( tile );
-					return;
-
-				} else {
-
-					const buffer = data;
-					return parseQueue.add( tile, tile => {
-
-						// if it has been unloaded then the tile has been disposed
-						if ( tile.__loadIndex !== loadIndex ) {
 
-							return Promise.resolve();
+				} )
+				.catch( errorCallback );
 
-						}
+		} else {
 
-						const uri = tile.content.uri;
-						const extension = uri.split( /\./g ).pop();
+			downloadQueue.add( tile, tile => {
 
-						return this.parseTile( buffer, tile, extension );
+				if ( tile.__loadIndex !== loadIndex ) {
 
-					} );
+					return Promise.resolve();
 
 				}
 
+				return fetch( tile.content.uri, Object.assign( { signal }, this.fetchOptions ) );
+
 			} )
-			.then( () => {
+				.then( res => {
 
-				// if it has been unloaded then the tile has been disposed
-				if ( tile.__loadIndex !== loadIndex ) {
+					if ( tile.__loadIndex !== loadIndex ) {
 
-					return;
+						return;
 
-				}
+					}
 
-				stats.parsing --;
-				tile.__loadingState = LOADED;
+					if ( res.ok ) {
 
-				if ( ! isExternalTileSet ) {
+						return res.arrayBuffer();
 
-					if ( tile.__wasSetVisible ) {
+					} else {
 
-						this.setTileVisible( tile, true );
+						throw new Error( `Failed to load model with error code ${res.status}` );
 
 					}
 
-					if ( tile.__wasSetActive ) {
+				} )
+				.then( data => {
 
-						this.setTileActive( tile, true );
+					// if it has been unloaded then the tile has been disposed
+					if ( tile.__loadIndex !== loadIndex ) {
+
+						return;
 
 					}
 
-				}
+					stats.downloading --;
+					stats.parsing ++;
+					tile.__loadAbort = null;
+					tile.__loadingState = PARSING;
 
-			} )
-			.catch( e => {
+					const buffer = data;
+					return parseQueue.add( tile, tile => {
 
-				// if it has been unloaded then the tile has been disposed
-				if ( tile.__loadIndex !== loadIndex ) {
+						// if it has been unloaded then the tile has been disposed
+						if ( tile.__loadIndex !== loadIndex ) {
 
-					return;
+							return Promise.resolve();
 
-				}
+						}
 
-				if ( e.name !== 'AbortError' ) {
+						const uri = tile.content.uri;
+						const extension = uri.split( /\./g ).pop();
 
-					parseQueue.remove( tile );
-					downloadQueue.remove( tile );
+						return this.parseTile( buffer, tile, extension );
 
-					if ( tile.__loadingState === PARSING ) {
+					} );
 
-						stats.parsing --;
+				} )
+				.then( () => {
 
-					} else if ( tile.__loadingState === LOADING ) {
+					// if it has been unloaded then the tile has been disposed
+					if ( tile.__loadIndex !== loadIndex ) {
 
-						stats.downloading --;
+						return;
 
 					}
 
-					stats.failed ++;
+					stats.parsing --;
+					tile.__loadingState = LOADED;
+
+					if ( tile.__wasSetVisible ) {
 
-					console.error( 'TilesRenderer : Failed to load tile.' );
-					console.error( e );
-					tile.__loadingState = FAILED;
+						this.setTileVisible( tile, true );
 
-				} else {
+					}
 
-					lruCache.remove( tile );
+					if ( tile.__wasSetActive ) {
 
-				}
+						this.setTileActive( tile, true );
 
-			} );
+					}
+
+				} )
+				.catch( errorCallback );
+
+		}
 
 	}