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

DracoLoader.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import defined from "../Core/defined.js";
  2. import FeatureDetection from "../Core/FeatureDetection.js";
  3. import RuntimeError from "../Core/RuntimeError.js";
  4. import TaskProcessor from "../Core/TaskProcessor.js";
  5. /**
  6. * @private
  7. */
  8. class DracoLoader {
  9. static _getDecoderTaskProcessor() {
  10. if (!defined(DracoLoader._decoderTaskProcessor)) {
  11. const processor = new TaskProcessor(
  12. "decodeDraco",
  13. DracoLoader._maxDecodingConcurrency,
  14. );
  15. processor
  16. .initWebAssemblyModule({
  17. wasmBinaryFile: "ThirdParty/draco_decoder.wasm",
  18. })
  19. .then(function (result) {
  20. if (result) {
  21. DracoLoader._taskProcessorReady = true;
  22. } else {
  23. DracoLoader._error = new RuntimeError(
  24. "Draco decoder could not be initialized.",
  25. );
  26. }
  27. })
  28. .catch((error) => {
  29. DracoLoader._error = error;
  30. });
  31. DracoLoader._decoderTaskProcessor = processor;
  32. }
  33. return DracoLoader._decoderTaskProcessor;
  34. }
  35. /**
  36. * Decodes a compressed point cloud. Returns undefined if the task cannot be scheduled.
  37. * @private
  38. *
  39. * @exception {RuntimeError} Draco decoder could not be initialized.
  40. */
  41. static decodePointCloud(parameters) {
  42. const decoderTaskProcessor = DracoLoader._getDecoderTaskProcessor();
  43. if (defined(DracoLoader._error)) {
  44. throw DracoLoader._error;
  45. }
  46. if (!DracoLoader._taskProcessorReady) {
  47. // The task processor is not ready to schedule tasks
  48. return;
  49. }
  50. return decoderTaskProcessor.scheduleTask(parameters, [
  51. parameters.buffer.buffer,
  52. ]);
  53. }
  54. /**
  55. * Decodes a buffer view. Returns undefined if the task cannot be scheduled.
  56. *
  57. * @param {object} options Object with the following properties:
  58. * @param {Uint8Array} options.array The typed array containing the buffer view data.
  59. * @param {object} options.bufferView The glTF buffer view object.
  60. * @param {Object<string, number>} options.compressedAttributes The compressed attributes.
  61. * @param {boolean} options.dequantizeInShader Whether POSITION and NORMAL attributes should be dequantized on the GPU.
  62. *
  63. * @returns {Promise} A promise that resolves to the decoded indices and attributes.
  64. * @private
  65. *
  66. * @exception {RuntimeError} Draco decoder could not be initialized.
  67. */
  68. static decodeBufferView(options) {
  69. const decoderTaskProcessor = DracoLoader._getDecoderTaskProcessor();
  70. if (defined(DracoLoader._error)) {
  71. throw DracoLoader._error;
  72. }
  73. if (!DracoLoader._taskProcessorReady) {
  74. // The task processor is not ready to schedule tasks
  75. return;
  76. }
  77. return decoderTaskProcessor.scheduleTask(options, [options.array.buffer]);
  78. }
  79. }
  80. // Maximum concurrency to use when decoding draco models
  81. DracoLoader._maxDecodingConcurrency = Math.max(
  82. FeatureDetection.hardwareConcurrency - 1,
  83. 1,
  84. );
  85. // Exposed for testing purposes
  86. DracoLoader._decoderTaskProcessor = undefined;
  87. DracoLoader._taskProcessorReady = false;
  88. DracoLoader._error = undefined;
  89. export default DracoLoader;