| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- import Cartesian3 from "../Core/Cartesian3.js";
- import defined from "../Core/defined.js";
- import destroyObject from "../Core/destroyObject.js";
- import getMagic from "../Core/getMagic.js";
- import RuntimeError from "../Core/RuntimeError.js";
-
- /**
- * Represents the contents of a
- * {@link https://github.com/CesiumGS/3d-tiles/tree/main/specification/TileFormats/Composite|Composite}
- * tile in a {@link https://github.com/CesiumGS/3d-tiles/tree/main/specification|3D Tiles} tileset.
- * <p>
- * Implements the {@link Cesium3DTileContent} interface.
- * </p>
- *
- * @implements Cesium3DTileContent
- * @private
- */
- class Composite3DTileContent {
- constructor(tileset, tile, resource, contents) {
- this._tileset = tileset;
- this._tile = tile;
- this._resource = resource;
-
- if (!defined(contents)) {
- contents = [];
- }
- this._contents = contents;
-
- this._metadata = undefined;
- this._group = undefined;
- this._ready = false;
- }
-
- get featurePropertiesDirty() {
- const contents = this._contents;
- const length = contents.length;
- for (let i = 0; i < length; ++i) {
- if (contents[i].featurePropertiesDirty) {
- return true;
- }
- }
-
- return false;
- }
-
- set featurePropertiesDirty(value) {
- const contents = this._contents;
- const length = contents.length;
- for (let i = 0; i < length; ++i) {
- contents[i].featurePropertiesDirty = value;
- }
- }
-
- /**
- * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
- * always returns <code>0</code>. Instead call <code>featuresLength</code> for a tile in the composite.
- */
- get featuresLength() {
- return 0;
- }
-
- /**
- * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
- * always returns <code>0</code>. Instead call <code>pointsLength</code> for a tile in the composite.
- */
- get pointsLength() {
- return 0;
- }
-
- /**
- * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
- * always returns <code>0</code>. Instead call <code>trianglesLength</code> for a tile in the composite.
- */
- get trianglesLength() {
- return 0;
- }
-
- /**
- * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
- * always returns <code>0</code>. Instead call <code>geometryByteLength</code> for a tile in the composite.
- */
- get geometryByteLength() {
- return 0;
- }
-
- /**
- * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
- * always returns <code>0</code>. Instead call <code>texturesByteLength</code> for a tile in the composite.
- */
- get texturesByteLength() {
- return 0;
- }
-
- /**
- * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
- * always returns <code>0</code>. Instead call <code>batchTableByteLength</code> for a tile in the composite.
- */
- get batchTableByteLength() {
- return 0;
- }
-
- get innerContents() {
- return this._contents;
- }
-
- /**
- * Returns true when the tile's content is ready to render; otherwise false
- *
- *
- * @type {boolean}
- * @readonly
- * @private
- */
- get ready() {
- return this._ready;
- }
-
- get tileset() {
- return this._tileset;
- }
-
- get tile() {
- return this._tile;
- }
-
- get url() {
- return this._resource.getUrlComponent(true);
- }
-
- /**
- * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
- * both stores the content metadata and propagates the content metadata to all of its children.
- * @private
- * @experimental This feature is using part of the 3D Tiles spec that is not final and is subject to change without Cesium's standard deprecation policy.
- */
- get metadata() {
- return this._metadata;
- }
-
- set metadata(value) {
- this._metadata = value;
- const contents = this._contents;
- const length = contents.length;
- for (let i = 0; i < length; ++i) {
- contents[i].metadata = value;
- }
- }
-
- /**
- * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
- * always returns <code>undefined</code>. Instead call <code>batchTable</code> for a tile in the composite.
- */
- get batchTable() {
- return undefined;
- }
-
- /**
- * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
- * both stores the group metadata and propagates the group metadata to all of its children.
- * @private
- * @experimental This feature is using part of the 3D Tiles spec that is not final and is subject to change without Cesium's standard deprecation policy.
- */
- get group() {
- return this._group;
- }
-
- set group(value) {
- this._group = value;
- const contents = this._contents;
- const length = contents.length;
- for (let i = 0; i < length; ++i) {
- contents[i].group = value;
- }
- }
-
- static async fromTileType(
- tileset,
- tile,
- resource,
- arrayBuffer,
- byteOffset,
- factory,
- ) {
- byteOffset = byteOffset ?? 0;
-
- const uint8Array = new Uint8Array(arrayBuffer);
- const view = new DataView(arrayBuffer);
- byteOffset += sizeOfUint32; // Skip magic
-
- const version = view.getUint32(byteOffset, true);
- if (version !== 1) {
- throw new RuntimeError(
- `Only Composite Tile version 1 is supported. Version ${version} is not.`,
- );
- }
- byteOffset += sizeOfUint32;
-
- // Skip byteLength
- byteOffset += sizeOfUint32;
-
- const tilesLength = view.getUint32(byteOffset, true);
- byteOffset += sizeOfUint32;
-
- // For caching purposes, models within the composite tile must be
- // distinguished. To do this, add a query parameter ?compositeIndex=i.
- // Since composite tiles may contain other composite tiles, check for an
- // existing prefix and separate them with underscores. e.g.
- // ?compositeIndex=0_1_1
- let prefix = resource.queryParameters.compositeIndex;
- if (defined(prefix)) {
- // We'll be adding another value at the end, so add an underscore.
- prefix = `${prefix}_`;
- } else {
- // no prefix
- prefix = "";
- }
-
- const promises = [];
- promises.length = tilesLength;
- for (let i = 0; i < tilesLength; ++i) {
- const tileType = getMagic(uint8Array, byteOffset);
-
- // Tile byte length is stored after magic and version
- const tileByteLength = view.getUint32(
- byteOffset + sizeOfUint32 * 2,
- true,
- );
-
- const contentFactory = factory[tileType];
-
- // Label which content within the composite this is
- const compositeIndex = `${prefix}${i}`;
- const childResource = resource.getDerivedResource({
- queryParameters: {
- compositeIndex: compositeIndex,
- },
- });
-
- if (defined(contentFactory)) {
- promises[i] = Promise.resolve(
- contentFactory(tileset, tile, childResource, arrayBuffer, byteOffset),
- );
- } else {
- throw new RuntimeError(
- `Unknown tile content type, ${tileType}, inside Composite tile`,
- );
- }
-
- byteOffset += tileByteLength;
- }
-
- const innerContents = await Promise.all(promises);
- const content = new Composite3DTileContent(
- tileset,
- tile,
- resource,
- innerContents,
- );
- return content;
- }
-
- /**
- * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
- * always returns <code>false</code>. Instead call <code>hasProperty</code> for a tile in the composite.
- */
- hasProperty(batchId, name) {
- return false;
- }
-
- /**
- * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
- * always returns <code>undefined</code>. Instead call <code>getFeature</code> for a tile in the composite.
- */
- getFeature(batchId) {
- return undefined;
- }
-
- applyDebugSettings(enabled, color) {
- const contents = this._contents;
- const length = contents.length;
- for (let i = 0; i < length; ++i) {
- contents[i].applyDebugSettings(enabled, color);
- }
- }
-
- applyStyle(style) {
- const contents = this._contents;
- const length = contents.length;
- for (let i = 0; i < length; ++i) {
- contents[i].applyStyle(style);
- }
- }
-
- update(tileset, frameState) {
- const contents = this._contents;
- const length = contents.length;
- let ready = true;
- for (let i = 0; i < length; ++i) {
- contents[i].update(tileset, frameState);
- ready = ready && contents[i].ready;
- }
-
- if (!this._ready && ready) {
- this._ready = true;
- }
- }
-
- /**
- * Find an intersection between a ray and the tile content surface that was rendered. The ray must be given in world coordinates.
- *
- * @param {Ray} ray The ray to test for intersection.
- * @param {FrameState} frameState The frame state.
- * @param {Cartesian3|undefined} [result] The intersection or <code>undefined</code> if none was found.
- * @returns {Cartesian3|undefined} The intersection or <code>undefined</code> if none was found.
- *
- * @private
- */
- pick(ray, frameState, result) {
- if (!this._ready) {
- return undefined;
- }
-
- let intersection;
- let minDistance = Number.POSITIVE_INFINITY;
- const contents = this._contents;
- const length = contents.length;
-
- for (let i = 0; i < length; ++i) {
- const candidate = contents[i].pick(ray, frameState, result);
-
- if (!defined(candidate)) {
- continue;
- }
-
- const distance = Cartesian3.distance(ray.origin, candidate);
- if (distance < minDistance) {
- intersection = candidate;
- minDistance = distance;
- }
- }
-
- if (!defined(intersection)) {
- return undefined;
- }
-
- return result;
- }
-
- isDestroyed() {
- return false;
- }
-
- destroy() {
- const contents = this._contents;
- const length = contents.length;
- for (let i = 0; i < length; ++i) {
- contents[i].destroy();
- }
- return destroyObject(this);
- }
- }
-
- const sizeOfUint32 = Uint32Array.BYTES_PER_ELEMENT;
-
- export default Composite3DTileContent;
|