DracoLoader.js 2.4 KB

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