USDLoader.js 6.1 KB

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