Composite3DTileContent.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. import defaultValue from "../Core/defaultValue.js";
  2. import defined from "../Core/defined.js";
  3. import deprecationWarning from "../Core/deprecationWarning.js";
  4. import destroyObject from "../Core/destroyObject.js";
  5. import getMagic from "../Core/getMagic.js";
  6. import RuntimeError from "../Core/RuntimeError.js";
  7. /**
  8. * Represents the contents of a
  9. * {@link https://github.com/CesiumGS/3d-tiles/tree/main/specification/TileFormats/Composite|Composite}
  10. * tile in a {@link https://github.com/CesiumGS/3d-tiles/tree/main/specification|3D Tiles} tileset.
  11. * <p>
  12. * Implements the {@link Cesium3DTileContent} interface.
  13. * </p>
  14. *
  15. * @alias Composite3DTileContent
  16. * @constructor
  17. *
  18. * @private
  19. */
  20. function Composite3DTileContent(tileset, tile, resource, contents) {
  21. this._tileset = tileset;
  22. this._tile = tile;
  23. this._resource = resource;
  24. if (!defined(contents)) {
  25. contents = [];
  26. }
  27. this._contents = contents;
  28. this._metadata = undefined;
  29. this._group = undefined;
  30. this._ready = false;
  31. this._resolveContent = undefined;
  32. this._readyPromise = new Promise((resolve) => {
  33. this._resolveContent = resolve;
  34. });
  35. }
  36. Object.defineProperties(Composite3DTileContent.prototype, {
  37. featurePropertiesDirty: {
  38. get: function () {
  39. const contents = this._contents;
  40. const length = contents.length;
  41. for (let i = 0; i < length; ++i) {
  42. if (contents[i].featurePropertiesDirty) {
  43. return true;
  44. }
  45. }
  46. return false;
  47. },
  48. set: function (value) {
  49. const contents = this._contents;
  50. const length = contents.length;
  51. for (let i = 0; i < length; ++i) {
  52. contents[i].featurePropertiesDirty = value;
  53. }
  54. },
  55. },
  56. /**
  57. * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
  58. * always returns <code>0</code>. Instead call <code>featuresLength</code> for a tile in the composite.
  59. * @memberof Composite3DTileContent.prototype
  60. */
  61. featuresLength: {
  62. get: function () {
  63. return 0;
  64. },
  65. },
  66. /**
  67. * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
  68. * always returns <code>0</code>. Instead call <code>pointsLength</code> for a tile in the composite.
  69. * @memberof Composite3DTileContent.prototype
  70. */
  71. pointsLength: {
  72. get: function () {
  73. return 0;
  74. },
  75. },
  76. /**
  77. * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
  78. * always returns <code>0</code>. Instead call <code>trianglesLength</code> for a tile in the composite.
  79. * @memberof Composite3DTileContent.prototype
  80. */
  81. trianglesLength: {
  82. get: function () {
  83. return 0;
  84. },
  85. },
  86. /**
  87. * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
  88. * always returns <code>0</code>. Instead call <code>geometryByteLength</code> for a tile in the composite.
  89. * @memberof Composite3DTileContent.prototype
  90. */
  91. geometryByteLength: {
  92. get: function () {
  93. return 0;
  94. },
  95. },
  96. /**
  97. * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
  98. * always returns <code>0</code>. Instead call <code>texturesByteLength</code> for a tile in the composite.
  99. * @memberof Composite3DTileContent.prototype
  100. */
  101. texturesByteLength: {
  102. get: function () {
  103. return 0;
  104. },
  105. },
  106. /**
  107. * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
  108. * always returns <code>0</code>. Instead call <code>batchTableByteLength</code> for a tile in the composite.
  109. * @memberof Composite3DTileContent.prototype
  110. */
  111. batchTableByteLength: {
  112. get: function () {
  113. return 0;
  114. },
  115. },
  116. innerContents: {
  117. get: function () {
  118. return this._contents;
  119. },
  120. },
  121. /**
  122. * Returns true when the tile's content is ready to render; otherwise false
  123. *
  124. * @memberof Composite3DTileContent.prototype
  125. *
  126. * @type {boolean}
  127. * @readonly
  128. * @private
  129. */
  130. ready: {
  131. get: function () {
  132. return this._ready;
  133. },
  134. },
  135. /**
  136. * Gets the promise that will be resolved when the tile's content is ready to render.
  137. *
  138. * @memberof Composite3DTileContent.prototype
  139. *
  140. * @type {Promise<Composite3DTileContent>}
  141. * @readonly
  142. * @deprecated
  143. * @private
  144. */
  145. readyPromise: {
  146. get: function () {
  147. deprecationWarning(
  148. "Composite3DTileContent.readyPromise",
  149. "Composite3DTileContent.readyPromise was deprecated in CesiumJS 1.104. It will be removed in 1.107. Wait for Composite3DTileContent.ready to return true instead."
  150. );
  151. return this._readyPromise;
  152. },
  153. },
  154. tileset: {
  155. get: function () {
  156. return this._tileset;
  157. },
  158. },
  159. tile: {
  160. get: function () {
  161. return this._tile;
  162. },
  163. },
  164. url: {
  165. get: function () {
  166. return this._resource.getUrlComponent(true);
  167. },
  168. },
  169. /**
  170. * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
  171. * both stores the content metadata and propagates the content metadata to all of its children.
  172. * @memberof Composite3DTileContent.prototype
  173. * @private
  174. * @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.
  175. */
  176. metadata: {
  177. get: function () {
  178. return this._metadata;
  179. },
  180. set: function (value) {
  181. this._metadata = value;
  182. const contents = this._contents;
  183. const length = contents.length;
  184. for (let i = 0; i < length; ++i) {
  185. contents[i].metadata = value;
  186. }
  187. },
  188. },
  189. /**
  190. * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
  191. * always returns <code>undefined</code>. Instead call <code>batchTable</code> for a tile in the composite.
  192. * @memberof Composite3DTileContent.prototype
  193. */
  194. batchTable: {
  195. get: function () {
  196. return undefined;
  197. },
  198. },
  199. /**
  200. * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
  201. * both stores the group metadata and propagates the group metadata to all of its children.
  202. * @memberof Composite3DTileContent.prototype
  203. * @private
  204. * @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.
  205. */
  206. group: {
  207. get: function () {
  208. return this._group;
  209. },
  210. set: function (value) {
  211. this._group = value;
  212. const contents = this._contents;
  213. const length = contents.length;
  214. for (let i = 0; i < length; ++i) {
  215. contents[i].group = value;
  216. }
  217. },
  218. },
  219. });
  220. const sizeOfUint32 = Uint32Array.BYTES_PER_ELEMENT;
  221. Composite3DTileContent.fromTileType = async function (
  222. tileset,
  223. tile,
  224. resource,
  225. arrayBuffer,
  226. byteOffset,
  227. factory
  228. ) {
  229. byteOffset = defaultValue(byteOffset, 0);
  230. const uint8Array = new Uint8Array(arrayBuffer);
  231. const view = new DataView(arrayBuffer);
  232. byteOffset += sizeOfUint32; // Skip magic
  233. const version = view.getUint32(byteOffset, true);
  234. if (version !== 1) {
  235. throw new RuntimeError(
  236. `Only Composite Tile version 1 is supported. Version ${version} is not.`
  237. );
  238. }
  239. byteOffset += sizeOfUint32;
  240. // Skip byteLength
  241. byteOffset += sizeOfUint32;
  242. const tilesLength = view.getUint32(byteOffset, true);
  243. byteOffset += sizeOfUint32;
  244. // For caching purposes, models within the composite tile must be
  245. // distinguished. To do this, add a query parameter ?compositeIndex=i.
  246. // Since composite tiles may contain other composite tiles, check for an
  247. // existing prefix and separate them with underscores. e.g.
  248. // ?compositeIndex=0_1_1
  249. let prefix = resource.queryParameters.compositeIndex;
  250. if (defined(prefix)) {
  251. // We'll be adding another value at the end, so add an underscore.
  252. prefix = `${prefix}_`;
  253. } else {
  254. // no prefix
  255. prefix = "";
  256. }
  257. const promises = [];
  258. promises.length = tilesLength;
  259. for (let i = 0; i < tilesLength; ++i) {
  260. const tileType = getMagic(uint8Array, byteOffset);
  261. // Tile byte length is stored after magic and version
  262. const tileByteLength = view.getUint32(byteOffset + sizeOfUint32 * 2, true);
  263. const contentFactory = factory[tileType];
  264. // Label which content within the composite this is
  265. const compositeIndex = `${prefix}${i}`;
  266. const childResource = resource.getDerivedResource({
  267. queryParameters: {
  268. compositeIndex: compositeIndex,
  269. },
  270. });
  271. if (defined(contentFactory)) {
  272. promises[i] = Promise.resolve(
  273. contentFactory(tileset, tile, childResource, arrayBuffer, byteOffset)
  274. );
  275. } else {
  276. throw new RuntimeError(
  277. `Unknown tile content type, ${tileType}, inside Composite tile`
  278. );
  279. }
  280. byteOffset += tileByteLength;
  281. }
  282. const innerContents = await Promise.all(promises);
  283. const content = new Composite3DTileContent(
  284. tileset,
  285. tile,
  286. resource,
  287. innerContents
  288. );
  289. return content;
  290. };
  291. /**
  292. * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
  293. * always returns <code>false</code>. Instead call <code>hasProperty</code> for a tile in the composite.
  294. */
  295. Composite3DTileContent.prototype.hasProperty = function (batchId, name) {
  296. return false;
  297. };
  298. /**
  299. * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
  300. * always returns <code>undefined</code>. Instead call <code>getFeature</code> for a tile in the composite.
  301. */
  302. Composite3DTileContent.prototype.getFeature = function (batchId) {
  303. return undefined;
  304. };
  305. Composite3DTileContent.prototype.applyDebugSettings = function (
  306. enabled,
  307. color
  308. ) {
  309. const contents = this._contents;
  310. const length = contents.length;
  311. for (let i = 0; i < length; ++i) {
  312. contents[i].applyDebugSettings(enabled, color);
  313. }
  314. };
  315. Composite3DTileContent.prototype.applyStyle = function (style) {
  316. const contents = this._contents;
  317. const length = contents.length;
  318. for (let i = 0; i < length; ++i) {
  319. contents[i].applyStyle(style);
  320. }
  321. };
  322. Composite3DTileContent.prototype.update = function (tileset, frameState) {
  323. const contents = this._contents;
  324. const length = contents.length;
  325. let ready = true;
  326. for (let i = 0; i < length; ++i) {
  327. contents[i].update(tileset, frameState);
  328. ready = ready && contents[i].ready;
  329. }
  330. if (!this._ready && ready) {
  331. this._ready = true;
  332. this._resolveContent(this);
  333. }
  334. };
  335. Composite3DTileContent.prototype.isDestroyed = function () {
  336. return false;
  337. };
  338. Composite3DTileContent.prototype.destroy = function () {
  339. const contents = this._contents;
  340. const length = contents.length;
  341. for (let i = 0; i < length; ++i) {
  342. contents[i].destroy();
  343. }
  344. return destroyObject(this);
  345. };
  346. export default Composite3DTileContent;