@loaders.gl/tiles 4.2.0-alpha.4 → 4.2.0-alpha.5

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.
Files changed (73) hide show
  1. package/dist/constants.js +34 -32
  2. package/dist/dist.dev.js +723 -342
  3. package/dist/dist.min.js +9 -0
  4. package/dist/index.cjs +98 -240
  5. package/dist/index.cjs.map +7 -0
  6. package/dist/index.d.ts +10 -10
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +3 -1
  9. package/dist/tileset/format-3d-tiles/tileset-3d-traverser.d.ts +1 -1
  10. package/dist/tileset/format-3d-tiles/tileset-3d-traverser.d.ts.map +1 -1
  11. package/dist/tileset/format-3d-tiles/tileset-3d-traverser.js +45 -35
  12. package/dist/tileset/format-i3s/i3s-pending-tiles-register.js +40 -22
  13. package/dist/tileset/format-i3s/i3s-tile-manager.d.ts +1 -1
  14. package/dist/tileset/format-i3s/i3s-tile-manager.d.ts.map +1 -1
  15. package/dist/tileset/format-i3s/i3s-tile-manager.js +67 -69
  16. package/dist/tileset/format-i3s/i3s-tileset-traverser.d.ts +2 -2
  17. package/dist/tileset/format-i3s/i3s-tileset-traverser.d.ts.map +1 -1
  18. package/dist/tileset/format-i3s/i3s-tileset-traverser.js +78 -57
  19. package/dist/tileset/helpers/3d-tiles-options.d.ts +1 -1
  20. package/dist/tileset/helpers/3d-tiles-options.d.ts.map +1 -1
  21. package/dist/tileset/helpers/3d-tiles-options.js +3 -4
  22. package/dist/tileset/helpers/bounding-volume.js +269 -128
  23. package/dist/tileset/helpers/frame-state.d.ts +1 -1
  24. package/dist/tileset/helpers/frame-state.d.ts.map +1 -1
  25. package/dist/tileset/helpers/frame-state.js +114 -95
  26. package/dist/tileset/helpers/i3s-lod.d.ts +2 -2
  27. package/dist/tileset/helpers/i3s-lod.d.ts.map +1 -1
  28. package/dist/tileset/helpers/i3s-lod.js +68 -39
  29. package/dist/tileset/helpers/tiles-3d-lod.js +97 -85
  30. package/dist/tileset/helpers/transform-utils.js +45 -44
  31. package/dist/tileset/helpers/zoom.d.ts +1 -1
  32. package/dist/tileset/helpers/zoom.d.ts.map +1 -1
  33. package/dist/tileset/helpers/zoom.js +70 -40
  34. package/dist/tileset/tile-3d.d.ts +5 -5
  35. package/dist/tileset/tile-3d.d.ts.map +1 -1
  36. package/dist/tileset/tile-3d.js +595 -399
  37. package/dist/tileset/tileset-3d.d.ts +5 -5
  38. package/dist/tileset/tileset-3d.d.ts.map +1 -1
  39. package/dist/tileset/tileset-3d.js +721 -597
  40. package/dist/tileset/tileset-cache.d.ts +2 -2
  41. package/dist/tileset/tileset-cache.d.ts.map +1 -1
  42. package/dist/tileset/tileset-cache.js +62 -48
  43. package/dist/tileset/tileset-traverser.d.ts +3 -3
  44. package/dist/tileset/tileset-traverser.d.ts.map +1 -1
  45. package/dist/tileset/tileset-traverser.js +281 -220
  46. package/dist/types.js +0 -1
  47. package/dist/utils/doubly-linked-list-node.js +14 -9
  48. package/dist/utils/doubly-linked-list.d.ts +1 -1
  49. package/dist/utils/doubly-linked-list.d.ts.map +1 -1
  50. package/dist/utils/doubly-linked-list.js +85 -56
  51. package/dist/utils/managed-array.js +138 -76
  52. package/package.json +10 -9
  53. package/dist/constants.js.map +0 -1
  54. package/dist/index.js.map +0 -1
  55. package/dist/tileset/format-3d-tiles/tileset-3d-traverser.js.map +0 -1
  56. package/dist/tileset/format-i3s/i3s-pending-tiles-register.js.map +0 -1
  57. package/dist/tileset/format-i3s/i3s-tile-manager.js.map +0 -1
  58. package/dist/tileset/format-i3s/i3s-tileset-traverser.js.map +0 -1
  59. package/dist/tileset/helpers/3d-tiles-options.js.map +0 -1
  60. package/dist/tileset/helpers/bounding-volume.js.map +0 -1
  61. package/dist/tileset/helpers/frame-state.js.map +0 -1
  62. package/dist/tileset/helpers/i3s-lod.js.map +0 -1
  63. package/dist/tileset/helpers/tiles-3d-lod.js.map +0 -1
  64. package/dist/tileset/helpers/transform-utils.js.map +0 -1
  65. package/dist/tileset/helpers/zoom.js.map +0 -1
  66. package/dist/tileset/tile-3d.js.map +0 -1
  67. package/dist/tileset/tileset-3d.js.map +0 -1
  68. package/dist/tileset/tileset-cache.js.map +0 -1
  69. package/dist/tileset/tileset-traverser.js.map +0 -1
  70. package/dist/types.js.map +0 -1
  71. package/dist/utils/doubly-linked-list-node.js.map +0 -1
  72. package/dist/utils/doubly-linked-list.js.map +0 -1
  73. package/dist/utils/managed-array.js.map +0 -1
@@ -1,3 +1,8 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ // This file is derived from the Cesium code base under Apache 2 license
5
+ // See LICENSE.md and https://github.com/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md
1
6
  import { Vector3, Matrix4 } from '@math.gl/core';
2
7
  import { CullingVolume } from '@math.gl/culling';
3
8
  import { load } from '@loaders.gl/core';
@@ -9,405 +14,596 @@ import { get3dTilesOptions } from "./helpers/3d-tiles-options.js";
9
14
  import { TilesetTraverser } from "./tileset-traverser.js";
10
15
  const scratchVector = new Vector3();
11
16
  function defined(x) {
12
- return x !== undefined && x !== null;
17
+ return x !== undefined && x !== null;
13
18
  }
