Composite3DTileContent.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. import defaultValue from "../Core/defaultValue.js";
  2. import defer from "../Core/defer.js";
  3. import defined from "../Core/defined.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(
  21. tileset,
  22. tile,
  23. resource,
  24. arrayBuffer,
  25. byteOffset,
  26. factory
  27. ) {
  28. this._tileset = tileset;
  29. this._tile = tile;
  30. this._resource = resource;
  31. this._contents = [];
  32. this._readyPromise = defer();
  33. this._metadata = undefined;
  34. this._group = undefined;
  35. initialize(this, arrayBuffer, byteOffset, factory);
  36. }
  37. Object.defineProperties(Composite3DTileContent.prototype, {
  38. featurePropertiesDirty: {
  39. get: function () {
  40. const contents = this._contents;
  41. const length = contents.length;
  42. for (let i = 0; i < length; ++i) {
  43. if (contents[i].featurePropertiesDirty) {
  44. return true;
  45. }
  46. }
  47. return false;
  48. },
  49. set: function (value) {
  50. const contents = this._contents;
  51. const length = contents.length;
  52. for (let i = 0; i < length; ++i) {
  53. contents[i].featurePropertiesDirty = value;
  54. }
  55. },
  56. },
  57. /**
  58. * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
  59. * always returns <code>0</code>. Instead call <code>featuresLength</code> for a tile in the composite.
  60. * @memberof Composite3DTileContent.prototype
  61. */
  62. featuresLength: {
  63. get: function () {
  64. return 0;
  65. },
  66. },
  67. /**
  68. * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
  69. * always returns <code>0</code>. Instead call <code>pointsLength</code> for a tile in the composite.
  70. * @memberof Composite3DTileContent.prototype
  71. */
  72. pointsLength: {
  73. get: function () {
  74. return 0;
  75. },
  76. },
  77. /**
  78. * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
  79. * always returns <code>0</code>. Instead call <code>trianglesLength</code> for a tile in the composite.
  80. * @memberof Composite3DTileContent.prototype
  81. */
  82. trianglesLength: {
  83. get: function () {
  84. return 0;
  85. },
  86. },
  87. /**
  88. * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
  89. * always returns <code>0</code>. Instead call <code>geometryByteLength</code> for a tile in the composite.
  90. * @memberof Composite3DTileContent.prototype
  91. */
  92. geometryByteLength: {
  93. get: function () {
  94. return 0;
  95. },
  96. },
  97. /**
  98. * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
  99. * always returns <code>0</code>. Instead call <code>texturesByteLength</code> for a tile in the composite.
  100. * @memberof Composite3DTileContent.prototype
  101. */
  102. texturesByteLength: {
  103. get: function () {
  104. return 0;
  105. },
  106. },
  107. /**
  108. * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
  109. * always returns <code>0</code>. Instead call <code>batchTableByteLength</code> for a tile in the composite.
  110. * @memberof Composite3DTileContent.prototype
  111. */
  112. batchTableByteLength: {
  113. get: function () {
  114. return 0;
  115. },
  116. },
  117. innerContents: {
  118. get: function () {
  119. return this._contents;
  120. },
  121. },
  122. readyPromise: {
  123. get: function () {
  124. return this._readyPromise.promise;
  125. },
  126. },
  127. tileset: {
  128. get: function () {
  129. return this._tileset;
  130. },
  131. },
  132. tile: {
  133. get: function () {
  134. return this._tile;
  135. },
  136. },
  137. url: {
  138. get: function () {
  139. return this._resource.getUrlComponent(true);
  140. },
  141. },
  142. /**
  143. * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
  144. * both stores the content metadata and propagates the content metadata to all of its children.
  145. * @memberof Composite3DTileContent.prototype
  146. * @private
  147. * @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.
  148. */
  149. metadata: {
  150. get: function () {
  151. return this._metadata;
  152. },
  153. set: function (value) {
  154. this._metadata = value;
  155. const contents = this._contents;
  156. const length = contents.length;
  157. for (let i = 0; i < length; ++i) {
  158. contents[i].metadata = value;
  159. }
  160. },
  161. },
  162. /**
  163. * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
  164. * always returns <code>undefined</code>. Instead call <code>batchTable</code> for a tile in the composite.
  165. * @memberof Composite3DTileContent.prototype
  166. */
  167. batchTable: {
  168. get: function () {
  169. return undefined;
  170. },
  171. },
  172. /**
  173. * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
  174. * both stores the group metadata and propagates the group metadata to all of its children.
  175. * @memberof Composite3DTileContent.prototype
  176. * @private
  177. * @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.
  178. */
  179. group: {
  180. get: function () {
  181. return this._group;
  182. },
  183. set: function (value) {
  184. this._group = value;
  185. const contents = this._contents;
  186. const length = contents.length;
  187. for (let i = 0; i < length; ++i) {
  188. contents[i].group = value;
  189. }
  190. },
  191. },
  192. });
  193. const sizeOfUint32 = Uint32Array.BYTES_PER_ELEMENT;
  194. function initialize(content, arrayBuffer, byteOffset, factory) {
  195. byteOffset = defaultValue(byteOffset, 0);
  196. const uint8Array = new Uint8Array(arrayBuffer);
  197. const view = new DataView(arrayBuffer);
  198. byteOffset += sizeOfUint32; // Skip magic
  199. const version = view.getUint32(byteOffset, true);
  200. if (version !== 1) {
  201. throw new RuntimeError(
  202. `Only Composite Tile version 1 is supported. Version ${version} is not.`
  203. );
  204. }
  205. byteOffset += sizeOfUint32;
  206. // Skip byteLength
  207. byteOffset += sizeOfUint32;
  208. const tilesLength = view.getUint32(byteOffset, true);
  209. byteOffset += sizeOfUint32;
  210. const contentPromises = [];
  211. for (let i = 0; i < tilesLength; ++i) {
  212. const tileType = getMagic(uint8Array, byteOffset);
  213. // Tile byte length is stored after magic and version
  214. const tileByteLength = view.getUint32(byteOffset + sizeOfUint32 * 2, true);
  215. const contentFactory = factory[tileType];
  216. if (defined(contentFactory)) {
  217. const innerContent = contentFactory(
  218. content._tileset,
  219. content._tile,
  220. content._resource,
  221. arrayBuffer,
  222. byteOffset
  223. );
  224. content._contents.push(innerContent);
  225. contentPromises.push(innerContent.readyPromise);
  226. } else {
  227. throw new RuntimeError(
  228. `Unknown tile content type, ${tileType}, inside Composite tile`
  229. );
  230. }
  231. byteOffset += tileByteLength;
  232. }
  233. Promise.all(contentPromises)
  234. .then(function () {
  235. content._readyPromise.resolve(content);
  236. })
  237. .catch(function (error) {
  238. content._readyPromise.reject(error);
  239. });
  240. }
  241. /**
  242. * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
  243. * always returns <code>false</code>. Instead call <code>hasProperty</code> for a tile in the composite.
  244. */
  245. Composite3DTileContent.prototype.hasProperty = function (batchId, name) {
  246. return false;
  247. };
  248. /**
  249. * Part of the {@link Cesium3DTileContent} interface. <code>Composite3DTileContent</code>
  250. * always returns <code>undefined</code>. Instead call <code>getFeature</code> for a tile in the composite.
  251. */
  252. Composite3DTileContent.prototype.getFeature = function (batchId) {
  253. return undefined;
  254. };
  255. Composite3DTileContent.prototype.applyDebugSettings = function (
  256. enabled,
  257. color
  258. ) {
  259. const contents = this._contents;
  260. const length = contents.length;
  261. for (let i = 0; i < length; ++i) {
  262. contents[i].applyDebugSettings(enabled, color);
  263. }
  264. };
  265. Composite3DTileContent.prototype.applyStyle = function (style) {
  266. const contents = this._contents;
  267. const length = contents.length;
  268. for (let i = 0; i < length; ++i) {
  269. contents[i].applyStyle(style);
  270. }
  271. };
  272. Composite3DTileContent.prototype.update = function (tileset, frameState) {
  273. const contents = this._contents;
  274. const length = contents.length;
  275. for (let i = 0; i < length; ++i) {
  276. contents[i].update(tileset, frameState);
  277. }
  278. };
  279. Composite3DTileContent.prototype.isDestroyed = function () {
  280. return false;
  281. };
  282. Composite3DTileContent.prototype.destroy = function () {
  283. const contents = this._contents;
  284. const length = contents.length;
  285. for (let i = 0; i < length; ++i) {
  286. contents[i].destroy();
  287. }
  288. return destroyObject(this);
  289. };
  290. export default Composite3DTileContent;