智慧水务管理系统 - 精河县供水工程综合管理平台

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import Check from "../Core/Check.js";
  2. import deprecationWarning from "../Core/deprecationWarning.js";
  3. import getJsonFromTypedArray from "../Core/getJsonFromTypedArray.js";
  4. import RuntimeError from "../Core/RuntimeError.js";
  5. /**
  6. * Handles parsing of an Instanced 3D Model.
  7. *
  8. * @namespace I3dmParser
  9. * @private
  10. */
  11. const I3dmParser = {};
  12. I3dmParser._deprecationWarning = deprecationWarning;
  13. const sizeOfUint32 = Uint32Array.BYTES_PER_ELEMENT;
  14. /**
  15. * Parses the contents of a {@link https://github.com/CesiumGS/3d-tiles/tree/main/specification/TileFormats/Instanced3DModel|Instanced 3D Model}.
  16. *
  17. * @private
  18. *
  19. * @param {ArrayBuffer} arrayBuffer The array buffer containing the i3dm.
  20. * @param {number} [byteOffset=0] The byte offset of the beginning of the i3dm in the array buffer.
  21. * @returns {object} Returns an object with the glTF format, feature table (binary and json), batch table (binary and json) and glTF parts of the i3dm.
  22. */
  23. I3dmParser.parse = function (arrayBuffer, byteOffset) {
  24. //>>includeStart('debug', pragmas.debug);
  25. Check.defined("arrayBuffer", arrayBuffer);
  26. //>>includeEnd('debug');
  27. const byteStart = byteOffset ?? 0;
  28. byteOffset = byteStart;
  29. const uint8Array = new Uint8Array(arrayBuffer);
  30. const view = new DataView(arrayBuffer);
  31. byteOffset += sizeOfUint32; // Skip magic
  32. const version = view.getUint32(byteOffset, true);
  33. if (version !== 1) {
  34. throw new RuntimeError(
  35. `Only Instanced 3D Model version 1 is supported. Version ${version} is not.`,
  36. );
  37. }
  38. byteOffset += sizeOfUint32;
  39. const byteLength = view.getUint32(byteOffset, true);
  40. byteOffset += sizeOfUint32;
  41. const featureTableJsonByteLength = view.getUint32(byteOffset, true);
  42. if (featureTableJsonByteLength === 0) {
  43. throw new RuntimeError(
  44. "featureTableJsonByteLength is zero, the feature table must be defined.",
  45. );
  46. }
  47. byteOffset += sizeOfUint32;
  48. const featureTableBinaryByteLength = view.getUint32(byteOffset, true);
  49. byteOffset += sizeOfUint32;
  50. const batchTableJsonByteLength = view.getUint32(byteOffset, true);
  51. byteOffset += sizeOfUint32;
  52. const batchTableBinaryByteLength = view.getUint32(byteOffset, true);
  53. byteOffset += sizeOfUint32;
  54. const gltfFormat = view.getUint32(byteOffset, true);
  55. if (gltfFormat !== 1 && gltfFormat !== 0) {
  56. throw new RuntimeError(
  57. `Only glTF format 0 (uri) or 1 (embedded) are supported. Format ${gltfFormat} is not.`,
  58. );
  59. }
  60. byteOffset += sizeOfUint32;
  61. const featureTableJson = getJsonFromTypedArray(
  62. uint8Array,
  63. byteOffset,
  64. featureTableJsonByteLength,
  65. );
  66. byteOffset += featureTableJsonByteLength;
  67. const featureTableBinary = new Uint8Array(
  68. arrayBuffer,
  69. byteOffset,
  70. featureTableBinaryByteLength,
  71. );
  72. byteOffset += featureTableBinaryByteLength;
  73. let batchTableJson;
  74. let batchTableBinary;
  75. if (batchTableJsonByteLength > 0) {
  76. batchTableJson = getJsonFromTypedArray(
  77. uint8Array,
  78. byteOffset,
  79. batchTableJsonByteLength,
  80. );
  81. byteOffset += batchTableJsonByteLength;
  82. if (batchTableBinaryByteLength > 0) {
  83. // Has a batch table binary
  84. batchTableBinary = new Uint8Array(
  85. arrayBuffer,
  86. byteOffset,
  87. batchTableBinaryByteLength,
  88. );
  89. // Copy the batchTableBinary section and let the underlying ArrayBuffer be freed
  90. batchTableBinary = new Uint8Array(batchTableBinary);
  91. byteOffset += batchTableBinaryByteLength;
  92. }
  93. }
  94. const gltfByteLength = byteStart + byteLength - byteOffset;
  95. if (gltfByteLength === 0) {
  96. throw new RuntimeError("glTF byte length must be greater than 0.");
  97. }
  98. let gltfView;
  99. if (byteOffset % 4 === 0) {
  100. gltfView = new Uint8Array(arrayBuffer, byteOffset, gltfByteLength);
  101. } else {
  102. // Create a copy of the glb so that it is 4-byte aligned
  103. I3dmParser._deprecationWarning(
  104. "i3dm-glb-unaligned",
  105. "The embedded glb is not aligned to a 4-byte boundary.",
  106. );
  107. gltfView = new Uint8Array(
  108. uint8Array.subarray(byteOffset, byteOffset + gltfByteLength),
  109. );
  110. }
  111. return {
  112. gltfFormat: gltfFormat,
  113. featureTableJson: featureTableJson,
  114. featureTableBinary: featureTableBinary,
  115. batchTableJson: batchTableJson,
  116. batchTableBinary: batchTableBinary,
  117. gltf: gltfView,
  118. };
  119. };
  120. export default I3dmParser;