Skip to content

Commit

Permalink
feat(3dtiles): add support for 3d tiles from cesium ion server
Browse files Browse the repository at this point in the history
  • Loading branch information
jailln committed Jan 17, 2023
1 parent d5c80f4 commit e9793a3
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Layer/C3DTilesLayer.js
Expand Up @@ -13,7 +13,7 @@ class C3DTilesLayer extends GeometryLayer {
* @extends GeometryLayer
*
* @example
* // Create a new Layer 3d-tiles For DiscreteLOD
* // Create a new 3d-tiles layer from a web server
* const l3dt = new C3DTilesLayer('3dtiles', {
* name: '3dtl',
* source: new C3DTilesSource({
Expand All @@ -22,6 +22,16 @@ class C3DTilesLayer extends GeometryLayer {
* }, view);
* View.prototype.addLayer.call(view, l3dt);
*
* // Create a new 3d-tiles layer from a Cesion ion server
* const l3dt = new C3DTilesLayer('3dtiles', {
* name: '3dtl',
* source: new C3DTilesIonSource({
* accessToken: 'myAccessToken',
assetId: 12
* })
* }, view);
* View.prototype.addLayer.call(view, l3dt);
*
* @param {string} id - The id of the layer, that should be unique.
* It is not mandatory, but an error will be emitted if this layer is
* added a
Expand Down
1 change: 1 addition & 0 deletions src/Main.js
Expand Up @@ -76,6 +76,7 @@ export { default as VectorTilesSource } from 'Source/VectorTilesSource';
export { default as OrientedImageSource } from 'Source/OrientedImageSource';
export { default as PotreeSource } from 'Source/PotreeSource';
export { default as C3DTilesSource } from 'Source/C3DTilesSource';
export { default as C3DTilesIonSource } from 'Source/C3DTilesIonSource';
export { default as EntwinePointTileSource } from 'Source/EntwinePointTileSource';

// Parsers provided by default in iTowns
Expand Down
60 changes: 60 additions & 0 deletions src/Source/C3DTilesIonSource.js
@@ -0,0 +1,60 @@
import Fetcher from 'Provider/Fetcher';
import C3DTilesSource from './C3DTilesSource';

/**
* @classdesc
* An object defining the source connection to a 3DTiles asset of a [Cesium ion server](https://cesium.com/learn/ion/).
*
* @extends Source
*
* @property {boolean} isC3DTilesIonSource - Used to checkout whether this source is a C3DTilesIonSource. Default is
* true. You should not change this, as it is used internally for optimisation.
* @property {string} url - The URL of the tileset json.
* @property {string} baseUrl - The base URL to access tiles.
* @property {string} accessToken - The Cesium ion access token used to retrieve the resource.
* @property {string} assetId - The id of the asset on Cesium ion.
*/
class C3DTilesIonSource extends C3DTilesSource {
/**
* Create a new Source for 3D Tiles data from Cesium ion.
*
* @constructor
* @extends Source
*
* @param {Object} source An object that can contain all properties of a C3DTilesIonSource and {@link Source}.
* Only `accessToken` and `assetId` are mandatory.
*/
constructor(source) {
if (!source.accessToken) {
throw new Error('New 3D Tiles Ion Source: access token is required');
}
if (!source.assetId) {
throw new Error('New 3D Tiles Ion Source: asset id is required');
}

// Url to query cesium ion the first time to retrieve metadata of the asset with assetId
source.url = `https://api.cesium.com/v1/assets/${source.assetId}/endpoint?access_token=${source.accessToken}`;
super(source);

this.isC3DTilesIonSource = true;
this.accessToken = source.accessToken;
this.assetId = source.assetId;

// get asset metadata
this.whenReady = Fetcher.json(source.url, this.networkOptions)
.then((json) => {
if (json.type !== '3DTILES') {
throw new Error(`${json.type} datasets from Cesium ion are not supported with C3DTilesIonSource. ` +
'Only 3D Tiles datasets are supported.');
}
this.url = json.url; // Store url to the tileset.json
this.baseUrl = json.url.slice(0, json.url.lastIndexOf('/') + 1); // baseUrl for tiles queries
this.networkOptions.headers = {};
this.networkOptions.headers.Authorization = `Bearer ${json.accessToken}`;
this.attribution = json.attributions;
return Fetcher.json(this.url, this.networkOptions);
});
}
}

export default C3DTilesIonSource;

0 comments on commit e9793a3

Please sign in to comment.