USDZExporter.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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 ) }
  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 ) {
  222. const array = [];
  223. for ( let i = 0; i < attribute.count; i ++ ) {
  224. const x = attribute.getX( i );
  225. const y = attribute.getY( i );
  226. array.push( `(${ x.toPrecision( PRECISION ) }, ${ 1 - y.toPrecision( PRECISION ) })` );
  227. }
  228. return array.join( ', ' );
  229. }
  230. function buildPrimvars( attributes ) {
  231. let string = '';
  232. for ( let i = 0; i < 4; i ++ ) {
  233. const id = ( i > 0 ? i : '' );
  234. const attribute = attributes[ 'uv' + id ];
  235. if ( attribute !== undefined ) {
  236. string += `
  237. texCoord2f[] primvars:st${ id } = [${ buildVector2Array( attribute )}] (
  238. interpolation = "vertex"
  239. )`;
  240. }
  241. }
  242. return string;
  243. }
  244. // Materials
  245. function buildMaterials( materials, textures, quickLookCompatible = false ) {
  246. const array = [];
  247. for ( const uuid in materials ) {
  248. const material = materials[ uuid ];
  249. array.push( buildMaterial( material, textures, quickLookCompatible ) );
  250. }
  251. return `def "Materials"
  252. {
  253. ${ array.join( '' ) }
  254. }
  255. `;
  256. }
  257. function buildMaterial( material, textures, quickLookCompatible = false ) {
  258. // https://graphics.pixar.com/usd/docs/UsdPreviewSurface-Proposal.html
  259. const pad = ' ';
  260. const inputs = [];
  261. const samplers = [];
  262. function buildTexture( texture, mapType, color ) {
  263. const id = texture.source.id + '_' + texture.flipY;
  264. textures[ id ] = texture;
  265. const uv = texture.channel > 0 ? 'st' + texture.channel : 'st';
  266. const WRAPPINGS = {
  267. 1000: 'repeat', // RepeatWrapping
  268. 1001: 'clamp', // ClampToEdgeWrapping
  269. 1002: 'mirror' // MirroredRepeatWrapping
  270. };
  271. const repeat = texture.repeat.clone();
  272. const offset = texture.offset.clone();
  273. const rotation = texture.rotation;
  274. // rotation is around the wrong point. after rotation we need to shift offset again so that we're rotating around the right spot
  275. const xRotationOffset = Math.sin( rotation );
  276. const yRotationOffset = Math.cos( rotation );
  277. // texture coordinates start in the opposite corner, need to correct
  278. offset.y = 1 - offset.y - repeat.y;
  279. // turns out QuickLook is buggy and interprets texture repeat inverted/applies operations in a different order.
  280. // Apple Feedback: FB10036297 and FB11442287
  281. if ( quickLookCompatible ) {
  282. // This is NOT correct yet in QuickLook, but comes close for a range of models.
  283. // It becomes more incorrect the bigger the offset is
  284. offset.x = offset.x / repeat.x;
  285. offset.y = offset.y / repeat.y;
  286. offset.x += xRotationOffset / repeat.x;
  287. offset.y += yRotationOffset - 1;
  288. } else {
  289. // results match glTF results exactly. verified correct in usdview.
  290. offset.x += xRotationOffset * repeat.x;
  291. offset.y += ( 1 - yRotationOffset ) * repeat.y;
  292. }
  293. return `
  294. def Shader "PrimvarReader_${ mapType }"
  295. {
  296. uniform token info:id = "UsdPrimvarReader_float2"
  297. float2 inputs:fallback = (0.0, 0.0)
  298. token inputs:varname = "${ uv }"
  299. float2 outputs:result
  300. }
  301. def Shader "Transform2d_${ mapType }"
  302. {
  303. uniform token info:id = "UsdTransform2d"
  304. token inputs:in.connect = </Materials/Material_${ material.id }/PrimvarReader_${ mapType }.outputs:result>
  305. float inputs:rotation = ${ ( rotation * ( 180 / Math.PI ) ).toFixed( PRECISION ) }
  306. float2 inputs:scale = ${ buildVector2( repeat ) }
  307. float2 inputs:translation = ${ buildVector2( offset ) }
  308. float2 outputs:result
  309. }
  310. def Shader "Texture_${ texture.id }_${ mapType }"
  311. {
  312. uniform token info:id = "UsdUVTexture"
  313. asset inputs:file = @textures/Texture_${ id }.png@
  314. float2 inputs:st.connect = </Materials/Material_${ material.id }/Transform2d_${ mapType }.outputs:result>
  315. ${ color !== undefined ? 'float4 inputs:scale = ' + buildColor4( color ) : '' }
  316. token inputs:sourceColorSpace = "${ texture.colorSpace === THREE.NoColorSpace ? 'raw' : 'sRGB' }"
  317. token inputs:wrapS = "${ WRAPPINGS[ texture.wrapS ] }"
  318. token inputs:wrapT = "${ WRAPPINGS[ texture.wrapT ] }"
  319. float outputs:r
  320. float outputs:g
  321. float outputs:b
  322. float3 outputs:rgb
  323. ${ material.transparent || material.alphaTest > 0.0 ? 'float outputs:a' : '' }
  324. }`;
  325. }
  326. if ( material.side === THREE.DoubleSide ) {
  327. console.warn( 'THREE.USDZExporter: USDZ does not support double sided materials', material );
  328. }
  329. if ( material.map !== null ) {
  330. inputs.push( `${ pad }color3f inputs:diffuseColor.connect = </Materials/Material_${ material.id }/Texture_${ material.map.id }_diffuse.outputs:rgb>` );
  331. if ( material.transparent ) {
  332. inputs.push( `${ pad }float inputs:opacity.connect = </Materials/Material_${ material.id }/Texture_${ material.map.id }_diffuse.outputs:a>` );
  333. } else if ( material.alphaTest > 0.0 ) {
  334. inputs.push( `${ pad }float inputs:opacity.connect = </Materials/Material_${ material.id }/Texture_${ material.map.id }_diffuse.outputs:a>` );
  335. inputs.push( `${ pad }float inputs:opacityThreshold = ${material.alphaTest}` );
  336. }
  337. samplers.push( buildTexture( material.map, 'diffuse', material.color ) );
  338. } else {
  339. inputs.push( `${ pad }color3f inputs:diffuseColor = ${ buildColor( material.color ) }` );
  340. }
  341. if ( material.emissiveMap !== null ) {
  342. inputs.push( `${ pad }color3f inputs:emissiveColor.connect = </Materials/Material_${ material.id }/Texture_${ material.emissiveMap.id }_emissive.outputs:rgb>` );
  343. samplers.push( buildTexture( material.emissiveMap, 'emissive' ) );
  344. } else if ( material.emissive.getHex() > 0 ) {
  345. inputs.push( `${ pad }color3f inputs:emissiveColor = ${ buildColor( material.emissive ) }` );
  346. }
  347. if ( material.normalMap !== null ) {
  348. inputs.push( `${ pad }normal3f inputs:normal.connect = </Materials/Material_${ material.id }/Texture_${ material.normalMap.id }_normal.outputs:rgb>` );
  349. samplers.push( buildTexture( material.normalMap, 'normal' ) );
  350. }
  351. if ( material.aoMap !== null ) {
  352. inputs.push( `${ pad }float inputs:occlusion.connect = </Materials/Material_${ material.id }/Texture_${ material.aoMap.id }_occlusion.outputs:r>` );
  353. samplers.push( buildTexture( material.aoMap, 'occlusion' ) );
  354. }
  355. if ( material.roughnessMap !== null && material.roughness === 1 ) {
  356. inputs.push( `${ pad }float inputs:roughness.connect = </Materials/Material_${ material.id }/Texture_${ material.roughnessMap.id }_roughness.outputs:g>` );
  357. samplers.push( buildTexture( material.roughnessMap, 'roughness' ) );
  358. } else {
  359. inputs.push( `${ pad }float inputs:roughness = ${ material.roughness }` );
  360. }
  361. if ( material.metalnessMap !== null && material.metalness === 1 ) {
  362. inputs.push( `${ pad }float inputs:metallic.connect = </Materials/Material_${ material.id }/Texture_${ material.metalnessMap.id }_metallic.outputs:b>` );
  363. samplers.push( buildTexture( material.metalnessMap, 'metallic' ) );
  364. } else {
  365. inputs.push( `${ pad }float inputs:metallic = ${ material.metalness }` );
  366. }
  367. if ( material.alphaMap !== null ) {
  368. inputs.push( `${pad}float inputs:opacity.connect = </Materials/Material_${material.id}/Texture_${material.alphaMap.id}_opacity.outputs:r>` );
  369. inputs.push( `${pad}float inputs:opacityThreshold = 0.0001` );
  370. samplers.push( buildTexture( material.alphaMap, 'opacity' ) );
  371. } else {
  372. inputs.push( `${pad}float inputs:opacity = ${material.opacity}` );
  373. }
  374. if ( material.isMeshPhysicalMaterial ) {
  375. inputs.push( `${ pad }float inputs:clearcoat = ${ material.clearcoat }` );
  376. inputs.push( `${ pad }float inputs:clearcoatRoughness = ${ material.clearcoatRoughness }` );
  377. inputs.push( `${ pad }float inputs:ior = ${ material.ior }` );
  378. }
  379. return `
  380. def Material "Material_${ material.id }"
  381. {
  382. def Shader "PreviewSurface"
  383. {
  384. uniform token info:id = "UsdPreviewSurface"
  385. ${ inputs.join( '\n' ) }
  386. int inputs:useSpecularWorkflow = 0
  387. token outputs:surface
  388. }
  389. token outputs:surface.connect = </Materials/Material_${ material.id }/PreviewSurface.outputs:surface>
  390. ${ samplers.join( '\n' ) }
  391. }
  392. `;
  393. }
  394. function buildColor( color ) {
  395. return `(${ color.r }, ${ color.g }, ${ color.b })`;
  396. }
  397. function buildColor4( color ) {
  398. return `(${ color.r }, ${ color.g }, ${ color.b }, 1.0)`;
  399. }
  400. function buildVector2( vector ) {
  401. return `(${ vector.x }, ${ vector.y })`;
  402. }
  403. function buildCamera( camera ) {
  404. const name = camera.name ? camera.name : 'Camera_' + camera.id;
  405. const transform = buildMatrix( camera.matrixWorld );
  406. if ( camera.matrixWorld.determinant() < 0 ) {
  407. console.warn( 'THREE.USDZExporter: USDZ does not support negative scales', camera );
  408. }
  409. if ( camera.isOrthographicCamera ) {
  410. return `def Camera "${name}"
  411. {
  412. matrix4d xformOp:transform = ${ transform }
  413. uniform token[] xformOpOrder = ["xformOp:transform"]
  414. float2 clippingRange = (${ camera.near.toPrecision( PRECISION ) }, ${ camera.far.toPrecision( PRECISION ) })
  415. float horizontalAperture = ${ ( ( Math.abs( camera.left ) + Math.abs( camera.right ) ) * 10 ).toPrecision( PRECISION ) }
  416. float verticalAperture = ${ ( ( Math.abs( camera.top ) + Math.abs( camera.bottom ) ) * 10 ).toPrecision( PRECISION ) }
  417. token projection = "orthographic"
  418. }
  419. `;
  420. } else {
  421. return `def Camera "${name}"
  422. {
  423. matrix4d xformOp:transform = ${ transform }
  424. uniform token[] xformOpOrder = ["xformOp:transform"]
  425. float2 clippingRange = (${ camera.near.toPrecision( PRECISION ) }, ${ camera.far.toPrecision( PRECISION ) })
  426. float focalLength = ${ camera.getFocalLength().toPrecision( PRECISION ) }
  427. float focusDistance = ${ camera.focus.toPrecision( PRECISION ) }
  428. float horizontalAperture = ${ camera.getFilmWidth().toPrecision( PRECISION ) }
  429. token projection = "perspective"
  430. float verticalAperture = ${ camera.getFilmHeight().toPrecision( PRECISION ) }
  431. }
  432. `;
  433. }
  434. }
  435. export { USDZExporter };