USDZLoader.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. import {
  2. BufferAttribute,
  3. BufferGeometry,
  4. ClampToEdgeWrapping,
  5. FileLoader,
  6. Group,
  7. NoColorSpace,
  8. Loader,
  9. Mesh,
  10. MeshPhysicalMaterial,
  11. MirroredRepeatWrapping,
  12. RepeatWrapping,
  13. SRGBColorSpace,
  14. TextureLoader,
  15. Object3D,
  16. Vector2
  17. } from 'three';
  18. import * as fflate from '../libs/fflate.module.js';
  19. class USDAParser {
  20. parse( text ) {
  21. const data = {};
  22. const lines = text.split( '\n' );
  23. let string = null;
  24. let target = data;
  25. const stack = [ data ];
  26. // debugger;
  27. for ( const line of lines ) {
  28. // console.log( line );
  29. if ( line.includes( '=' ) ) {
  30. const assignment = line.split( '=' );
  31. const lhs = assignment[ 0 ].trim();
  32. const rhs = assignment[ 1 ].trim();
  33. if ( rhs.endsWith( '{' ) ) {
  34. const group = {};
  35. stack.push( group );
  36. target[ lhs ] = group;
  37. target = group;
  38. } else if ( rhs.endsWith( '(' ) ) {
  39. // see #28631
  40. const values = rhs.slice( 0, - 1 );
  41. target[ lhs ] = values;
  42. const meta = {};
  43. stack.push( meta );
  44. target = meta;
  45. } else {
  46. target[ lhs ] = rhs;
  47. }
  48. } else if ( line.endsWith( '{' ) ) {
  49. const group = target[ string ] || {};
  50. stack.push( group );
  51. target[ string ] = group;
  52. target = group;
  53. } else if ( line.endsWith( '}' ) ) {
  54. stack.pop();
  55. if ( stack.length === 0 ) continue;
  56. target = stack[ stack.length - 1 ];
  57. } else if ( line.endsWith( '(' ) ) {
  58. const meta = {};
  59. stack.push( meta );
  60. string = line.split( '(' )[ 0 ].trim() || string;
  61. target[ string ] = meta;
  62. target = meta;
  63. } else if ( line.endsWith( ')' ) ) {
  64. stack.pop();
  65. target = stack[ stack.length - 1 ];
  66. } else {
  67. string = line.trim();
  68. }
  69. }
  70. return data;
  71. }
  72. }
  73. /**
  74. * A loader for the USDZ format.
  75. *
  76. * USDZ files that use USDC internally are not yet supported, only USDA.
  77. *
  78. * ```js
  79. * const loader = new USDZLoader();
  80. * const model = await loader.loadAsync( 'saeukkang.usdz' );
  81. * scene.add( model );
  82. * ```
  83. *
  84. * @augments Loader
  85. */
  86. class USDZLoader extends Loader {
  87. /**
  88. * Constructs a new USDZ loader.
  89. *
  90. * @param {LoadingManager} [manager] - The loading manager.
  91. */
  92. constructor( manager ) {
  93. super( manager );
  94. }
  95. /**
  96. * Starts loading from the given URL and passes the loaded USDZ asset
  97. * to the `onLoad()` callback.
  98. *
  99. * @param {string} url - The path/URL of the file to be loaded. This can also be a data URI.
  100. * @param {function(Group)} onLoad - Executed when the loading process has been finished.
  101. * @param {onProgressCallback} onProgress - Executed while the loading is in progress.
  102. * @param {onErrorCallback} onError - Executed when errors occur.
  103. */
  104. load( url, onLoad, onProgress, onError ) {
  105. const scope = this;
  106. const loader = new FileLoader( scope.manager );
  107. loader.setPath( scope.path );
  108. loader.setResponseType( 'arraybuffer' );
  109. loader.setRequestHeader( scope.requestHeader );
  110. loader.setWithCredentials( scope.withCredentials );
  111. loader.load( url, function ( text ) {
  112. try {
  113. onLoad( scope.parse( text ) );
  114. } catch ( e ) {
  115. if ( onError ) {
  116. onError( e );
  117. } else {
  118. console.error( e );
  119. }
  120. scope.manager.itemError( url );
  121. }
  122. }, onProgress, onError );
  123. }
  124. /**
  125. * Parses the given USDZ data and returns the resulting group.
  126. *
  127. * @param {ArrayBuffer} buffer - The raw USDZ data as an array buffer.
  128. * @return {Group} The parsed asset as a group.
  129. */
  130. parse( buffer ) {
  131. const parser = new USDAParser();
  132. function parseAssets( zip ) {
  133. const data = {};
  134. const loader = new FileLoader();
  135. loader.setResponseType( 'arraybuffer' );
  136. for ( const filename in zip ) {
  137. if ( filename.endsWith( 'png' ) ) {
  138. const blob = new Blob( [ zip[ filename ] ], { type: 'image/png' } );
  139. data[ filename ] = URL.createObjectURL( blob );
  140. }
  141. if ( filename.endsWith( 'usd' ) || filename.endsWith( 'usda' ) ) {
  142. if ( isCrateFile( zip[ filename ] ) ) {
  143. throw Error( 'THREE.USDZLoader: Crate files (.usdc or binary .usd) are not supported.' );
  144. }
  145. const text = fflate.strFromU8( zip[ filename ] );
  146. data[ filename ] = parser.parse( text );
  147. }
  148. }
  149. return data;
  150. }
  151. function isCrateFile( buffer ) {
  152. // Check if this a crate file. First 7 bytes of a crate file are "PXR-USDC".
  153. const fileHeader = buffer.slice( 0, 7 );
  154. const crateHeader = new Uint8Array( [ 0x50, 0x58, 0x52, 0x2D, 0x55, 0x53, 0x44, 0x43 ] );
  155. // If this is not a crate file, we assume it is a plain USDA file.
  156. return fileHeader.every( ( value, index ) => value === crateHeader[ index ] );
  157. }
  158. function findUSD( zip ) {
  159. if ( zip.length < 1 ) return undefined;
  160. const firstFileName = Object.keys( zip )[ 0 ];
  161. let isCrate = false;
  162. // As per the USD specification, the first entry in the zip archive is used as the main file ("UsdStage").
  163. // ASCII files can end in either .usda or .usd.
  164. // See https://openusd.org/release/spec_usdz.html#layout
  165. if ( firstFileName.endsWith( 'usda' ) ) return zip[ firstFileName ];
  166. if ( firstFileName.endsWith( 'usdc' ) ) {
  167. isCrate = true;
  168. } else if ( firstFileName.endsWith( 'usd' ) ) {
  169. // If this is not a crate file, we assume it is a plain USDA file.
  170. if ( ! isCrateFile( zip[ firstFileName ] ) ) {
  171. return zip[ firstFileName ];
  172. } else {
  173. isCrate = true;
  174. }
  175. }
  176. if ( isCrate ) {
  177. throw Error( 'THREE.USDZLoader: Crate files (.usdc or binary .usd) are not supported.' );
  178. }
  179. }
  180. const zip = fflate.unzipSync( new Uint8Array( buffer ) );
  181. // console.log( zip );
  182. const assets = parseAssets( zip );
  183. // console.log( assets )
  184. const file = findUSD( zip );
  185. // Parse file
  186. const text = fflate.strFromU8( file );
  187. const root = parser.parse( text );
  188. // Build scene
  189. function findMeshGeometry( data ) {
  190. if ( ! data ) return undefined;
  191. if ( 'prepend references' in data ) {
  192. const reference = data[ 'prepend references' ];
  193. const parts = reference.split( '@' );
  194. const path = parts[ 1 ].replace( /^.\//, '' );
  195. const id = parts[ 2 ].replace( /^<\//, '' ).replace( />$/, '' );
  196. return findGeometry( assets[ path ], id );
  197. }
  198. return findGeometry( data );
  199. }
  200. function findGeometry( data, id ) {
  201. if ( ! data ) return undefined;
  202. if ( id !== undefined ) {
  203. const def = `def Mesh "${id}"`;
  204. if ( def in data ) {
  205. return data[ def ];
  206. }
  207. }
  208. for ( const name in data ) {
  209. const object = data[ name ];
  210. if ( name.startsWith( 'def Mesh' ) ) {
  211. return object;
  212. }
  213. if ( typeof object === 'object' ) {
  214. const geometry = findGeometry( object );
  215. if ( geometry ) return geometry;
  216. }
  217. }
  218. }
  219. function buildGeometry( data ) {
  220. if ( ! data ) return undefined;
  221. const geometry = new BufferGeometry();
  222. let indices = null;
  223. let counts = null;
  224. let uvs = null;
  225. let positionsLength = - 1;
  226. // index
  227. if ( 'int[] faceVertexIndices' in data ) {
  228. indices = JSON.parse( data[ 'int[] faceVertexIndices' ] );
  229. }
  230. // face count
  231. if ( 'int[] faceVertexCounts' in data ) {
  232. counts = JSON.parse( data[ 'int[] faceVertexCounts' ] );
  233. indices = toTriangleIndices( indices, counts );
  234. }
  235. // position
  236. if ( 'point3f[] points' in data ) {
  237. const positions = JSON.parse( data[ 'point3f[] points' ].replace( /[()]*/g, '' ) );
  238. positionsLength = positions.length;
  239. let attribute = new BufferAttribute( new Float32Array( positions ), 3 );
  240. if ( indices !== null ) attribute = toFlatBufferAttribute( attribute, indices );
  241. geometry.setAttribute( 'position', attribute );
  242. }
  243. // uv
  244. if ( 'float2[] primvars:st' in data ) {
  245. data[ 'texCoord2f[] primvars:st' ] = data[ 'float2[] primvars:st' ];
  246. }
  247. if ( 'texCoord2f[] primvars:st' in data ) {
  248. uvs = JSON.parse( data[ 'texCoord2f[] primvars:st' ].replace( /[()]*/g, '' ) );
  249. let attribute = new BufferAttribute( new Float32Array( uvs ), 2 );
  250. if ( indices !== null ) attribute = toFlatBufferAttribute( attribute, indices );
  251. geometry.setAttribute( 'uv', attribute );
  252. }
  253. if ( 'int[] primvars:st:indices' in data && uvs !== null ) {
  254. // custom uv index, overwrite uvs with new data
  255. const attribute = new BufferAttribute( new Float32Array( uvs ), 2 );
  256. let indices = JSON.parse( data[ 'int[] primvars:st:indices' ] );
  257. indices = toTriangleIndices( indices, counts );
  258. geometry.setAttribute( 'uv', toFlatBufferAttribute( attribute, indices ) );
  259. }
  260. // normal
  261. if ( 'normal3f[] normals' in data ) {
  262. const normals = JSON.parse( data[ 'normal3f[] normals' ].replace( /[()]*/g, '' ) );
  263. let attribute = new BufferAttribute( new Float32Array( normals ), 3 );
  264. // normals require a special treatment in USD
  265. if ( normals.length === positionsLength ) {
  266. // raw normal and position data have equal length (like produced by USDZExporter)
  267. if ( indices !== null ) attribute = toFlatBufferAttribute( attribute, indices );
  268. } else {
  269. // unequal length, normals are independent of faceVertexIndices
  270. let indices = Array.from( Array( normals.length / 3 ).keys() ); // [ 0, 1, 2, 3 ... ]
  271. indices = toTriangleIndices( indices, counts );
  272. attribute = toFlatBufferAttribute( attribute, indices );
  273. }
  274. geometry.setAttribute( 'normal', attribute );
  275. } else {
  276. // compute flat vertex normals
  277. geometry.computeVertexNormals();
  278. }
  279. return geometry;
  280. }
  281. function toTriangleIndices( rawIndices, counts ) {
  282. const indices = [];
  283. for ( let i = 0; i < counts.length; i ++ ) {
  284. const count = counts[ i ];
  285. const stride = i * count;
  286. if ( count === 3 ) {
  287. const a = rawIndices[ stride + 0 ];
  288. const b = rawIndices[ stride + 1 ];
  289. const c = rawIndices[ stride + 2 ];
  290. indices.push( a, b, c );
  291. } else if ( count === 4 ) {
  292. const a = rawIndices[ stride + 0 ];
  293. const b = rawIndices[ stride + 1 ];
  294. const c = rawIndices[ stride + 2 ];
  295. const d = rawIndices[ stride + 3 ];
  296. indices.push( a, b, c );
  297. indices.push( a, c, d );
  298. } else {
  299. console.warn( 'THREE.USDZLoader: Face vertex count of %s unsupported.', count );
  300. }
  301. }
  302. return indices;
  303. }
  304. function toFlatBufferAttribute( attribute, indices ) {
  305. const array = attribute.array;
  306. const itemSize = attribute.itemSize;
  307. const array2 = new array.constructor( indices.length * itemSize );
  308. let index = 0, index2 = 0;
  309. for ( let i = 0, l = indices.length; i < l; i ++ ) {
  310. index = indices[ i ] * itemSize;
  311. for ( let j = 0; j < itemSize; j ++ ) {
  312. array2[ index2 ++ ] = array[ index ++ ];
  313. }
  314. }
  315. return new BufferAttribute( array2, itemSize );
  316. }
  317. function findMeshMaterial( data ) {
  318. if ( ! data ) return undefined;
  319. if ( 'rel material:binding' in data ) {
  320. const reference = data[ 'rel material:binding' ];
  321. const id = reference.replace( /^<\//, '' ).replace( />$/, '' );
  322. const parts = id.split( '/' );
  323. return findMaterial( root, ` "${ parts[ 1 ] }"` );
  324. }
  325. return findMaterial( data );
  326. }
  327. function findMaterial( data, id = '' ) {
  328. for ( const name in data ) {
  329. const object = data[ name ];
  330. if ( name.startsWith( 'def Material' + id ) ) {
  331. return object;
  332. }
  333. if ( typeof object === 'object' ) {
  334. const material = findMaterial( object, id );
  335. if ( material ) return material;
  336. }
  337. }
  338. }
  339. function setTextureParams( map, data_value ) {
  340. // rotation, scale and translation
  341. if ( data_value[ 'float inputs:rotation' ] ) {
  342. map.rotation = parseFloat( data_value[ 'float inputs:rotation' ] );
  343. }
  344. if ( data_value[ 'float2 inputs:scale' ] ) {
  345. map.repeat = new Vector2().fromArray( JSON.parse( '[' + data_value[ 'float2 inputs:scale' ].replace( /[()]*/g, '' ) + ']' ) );
  346. }
  347. if ( data_value[ 'float2 inputs:translation' ] ) {
  348. map.offset = new Vector2().fromArray( JSON.parse( '[' + data_value[ 'float2 inputs:translation' ].replace( /[()]*/g, '' ) + ']' ) );
  349. }
  350. }
  351. function buildMaterial( data ) {
  352. const material = new MeshPhysicalMaterial();
  353. if ( data !== undefined ) {
  354. const surfaceConnection = data[ 'token outputs:surface.connect' ];
  355. const surfaceName = /(\w+).output/.exec( surfaceConnection )[ 1 ];
  356. const surface = data[ `def Shader "${surfaceName}"` ];
  357. if ( surface !== undefined ) {
  358. if ( 'color3f inputs:diffuseColor.connect' in surface ) {
  359. const path = surface[ 'color3f inputs:diffuseColor.connect' ];
  360. const sampler = findTexture( root, /(\w+).output/.exec( path )[ 1 ] );
  361. material.map = buildTexture( sampler );
  362. material.map.colorSpace = SRGBColorSpace;
  363. if ( 'def Shader "Transform2d_diffuse"' in data ) {
  364. setTextureParams( material.map, data[ 'def Shader "Transform2d_diffuse"' ] );
  365. }
  366. } else if ( 'color3f inputs:diffuseColor' in surface ) {
  367. const color = surface[ 'color3f inputs:diffuseColor' ].replace( /[()]*/g, '' );
  368. material.color.fromArray( JSON.parse( '[' + color + ']' ) );
  369. }
  370. if ( 'color3f inputs:emissiveColor.connect' in surface ) {
  371. const path = surface[ 'color3f inputs:emissiveColor.connect' ];
  372. const sampler = findTexture( root, /(\w+).output/.exec( path )[ 1 ] );
  373. material.emissiveMap = buildTexture( sampler );
  374. material.emissiveMap.colorSpace = SRGBColorSpace;
  375. material.emissive.set( 0xffffff );
  376. if ( 'def Shader "Transform2d_emissive"' in data ) {
  377. setTextureParams( material.emissiveMap, data[ 'def Shader "Transform2d_emissive"' ] );
  378. }
  379. } else if ( 'color3f inputs:emissiveColor' in surface ) {
  380. const color = surface[ 'color3f inputs:emissiveColor' ].replace( /[()]*/g, '' );
  381. material.emissive.fromArray( JSON.parse( '[' + color + ']' ) );
  382. }
  383. if ( 'normal3f inputs:normal.connect' in surface ) {
  384. const path = surface[ 'normal3f inputs:normal.connect' ];
  385. const sampler = findTexture( root, /(\w+).output/.exec( path )[ 1 ] );
  386. material.normalMap = buildTexture( sampler );
  387. material.normalMap.colorSpace = NoColorSpace;
  388. if ( 'def Shader "Transform2d_normal"' in data ) {
  389. setTextureParams( material.normalMap, data[ 'def Shader "Transform2d_normal"' ] );
  390. }
  391. }
  392. if ( 'float inputs:roughness.connect' in surface ) {
  393. const path = surface[ 'float inputs:roughness.connect' ];
  394. const sampler = findTexture( root, /(\w+).output/.exec( path )[ 1 ] );
  395. material.roughness = 1.0;
  396. material.roughnessMap = buildTexture( sampler );
  397. material.roughnessMap.colorSpace = NoColorSpace;
  398. if ( 'def Shader "Transform2d_roughness"' in data ) {
  399. setTextureParams( material.roughnessMap, data[ 'def Shader "Transform2d_roughness"' ] );
  400. }
  401. } else if ( 'float inputs:roughness' in surface ) {
  402. material.roughness = parseFloat( surface[ 'float inputs:roughness' ] );
  403. }
  404. if ( 'float inputs:metallic.connect' in surface ) {
  405. const path = surface[ 'float inputs:metallic.connect' ];
  406. const sampler = findTexture( root, /(\w+).output/.exec( path )[ 1 ] );
  407. material.metalness = 1.0;
  408. material.metalnessMap = buildTexture( sampler );
  409. material.metalnessMap.colorSpace = NoColorSpace;
  410. if ( 'def Shader "Transform2d_metallic"' in data ) {
  411. setTextureParams( material.metalnessMap, data[ 'def Shader "Transform2d_metallic"' ] );
  412. }
  413. } else if ( 'float inputs:metallic' in surface ) {
  414. material.metalness = parseFloat( surface[ 'float inputs:metallic' ] );
  415. }
  416. if ( 'float inputs:clearcoat.connect' in surface ) {
  417. const path = surface[ 'float inputs:clearcoat.connect' ];
  418. const sampler = findTexture( root, /(\w+).output/.exec( path )[ 1 ] );
  419. material.clearcoat = 1.0;
  420. material.clearcoatMap = buildTexture( sampler );
  421. material.clearcoatMap.colorSpace = NoColorSpace;
  422. if ( 'def Shader "Transform2d_clearcoat"' in data ) {
  423. setTextureParams( material.clearcoatMap, data[ 'def Shader "Transform2d_clearcoat"' ] );
  424. }
  425. } else if ( 'float inputs:clearcoat' in surface ) {
  426. material.clearcoat = parseFloat( surface[ 'float inputs:clearcoat' ] );
  427. }
  428. if ( 'float inputs:clearcoatRoughness.connect' in surface ) {
  429. const path = surface[ 'float inputs:clearcoatRoughness.connect' ];
  430. const sampler = findTexture( root, /(\w+).output/.exec( path )[ 1 ] );
  431. material.clearcoatRoughness = 1.0;
  432. material.clearcoatRoughnessMap = buildTexture( sampler );
  433. material.clearcoatRoughnessMap.colorSpace = NoColorSpace;
  434. if ( 'def Shader "Transform2d_clearcoatRoughness"' in data ) {
  435. setTextureParams( material.clearcoatRoughnessMap, data[ 'def Shader "Transform2d_clearcoatRoughness"' ] );
  436. }
  437. } else if ( 'float inputs:clearcoatRoughness' in surface ) {
  438. material.clearcoatRoughness = parseFloat( surface[ 'float inputs:clearcoatRoughness' ] );
  439. }
  440. if ( 'float inputs:ior' in surface ) {
  441. material.ior = parseFloat( surface[ 'float inputs:ior' ] );
  442. }
  443. if ( 'float inputs:occlusion.connect' in surface ) {
  444. const path = surface[ 'float inputs:occlusion.connect' ];
  445. const sampler = findTexture( root, /(\w+).output/.exec( path )[ 1 ] );
  446. material.aoMap = buildTexture( sampler );
  447. material.aoMap.colorSpace = NoColorSpace;
  448. if ( 'def Shader "Transform2d_occlusion"' in data ) {
  449. setTextureParams( material.aoMap, data[ 'def Shader "Transform2d_occlusion"' ] );
  450. }
  451. }
  452. }
  453. }
  454. return material;
  455. }
  456. function findTexture( data, id ) {
  457. for ( const name in data ) {
  458. const object = data[ name ];
  459. if ( name.startsWith( `def Shader "${ id }"` ) ) {
  460. return object;
  461. }
  462. if ( typeof object === 'object' ) {
  463. const texture = findTexture( object, id );
  464. if ( texture ) return texture;
  465. }
  466. }
  467. }
  468. function buildTexture( data ) {
  469. if ( 'asset inputs:file' in data ) {
  470. const path = data[ 'asset inputs:file' ].replace( /@*/g, '' ).trim();
  471. const loader = new TextureLoader();
  472. const texture = loader.load( assets[ path ] );
  473. const map = {
  474. '"clamp"': ClampToEdgeWrapping,
  475. '"mirror"': MirroredRepeatWrapping,
  476. '"repeat"': RepeatWrapping
  477. };
  478. if ( 'token inputs:wrapS' in data ) {
  479. texture.wrapS = map[ data[ 'token inputs:wrapS' ] ];
  480. }
  481. if ( 'token inputs:wrapT' in data ) {
  482. texture.wrapT = map[ data[ 'token inputs:wrapT' ] ];
  483. }
  484. return texture;
  485. }
  486. return null;
  487. }
  488. function buildObject( data ) {
  489. const geometry = buildGeometry( findMeshGeometry( data ) );
  490. const material = buildMaterial( findMeshMaterial( data ) );
  491. const mesh = geometry ? new Mesh( geometry, material ) : new Object3D();
  492. if ( 'matrix4d xformOp:transform' in data ) {
  493. const array = JSON.parse( '[' + data[ 'matrix4d xformOp:transform' ].replace( /[()]*/g, '' ) + ']' );
  494. mesh.matrix.fromArray( array );
  495. mesh.matrix.decompose( mesh.position, mesh.quaternion, mesh.scale );
  496. }
  497. return mesh;
  498. }
  499. function buildHierarchy( data, group ) {
  500. for ( const name in data ) {
  501. if ( name.startsWith( 'def Scope' ) ) {
  502. buildHierarchy( data[ name ], group );
  503. } else if ( name.startsWith( 'def Xform' ) ) {
  504. const mesh = buildObject( data[ name ] );
  505. if ( /def Xform "(\w+)"/.test( name ) ) {
  506. mesh.name = /def Xform "(\w+)"/.exec( name )[ 1 ];
  507. }
  508. group.add( mesh );
  509. buildHierarchy( data[ name ], mesh );
  510. }
  511. }
  512. }
  513. const group = new Group();
  514. buildHierarchy( root, group );
  515. return group;
  516. }
  517. }
  518. export { USDZLoader };
粤ICP备19079148号