preprocess3DTileContent.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import defined from "../Core/defined.js";
  2. import getJsonFromTypedArray from "../Core/getJsonFromTypedArray.js";
  3. import getMagic from "../Core/getMagic.js";
  4. import RuntimeError from "../Core/RuntimeError.js";
  5. import Cesium3DTileContentType from "./Cesium3DTileContentType.js";
  6. /**
  7. * Results of the preprocess3DTileContent() function. This includes the
  8. * {@link Cesium3DTileContentType} and the payload. The payload is either
  9. * binary or JSON depending on the content type.
  10. *
  11. * @typedef {object} PreprocessedContent
  12. * @property {Cesium3DTileContentType} contentType The type of the content
  13. * @property {Uint8Array} [binaryPayload] For binary files, the payload is returned as a typed array with byteOffset of 0
  14. * @property {object} [jsonPayload] For JSON files, the results are returned as a JSON object.
  15. * @private
  16. */
  17. /**
  18. * Preprocess a {@link Cesium3DTileContent}, to determine the type of content
  19. * and to parse JSON files into objects.
  20. *
  21. * @param {ArrayBuffer} arrayBuffer The raw binary payload
  22. * @return {PreprocessedContent}
  23. * @private
  24. */
  25. function preprocess3DTileContent(arrayBuffer) {
  26. const uint8Array = new Uint8Array(arrayBuffer);
  27. let contentType = getMagic(uint8Array);
  28. // We use glTF for JSON glTF files. For binary glTF, we rename this
  29. // to glb to disambiguate
  30. if (contentType === "glTF") {
  31. contentType = "glb";
  32. }
  33. if (Cesium3DTileContentType.isBinaryFormat(contentType)) {
  34. return {
  35. // For binary files, the enum value is the magic number
  36. contentType: contentType,
  37. binaryPayload: uint8Array,
  38. };
  39. }
  40. const json = getJsonContent(uint8Array);
  41. if (defined(json.root)) {
  42. // Most likely a tileset JSON
  43. return {
  44. contentType: Cesium3DTileContentType.EXTERNAL_TILESET,
  45. jsonPayload: json,
  46. };
  47. }
  48. if (defined(json.asset)) {
  49. // Most likely a glTF. Tileset JSON also has an "asset" property
  50. // so this check needs to happen second
  51. return {
  52. contentType: Cesium3DTileContentType.GLTF,
  53. jsonPayload: json,
  54. };
  55. }
  56. if (defined(json.tileAvailability)) {
  57. // Most likely a subtree JSON.
  58. return {
  59. contentType: Cesium3DTileContentType.IMPLICIT_SUBTREE_JSON,
  60. jsonPayload: json,
  61. };
  62. }
  63. if (defined(json.type)) {
  64. // Most likely a GeoJSON
  65. return {
  66. contentType: Cesium3DTileContentType.GEOJSON,
  67. jsonPayload: json,
  68. };
  69. }
  70. if (defined(json.voxelTable)) {
  71. // Most likely a voxel JSON
  72. return {
  73. contentType: Cesium3DTileContentType.VOXEL_JSON,
  74. jsonPayload: json,
  75. };
  76. }
  77. throw new RuntimeError("Invalid tile content.");
  78. }
  79. function getJsonContent(uint8Array) {
  80. let json;
  81. try {
  82. json = getJsonFromTypedArray(uint8Array);
  83. } catch (error) {
  84. throw new RuntimeError("Invalid tile content.");
  85. }
  86. return json;
  87. }
  88. export default preprocess3DTileContent;