Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/core/plugins/CesiumIonAuthPlugin.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { TilesRendererBase } from '3d-tiles-renderer/core';

export class CesiumIonAuthPlugin {

constructor( options : {
apiToken: string,
assetId?: string | null,
autoRefreshToken?: boolean,
assetTypeHandler?: ( type: string, tiles: TilesRendererBase, asset: object ) => void,
} );

}
193 changes: 193 additions & 0 deletions src/core/plugins/CesiumIonAuthPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
import { CesiumIonAuth } from './auth/CesiumIonAuth.js';
import { GoogleCloudAuthPlugin } from './GoogleCloudAuthPlugin.js';

export class CesiumIonAuthPlugin {

get apiToken() {

return this.auth.apiToken;

}

set apiToken( v ) {

this.auth.apiToken = v;

}

get autoRefreshToken() {

return this.auth.autoRefreshToken;

}

set autoRefreshToken( v ) {

this.auth.autoRefreshToken = v;

}

constructor( options = {} ) {

const {
apiToken,
assetId = null,
autoRefreshToken = false,
useRecommendedSettings = true,
assetTypeHandler = ( type, tiles, asset ) => {

console.warn( `CesiumIonAuthPlugin: Cesium Ion asset type "${ type }" unhandled.` );

},
} = options;

this.name = 'CESIUM_ION_AUTH_PLUGIN';
this.auth = new CesiumIonAuth( { apiToken, autoRefreshToken } );

this.assetId = assetId;
this.autoRefreshToken = autoRefreshToken;
this.useRecommendedSettings = useRecommendedSettings;
this.assetTypeHandler = assetTypeHandler;
this.tiles = null;

this._tileSetVersion = - 1;
this._attributions = [];

}

init( tiles ) {

if ( this.assetId !== null ) {

tiles.rootURL = `https://api.cesium.com/v1/assets/${ this.assetId }/endpoint`;

}

this.tiles = tiles;
this.auth.authURL = tiles.rootURL;

// reset the tiles in case this plugin was removed and re-added
tiles.resetFailedTiles();

}

loadRootTileSet() {

// ensure we have an up-to-date token and root url, then trigger the internal
// root tile set load function
return this
.auth
.refreshToken()
.then( json => {

this._initializeFromAsset( json );
return this.tiles.invokeOnePlugin( plugin => plugin !== this && plugin.loadRootTileSet && plugin.loadRootTileSet() );

} )
.catch( error => {

this.tiles.dispatchEvent( {
type: 'load-error',
tile: null,
error,
url: this.auth.authURL,
} );

} );

}

preprocessURL( uri ) {

uri = new URL( uri );
if ( /^http/.test( uri.protocol ) && this._tileSetVersion != - 1 ) {

uri.searchParams.set( 'v', this._tileSetVersion );

}
return uri.toString();

}

fetchData( uri, options ) {

const tiles = this.tiles;
if ( tiles.getPluginByName( 'GOOGLE_CLOUD_AUTH_PLUGIN' ) !== null ) {

return null;

} else {

return this.auth.fetch( uri, options );

}

}

getAttributions( target ) {

if ( this.tiles.visibleTiles.size > 0 ) {

target.push( ...this._attributions );

}

}

_initializeFromAsset( json ) {

const tiles = this.tiles;
if ( 'externalType' in json ) {

const url = new URL( json.options.url );
tiles.rootURL = json.options.url;

// if the tile set is "external" then assume it's a google API tile set
tiles.registerPlugin( new GoogleCloudAuthPlugin( {
apiToken: url.searchParams.get( 'key' ),
autoRefreshToken: this.autoRefreshToken,
useRecommendedSettings: this.useRecommendedSettings,
} ) );

} else {

// fire callback for unhandled asset types
if ( json.type !== '3DTILES' ) {

// Other types include:
// - GLTF
// - CZML
// - KML
// - GEOJSON
// - TERRAIN (QuantizedMesh)
// - IMAGERY (TSM Tiles)

this.assetTypeHandler( json.type, tiles, json );

}

tiles.rootURL = json.url;

// save the version key if present
const url = new URL( json.url );
if ( url.searchParams.has( 'v' ) && this._tileSetVersion === - 1 ) {

this._tileSetVersion = url.searchParams.get( 'v' );

}

if ( json.attributions ) {

this._attributions = json.attributions.map( att => ( {
value: att.html,
type: 'html',
collapsible: att.collapsible,
} ) );

}

}

}

}
11 changes: 11 additions & 0 deletions src/core/plugins/GoogleCloudAuthPlugin.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export class GoogleCloudAuthPlugin {

constructor( options: {
apiToken: string,
autoRefreshToken?: boolean,
logoUrl?: string,
useRecommendedSettings?: boolean;
sessionOptions?: null | { mapType: string, language: string, region: string, [key: string]: any },
} );

}
117 changes: 117 additions & 0 deletions src/core/plugins/GoogleCloudAuthPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { GoogleCloudAuth } from '3d-tiles-renderer/core/plugins';
import { GoogleAttributionsManager } from './GoogleAttributionsManager.js';

