USDZExporter.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. import * as THREE from 'three';
  2. import * as fflate from '../libs/fflate.module.js';
  3. class USDZExporter {
  4. async parse( scene, options = {} ) {
  5. options = Object.assign( {
  6. ar: {
  7. anchoring: { type: 'plane' },
  8. planeAnchoring: { alignment: 'horizontal' }
  9. },
  10. quickLookCompatible: false,
  11. }, options );
  12. const files = {};
  13. const modelFileName = 'model.usda';
  14. // model file should be first in USDZ archive so we init it here
  15. files[ modelFileName ] = null;
  16. let output = buildHeader();
  17. output += buildSceneStart( options );
  18. const materials = {};
  19. const textures = {};
  20. scene.traverseVisible( ( object ) => {
  21. if ( object.isMesh ) {
  22. const geometry = object.geometry;
  23. const material = object.material;
  24. if ( material.isMeshStandardMaterial ) {
  25. const geometryFileName = 'geometries/Geometry_' + geometry.id + '.usda';
  26. if ( ! ( geometryFileName in files ) ) {
  27. const meshObject = buildMeshObject( geometry );
  28. files[ geometryFileName ] = buildUSDFileAsString( meshObject );
  29. }
  30. if ( ! ( material.uuid in materials ) ) {
  31. materials[ material.uuid ] = material;
  32. }
  33. output += buildXform( object, geometry, material );
  34. } else {
  35. console.warn( 'THREE.USDZExporter: Unsupported material type (USDZ only supports MeshStandardMaterial)', object );
  36. }
  37. } else if ( object.isCamera ) {
  38. output += buildCamera( object );
  39. }
  40. } );
  41. output += buildSceneEnd();
  42. output += buildMaterials( materials, textures, options.quickLookCompatible );
  43. files[ modelFileName ] = fflate.strToU8( output );
  44. output = null;
  45. for ( const id in textures ) {
  46. const texture = textures[ id ];
  47. const canvas = imageToCanvas( texture.image, texture.flipY );
  48. const blob = await new Promise( resolve => canvas.toBlob( resolve, 'image/png', 1 ) );
  49. files[ `textures/Texture_${ id }.png` ] = new Uint8Array( await blob.arrayBuffer() );
  50. }
  51. // 64 byte alignment
  52. // https://github.com/101arrowz/fflate/issues/39#issuecomment-777263109
  53. let offset = 0;
  54. for ( const filename in files ) {
  55. const file = files[ filename ];
  56. const headerSize = 34 + filename.length;
  57. offset += headerSize;
  58. const offsetMod64 = offset & 63;
  59. if ( offsetMod64 !== 4 ) {
  60. const padLength = 64 - offsetMod64;
  61. const padding = new Uint8Array( padLength );
  62. files[ filename ] = [ file, { extra: { 12345: padding } } ];
  63. }
  64. offset = file.length;
  65. }
  66. return fflate.zipSync( files, { level: 0 } );
  67. }
  68. }
  69. function imageToCanvas( image, flipY ) {
  70. if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
  71. ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ||
  72. ( typeof OffscreenCanvas !== 'undefined' && image instanceof OffscreenCanvas ) ||
  73. ( typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap ) ) {
  74. const scale = 1024 / Math.max( image.width, image.height );
  75. const canvas = document.createElement( 'canvas' );
  76. canvas.width = image.width * Math.min( 1, scale );
  77. canvas.height = image.height * Math.min( 1, scale );
  78. const context = canvas.getContext( '2d' );
  79. // TODO: We should be able to do this in the UsdTransform2d?
  80. if ( flipY === true ) {
  81. context.translate( 0, canvas.height );
  82. context.scale( 1, - 1 );
  83. }
  84. context.drawImage( image, 0, 0, canvas.width, canvas.height );
  85. return canvas;
  86. } else {
  87. throw new Error( 'THREE.USDZExporter: No valid image data found. Unable to process texture.' );
  88. }
  89. }
  90. //
  91. const PRECISION = 7;
  92. function buildHeader() {
  93. return `#usda 1.0
  94. (
  95. customLayerData = {
  96. string creator = "Three.js USDZExporter"
  97. }
  98. defaultPrim = "Root"
  99. metersPerUnit = 1
  100. upAxis = "Y"
  101. )
  102. `;
  103. }
  104. function buildSceneStart( options ) {
  105. return `def Xform "Root"
  106. {
  107. def Scope "Scenes" (
  108. kind = "sceneLibrary"
  109. )
  110. {
  111. def Xform "Scene" (
  112. customData = {
  113. bool preliminary_collidesWithEnvironment = 0
  114. string sceneName = "Scene"
  115. }
  116. sceneName = "Scene"
  117. )
  118. {
  119. token preliminary:anchoring:type = "${options.ar.anchoring.type}"
  120. token preliminary:planeAnchoring:alignment = "${options.ar.planeAnchoring.alignment}"
  121. `;
  122. }
  123. function buildSceneEnd() {
  124. return `
  125. }
  126. }
  127. }
  128. `;
  129. }
  130. function buildUSDFileAsString( dataToInsert ) {
  131. let output = buildHeader();
  132. output += dataToInsert;
  133. return fflate.strToU8( output );
  134. }
  135. // Xform
  136. function buildXform( object, geometry, material ) {
  137. const name = 'Object_' + object.id;
  138. const transform = buildMatrix( object.matrixWorld );
  139. if ( object.matrixWorld.determinant() < 0 ) {
  140. console.warn( 'THREE.USDZExporter: USDZ does not support negative scales', object );
  141. }
  142. return `def Xform "${ name }" (
  143. prepend references = @./geometries/Geometry_${ geometry.id }.usda@</Geometry>
  144. prepend apiSchemas = ["MaterialBindingAPI"]
  145. )
  146. {
  147. matrix4d xformOp:transform = ${ transform }
  148. uniform token[] xformOpOrder = ["xformOp:transform"]
  149. rel material:binding = </Materials/Material_${ material.id }>
  150. }
  151. `;
  152. }
  153. function buildMatrix( matrix ) {
  154. const array = matrix.elements;
  155. return `( ${ buildMatrixRow( array, 0 ) }, ${ buildMatrixRow( array, 4 ) }, ${ buildMatrixRow( array, 8 ) }, ${ buildMatrixRow( array, 12 ) } )`;
  156. }
  157. function buildMatrixRow( array, offset ) {
  158. return `(${ array[ offset + 0 ] }, ${ array[ offset + 1 ] }, ${ array[ offset + 2 ] }, ${ array[ offset + 3 ] })`;
  159. }
  160. // Mesh
  161. function buildMeshObject( geometry ) {
  162. const mesh = buildMesh( geometry );
  163. return `
  164. def "Geometry"
  165. {
  166. ${mesh}
  167. }
  168. `;
  169. }
  170. function buildMesh( geometry ) {
  171. const name = 'Geometry';
  172. const attributes = geometry.attributes;
  173. const count = attributes.position.count;
  174. return `
  175. def Mesh "${ name }"
  176. {
  177. int[] faceVertexCounts = [${ buildMeshVertexCount( geometry ) }]
  178. int[] faceVertexIndices = [${ buildMeshVertexIndices( geometry ) }]
  179. normal3f[] normals = [${ buildVector3Array( attributes.normal, count )}] (
  180. interpolation = "vertex"
  181. )
  182. point3f[] points = [${ buildVector3Array( attributes.position, count )}]
  183. ${ buildPrimvars( attributes, count ) }
  184. uniform token subdivisionScheme = "none"
  185. }
  186. `;
  187. }
  188. function buildMeshVertexCount( geometry ) {
  189. const count = geometry.index !== null ? geometry.index.count : geometry.attributes.position.count;
  190. return Array( count / 3 ).fill( 3 ).join( ', ' );
  191. }
  192. function buildMeshVertexIndices( geometry ) {
  193. const index = geometry.index;
  194. const array = [];
  195. if ( index !== null ) {
  196. for ( let i = 0; i < index.count; i ++ ) {
  197. array.push( index.getX( i ) );
  198. }
  199. } else {
  200. const length = geometry.attributes.position.count;
  201. for ( let i = 0; i < length; i ++ ) {
  202. array.push( i );
  203. }
  204. }
  205. return array.join( ', ' );
  206. }
  207. function buildVector3Array( attribute, count ) {
  208. if ( attribute === undefined ) {
  209. console.warn( 'USDZExporter: Normals missing.' );
  210. return Array( count ).fill( '(0, 0, 0)' ).join( ', ' );
  211. }
  212. const array = [];
  213. for ( let i = 0; i < attribute.count; i ++ ) {
  214. const x = attribute.getX( i );
  215. const y = attribute.getY( i );
  216. const z = attribute.getZ( i );
  217. array.push( `(${ x.toPrecision( PRECISION ) }, ${ y.toPrecision( PRECISION ) }, ${ z.toPrecision( PRECISION ) })` );
  218. }
  219. return array.join( ', ' );
  220. }
  221. function buildVector2Array( attribute, count ) {
  222. if ( attribute === undefined ) {
  223. console.warn( 'USDZExporter: UVs missing.' );
  224. return Array( count ).fill( '(0, 0)' ).join( ', ' );
  225. }
  226. const array = [];
  227. for ( let i = 0; i < attribute.count; i ++ ) {
  228. const x = attribute.getX( i );
  229. const y = attribute.getY( i );
  230. array.push( `(${ x.toPrecision( PRECISION ) }, ${ 1 - y.toPrecision( PRECISION ) })` );
  231. }
  232. return array.join( ', ' );
  233. }
  234. function buildPrimvars( attributes, count ) {
  235. let string = '';
  236. for ( let i = 0; i < 4; i ++ ) {
  237. const id = ( i > 0 ? i : '' );
  238. const attribute = attributes[ 'uv' + id ];
  239. if ( attribute !== undefined ) {
  240. string += `
  241. texCoord2f[] primvars:st${ id } = [${ buildVector2Array( attribute, count )}] (
  242. interpolation = "vertex"
  243. )`;
  244. }
  245. }
  246. return string;
  247. }
  248. // Materials
  249. function buildMaterials( materials, textures, quickLookCompatible = false ) {
  250. const array = [];
  251. for ( const uuid in materials ) {
  252. const material = materials[ uuid ];
  253. array.push( buildMaterial( material, textures, quickLookCompatible ) );
  254. }
  255. return `def "Materials"
  256. {
  257. ${ array.join( '' ) }
  258. }
  259. `;
  260. }
  261. function buildMaterial( material, textures, quickLookCompatible = false ) {
  262. // https://graphics.pixar.com/usd/docs/UsdPreviewSurface-Proposal.html
  263. const pad = ' ';
  264. const inputs = [];
  265. const samplers = [];
  266. function buildTexture( texture, mapType, color ) {
  267. const id = texture.source.id + '_' + texture.flipY;
  268. textures[ id ] = texture;
  269. const uv = texture.channel > 0 ? 'st' + texture.channel : 'st';
  270. const WRAPPINGS = {
  271. 1000: 'repeat', // RepeatWrapping
  272. 1001: 'clamp', // ClampToEdgeWrapping
  273. 1002: 'mirror' // MirroredRepeatWrapping
  274. };
  275. const repeat = texture.repeat.clone();
  276. const offset = texture.offset.clone();
  277. const rotation = texture.rotation;
  278. // rotation is around the wrong point. after rotation we need to shift offset again so that we're rotating around the right spot
  279. const xRotationOffset = Math.sin( rotation );
  280. const yRotationOffset = Math.cos( rotation );
  281. // texture coordinates start in the opposite corner, need to correct
  282. offset.y = 1 - offset.y - repeat.y;
  283. // turns out QuickLook is buggy and interprets texture repeat inverted/applies operations in a different order.
  284. // Apple Feedback: FB10036297 and FB11442287
  285. if ( quickLookCompatible ) {
  286. // This is NOT correct yet in QuickLook, but comes close for a range of models.
  287. // It becomes more incorrect the bigger the offset is
  288. offset.x = offset.x / repeat.x;
  289. offset.y = offset.y / repeat.y;
  290. offset.x += xRotationOffset / repeat.x;
  291. offset.y += yRotationOffset - 1;
  292. } else {
  293. // results match glTF results exactly. verified correct in usdview.
  294. offset.x += xRotationOffset * repeat.x;
  295. offset.y += ( 1 - yRotationOffset ) * repeat.y;
  296. }
  297. return `
  298. def Shader "PrimvarReader_${ mapType }"
  299. {
  300. uniform token info:id = "UsdPrimvarReader_float2"
  301. float2 inputs:fallback = (0.0, 0.0)
  302. token inputs:varname = "${ uv }"
  303. float2 outputs:result
  304. }
  305. def Shader "Transform2d_${ mapType }"
  306. {
  307. uniform token info:id = "UsdTransform2d"
  308. token inputs:in.connect = </Materials/Material_${ material.id }/PrimvarReader_${ mapType }.outputs:result>
  309. float inputs:rotation = ${ ( rotation * ( 180 / Math.PI ) ).toFixed( PRECISION ) }
  310. float2 inputs:scale = ${ buildVector2( repeat ) }
  311. float2 inputs:translation = ${ buildVector2( offset ) }
  312. float2 outputs:result
  313. }
  314. def Shader "Texture_${ texture.id }_${ mapType }"
  315. {
  316. uniform token info:id = "UsdUVTexture"
  317. asset inputs:file = @textures/Texture_${ id }.png@
  318. float2 inputs:st.connect = </Materials/Material_${ material.id }/Transform2d_${ mapType }.outputs:result>
  319. ${ color !== undefined ? 'float4 inputs:scale = ' + buildColor4( color ) : '' }
  320. token inputs:sourceColorSpace = "${ texture.colorSpace === THREE.NoColorSpace ? 'raw' : 'sRGB' }"
  321. token inputs:wrapS = "${ WRAPPINGS[ texture.wrapS ] }"
  322. token inputs:wrapT = "${ WRAPPINGS[ texture.wrapT ] }"
  323. float outputs:r
  324. float outputs:g
  325. float outputs:b
  326. float3 outputs:rgb
  327. ${ material.transparent || material.alphaTest > 0.0 ? 'float outputs:a' : '' }
  328. }`;
  329. }
  330. if ( material.side === THREE.DoubleSide ) {
  331. console.warn( 'THREE.USDZExporter: USDZ does not support double sided materials', material );
  332. }
  333. if ( material.map !== null ) {
  334. inputs.push( `${ pad }color3f inputs:diffuseColor.connect = </Materials/Material_${ material.id }/Texture_${ material.map.id }_diffuse.outputs:rgb>` );
  335. if ( material.transparent ) {
  336. inputs.push( `${ pad }float inputs:opacity.connect = </Materials/Material_${ material.id }/Texture_${ material.map.id }_diffuse.outputs:a>` );
  337. } else if ( material.alphaTest > 0.0 ) {
  338. inputs.push( `${ pad }float inputs:opacity.connect = </Materials/Material_${ material.id }/Texture_${ material.map.id }_diffuse.outputs:a>` );
  339. inputs.push( `${ pad }float inputs:opacityThreshold = ${material.alphaTest}` );
  340. }
  341. samplers.push( buildTexture( material.map, 'diffuse', material.color ) );
  342. } else {
  343. inputs.push( `${ pad }color3f inputs:diffuseColor = ${ buildColor( material.color ) }` );
  344. }
  345. if ( material.emissiveMap !== null ) {
  346. inputs.push( `${ pad }color3f inputs:emissiveColor.connect = </Materials/Material_${ material.id }/Texture_${ material.emissiveMap.id }_emissive.outputs:rgb>` );
  347. samplers.push( buildTexture( material.emissiveMap, 'emissive' ) );
  348. } else if ( material.emissive.getHex() > 0 ) {
  349. inputs.push( `${ pad }color3f inputs:emissiveColor = ${ buildColor( material.emissive ) }` );
  350. }
  351. if ( material.normalMap !== null ) {
  352. inputs.push( `${ pad }normal3f inputs:normal.connect = </Materials/Material_${ material.id }/Texture_${ material.normalMap.id }_normal.outputs:rgb>` );
  353. samplers.push( buildTexture( material.normalMap, 'normal' ) );
  354. }
  355. if ( material.aoMap !== null ) {
  356. inputs.push( `${ pad }float inputs:occlusion.connect = </Materials/Material_${ material.id }/Texture_${ material.aoMap.id }_occlusion.outputs:r>` );
  357. samplers.push( buildTexture( material.aoMap, 'occlusion' ) );
  358. }
  359. if ( material.roughnessMap !== null && material.roughness === 1 ) {
  360. inputs.push( `${ pad }float inputs:roughness.connect = </Materials/Material_${ material.id }/Texture_${ material.roughnessMap.id }_roughness.outputs:g>` );
  361. samplers.push( buildTexture( material.roughnessMap, 'roughness' ) );
  362. } else {
  363. inputs.push( `${ pad }float inputs:roughness = ${ material.roughness }` );
  364. }
  365. if ( material.metalnessMap !== null && material.metalness === 1 ) {
  366. inputs.push( `${ pad }float inputs:metallic.connect = </Materials/Material_${ material.id }/Texture_${ material.metalnessMap.id }_metallic.outputs:b>` );
  367. samplers.push( buildTexture( material.metalnessMap, 'metallic' ) );
  368. } else {
  369. inputs.push( `${ pad }float inputs:metallic = ${ material.metalness }` );
  370. }
  371. if ( material.alphaMap !== null ) {
  372. inputs.push( `${pad}float inputs:opacity.connect = </Materials/Material_${material.id}/Texture_${material.alphaMap.id}_opacity.outputs:r>` );
  373. inputs.push( `${pad}float inputs:opacityThreshold = 0.0001` );
  374. samplers.push( buildTexture( material.alphaMap, 'opacity' ) );
  375. } else {
  376. inputs.push( `${pad}float inputs:opacity = ${material.opacity}` );
  377. }
  378. if ( material.isMeshPhysicalMaterial ) {
  379. inputs.push( `${ pad }float inputs:clearcoat = ${ material.clearcoat }` );
  380. inputs.push( `${ pad }float inputs:clearcoatRoughness = ${ material.clearcoatRoughness }` );
  381. inputs.push( `${ pad }float inputs:ior = ${ material.ior }` );
  382. }
  383. return `
  384. def Material "Material_${ material.id }"
  385. {
  386. def Shader "PreviewSurface"
  387. {
  388. uniform token info:id = "UsdPreviewSurface"
  389. ${ inputs.join( '\n' ) }
  390. int inputs:useSpecularWorkflow = 0
  391. token outputs:surface
  392. }
  393. token outputs:surface.connect = </Materials/Material_${ material.id }/PreviewSurface.outputs:surface>
  394. ${ samplers.join( '\n' ) }
  395. }
  396. `;
  397. }
  398. function buildColor( color ) {
  399. return `(${ color.r }, ${ color.g }, ${ color.b })`;
  400. }
  401. function buildColor4( color ) {
  402. return `(${ color.r }, ${ color.g }, ${ color.b }, 1.0)`;
  403. }
  404. function buildVector2( vector ) {
  405. return `(${ vector.x }, ${ vector.y })`;
  406. }
  407. function buildCamera( camera ) {
  408. const name = camera.name ? camera.name : 'Camera_' + camera.id;
  409. const transform = buildMatrix( camera.matrixWorld );
  410. if ( camera.matrixWorld.determinant() < 0 ) {
  411. console.warn( 'THREE.USDZExporter: USDZ does not support negative scales', camera );
  412. }
  413. if ( camera.isOrthographicCamera ) {
  414. return `def Camera "${name}"
  415. {
  416. matrix4d xformOp:transform = ${ transform }
  417. uniform token[] xformOpOrder = ["xformOp:transform"]
  418. float2 clippingRange = (${ camera.near.toPrecision( PRECISION ) }, ${ camera.far.toPrecision( PRECISION ) })
  419. float horizontalAperture = ${ ( ( Math.abs( camera.left ) + Math.abs( camera.right ) ) * 10 ).toPrecision( PRECISION ) }
  420. float verticalAperture = ${ ( ( Math.abs( camera.top ) + Math.abs( camera.bottom ) ) * 10 ).toPrecision( PRECISION ) }
  421. token projection = "orthographic"
  422. }
  423. `;
  424. } else {
  425. return `def Camera "${name}"
  426. {
  427. matrix4d xformOp:transform = ${ transform }
  428. uniform token[] xformOpOrder = ["xformOp:transform"]
  429. float2 clippingRange = (${ camera.near.toPrecision( PRECISION ) }, ${ camera.far.toPrecision( PRECISION ) })
  430. float focalLength = ${ camera.getFocalLength().toPrecision( PRECISION ) }
  431. float focusDistance = ${ camera.focus.toPrecision( PRECISION ) }
  432. float horizontalAperture = ${ camera.getFilmWidth().toPrecision( PRECISION ) }
  433. token projection = "perspective"
  434. float verticalAperture = ${ camera.getFilmHeight().toPrecision( PRECISION ) }
  435. }
  436. `;
  437. }
  438. }
  439. export { USDZExporter };