preprocess3DTileContent.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. export default 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. return {
  58. // Most likely a subtree JSON.
  59. contentType: Cesium3DTileContentType.IMPLICIT_SUBTREE_JSON,
  60. jsonPayload: json,
  61. };
  62. }
  63. throw new RuntimeError("Invalid tile content.");
  64. }
  65. function getJsonContent(uint8Array) {
  66. let json;
  67. try {
  68. json = getJsonFromTypedArray(uint8Array);
  69. } catch (error) {
  70. throw new RuntimeError("Invalid tile content.");
  71. }
  72. return json;
  73. }