USDZExporter.js 16 KB

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