Composite3DTileContent.js 7.4 KB

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