Ver código fonte

Add external tileset support

Garrett Johnson 4 anos atrás
pai
commit
f5dc7f9004
2 arquivos alterados com 90 adições e 26 exclusões
  1. 87 23
      src/base/TilesRendererBase.js
  2. 3 3
      src/base/traverseFunctions.js

+ 87 - 23
src/base/TilesRendererBase.js

@@ -167,7 +167,20 @@ export class TilesRendererBase {
 
 		tile.parent = parentTile;
 		tile.children = tile.children || [];
-		tile.__contentEmpty = ! tile.content || ! tile.content.uri;
+
+		const uri = tile.content && tile.content.uri;
+		if ( uri ) {
+
+			const isExternalTileSet = /\.json$/i.test( tile.content.uri );
+			tile.__externalTileSet = isExternalTileSet;
+			tile.__contentEmpty = isExternalTileSet;
+
+		} else {
+
+			tile.__externalTileSet = false;
+			tile.__contentEmpty = true;
+
+		}
 
 		tile.__error = 0.0;
 		tile.__inFrustum = false;
@@ -298,6 +311,7 @@ export class TilesRendererBase {
 		const lruCache = this.lruCache;
 		const downloadQueue = this.downloadQueue;
 		const parseQueue = this.parseQueue;
+		const isExternalTileSet = tile.__externalTileSet;
 		lruCache.add( tile, t => {
 
 			// Stop the load if it's started
@@ -306,6 +320,10 @@ export class TilesRendererBase {
 				t.__loadAbort.abort();
 				t.__loadAbort = null;
 
+			} else if ( isExternalTileSet ) {
+
+				t.children.length = 0;
+
 			} else {
 
 				this.disposeTile( t );
@@ -323,7 +341,6 @@ export class TilesRendererBase {
 
 			}
 
-			t.__buffer = null;
 			t.__loadingState = UNLOADED;
 			t.__loadIndex ++;
 
@@ -342,6 +359,7 @@ export class TilesRendererBase {
 		stats.downloading ++;
 		tile.__loadAbort = controller;
 		tile.__loadingState = LOADING;
+
 		downloadQueue.add( tile, tile => {
 
 			if ( tile.__loadIndex !== loadIndex ) {
@@ -361,18 +379,34 @@ export class TilesRendererBase {
 
 				}
 
-				if ( res.ok ) {
+				if ( isExternalTileSet ) {
+
+					if ( res.ok ) {
+
+						return res.json();
 
-					return res.arrayBuffer();
+					} else {
+
+						throw new Error( `Failed to external tileset with error code ${res.status}` );
+
+					}
 
 				} else {
 
-					throw new Error( `Failed to load model with error code ${res.status}` );
+					if ( res.ok ) {
+
+						return res.arrayBuffer();
+
+					} else {
+
+						throw new Error( `Failed to load model with error code ${res.status}` );
+
+					}
 
 				}
 
 			} )
-			.then( buffer => {
+			.then( data => {
 
 				// if it has been unloaded then the tile has been disposed
 				if ( tile.__loadIndex !== loadIndex ) {
@@ -385,25 +419,50 @@ export class TilesRendererBase {
 				stats.parsing ++;
 				tile.__loadAbort = null;
 				tile.__loadingState = PARSING;
-				tile.__buffer = buffer;
 
-				return parseQueue.add( tile, tile => {
+				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,
+					);
+
+					tile.children.push( json.root );
+					console.log( 'LOADED' );
+					console.log( tile );
+					return;
 
-					// if it has been unloaded then the tile has been disposed
-					if ( tile.__loadIndex !== loadIndex ) {
+				} else {
 
-						return Promise.resolve();
+					const buffer = data;
+					return parseQueue.add( tile, tile => {
 
-					}
+						// if it has been unloaded then the tile has been disposed
+						if ( tile.__loadIndex !== loadIndex ) {
 
-					const uri = tile.content.uri;
-					const extension = uri.split( /\./g ).pop();
-					const buffer = tile.__buffer;
-					tile.__buffer = null;
+							return Promise.resolve();
 
-					return this.parseTile( buffer, tile, extension );
+						}
+
+						const uri = tile.content.uri;
+						const extension = uri.split( /\./g ).pop();
 
-				} );
+						return this.parseTile( buffer, tile, extension );
+
+					} );
+
+				}
 
 			} )
 			.then( () => {
@@ -417,15 +476,20 @@ export class TilesRendererBase {
 
 				stats.parsing --;
 				tile.__loadingState = LOADED;
-				if ( tile.__wasSetVisible ) {
 
-					this.setTileVisible( tile, true );
+				if ( ! isExternalTileSet ) {
 
-				}
+					if ( tile.__wasSetVisible ) {
+
+						this.setTileVisible( tile, true );
 
-				if ( tile.__wasSetActive ) {
+					}
+
+					if ( tile.__wasSetActive ) {
 
-					this.setTileActive( tile, true );
+						this.setTileActive( tile, true );
+
+					}
 
 				}
 

+ 3 - 3
src/base/traverseFunctions.js

@@ -54,7 +54,7 @@ function recursivelyMarkUsed( tile, frameCount, lruCache ) {
 
 function recursivelyLoadTiles( tile, depthFromRenderedParent, renderer ) {
 
-	if ( tile.__contentEmpty ) {
+	if ( tile.__contentEmpty && ! tile.__externalTileSet ) {
 
 		const children = tile.children;
 		for ( let i = 0, l = children.length; i < l; i ++ ) {
@@ -271,7 +271,7 @@ export function skipTraversal( tile, renderer ) {
 			tile.__active = true;
 			stats.active ++;
 
-		} else if ( ! lruCache.isFull() && ! tile.__contentEmpty ) {
+		} else if ( ! lruCache.isFull() && ( ! tile.__contentEmpty || tile.__externalTileSet ) ) {
 
 			renderer.requestTileContents( tile );
 
@@ -284,7 +284,7 @@ export function skipTraversal( tile, renderer ) {
 	const errorRequirement = ( renderer.errorTarget + 1 ) * renderer.errorThreshold;
 	const meetsSSE = tile.__error <= errorRequirement;
 	const includeTile = meetsSSE || tile.refine === 'ADD';
-	const hasContent = ! tile.__contentEmpty;
+	const hasContent = ! tile.__contentEmpty || tile.__externalTileSet;
 	const loadedContent = isDownloadFinished( tile.__loadingState ) && hasContent;
 	const childrenWereVisible = tile.__childrenWereVisible;
 	const children = tile.children;