draco_minimal_encoder_decoder_example.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // This is a minimal example showing how to create the Draco decoder and
  2. // encoder module. The modules are created asynchronously, so you need to set
  3. // callbacks to make sure they are initialized before you try and call the
  4. // modules.
  5. 'use_strict';
  6. const draco3d = require('./draco3d');
  7. // Global decoder and encoder module variables.
  8. let decoderModule = null;
  9. let encoderModule = null;
  10. // The code to create the encoder and decoder modules is asynchronous.
  11. // draco3d.createDecoderModule will return a promise to a funciton with a
  12. // module as a parameter when the module has been fully initialized.
  13. // Create and set the decoder module.
  14. draco3d.createDecoderModule({}).then(function(module) {
  15. // This is reached when everything is ready, and you can call methods on
  16. // Module.
  17. decoderModule = module;
  18. console.log('Decoder Module Initialized!');
  19. moduleInitialized();
  20. });
  21. // Create and set the encoder module.
  22. draco3d.createEncoderModule({}).then(function(module) {
  23. // This is reached when everything is ready, and you can call methods on
  24. // Module.
  25. encoderModule = module;
  26. console.log('Encoder Module Initialized!');
  27. moduleInitialized();
  28. });
  29. function moduleInitialized() {
  30. if (encoderModule && decoderModule) {
  31. console.log('Both Modules Initialized!');
  32. let encoder = new encoderModule.Encoder();
  33. let decoder = new decoderModule.Decoder();
  34. // Do the actual encoding and decoding here. See 'draco_nodejs_example.js'
  35. // for a more comprehensive example.
  36. cleanup(encoder, decoder);
  37. }
  38. }
  39. function cleanup(encoder, decoder) {
  40. encoderModule.destroy(encoder);
  41. decoderModule.destroy(decoder);
  42. }