maplibre-gl-3d-tiles 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -39,6 +39,7 @@ const map = new maplibregl.Map({
39
39
  map.on('load', () => {
40
40
  const control = new ThreeDTilesControl({
41
41
  collapsed: false,
42
+ layerName: 'AGI Headquarters',
42
43
  tilesetUrl: 'https://pelican-public.s3.amazonaws.com/3dtiles/agi-hq/tileset.json',
43
44
  altitudeOffset: -300,
44
45
  });
@@ -46,6 +47,8 @@ map.on('load', () => {
46
47
  map.addControl(control, 'top-right');
47
48
  void control.loadTileset();
48
49
  void control.loadTileset('https://example.com/another/tileset.json', {
50
+ layerName: 'Second tileset',
51
+ beforeId: 'place-labels',
49
52
  altitudeOffset: 0,
50
53
  });
51
54
  });
@@ -108,6 +111,8 @@ Constructor options include:
108
111
  | Option | Type | Default |
109
112
  | --- | --- | --- |
110
113
  | `tilesetUrl` | `string` | MapLibre example tileset |
114
+ | `layerName` | `string` | `3D Tiles` |
115
+ | `beforeId` | `string` | `undefined` |
111
116
  | `altitudeOffset` | `number` | `-300` |
112
117
  | `flyToOnLoad` | `boolean` | `true` |
113
118
  | `visible` | `boolean` | `true` |
@@ -36748,6 +36748,20 @@ function parseColorSpace(container) {
36748
36748
  return NoColorSpace;
36749
36749
  }
36750
36750
  }
36751
+ const MAX_METADATA_RETRIES = 120;
36752
+ function requestMetadataFrame(callback) {
36753
+ if (typeof globalThis.requestAnimationFrame === "function") {
36754
+ return globalThis.requestAnimationFrame(callback);
36755
+ }
36756
+ return globalThis.setTimeout(() => callback(Date.now()), 16);
36757
+ }
36758
+ function cancelMetadataFrame(handle) {
36759
+ if (typeof globalThis.cancelAnimationFrame === "function") {
36760
+ globalThis.cancelAnimationFrame(handle);
36761
+ return;
36762
+ }
36763
+ globalThis.clearTimeout(handle);
36764
+ }
36751
36765
  function ecefToLngLatAlt(x3, y3, z2) {
36752
36766
  const a2 = 6378137;
36753
36767
  const e2 = 0.00669437999014;
@@ -36787,6 +36801,8 @@ class ThreeDTilesLayer {
36787
36801
  __publicField(this, "_visible");
36788
36802
  __publicField(this, "_loadTilesetHandler");
36789
36803
  __publicField(this, "_loadErrorHandler");
36804
+ __publicField(this, "_metadataRetryFrame", null);
36805
+ __publicField(this, "_metadataRetryCount", 0);
36790
36806
  this.id = options.id;
36791
36807
  this._options = options;
36792
36808
  this._opacity = options.opacity;
@@ -36830,6 +36846,9 @@ class ThreeDTilesLayer {
36830
36846
  }
36831
36847
  onRemove() {
36832
36848
  var _a, _b, _c;
36849
+ if (this._metadataRetryFrame !== null) {
36850
+ cancelMetadataFrame(this._metadataRetryFrame);
36851
+ }
36833
36852
  if (this._tiles && this._loadTilesetHandler) {
36834
36853
  this._tiles.removeEventListener("load-tileset", this._loadTilesetHandler);
36835
36854
  }
@@ -36848,6 +36867,8 @@ class ThreeDTilesLayer {
36848
36867
  this._localTransform = void 0;
36849
36868
  this._loadTilesetHandler = void 0;
36850
36869
  this._loadErrorHandler = void 0;
36870
+ this._metadataRetryFrame = null;
36871
+ this._metadataRetryCount = 0;
36851
36872
  }
36852
36873
  setVisible(visible) {
36853
36874
  var _a;
@@ -36905,12 +36926,20 @@ class ThreeDTilesLayer {
36905
36926
  }
36906
36927
  _handleTilesetLoaded() {
36907
36928
  var _a, _b, _c;
36908
- if (!this._tiles) return;
36929
+ if (!this._tiles || this._metadata) return;
36930
+ const sphere = new Sphere();
36931
+ if (!this._tiles.getBoundingSphere(sphere)) {
36932
+ this._retryTilesetMetadata();
36933
+ return;
36934
+ }
36935
+ if (this._metadataRetryFrame !== null) {
36936
+ cancelMetadataFrame(this._metadataRetryFrame);
36937
+ this._metadataRetryFrame = null;
36938
+ }
36939
+ this._metadataRetryCount = 0;
36909
36940
  if (this._loadTilesetHandler) {
36910
36941
  this._tiles.removeEventListener("load-tileset", this._loadTilesetHandler);
36911
36942
  }
36912
- const sphere = new Sphere();
36913
- if (!this._tiles.getBoundingSphere(sphere)) return;
36914
36943
  const center = sphere.center.clone();
36915
36944
  const rootTransform = ((_a = this._tiles.root) == null ? void 0 : _a.transform) ?? [
36916
36945
  1,
@@ -36958,6 +36987,19 @@ class ThreeDTilesLayer {
36958
36987
  };
36959
36988
  (_c = (_b = this._options).onLoad) == null ? void 0 : _c.call(_b, this._metadata);
36960
36989
  }
36990
+ _retryTilesetMetadata() {
36991
+ var _a, _b;
36992
+ if (this._metadataRetryFrame !== null) return;
36993
+ if (this._metadataRetryCount >= MAX_METADATA_RETRIES) {
36994
+ (_b = (_a = this._options).onError) == null ? void 0 : _b.call(_a, new Error("Unable to read 3D Tiles bounds."));
36995
+ return;
36996
+ }
36997
+ this._metadataRetryCount += 1;
36998
+ this._metadataRetryFrame = requestMetadataFrame(() => {
36999
+ this._metadataRetryFrame = null;
37000
+ this._handleTilesetLoaded();
37001
+ });
37002
+ }
36961
37003
  _updateLocalTransform(modelOrigin, rotate = [Math.PI / 2, 0, 0]) {
36962
37004
  const modelAsMercatorCoordinate = maplibregl.MercatorCoordinate.fromLngLat(
36963
37005
  [modelOrigin[0], modelOrigin[1]],
@@ -37012,6 +37054,8 @@ const DEFAULT_OPTIONS = {
37012
37054
  collapseOnClickOutside: true,
37013
37055
  layerId: "maplibre-gl-3d-tiles",
37014
37056
  tilesetUrl: DEFAULT_TILESET_URL,
37057
+ layerName: "3D Tiles",
37058
+ beforeId: void 0,
37015
37059
  altitudeOffset: -300,
37016
37060
  flyToOnLoad: true,
37017
37061
  opacity: 1,
@@ -37026,7 +37070,9 @@ class ThreeDTilesControl {
37026
37070
  __publicField(this, "_container");
37027
37071
  __publicField(this, "_panel");
37028
37072
  __publicField(this, "_content");
37073
+ __publicField(this, "_layerNameInput");
37029
37074
  __publicField(this, "_urlInput");
37075
+ __publicField(this, "_beforeIdInput");
37030
37076
  __publicField(this, "_altitudeInput");
37031
37077
  __publicField(this, "_flyToCheckbox");
37032
37078
  __publicField(this, "_visibleCheckbox");
@@ -37047,6 +37093,8 @@ class ThreeDTilesControl {
37047
37093
  collapsed: this._options.collapsed,
37048
37094
  panelWidth: this._options.panelWidth,
37049
37095
  tilesetUrl: this._options.tilesetUrl,
37096
+ layerName: this._options.layerName,
37097
+ beforeId: this._options.beforeId,
37050
37098
  altitudeOffset: this._options.altitudeOffset,
37051
37099
  flyToOnLoad: this._options.flyToOnLoad,
37052
37100
  opacity: this._options.opacity,
@@ -37120,11 +37168,15 @@ class ThreeDTilesControl {
37120
37168
  const flyToOnLoad = (options == null ? void 0 : options.flyToOnLoad) ?? Boolean((_a2 = this._flyToCheckbox) == null ? void 0 : _a2.checked);
37121
37169
  const opacity = (options == null ? void 0 : options.opacity) ?? this._state.opacity;
37122
37170
  const visible = (options == null ? void 0 : options.visible) ?? Boolean((_b = this._visibleCheckbox) == null ? void 0 : _b.checked);
37171
+ const layerName = (options == null ? void 0 : options.layerName) ?? this._getLayerName();
37172
+ const beforeId = (options == null ? void 0 : options.beforeId) ?? this._getBeforeId();
37123
37173
  const id = this._createTilesetId();
37124
37174
  const layerId = this._createLayerId(id);
37125
37175
  const item = {
37126
37176
  id,
37127
37177
  layerId,
37178
+ layerName,
37179
+ beforeId,
37128
37180
  tilesetUrl: url,
37129
37181
  altitudeOffset,
37130
37182
  opacity,
@@ -37134,6 +37186,8 @@ class ThreeDTilesControl {
37134
37186
  this._state = {
37135
37187
  ...this._state,
37136
37188
  tilesetUrl: url,
37189
+ layerName,
37190
+ beforeId,
37137
37191
  altitudeOffset,
37138
37192
  flyToOnLoad,
37139
37193
  opacity,
@@ -37164,7 +37218,7 @@ class ThreeDTilesControl {
37164
37218
  this._layers.set(id, layer);
37165
37219
  await this._waitForStyle();
37166
37220
  if (!this._map || !this._layers.has(id)) return void 0;
37167
- this._map.addLayer(layer);
37221
+ this._map.addLayer(layer, beforeId);
37168
37222
  return id;
37169
37223
  }
37170
37224
  removeTileset(id, emit = true) {
@@ -37188,6 +37242,8 @@ class ThreeDTilesControl {
37188
37242
  altitude: activeTileset == null ? void 0 : activeTileset.altitude,
37189
37243
  visible: (activeTileset == null ? void 0 : activeTileset.visible) ?? this._state.visible,
37190
37244
  tilesetUrl: (activeTileset == null ? void 0 : activeTileset.tilesetUrl) ?? this._state.tilesetUrl,
37245
+ layerName: (activeTileset == null ? void 0 : activeTileset.layerName) ?? this._state.layerName,
37246
+ beforeId: activeTileset == null ? void 0 : activeTileset.beforeId,
37191
37247
  altitudeOffset: (activeTileset == null ? void 0 : activeTileset.altitudeOffset) ?? this._state.altitudeOffset,
37192
37248
  opacity: (activeTileset == null ? void 0 : activeTileset.opacity) ?? this._state.opacity,
37193
37249
  activeTilesetId: activeTileset == null ? void 0 : activeTileset.id,
@@ -37338,7 +37394,9 @@ class ThreeDTilesControl {
37338
37394
  event.preventDefault();
37339
37395
  void this.loadTileset();
37340
37396
  });
37397
+ this._layerNameInput = this._createInput("Layer name", "text", this._state.layerName);
37341
37398
  this._urlInput = this._createInput("Tileset URL", "url", this._state.tilesetUrl);
37399
+ this._beforeIdInput = this._createInput("Before layer ID", "text", this._state.beforeId ?? "");
37342
37400
  this._altitudeInput = this._createInput(
37343
37401
  "Altitude offset",
37344
37402
  "number",
@@ -37359,6 +37417,8 @@ class ThreeDTilesControl {
37359
37417
  this._tilesetList = document.createElement("div");
37360
37418
  this._tilesetList.className = "three-d-tiles-list";
37361
37419
  form.appendChild(this._wrapField("Tileset URL", this._urlInput));
37420
+ form.appendChild(this._wrapField("Layer name", this._layerNameInput));
37421
+ form.appendChild(this._wrapField("Before layer ID", this._beforeIdInput));
37362
37422
  form.appendChild(this._wrapField("Altitude offset", this._altitudeInput));
37363
37423
  form.appendChild(this._flyToCheckbox.parentElement);
37364
37424
  form.appendChild(this._visibleCheckbox.parentElement);
@@ -37422,7 +37482,7 @@ class ThreeDTilesControl {
37422
37482
  const title = document.createElement("button");
37423
37483
  title.className = "three-d-tiles-list-title";
37424
37484
  title.type = "button";
37425
- title.textContent = `Tileset ${index + 1}`;
37485
+ title.textContent = tileset.layerName || `Tileset ${index + 1}`;
37426
37486
  title.addEventListener("click", () => this._setActiveTileset(tileset.id));
37427
37487
  const url = document.createElement("span");
37428
37488
  url.className = "three-d-tiles-list-url";
@@ -37430,7 +37490,7 @@ class ThreeDTilesControl {
37430
37490
  const status = document.createElement("span");
37431
37491
  status.className = "three-d-tiles-list-status";
37432
37492
  status.dataset.status = tileset.status;
37433
- status.textContent = tileset.error ?? tileset.status;
37493
+ status.textContent = tileset.error ?? this._formatTilesetStatus(tileset);
37434
37494
  meta.appendChild(title);
37435
37495
  meta.appendChild(url);
37436
37496
  meta.appendChild(status);
@@ -37546,6 +37606,16 @@ class ThreeDTilesControl {
37546
37606
  const value = Number(((_a = this._altitudeInput) == null ? void 0 : _a.value) ?? this._state.altitudeOffset);
37547
37607
  return Number.isFinite(value) ? value : this._state.altitudeOffset;
37548
37608
  }
37609
+ _getLayerName() {
37610
+ var _a;
37611
+ const value = ((_a = this._layerNameInput) == null ? void 0 : _a.value.trim()) || this._state.layerName;
37612
+ return value || "3D Tiles";
37613
+ }
37614
+ _getBeforeId() {
37615
+ var _a;
37616
+ const value = ((_a = this._beforeIdInput) == null ? void 0 : _a.value.trim()) || this._state.beforeId;
37617
+ return value || void 0;
37618
+ }
37549
37619
  _handleTilesetLoaded(id, metadata) {
37550
37620
  if (!this._layers.has(id)) return;
37551
37621
  this._state = {
@@ -37591,6 +37661,8 @@ class ThreeDTilesControl {
37591
37661
  }
37592
37662
  _syncFormFromState() {
37593
37663
  if (this._urlInput) this._urlInput.value = this._state.tilesetUrl;
37664
+ if (this._layerNameInput) this._layerNameInput.value = this._state.layerName;
37665
+ if (this._beforeIdInput) this._beforeIdInput.value = this._state.beforeId ?? "";
37594
37666
  if (this._altitudeInput) this._altitudeInput.value = String(this._state.altitudeOffset);
37595
37667
  if (this._flyToCheckbox) this._flyToCheckbox.checked = this._state.flyToOnLoad;
37596
37668
  if (this._visibleCheckbox) this._visibleCheckbox.checked = this._state.visible;
@@ -37621,6 +37693,8 @@ class ThreeDTilesControl {
37621
37693
  this._state = {
37622
37694
  ...this._state,
37623
37695
  tilesetUrl: activeTileset.tilesetUrl,
37696
+ layerName: activeTileset.layerName,
37697
+ beforeId: activeTileset.beforeId,
37624
37698
  altitudeOffset: activeTileset.altitudeOffset,
37625
37699
  opacity: activeTileset.opacity,
37626
37700
  visible: activeTileset.visible,
@@ -37636,6 +37710,12 @@ class ThreeDTilesControl {
37636
37710
  _getTileset(id) {
37637
37711
  return this._state.tilesets.find((tileset) => tileset.id === id);
37638
37712
  }
37713
+ _formatTilesetStatus(tileset) {
37714
+ if (tileset.beforeId) {
37715
+ return `${tileset.status} before ${tileset.beforeId}`;
37716
+ }
37717
+ return tileset.status;
37718
+ }
37639
37719
  _createTilesetId() {
37640
37720
  this._tilesetCounter += 1;
37641
37721
  return `tileset-${this._tilesetCounter}`;
@@ -37655,4 +37735,4 @@ exports.DEFAULT_TILESET_URL = DEFAULT_TILESET_URL;
37655
37735
  exports.ThreeDTilesControl = ThreeDTilesControl;
37656
37736
  exports.ThreeDTilesLayer = ThreeDTilesLayer;
37657
37737
  exports.ecefToLngLatAlt = ecefToLngLatAlt;
37658
- //# sourceMappingURL=ThreeDTilesControl-DtHlKHVp.cjs.map
37738
+ //# sourceMappingURL=ThreeDTilesControl-BcJIWra6.cjs.map