Skip to content

Commit 33c290d

Browse files
authored
Move plugins, logic to core (#1374)
* Add callback overrides * Move GooglePhotorealisticTiles Plugin to core plugins * Add deprecation warning * Reorganization * Add handler for asset types * Types update * Move CesiumIonAuthPlugin plugin to core * README update * Log only as necessary * README update
1 parent a4d54bc commit 33c290d

16 files changed

Lines changed: 522 additions & 395 deletions
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { TilesRendererBase } from '3d-tiles-renderer/core';
2+
3+
export class CesiumIonAuthPlugin {
4+
5+
constructor( options : {
6+
apiToken: string,
7+
assetId?: string | null,
8+
autoRefreshToken?: boolean,
9+
assetTypeHandler?: ( type: string, tiles: TilesRendererBase, asset: object ) => void,
10+
} );
11+
12+
}
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
import { CesiumIonAuth } from './auth/CesiumIonAuth.js';
2+
import { GoogleCloudAuthPlugin } from './GoogleCloudAuthPlugin.js';
3+
4+
export class CesiumIonAuthPlugin {
5+
6+
get apiToken() {
7+
8+
return this.auth.apiToken;
9+
10+
}
11+
12+
set apiToken( v ) {
13+
14+
this.auth.apiToken = v;
15+
16+
}
17+
18+
get autoRefreshToken() {
19+
20+
return this.auth.autoRefreshToken;
21+
22+
}
23+
24+
set autoRefreshToken( v ) {
25+
26+
this.auth.autoRefreshToken = v;
27+
28+
}
29+
30+
constructor( options = {} ) {
31+
32+
const {
33+
apiToken,
34+
assetId = null,
35+
autoRefreshToken = false,
36+
useRecommendedSettings = true,
37+
assetTypeHandler = ( type, tiles, asset ) => {
38+
39+
console.warn( `CesiumIonAuthPlugin: Cesium Ion asset type "${ type }" unhandled.` );
40+
41+
},
42+
} = options;
43+
44+
this.name = 'CESIUM_ION_AUTH_PLUGIN';
45+
this.auth = new CesiumIonAuth( { apiToken, autoRefreshToken } );
46+
47+
this.assetId = assetId;
48+
this.autoRefreshToken = autoRefreshToken;
49+
this.useRecommendedSettings = useRecommendedSettings;
50+
this.assetTypeHandler = assetTypeHandler;
51+
this.tiles = null;
52+
53+
this._tileSetVersion = - 1;
54+
this._attributions = [];
55+
56+
}
57+
58+
init( tiles ) {
59+
60+
if ( this.assetId !== null ) {
61+
62+
tiles.rootURL = `https://api.cesium.com/v1/assets/${ this.assetId }/endpoint`;
63+
64+
}
65+
66+
this.tiles = tiles;
67+
this.auth.authURL = tiles.rootURL;
68+
69+
// reset the tiles in case this plugin was removed and re-added
70+
tiles.resetFailedTiles();
71+
72+
}
73+
74+
loadRootTileSet() {
75+
76+
// ensure we have an up-to-date token and root url, then trigger the internal
77+
// root tile set load function
78+
return this
79+
.auth
80+
.refreshToken()
81+
.then( json => {
82+
83+
this._initializeFromAsset( json );
84+
return this.tiles.invokeOnePlugin( plugin => plugin !== this && plugin.loadRootTileSet && plugin.loadRootTileSet() );
85+
86+
} )
87+
.catch( error => {
88+
89+
this.tiles.dispatchEvent( {
90+
type: 'load-error',
91+
tile: null,
92+
error,
93+
url: this.auth.authURL,
94+
} );
95+
96+
} );
97+
98+
}
99+
100+
preprocessURL( uri ) {
101+
102+
uri = new URL( uri );
103+
if ( /^http/.test( uri.protocol ) && this._tileSetVersion != - 1 ) {
104+
105+
uri.searchParams.set( 'v', this._tileSetVersion );
106+
107+
}
108+
return uri.toString();
109+
110+
}
111+
112+
fetchData( uri, options ) {
113+
114+
const tiles = this.tiles;
115+
if ( tiles.getPluginByName( 'GOOGLE_CLOUD_AUTH_PLUGIN' ) !== null ) {
116+
117+
return null;
118+
119+
} else {
120+
121+
return this.auth.fetch( uri, options );
122+
123+
}
124+
125+
}
126+
127+
getAttributions( target ) {
128+
129+
if ( this.tiles.visibleTiles.size > 0 ) {
130+
131+
target.push( ...this._attributions );
132+
133+
}
134+
135+
}
136+
137+
_initializeFromAsset( json ) {
138+
139+
const tiles = this.tiles;
140+
if ( 'externalType' in json ) {
141+
142+
const url = new URL( json.options.url );
143+
tiles.rootURL = json.options.url;
144+
145+
// if the tile set is "external" then assume it's a google API tile set
146+
tiles.registerPlugin( new GoogleCloudAuthPlugin( {
147+
apiToken: url.searchParams.get( 'key' ),
148+
autoRefreshToken: this.autoRefreshToken,
149+
useRecommendedSettings: this.useRecommendedSettings,
150+
} ) );
151+
152+
} else {
153+
154+
// fire callback for unhandled asset types
155+
if ( json.type !== '3DTILES' ) {
156+
157+
// Other types include:
158+
// - GLTF
159+
// - CZML
160+
// - KML
161+
// - GEOJSON
162+
// - TERRAIN (QuantizedMesh)
163+
// - IMAGERY (TSM Tiles)
164+
165+
this.assetTypeHandler( json.type, tiles, json );
166+
167+
}
168+
169+
tiles.rootURL = json.url;
170+
171+
// save the version key if present
172+
const url = new URL( json.url );
173+
if ( url.searchParams.has( 'v' ) && this._tileSetVersion === - 1 ) {
174+
175+
this._tileSetVersion = url.searchParams.get( 'v' );
176+
177+
}
178+
179+
if ( json.attributions ) {
180+
181+
this._attributions = json.attributions.map( att => ( {
182+
value: att.html,
183+
type: 'html',
184+
collapsible: att.collapsible,
185+
} ) );
186+
187+
}
188+
189+
}
190+
191+
}
192+
193+
}
File renamed without changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export class GoogleCloudAuthPlugin {
2+
3+
constructor( options: {
4+
apiToken: string,
5+
autoRefreshToken?: boolean,
6+
logoUrl?: string,
7+
useRecommendedSettings?: boolean;
8+
sessionOptions?: null | { mapType: string, language: string, region: string, [key: string]: any },
9+
} );
10+
11+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import { GoogleCloudAuth } from '3d-tiles-renderer/core/plugins';
2+
import { GoogleAttributionsManager } from './GoogleAttributionsManager.js';
3+
4+
const TILES_3D_API = 'https://tile.googleapis.com/v1/3dtiles/root.json';
5+
6+
export class GoogleCloudAuthPlugin {
7+
8+
constructor( {
9+
apiToken,
10+
sessionOptions = null,
11+
autoRefreshToken = false,
12+
logoUrl = null,
13+
useRecommendedSettings = true,
14+
} ) {
15+
16+
this.name = 'GOOGLE_CLOUD_AUTH_PLUGIN';
17+
18+
this.apiToken = apiToken;
19+
this.useRecommendedSettings = useRecommendedSettings;
20+
this.logoUrl = logoUrl;
21+
22+
this.auth = new GoogleCloudAuth( { apiToken, autoRefreshToken, sessionOptions } );
23+
this.tiles = null;
24+
25+
this._visibilityChangeCallback = null;
26+
this._attributionsManager = new GoogleAttributionsManager();
27+
this._logoAttribution = {
28+
value: '',
29+
type: 'image',
30+
collapsible: false,
31+
};
32+
this._attribution = {
33+
value: '',
34+
type: 'string',
35+
collapsible: true,
36+
};
37+
38+
}
39+
40+
init( tiles ) {
41+
42+
const { useRecommendedSettings, auth } = this;
43+
44+
// reset the tiles in case this plugin was removed and re-added
45+
tiles.resetFailedTiles();
46+
47+
if ( tiles.rootURL == null ) {
48+
49+
tiles.rootURL = TILES_3D_API;
50+
51+
}
52+
53+
if ( ! auth.sessionOptions ) {
54+
55+
auth.authURL = tiles.rootURL;
56+
57+
}
58+
59+
if ( useRecommendedSettings && ! auth.isMapTilesSession ) {
60+
61+
// This plugin changes below values to be more efficient for the photorealistic tiles
62+
tiles.errorTarget = 20;
63+
64+
}
65+
66+
this.tiles = tiles;
67+
68+
this._visibilityChangeCallback = ( { tile, visible } ) => {
69+
70+
const copyright = tile.cached.metadata?.asset?.copyright || '';
71+
if ( visible ) {
72+
73+
this._attributionsManager.addAttributions( copyright );
74+
75+
} else {
76+
77+
this._attributionsManager.removeAttributions( copyright );
78+
79+
}
80+
81+
};
82+
83+
tiles.addEventListener( 'tile-visibility-change', this._visibilityChangeCallback );
84+
85+
}
86+
87+
getAttributions( target ) {
88+
89+
if ( this.tiles.visibleTiles.size > 0 ) {
90+
91+
if ( this.logoUrl ) {
92+
93+
this._logoAttribution.value = this.logoUrl;
94+
target.push( this._logoAttribution );
95+
96+
}
97+
98+
this._attribution.value = this._attributionsManager.toString();
99+
target.push( this._attribution );
100+
101+
}
102+
103+
}
104+
105+
dispose() {
106+
107+
this.tiles.removeEventListener( 'tile-visibility-change', this._visibilityChangeCallback );
108+
109+
}
110+
111+
async fetchData( uri, options ) {
112+
113+
return this.auth.fetch( uri, options );
114+
115+
}
116+
117+
}

src/core/plugins/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
export * from './CesiumIonAuthPlugin.js';
2+
export * from './GoogleCloudAuthPlugin.js';
13
export * from './ImplicitTilingPlugin.js';
24
export * from './EnforceNonZeroErrorPlugin.js';

src/core/plugins/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export * from './CesiumIonAuthPlugin.js';
2+
export * from './GoogleCloudAuthPlugin.js';
13
export * from './ImplicitTilingPlugin.js';
24
export * from './EnforceNonZeroErrorPlugin.js';
35
export * from './auth/GoogleCloudAuth.js';

0 commit comments

Comments
 (0)