All files / engine/Source/Scene DracoLoader.js

82.14% Statements 23/28
66.66% Branches 8/12
66.66% Functions 4/6
82.14% Lines 23/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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