3DMLoader.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * @author Luis Fraguada / https://github.com/fraguada
  3. */
  4. import {
  5. BufferAttribute,
  6. BufferGeometry,
  7. FileLoader,
  8. Loader
  9. } from "../../../build/three.module.js";
  10. var Rhino3dmLoader = function ( manager ) {
  11. Loader.call( this, manager );
  12. this.libraryPath = '';
  13. this.libraryPending = null;
  14. this.libraryBinary = null;
  15. };
  16. Rhino3dmLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
  17. constructor: Rhino3dmLoader,
  18. setLibraryPath: function ( path ) {
  19. this.libraryPath = path;
  20. return this;
  21. },
  22. load: function ( url, onLoad, onProgress, onError ) {
  23. var scope = this;
  24. var path = ( this.path !== undefined ) ? this.path : LoaderUtils.extractUrlBase( url );
  25. var loader = new FileLoader( scope.manager );
  26. loader.load( url, function ( text ) {
  27. try {
  28. scope.parse( text, path, onLoad, onError );
  29. } catch ( e ) {
  30. if ( onError !== undefined ) {
  31. onError( e );
  32. } else {
  33. throw e;
  34. }
  35. }
  36. }, onProgress, onError );
  37. },
  38. parse: function ( ) {
  39. // parsing logic goes here
  40. console.log('3dm parsing');
  41. },
  42. _initLibrary: function () {
  43. if ( ! this.libraryPending ) {
  44. // Load transcoder wrapper.
  45. var jsLoader = new FileLoader( this.manager );
  46. jsLoader.setPath( this.libraryPath );
  47. var jsContent = new Promise( ( resolve, reject ) => {
  48. jsLoader.load( 'rhino3dm.js', resolve, undefined, reject );
  49. } );
  50. // Load transcoder WASM binary.
  51. var binaryLoader = new FileLoader( this.manager );
  52. binaryLoader.setPath( this.libraryPath );
  53. binaryLoader.setResponseType( 'arraybuffer' );
  54. var binaryContent = new Promise( ( resolve, reject ) => {
  55. binaryLoader.load( 'rhino3dm.wasm', resolve, undefined, reject );
  56. } );
  57. this.libraryPending = Promise.all( [ jsContent, binaryContent ] )
  58. .then( ( [ jsContent, binaryContent ] ) => {
  59. var fn = Rhino3dmLoader.Rhino3dmWorker.toString();
  60. var body = [
  61. '/* rhino3dm.js */',
  62. jsContent,
  63. '/* worker */',
  64. fn.substring( fn.indexOf( '{' ) + 1, fn.lastIndexOf( '}' ) )
  65. ].join( '\n' );
  66. this.workerSourceURL = URL.createObjectURL( new Blob( [ body ] ) );
  67. this.libraryBinary = binaryContent;
  68. } );
  69. }
  70. return this.libraryPending;
  71. }
  72. } );
  73. /* WEB WORKER */
  74. Rhino3dmLoader.Rhino3dmWorker = function () {
  75. var libraryConfig;
  76. var libraryPending;
  77. var _RhinoFile;
  78. onmessage = function ( e ) {
  79. var message = e.data;
  80. switch ( message.type ) {
  81. case 'init':
  82. libraryPending = new Promise( function ( resolve/*, reject*/ ) {
  83. libraryPending.onModuleLoaded = function ( draco ) {
  84. // Module is Promise-like. Wrap before resolving to avoid loop.
  85. resolve( { rhino3dm: rhino3dm } );
  86. };
  87. //DracoDecoderModule( decoderConfig );
  88. } );
  89. break;
  90. case 'decode':
  91. break;
  92. }
  93. }
  94. function init( wasmBinary ) {
  95. var rhino3dmModule;
  96. libraryPending = new Promise( ( resolve ) => {
  97. rhino3dmModule = { wasmBinary, onRuntimeInitialized: resolve };
  98. //BASIS( rhino3dmModule );
  99. } ).then( () => {
  100. var { BasisFile, initializeBasis } = rhino3dmModule;
  101. _RhinoFile = BasisFile;
  102. //initializeBasis();
  103. } );
  104. }
  105. };
  106. export { Rhino3dmLoader };