const TILES_3D_API = 'https://tile.googleapis.com/v1/3dtiles/root.json';

export class GoogleCloudAuthPlugin {

constructor( {
apiToken,
sessionOptions = null,
autoRefreshToken = false,
logoUrl = null,
useRecommendedSettings = true,
} ) {

this.name = 'GOOGLE_CLOUD_AUTH_PLUGIN';

this.apiToken = apiToken;
this.useRecommendedSettings = useRecommendedSettings;
this.logoUrl = logoUrl;

this.auth = new GoogleCloudAuth( { apiToken, autoRefreshToken, sessionOptions } );
this.tiles = null;

this._visibilityChangeCallback = null;
this._attributionsManager = new GoogleAttributionsManager();
this._logoAttribution = {
value: '',
type: 'image',
collapsible: false,
};
this._attribution = {
value: '',
type: 'string',
collapsible: true,
};

}

init( tiles ) {

const { useRecommendedSettings, auth } = this;

// reset the tiles in case this plugin was removed and re-added
tiles.resetFailedTiles();

if ( tiles.rootURL == null ) {

tiles.rootURL = TILES_3D_API;

}

if ( ! auth.sessionOptions ) {

auth.authURL = tiles.rootURL;

}

if ( useRecommendedSettings && ! auth.isMapTilesSession ) {

// This plugin changes below values to be more efficient for the photorealistic tiles
tiles.errorTarget = 20;

}

this.tiles = tiles;

this._visibilityChangeCallback = ( { tile, visible } ) => {

const copyright = tile.cached.metadata?.asset?.copyright || '';
if ( visible ) {

this._attributionsManager.addAttributions( copyright );

} else {

this._attributionsManager.removeAttributions( copyright );

}

};

tiles.addEventListener( 'tile-visibility-change', this._visibilityChangeCallback );

}

getAttributions( target ) {

if ( this.tiles.visibleTiles.size > 0 ) {

if ( this.logoUrl ) {

this._logoAttribution.value = this.logoUrl;
target.push( this._logoAttribution );

}

this._attribution.value = this._attributionsManager.toString();
target.push( this._attribution );

}

}

dispose() {

this.tiles.removeEventListener( 'tile-visibility-change', this._visibilityChangeCallback );

}

async fetchData( uri, options ) {

return this.auth.fetch( uri, options );

}

}
2 changes: 2 additions & 0 deletions src/core/plugins/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './CesiumIonAuthPlugin.js';
export * from './GoogleCloudAuthPlugin.js';
export * from './ImplicitTilingPlugin.js';
export * from './EnforceNonZeroErrorPlugin.js';
2 changes: 2 additions & 0 deletions src/core/plugins/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from './CesiumIonAuthPlugin.js';
export * from './GoogleCloudAuthPlugin.js';
export * from './ImplicitTilingPlugin.js';
export * from './EnforceNonZeroErrorPlugin.js';
export * from './auth/GoogleCloudAuth.js';
Expand Down
Loading
Loading