19
+ /**
20
+ * A Tile3DHeader represents a tile as Tileset3D. When a tile is first created, its content is not loaded;
21
+ * the content is loaded on-demand when needed based on the view.
22
+ * Do not construct this directly, instead access tiles through {@link Tileset3D#tileVisible}.
23
+ */
14
24
  export class Tile3D {
15
- constructor(tileset, header, parentHeader) {
16
- let extendedId = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '';
17
- this.tileset = void 0;
18
- this.header = void 0;
19
- this.id = void 0;
20
- this.url = void 0;
21
- this.parent = void 0;
22
- this.refine = void 0;
23
- this.type = void 0;
24
- this.contentUrl = void 0;
25
- this.lodMetricType = 'geometricError';
26
- this.lodMetricValue = 0;
27
- this.boundingVolume = null;
28
- this.content = null;
29
- this.contentState = TILE_CONTENT_STATE.UNLOADED;
30
- this.gpuMemoryUsageInBytes = 0;
31
- this.children = [];
32
- this.depth = 0;
33
- this.viewportIds = [];
34
- this.transform = new Matrix4();
35
- this.extensions = null;
36
- this.implicitTiling = null;
37
- this.userData = {};
38
- this.computedTransform = void 0;
39
- this.hasEmptyContent = false;
40
- this.hasTilesetContent = false;
41
- this.traverser = new TilesetTraverser({});
42
- this._cacheNode = null;
43
- this._frameNumber = null;
44
- this._expireDate = null;
45
- this._expiredContent = null;
46
- this._boundingBox = undefined;
47
- this._distanceToCamera = 0;
48
- this._screenSpaceError = 0;
49
- this._visibilityPlaneMask = void 0;
50
- this._visible = undefined;
51
- this._contentBoundingVolume = void 0;
52
- this._viewerRequestVolume = void 0;
53
- this._initialTransform = new Matrix4();
54
- this._priority = 0;
55
- this._selectedFrame = 0;
56
- this._requestedFrame = 0;
57
- this._selectionDepth = 0;
58
- this._touchedFrame = 0;
59
- this._centerZDepth = 0;
60
- this._shouldRefine = false;
61
- this._stackLength = 0;
62
- this._visitedFrame = 0;
63
- this._inRequestVolume = false;
64
- this._lodJudge = null;
65
- this.header = header;
66
- this.tileset = tileset;
67
- this.id = extendedId || header.id;
68
- this.url = header.url;
69
- this.parent = parentHeader;
70
- this.refine = this._getRefine(header.refine);
71
- this.type = header.type;
72
- this.contentUrl = header.contentUrl;
73
- this._initializeLodMetric(header);
74
- this._initializeTransforms(header);
75
- this._initializeBoundingVolumes(header);
76
- this._initializeContent(header);
77
- this._initializeRenderingState(header);
78
- Object.seal(this);
79
- }
80
- destroy() {
81
- this.header = null;
82
- }
83
- isDestroyed() {
84
- return this.header === null;
85
- }
86
- get selected() {
87
- return this._selectedFrame === this.tileset._frameNumber;
88
- }
89
- get isVisible() {
90
- return this._visible;
91
- }
92
- get isVisibleAndInRequestVolume() {
93
- return this._visible && this._inRequestVolume;
94
- }
95
- get hasRenderContent() {
96
- return !this.hasEmptyContent && !this.hasTilesetContent;
97
- }
98
- get hasChildren() {
99
- return this.children.length > 0 || this.header.children && this.header.children.length > 0;
100
- }
101
- get contentReady() {
102
- return this.contentState === TILE_CONTENT_STATE.READY || this.hasEmptyContent;
103
- }
104
- get contentAvailable() {
105
- return Boolean(this.contentReady && this.hasRenderContent || this._expiredContent && !this.contentFailed);
106
- }
107
- get hasUnloadedContent() {
108
- return this.hasRenderContent && this.contentUnloaded;
109
- }
110
- get contentUnloaded() {
111
- return this.contentState === TILE_CONTENT_STATE.UNLOADED;
112
- }
113
- get contentExpired() {
114
- return this.contentState === TILE_CONTENT_STATE.EXPIRED;
115
- }
116
- get contentFailed() {
117
- return this.contentState === TILE_CONTENT_STATE.FAILED;
118
- }
119
- get distanceToCamera() {
120
- return this._distanceToCamera;
121
- }
122
- get screenSpaceError() {
123
- return this._screenSpaceError;
124
- }
125
- get boundingBox() {
126
- if (!this._boundingBox) {
127
- this._boundingBox = getCartographicBounds(this.header.boundingVolume, this.boundingVolume);
128
- }
129
- return this._boundingBox;
130
- }
131
- getScreenSpaceError(frameState, useParentLodMetric) {
132
- switch (this.tileset.type) {
133
- case TILESET_TYPE.I3S:
134
- return getProjectedRadius(this, frameState);
135
- case TILESET_TYPE.TILES3D:
136
- return getTiles3DScreenSpaceError(this, frameState, useParentLodMetric);
137
- default:
138
- throw new Error('Unsupported tileset type');
139
- }
140
- }
141
- unselect() {
142
- this._selectedFrame = 0;
143
- }
144
- _getGpuMemoryUsageInBytes() {
145
- return this.content.gpuMemoryUsageInBytes || this.content.byteLength || 0;
146
- }
147
- _getPriority() {
148
- const traverser = this.tileset._traverser;
149
- const {
150
- skipLevelOfDetail
151
- } = traverser.options;
152
- const maySkipTile = this.refine === TILE_REFINEMENT.ADD || skipLevelOfDetail;
153
- if (maySkipTile && !this.isVisible && this._visible !== undefined) {
154
- return -1;
155
- }
156
- if (this.tileset._frameNumber - this._touchedFrame >= 1) {
157
- return -1;
158
- }
159
- if (this.contentState === TILE_CONTENT_STATE.UNLOADED) {
160
- return -1;
161
- }
162
- const parent = this.parent;
163
- const useParentScreenSpaceError = parent && (!maySkipTile || this._screenSpaceError === 0.0 || parent.hasTilesetContent);
164
- const screenSpaceError = useParentScreenSpaceError ? parent._screenSpaceError : this._screenSpaceError;
165
- const rootScreenSpaceError = traverser.root ? traverser.root._screenSpaceError : 0.0;
166
- return Math.max(rootScreenSpaceError - screenSpaceError, 0);
167
- }
168
- async loadContent() {
169
- if (this.hasEmptyContent) {
170
- return false;
171
- }
172
- if (this.content) {
173
- return true;
174
- }
175
- const expired = this.contentExpired;
176
- if (expired) {
177
- this._expireDate = null;
178
- }
179
- this.contentState = TILE_CONTENT_STATE.LOADING;
180
- const requestToken = await this.tileset._requestScheduler.scheduleRequest(this.id, this._getPriority.bind(this));
181
- if (!requestToken) {
182
- this.contentState = TILE_CONTENT_STATE.UNLOADED;
183
- return false;
184
- }
185
- try {
186
- const contentUrl = this.tileset.getTileUrl(this.contentUrl);
187
- const loader = this.tileset.loader;
188
- const options = {
189
- ...this.tileset.loadOptions,
190
- [loader.id]: {
191
- ...this.tileset.loadOptions[loader.id],
192
- isTileset: this.type === 'json',
193
- ...this._getLoaderSpecificOptions(loader.id)
194
- }
195
- };
196
- this.content = await load(contentUrl, loader, options);
197
- if (this.tileset.options.contentLoader) {
198
- await this.tileset.options.contentLoader(this);
199
- }
200
- if (this._isTileset()) {
201
- this.tileset._initializeTileHeaders(this.content, this);
202
- }
203
- this.contentState = TILE_CONTENT_STATE.READY;
204
- this._onContentLoaded();
205
- return true;
206
- } catch (error) {
207
- this.contentState = TILE_CONTENT_STATE.FAILED;
208
- throw error;
209
- } finally {
210
- requestToken.done();
211
- }
212
- }
213
- unloadContent() {
214
- if (this.content && this.content.destroy) {
215
- this.content.destroy();
216
- }
217
- this.content = null;
218
- if (this.header.content && this.header.content.destroy) {
219
- this.header.content.destroy();
220
- }
221
- this.header.content = null;
222
- this.contentState = TILE_CONTENT_STATE.UNLOADED;
223
- return true;
224
- }
225
- updateVisibility(frameState, viewportIds) {
226
- if (this._frameNumber === frameState.frameNumber) {
227
- return;
228
- }
229
- const parent = this.parent;
230
- const parentVisibilityPlaneMask = parent ? parent._visibilityPlaneMask : CullingVolume.MASK_INDETERMINATE;
231
- if (this.tileset._traverser.options.updateTransforms) {
232
- const parentTransform = parent ? parent.computedTransform : this.tileset.modelMatrix;
233
- this._updateTransform(parentTransform);
234
- }
235
- this._distanceToCamera = this.distanceToTile(frameState);
236
- this._screenSpaceError = this.getScreenSpaceError(frameState, false);
237
- this._visibilityPlaneMask = this.visibility(frameState, parentVisibilityPlaneMask);
238
- this._visible = this._visibilityPlaneMask !== CullingVolume.MASK_OUTSIDE;
239
- this._inRequestVolume = this.insideViewerRequestVolume(frameState);
240
- this._frameNumber = frameState.frameNumber;
241
- this.viewportIds = viewportIds;
242
- }
243
- visibility(frameState, parentVisibilityPlaneMask) {
244
- const {
245
- cullingVolume
246
- } = frameState;
247
- const {
248
- boundingVolume
249
- } = this;
250
- return cullingVolume.computeVisibilityWithPlaneMask(boundingVolume, parentVisibilityPlaneMask);
251
- }
252
- contentVisibility() {
253
- return true;
254
- }
255
- distanceToTile(frameState) {
256
- const boundingVolume = this.boundingVolume;
257
- return Math.sqrt(Math.max(boundingVolume.distanceSquaredTo(frameState.camera.position), 0));
258
- }
259
- cameraSpaceZDepth(_ref) {
260
- let {
261
- camera
262
- } = _ref;
263
- const boundingVolume = this.boundingVolume;
264
- scratchVector.subVectors(boundingVolume.center, camera.position);
265
- return camera.direction.dot(scratchVector);
266
- }
267
- insideViewerRequestVolume(frameState) {
268
- const viewerRequestVolume = this._viewerRequestVolume;
269
- return !viewerRequestVolume || viewerRequestVolume.distanceSquaredTo(frameState.camera.position) <= 0;
270
- }
271
- updateExpiration() {
272
- if (defined(this._expireDate) && this.contentReady && !this.hasEmptyContent) {
273
- const now = Date.now();
274
- if (Date.lessThan(this._expireDate, now)) {
275
- this.contentState = TILE_CONTENT_STATE.EXPIRED;
276
- this._expiredContent = this.content;
277
- }
278
- }
279
- }
280
- get extras() {
281
- return this.header.extras;
282
- }
283
- _initializeLodMetric(header) {
284
- if ('lodMetricType' in header) {
285
- this.lodMetricType = header.lodMetricType;
286
- } else {
287
- this.lodMetricType = this.parent && this.parent.lodMetricType || this.tileset.lodMetricType;
288
- console.warn(`3D Tile: Required prop lodMetricType is undefined. Using parent lodMetricType`);
289
- }
290
- if ('lodMetricValue' in header) {
291
- this.lodMetricValue = header.lodMetricValue;
292
- } else {
293
- this.lodMetricValue = this.parent && this.parent.lodMetricValue || this.tileset.lodMetricValue;
294
- console.warn('3D Tile: Required prop lodMetricValue is undefined. Using parent lodMetricValue');
295
- }
296
- }
297
- _initializeTransforms(tileHeader) {
298
- this.transform = tileHeader.transform ? new Matrix4(tileHeader.transform) : new Matrix4();
299
- const parent = this.parent;
300
- const tileset = this.tileset;
301
- const parentTransform = parent && parent.computedTransform ? parent.computedTransform.clone() : tileset.modelMatrix.clone();
302
- this.computedTransform = new Matrix4(parentTransform).multiplyRight(this.transform);
303
- const parentInitialTransform = parent && parent._initialTransform ? parent._initialTransform.clone() : new Matrix4();
304
- this._initialTransform = new Matrix4(parentInitialTransform).multiplyRight(this.transform);
305
- }
306
- _initializeBoundingVolumes(tileHeader) {
307
- this._contentBoundingVolume = null;
308
- this._viewerRequestVolume = null;
309
- this._updateBoundingVolume(tileHeader);
310
- }
311
- _initializeContent(tileHeader) {
312
- this.content = {
313
- _tileset: this.tileset,
314
- _tile: this
315
- };
316
- this.hasEmptyContent = true;
317
- this.contentState = TILE_CONTENT_STATE.UNLOADED;
318
- this.hasTilesetContent = false;
319
- if (tileHeader.contentUrl) {
320
- this.content = null;
321
- this.hasEmptyContent = false;
322
- }
323
- }
324
- _initializeRenderingState(header) {
325
- this.depth = header.level || (this.parent ? this.parent.depth + 1 : 0);
326
- this._shouldRefine = false;
327
- this._distanceToCamera = 0;
328
- this._centerZDepth = 0;
329
- this._screenSpaceError = 0;
330
- this._visibilityPlaneMask = CullingVolume.MASK_INDETERMINATE;
331
- this._visible = undefined;
332
- this._inRequestVolume = false;
333
- this._stackLength = 0;
334
- this._selectionDepth = 0;
335
- this._frameNumber = 0;
336
- this._touchedFrame = 0;
337
- this._visitedFrame = 0;
338
- this._selectedFrame = 0;
339
- this._requestedFrame = 0;
340
- this._priority = 0.0;
341
- }
342
- _getRefine(refine) {
343
- return refine || this.parent && this.parent.refine || TILE_REFINEMENT.REPLACE;
344
- }
345
- _isTileset() {
346
- return this.contentUrl.indexOf('.json') !== -1;
347
- }
348
- _onContentLoaded() {
349
- switch (this.content && this.content.type) {
350
- case 'vctr':
351
- case 'geom':
352
- this.tileset._traverser.disableSkipLevelOfDetail = true;
353
- break;
354
- default:
355
- }
356
- if (this._isTileset()) {
357
- this.hasTilesetContent = true;
358
- } else {
359
- this.gpuMemoryUsageInBytes = this._getGpuMemoryUsageInBytes();
360
- }
361
- }
362
- _updateBoundingVolume(header) {
363
- this.boundingVolume = createBoundingVolume(header.boundingVolume, this.computedTransform, this.boundingVolume);
364
- const content = header.content;
365
- if (!content) {
366
- return;
367
- }
368
- if (content.boundingVolume) {
369
- this._contentBoundingVolume = createBoundingVolume(content.boundingVolume, this.computedTransform, this._contentBoundingVolume);
370
- }
371
- if (header.viewerRequestVolume) {
372
- this._viewerRequestVolume = createBoundingVolume(header.viewerRequestVolume, this.computedTransform, this._viewerRequestVolume);
373
- }
374
- }
375
- _updateTransform() {
376
- let parentTransform = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : new Matrix4();
377
- const computedTransform = parentTransform.clone().multiplyRight(this.transform);
378
- const didTransformChange = !computedTransform.equals(this.computedTransform);
379
- if (!didTransformChange) {
380
- return;
381
- }
382
- this.computedTransform = computedTransform;
383
- this._updateBoundingVolume(this.header);
384
- }
385
- _getLoaderSpecificOptions(loaderId) {
386
- switch (loaderId) {
387
- case 'i3s':
388
- return {
389
- ...this.tileset.options.i3s,
390
- _tileOptions: {
391
- attributeUrls: this.header.attributeUrls,
392
- textureUrl: this.header.textureUrl,
393
- textureFormat: this.header.textureFormat,
394
- textureLoaderOptions: this.header.textureLoaderOptions,
395
- materialDefinition: this.header.materialDefinition,
396
- isDracoGeometry: this.header.isDracoGeometry,
397
- mbs: this.header.mbs
398
- },
399
- _tilesetOptions: {
400
- store: this.tileset.tileset.store,
401
- attributeStorageInfo: this.tileset.tileset.attributeStorageInfo,
402
- fields: this.tileset.tileset.fields
403
- },
404
- isTileHeader: false
405
- };
406
- case '3d-tiles':
407
- case 'cesium-ion':
408
- default:
409
- return get3dTilesOptions(this.tileset.tileset);
410
- }
411
- }
25
+ /**
26
+ * @constructs
27
+ * Create a Tile3D instance
28
+ * @param tileset - Tileset3D instance
29
+ * @param header - tile header - JSON loaded from a dataset
30
+ * @param parentHeader - parent Tile3D instance
31
+ * @param extendedId - optional ID to separate copies of a tile for different viewports.
32
+ * const extendedId = `${tile.id}-${frameState.viewport.id}`;
33
+ */
34
+ // eslint-disable-next-line max-statements
35
+ constructor(tileset, header, parentHeader, extendedId = '') {
36
+ /** Different refinement algorithms used by I3S and 3D tiles */
37
+ this.lodMetricType = 'geometricError';
38
+ /** The error, in meters, introduced if this tile is rendered and its children are not. */
39
+ this.lodMetricValue = 0;
40
+ /** @todo math.gl is not exporting BoundingVolume base type? */
41
+ this.boundingVolume = null;
42
+ /**
43
+ * The tile's content. This represents the actual tile's payload,
44
+ * not the content's metadata in the tileset JSON file.
45
+ */
46
+ this.content = null;
47
+ this.contentState = TILE_CONTENT_STATE.UNLOADED;
48
+ this.gpuMemoryUsageInBytes = 0;
49
+ /** The tile's children - an array of Tile3D objects. */
50
+ this.children = [];
51
+ this.depth = 0;
52
+ this.viewportIds = [];
53
+ this.transform = new Matrix4();
54
+ this.extensions = null;
55
+ /** TODO Cesium 3d tiles specific */
56
+ this.implicitTiling = null;
57
+ /** Container to store application specific data */
58
+ this.userData = {};
59
+ this.hasEmptyContent = false;
60
+ this.hasTilesetContent = false;
61
+ this.traverser = new TilesetTraverser({});
62
+ /** Used by TilesetCache */
63
+ this._cacheNode = null;
64
+ this._frameNumber = null;
65
+ // TODO Cesium 3d tiles specific
66
+ this._expireDate = null;
67
+ this._expiredContent = null;
68
+ this._boundingBox = undefined;
69
+ /** updated every frame for tree traversal and rendering optimizations: */
70
+ this._distanceToCamera = 0;
71
+ this._screenSpaceError = 0;
72
+ this._visible = undefined;
73
+ this._initialTransform = new Matrix4();
74
+ // Used by traverser, cannot be marked private
75
+ this._priority = 0;
76
+ this._selectedFrame = 0;
77
+ this._requestedFrame = 0;
78
+ this._selectionDepth = 0;
79
+ this._touchedFrame = 0;
80
+ this._centerZDepth = 0;
81
+ this._shouldRefine = false;
82
+ this._stackLength = 0;
83
+ this._visitedFrame = 0;
84
+ this._inRequestVolume = false;
85
+ this._lodJudge = null; // TODO i3s specific, needs to remove
86
+ // PUBLIC MEMBERS
87
+ // original tile data
88
+ this.header = header;
89
+ // The tileset containing this tile.
90
+ this.tileset = tileset;
91
+ this.id = extendedId || header.id;
92
+ this.url = header.url;
93
+ // This tile's parent or `undefined` if this tile is the root.
94
+ // @ts-ignore
95
+ this.parent = parentHeader;
96
+ this.refine = this._getRefine(header.refine);
97
+ this.type = header.type;
98
+ this.contentUrl = header.contentUrl;
99
+ this._initializeLodMetric(header);
100
+ this._initializeTransforms(header);
101
+ this._initializeBoundingVolumes(header);
102
+ this._initializeContent(header);
103
+ this._initializeRenderingState(header);
104
+ Object.seal(this);
105
+ }
106
+ destroy() {
107
+ this.header = null;
108
+ }
109
+ isDestroyed() {
110
+ return this.header === null;
111
+ }
112
+ get selected() {
113
+ return this._selectedFrame === this.tileset._frameNumber;
114
+ }
115
+ get isVisible() {
116
+ return this._visible;
117
+ }
118
+ get isVisibleAndInRequestVolume() {
119
+ return this._visible && this._inRequestVolume;
120
+ }
121
+ /** Returns true if tile is not an empty tile and not an external tileset */
122
+ get hasRenderContent() {
123
+ return !this.hasEmptyContent && !this.hasTilesetContent;
124
+ }
125
+ /** Returns true if tile has children */
126
+ get hasChildren() {
127
+ return this.children.length > 0 || (this.header.children && this.header.children.length > 0);
128
+ }
129
+ /**
130
+ * Determines if the tile's content is ready. This is automatically `true` for
131
+ * tiles with empty content.
132
+ */
133
+ get contentReady() {
134
+ return this.contentState === TILE_CONTENT_STATE.READY || this.hasEmptyContent;
135
+ }
136
+ /**
137
+ * Determines if the tile has available content to render. `true` if the tile's
138
+ * content is ready or if it has expired content this renders while new content loads; otherwise,
139
+ */
140
+ get contentAvailable() {
141
+ return Boolean((this.contentReady && this.hasRenderContent) || (this._expiredContent && !this.contentFailed));
142
+ }
143
+ /** Returns true if tile has renderable content but it's unloaded */
144
+ get hasUnloadedContent() {
145
+ return this.hasRenderContent && this.contentUnloaded;
146
+ }
147
+ /**
148
+ * Determines if the tile's content has not be requested. `true` if tile's
149
+ * content has not be requested; otherwise, `false`.
150
+ */
151
+ get contentUnloaded() {
152
+ return this.contentState === TILE_CONTENT_STATE.UNLOADED;
153
+ }
154
+ /**
155
+ * Determines if the tile's content is expired. `true` if tile's
156
+ * content is expired; otherwise, `false`.
157
+ */
158
+ get contentExpired() {
159
+ return this.contentState === TILE_CONTENT_STATE.EXPIRED;
160
+ }
161
+ // Determines if the tile's content failed to load. `true` if the tile's
162
+ // content failed to load; otherwise, `false`.
163
+ get contentFailed() {
164
+ return this.contentState === TILE_CONTENT_STATE.FAILED;
165
+ }
166
+ /**
167
+ * Distance from the tile's bounding volume center to the camera
168
+ */
169
+ get distanceToCamera() {
170
+ return this._distanceToCamera;
171
+ }
172
+ /**
173
+ * Screen space error for LOD selection
174
+ */
175
+ get screenSpaceError() {
176
+ return this._screenSpaceError;
177
+ }
178
+ /**
179
+ * Get bounding box in cartographic coordinates
180
+ * @returns [min, max] each in [longitude, latitude, altitude]
181
+ */
182
+ get boundingBox() {
183
+ if (!this._boundingBox) {
184
+ this._boundingBox = getCartographicBounds(this.header.boundingVolume, this.boundingVolume);
185
+ }
186
+ return this._boundingBox;
187
+ }
188
+ /** Get the tile's screen space error. */
189
+ getScreenSpaceError(frameState, useParentLodMetric) {
190
+ switch (this.tileset.type) {
191
+ case TILESET_TYPE.I3S:
192
+ return getProjectedRadius(this, frameState);
193
+ case TILESET_TYPE.TILES3D:
194
+ return getTiles3DScreenSpaceError(this, frameState, useParentLodMetric);
195
+ default:
196
+ // eslint-disable-next-line
197
+ throw new Error('Unsupported tileset type');
198
+ }
199
+ }
200
+ /**
201
+ * Make tile unselected than means it won't be shown
202
+ * but it can be still loaded in memory
203
+ */
204
+ unselect() {
205
+ this._selectedFrame = 0;
206
+ }
207
+ /**
208
+ * Memory usage of tile on GPU
209
+ */
210
+ _getGpuMemoryUsageInBytes() {
211
+ return this.content.gpuMemoryUsageInBytes || this.content.byteLength || 0;
212
+ }
213
+ /*
214
+ * If skipLevelOfDetail is off try to load child tiles as soon as possible so that their parent can refine sooner.
215
+ * Tiles are prioritized by screen space error.
216
+ */
217
+ // eslint-disable-next-line complexity
218
+ _getPriority() {
219
+ const traverser = this.tileset._traverser;
220
+ const { skipLevelOfDetail } = traverser.options;
221
+ /*
222
+ * Tiles that are outside of the camera's frustum could be skipped if we are in 'ADD' mode
223
+ * or if we are using 'Skip Traversal' in 'REPLACE' mode.
224
+ * Otherewise, all 'touched' child tiles have to be loaded and displayed,
225
+ * this may include tiles that are outide of the camera frustum (so that we can hide the parent tile).
226
+ */
227
+ const maySkipTile = this.refine === TILE_REFINEMENT.ADD || skipLevelOfDetail;
228
+ // Check if any reason to abort
229
+ if (maySkipTile && !this.isVisible && this._visible !== undefined) {
230
+ return -1;
231
+ }
232
+ // Condition used in `cancelOutOfViewRequests` function in CesiumJS/Cesium3DTileset.js
233
+ if (this.tileset._frameNumber - this._touchedFrame >= 1) {
234
+ return -1;
235
+ }
236
+ if (this.contentState === TILE_CONTENT_STATE.UNLOADED) {
237
+ return -1;
238
+ }
239
+ // Based on the priority function `getPriorityReverseScreenSpaceError` in CesiumJS. Scheduling priority is based on the parent's screen space error when possible.
240
+ const parent = this.parent;
241
+ const useParentScreenSpaceError = parent && (!maySkipTile || this._screenSpaceError === 0.0 || parent.hasTilesetContent);
242
+ const screenSpaceError = useParentScreenSpaceError
243
+ ? parent._screenSpaceError
244
+ : this._screenSpaceError;
245
+ const rootScreenSpaceError = traverser.root ? traverser.root._screenSpaceError : 0.0;
246
+ // Map higher SSE to lower values (e.g. root tile is highest priority)
247
+ return Math.max(rootScreenSpaceError - screenSpaceError, 0);
248
+ }
249
+ /**
250
+ * Requests the tile's content.
251
+ * The request may not be made if the Request Scheduler can't prioritize it.
252
+ */
253
+ // eslint-disable-next-line max-statements, complexity
254
+ async loadContent() {
255
+ if (this.hasEmptyContent) {
256
+ return false;
257
+ }
258
+ if (this.content) {
259
+ return true;
260
+ }
261
+ const expired = this.contentExpired;
262
+ if (expired) {
263
+ this._expireDate = null;
264
+ }
265
+ this.contentState = TILE_CONTENT_STATE.LOADING;
266
+ const requestToken = await this.tileset._requestScheduler.scheduleRequest(this.id, this._getPriority.bind(this));
267
+ if (!requestToken) {
268
+ // cancelled
269
+ this.contentState = TILE_CONTENT_STATE.UNLOADED;
270
+ return false;
271
+ }
272
+ try {
273
+ const contentUrl = this.tileset.getTileUrl(this.contentUrl);
274
+ // The content can be a binary tile ot a JSON tileset
275
+ const loader = this.tileset.loader;
276
+ const options = {
277
+ ...this.tileset.loadOptions,
278
+ [loader.id]: {
279
+ // @ts-expect-error
280
+ ...this.tileset.loadOptions[loader.id],
281
+ isTileset: this.type === 'json',
282
+ ...this._getLoaderSpecificOptions(loader.id)
283
+ }
284
+ };
285
+ this.content = await load(contentUrl, loader, options);
286
+ if (this.tileset.options.contentLoader) {
287
+ await this.tileset.options.contentLoader(this);
288
+ }
289
+ if (this._isTileset()) {
290
+ // Add tile headers for the nested tilset's subtree
291
+ // Async update of the tree should be fine since there would never be edits to the same node
292
+ // TODO - we need to capture the child tileset's URL
293
+ this.tileset._initializeTileHeaders(this.content, this);
294
+ }
295
+ this.contentState = TILE_CONTENT_STATE.READY;
296
+ this._onContentLoaded();
297
+ return true;
298
+ }
299
+ catch (error) {
300
+ // Tile is unloaded before the content finishes loading
301
+ this.contentState = TILE_CONTENT_STATE.FAILED;
302
+ throw error;
303
+ }
304
+ finally {
305
+ requestToken.done();
306
+ }
307
+ }
308
+ // Unloads the tile's content.
309
+ unloadContent() {
310
+ if (this.content && this.content.destroy) {
311
+ this.content.destroy();
312
+ }
313
+ this.content = null;
314
+ if (this.header.content && this.header.content.destroy) {
315
+ this.header.content.destroy();
316
+ }
317
+ this.header.content = null;
318
+ this.contentState = TILE_CONTENT_STATE.UNLOADED;
319
+ return true;
320
+ }
321
+ /**
322
+ * Update the tile's visibility
323
+ * @param {Object} frameState - frame state for tile culling
324
+ * @param {string[]} viewportIds - a list of viewport ids that show this tile
325
+ * @return {void}
326
+ */
327
+ updateVisibility(frameState, viewportIds) {
328
+ if (this._frameNumber === frameState.frameNumber) {
329
+ // Return early if visibility has already been checked during the traversal.
330
+ // The visibility may have already been checked if the cullWithChildrenBounds optimization is used.
331
+ return;
332
+ }
333
+ const parent = this.parent;
334
+ const parentVisibilityPlaneMask = parent
335
+ ? parent._visibilityPlaneMask
336
+ : CullingVolume.MASK_INDETERMINATE;
337
+ if (this.tileset._traverser.options.updateTransforms) {
338
+ const parentTransform = parent ? parent.computedTransform : this.tileset.modelMatrix;
339
+ this._updateTransform(parentTransform);
340
+ }
341
+ this._distanceToCamera = this.distanceToTile(frameState);
342
+ this._screenSpaceError = this.getScreenSpaceError(frameState, false);
343
+ this._visibilityPlaneMask = this.visibility(frameState, parentVisibilityPlaneMask); // Use parent's plane mask to speed up visibility test
344
+ this._visible = this._visibilityPlaneMask !== CullingVolume.MASK_OUTSIDE;
345
+ this._inRequestVolume = this.insideViewerRequestVolume(frameState);
346
+ this._frameNumber = frameState.frameNumber;
347
+ this.viewportIds = viewportIds;
348
+ }
349
+ // Determines whether the tile's bounding volume intersects the culling volume.
350
+ // @param {FrameState} frameState The frame state.
351
+ // @param {Number} parentVisibilityPlaneMask The parent's plane mask to speed up the visibility check.
352
+ // @returns {Number} A plane mask as described above in {@link CullingVolume#computeVisibilityWithPlaneMask}.
353
+ visibility(frameState, parentVisibilityPlaneMask) {
354
+ const { cullingVolume } = frameState;
355
+ const { boundingVolume } = this;
356
+ // TODO Cesium specific - restore clippingPlanes
357
+ // const {clippingPlanes, clippingPlanesOriginMatrix} = tileset;
358
+ // if (clippingPlanes && clippingPlanes.enabled) {
359
+ // const intersection = clippingPlanes.computeIntersectionWithBoundingVolume(
360
+ // boundingVolume,
361
+ // clippingPlanesOriginMatrix
362
+ // );
363
+ // this._isClipped = intersection !== Intersect.INSIDE;
364
+ // if (intersection === Intersect.OUTSIDE) {
365
+ // return CullingVolume.MASK_OUTSIDE;
366
+ // }
367
+ // }
368
+ // return cullingVolume.computeVisibilityWithPlaneMask(boundingVolume, parentVisibilityPlaneMask);
369
+ return cullingVolume.computeVisibilityWithPlaneMask(boundingVolume, parentVisibilityPlaneMask);
370
+ }
371
+ // Assuming the tile's bounding volume intersects the culling volume, determines
372
+ // whether the tile's content's bounding volume intersects the culling volume.
373
+ // @param {FrameState} frameState The frame state.
374
+ // @returns {Intersect} The result of the intersection: the tile's content is completely outside, completely inside, or intersecting the culling volume.
375
+ contentVisibility() {
376
+ return true;
377
+ // TODO restore
378
+ /*
379
+ // Assumes the tile's bounding volume intersects the culling volume already, so
380
+ // just return Intersect.INSIDE if there is no content bounding volume.
381
+ if (!defined(this.contentBoundingVolume)) {
382
+ return Intersect.INSIDE;
383
+ }
384
+
385
+ if (this._visibilityPlaneMask === CullingVolume.MASK_INSIDE) {
386
+ // The tile's bounding volume is completely inside the culling volume so
387
+ // the content bounding volume must also be inside.
388
+ return Intersect.INSIDE;
389
+ }
390
+
391
+ // PERFORMANCE_IDEA: is it possible to burn less CPU on this test since we know the
392
+ // tile's (not the content's) bounding volume intersects the culling volume?
393
+ const cullingVolume = frameState.cullingVolume;
394
+ const boundingVolume = tile.contentBoundingVolume;
395
+
396
+ const tileset = this.tileset;
397
+ const clippingPlanes = tileset.clippingPlanes;
398
+ if (defined(clippingPlanes) && clippingPlanes.enabled) {
399
+ const intersection = clippingPlanes.computeIntersectionWithBoundingVolume(
400
+ boundingVolume,
401
+ tileset.clippingPlanesOriginMatrix
402
+ );
403
+ this._isClipped = intersection !== Intersect.INSIDE;
404
+ if (intersection === Intersect.OUTSIDE) {
405
+ return Intersect.OUTSIDE;
406
+ }
407
+ }
408
+
409
+ return cullingVolume.computeVisibility(boundingVolume);
410
+ */
411
+ }
412
+ /**
413
+ * Computes the (potentially approximate) distance from the closest point of the tile's bounding volume to the camera.
414
+ * @param frameState The frame state.
415
+ * @returns {Number} The distance, in meters, or zero if the camera is inside the bounding volume.
416
+ */
417
+ distanceToTile(frameState) {
418
+ const boundingVolume = this.boundingVolume;
419
+ return Math.sqrt(Math.max(boundingVolume.distanceSquaredTo(frameState.camera.position), 0));
420
+ }
421
+ /**
422
+ * Computes the tile's camera-space z-depth.
423
+ * @param frameState The frame state.
424
+ * @returns The distance, in meters.
425
+ */
426
+ cameraSpaceZDepth({ camera }) {
427
+ const boundingVolume = this.boundingVolume; // Gets the underlying OrientedBoundingBox or BoundingSphere
428
+ scratchVector.subVectors(boundingVolume.center, camera.position);
429
+ return camera.direction.dot(scratchVector);
430
+ }
431
+ /**
432
+ * Checks if the camera is inside the viewer request volume.
433
+ * @param {FrameState} frameState The frame state.
434
+ * @returns {Boolean} Whether the camera is inside the volume.
435
+ */
436
+ insideViewerRequestVolume(frameState) {
437
+ const viewerRequestVolume = this._viewerRequestVolume;
438
+ return (!viewerRequestVolume || viewerRequestVolume.distanceSquaredTo(frameState.camera.position) <= 0);
439
+ }
440
+ // TODO Cesium specific
441
+ // Update whether the tile has expired.
442
+ updateExpiration() {
443
+ if (defined(this._expireDate) && this.contentReady && !this.hasEmptyContent) {
444
+ const now = Date.now();
445
+ // @ts-ignore Date.lessThan - replace with ms compare?
446
+ if (Date.lessThan(this._expireDate, now)) {
447
+ this.contentState = TILE_CONTENT_STATE.EXPIRED;
448
+ this._expiredContent = this.content;
449
+ }
450
+ }
451
+ }
452
+ get extras() {
453
+ return this.header.extras;
454
+ }
455
+ // INTERNAL METHODS
456
+ _initializeLodMetric(header) {
457
+ if ('lodMetricType' in header) {
458
+ this.lodMetricType = header.lodMetricType;
459
+ }
460
+ else {
461
+ this.lodMetricType = (this.parent && this.parent.lodMetricType) || this.tileset.lodMetricType;
462
+ // eslint-disable-next-line
463
+ console.warn(`3D Tile: Required prop lodMetricType is undefined. Using parent lodMetricType`);
464
+ }
465
+ // This is used to compute screen space error, i.e., the error measured in pixels.
466
+ if ('lodMetricValue' in header) {
467
+ this.lodMetricValue = header.lodMetricValue;
468
+ }
469
+ else {
470
+ this.lodMetricValue =
471
+ (this.parent && this.parent.lodMetricValue) || this.tileset.lodMetricValue;
472
+ // eslint-disable-next-line
473
+ console.warn('3D Tile: Required prop lodMetricValue is undefined. Using parent lodMetricValue');
474
+ }
475
+ }
476
+ _initializeTransforms(tileHeader) {
477
+ // The local transform of this tile.
478
+ this.transform = tileHeader.transform ? new Matrix4(tileHeader.transform) : new Matrix4();
479
+ const parent = this.parent;
480
+ const tileset = this.tileset;
481
+ const parentTransform = parent && parent.computedTransform
482
+ ? parent.computedTransform.clone()
483
+ : tileset.modelMatrix.clone();
484
+ this.computedTransform = new Matrix4(parentTransform).multiplyRight(this.transform);
485
+ const parentInitialTransform = parent && parent._initialTransform ? parent._initialTransform.clone() : new Matrix4();
486
+ this._initialTransform = new Matrix4(parentInitialTransform).multiplyRight(this.transform);
487
+ }
488
+ _initializeBoundingVolumes(tileHeader) {
489
+ this._contentBoundingVolume = null;
490
+ this._viewerRequestVolume = null;
491
+ this._updateBoundingVolume(tileHeader);
492
+ }
493
+ _initializeContent(tileHeader) {
494
+ // Empty tile by default
495
+ this.content = { _tileset: this.tileset, _tile: this };
496
+ this.hasEmptyContent = true;
497
+ this.contentState = TILE_CONTENT_STATE.UNLOADED;
498
+ // When `true`, the tile's content points to an external tileset.
499
+ // This is `false` until the tile's content is loaded.
500
+ this.hasTilesetContent = false;
501
+ if (tileHeader.contentUrl) {
502
+ this.content = null;
503
+ this.hasEmptyContent = false;
504
+ }
505
+ }
506
+ // TODO - remove anything not related to basic visibility detection
507
+ _initializeRenderingState(header) {
508
+ this.depth = header.level || (this.parent ? this.parent.depth + 1 : 0);
509
+ this._shouldRefine = false;
510
+ // Members this are updated every frame for tree traversal and rendering optimizations:
511
+ this._distanceToCamera = 0;
512
+ this._centerZDepth = 0;
513
+ this._screenSpaceError = 0;
514
+ this._visibilityPlaneMask = CullingVolume.MASK_INDETERMINATE;
515
+ this._visible = undefined;
516
+ this._inRequestVolume = false;
517
+ this._stackLength = 0;
518
+ this._selectionDepth = 0;
519
+ this._frameNumber = 0;
520
+ this._touchedFrame = 0;
521
+ this._visitedFrame = 0;
522
+ this._selectedFrame = 0;
523
+ this._requestedFrame = 0;
524
+ this._priority = 0.0;
525
+ }
526
+ _getRefine(refine) {
527
+ // Inherit from parent tile if omitted.
528
+ return refine || (this.parent && this.parent.refine) || TILE_REFINEMENT.REPLACE;
529
+ }
530
+ _isTileset() {
531
+ return this.contentUrl.indexOf('.json') !== -1;
532
+ }
533
+ _onContentLoaded() {
534
+ // Vector and Geometry tile rendering do not support the skip LOD optimization.
535
+ switch (this.content && this.content.type) {
536
+ case 'vctr':
537
+ case 'geom':
538
+ // @ts-ignore
539
+ this.tileset._traverser.disableSkipLevelOfDetail = true;
540
+ break;
541
+ default:
542
+ }
543
+ // The content may be tileset json
544
+ if (this._isTileset()) {
545
+ this.hasTilesetContent = true;
546
+ }
547
+ else {
548
+ this.gpuMemoryUsageInBytes = this._getGpuMemoryUsageInBytes();
549
+ }
550
+ }
551
+ _updateBoundingVolume(header) {
552
+ // Update the bounding volumes
553
+ this.boundingVolume = createBoundingVolume(header.boundingVolume, this.computedTransform, this.boundingVolume);
554
+ const content = header.content;
555
+ if (!content) {
556
+ return;
557
+ }
558
+ // TODO Cesium specific
559
+ // Non-leaf tiles may have a content bounding-volume, which is a tight-fit bounding volume
560
+ // around only the features in the tile. This box is useful for culling for rendering,
561
+ // but not for culling for traversing the tree since it does not guarantee spatial coherence, i.e.,
562
+ // since it only bounds features in the tile, not the entire tile, children may be
563
+ // outside of this box.
564
+ if (content.boundingVolume) {
565
+ this._contentBoundingVolume = createBoundingVolume(content.boundingVolume, this.computedTransform, this._contentBoundingVolume);
566
+ }
567
+ if (header.viewerRequestVolume) {
568
+ this._viewerRequestVolume = createBoundingVolume(header.viewerRequestVolume, this.computedTransform, this._viewerRequestVolume);
569
+ }
570
+ }
571
+ // Update the tile's transform. The transform is applied to the tile's bounding volumes.
572
+ _updateTransform(parentTransform = new Matrix4()) {
573
+ const computedTransform = parentTransform.clone().multiplyRight(this.transform);
574
+ const didTransformChange = !computedTransform.equals(this.computedTransform);
575
+ if (!didTransformChange) {
576
+ return;
577
+ }
578
+ this.computedTransform = computedTransform;
579
+ this._updateBoundingVolume(this.header);
580
+ }
581
+ // Get options which are applicable only for the particular loader
582
+ _getLoaderSpecificOptions(loaderId) {
583
+ switch (loaderId) {
584
+ case 'i3s':
585
+ return {
586
+ ...this.tileset.options.i3s,
587
+ _tileOptions: {
588
+ attributeUrls: this.header.attributeUrls,
589
+ textureUrl: this.header.textureUrl,
590
+ textureFormat: this.header.textureFormat,
591
+ textureLoaderOptions: this.header.textureLoaderOptions,
592
+ materialDefinition: this.header.materialDefinition,
593
+ isDracoGeometry: this.header.isDracoGeometry,
594
+ mbs: this.header.mbs
595
+ },
596
+ _tilesetOptions: {
597
+ store: this.tileset.tileset.store,
598
+ attributeStorageInfo: this.tileset.tileset.attributeStorageInfo,
599
+ fields: this.tileset.tileset.fields
600
+ },
601
+ isTileHeader: false
602
+ };
603
+ case '3d-tiles':
604
+ case 'cesium-ion':
605
+ default:
606
+ return get3dTilesOptions(this.tileset.tileset);
607
+ }
608
+ }
412
609
  }
413
- //# sourceMappingURL=tile-3d.js.map