1
0

USDLoader.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import {
  2. FileLoader,
  3. Loader
  4. } from 'three';
  5. import * as fflate from '../libs/fflate.module.js';
  6. import { USDAParser } from './usd/USDAParser.js';
  7. import { USDCParser } from './usd/USDCParser.js';
  8. /**
  9. * A loader for the USDZ format.
  10. *
  11. * USDZ files that use USDC internally are not yet supported, only USDA.
  12. *
  13. * ```js
  14. * const loader = new USDZLoader();
  15. * const model = await loader.loadAsync( 'saeukkang.usdz' );
  16. * scene.add( model );
  17. * ```
  18. *
  19. * @augments Loader
  20. * @three_import import { USDLoader } from 'three/addons/loaders/USDLoader.js';
  21. */
  22. class USDLoader extends Loader {
  23. /**
  24. * Constructs a new USDZ loader.
  25. *
  26. * @param {LoadingManager} [manager] - The loading manager.
  27. */
  28. constructor( manager ) {
  29. super( manager );
  30. }
  31. /**
  32. * Starts loading from the given URL and passes the loaded USDZ asset
  33. * to the `onLoad()` callback.
  34. *
  35. * @param {string} url - The path/URL of the file to be loaded. This can also be a data URI.
  36. * @param {function(Group)} onLoad - Executed when the loading process has been finished.
  37. * @param {onProgressCallback} onProgress - Executed while the loading is in progress.
  38. * @param {onErrorCallback} onError - Executed when errors occur.
  39. */
  40. load( url, onLoad, onProgress, onError ) {
  41. const scope = this;
  42. const loader = new FileLoader( scope.manager );
  43. loader.setPath( scope.path );
  44. loader.setResponseType( 'arraybuffer' );
  45. loader.setRequestHeader( scope.requestHeader );
  46. loader.setWithCredentials( scope.withCredentials );
  47. loader.load( url, function ( text ) {
  48. try {
  49. onLoad( scope.parse( text ) );
  50. } catch ( e ) {
  51. if ( onError ) {
  52. onError( e );
  53. } else {
  54. console.error( e );
  55. }
  56. scope.manager.itemError( url );
  57. }
  58. }, onProgress, onError );
  59. }
  60. /**
  61. * Parses the given USDZ data and returns the resulting group.
  62. *
  63. * @param {ArrayBuffer|string} buffer - The raw USDZ data as an array buffer.
  64. * @return {Group} The parsed asset as a group.
  65. */
  66. parse( buffer ) {
  67. const usda = new USDAParser();
  68. const usdc = new USDCParser();
  69. function parseAssets( zip ) {
  70. const data = {};
  71. const loader = new FileLoader();
  72. loader.setResponseType( 'arraybuffer' );
  73. for ( const filename in zip ) {
  74. if ( filename.endsWith( 'png' ) ) {
  75. const blob = new Blob( [ zip[ filename ] ], { type: 'image/png' } );
  76. data[ filename ] = URL.createObjectURL( blob );
  77. }
  78. if ( filename.endsWith( 'usd' ) || filename.endsWith( 'usda' ) || filename.endsWith( 'usdc' ) ) {
  79. if ( isCrateFile( zip[ filename ] ) ) {
  80. data[ filename ] = usdc.parse( zip[ filename ].buffer, data );
  81. } else {
  82. const text = fflate.strFromU8( zip[ filename ] );
  83. data[ filename ] = usda.parseText( text );
  84. }
  85. }
  86. }
  87. return data;
  88. }
  89. function isCrateFile( buffer ) {
  90. const crateHeader = new Uint8Array( [ 0x50, 0x58, 0x52, 0x2D, 0x55, 0x53, 0x44, 0x43 ] ); // PXR-USDC
  91. if ( buffer.byteLength < crateHeader.length ) return false;
  92. const view = new Uint8Array( buffer, 0, crateHeader.length );
  93. for ( let i = 0; i < crateHeader.length; i ++ ) {
  94. if ( view[ i ] !== crateHeader[ i ] ) return false;
  95. }
  96. return true;
  97. }
  98. function findUSD( zip ) {
  99. if ( zip.length < 1 ) return undefined;
  100. const firstFileName = Object.keys( zip )[ 0 ];
  101. let isCrate = false;
  102. // As per the USD specification, the first entry in the zip archive is used as the main file ("UsdStage").
  103. // ASCII files can end in either .usda or .usd.
  104. // See https://openusd.org/release/spec_usdz.html#layout
  105. if ( firstFileName.endsWith( 'usda' ) ) return zip[ firstFileName ];
  106. if ( firstFileName.endsWith( 'usdc' ) ) {
  107. isCrate = true;
  108. } else if ( firstFileName.endsWith( 'usd' ) ) {
  109. // If this is not a crate file, we assume it is a plain USDA file.
  110. if ( ! isCrateFile( zip[ firstFileName ] ) ) {
  111. return zip[ firstFileName ];
  112. } else {
  113. isCrate = true;
  114. }
  115. }
  116. if ( isCrate ) {
  117. return zip[ firstFileName ];
  118. }
  119. }
  120. // USDA
  121. if ( typeof buffer === 'string' ) {
  122. return usda.parse( buffer, {} );
  123. }
  124. // USDC
  125. if ( isCrateFile( buffer ) ) {
  126. return usdc.parse( buffer );
  127. }
  128. // USDZ
  129. const zip = fflate.unzipSync( new Uint8Array( buffer ) );
  130. const assets = parseAssets( zip );
  131. // console.log( assets );
  132. const file = findUSD( zip );
  133. const text = fflate.strFromU8( file );
  134. return usda.parse( text, assets );
  135. }
  136. }
  137. export { USDLoader };
粤ICP备19079148号