123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- import { zipSync, strToU8 } from '../libs/fflate.module.min.js';
- class USDZExporter {
- parse( scene ) {
- let output = buildHeader();
- const materials = {};
- scene.traverse( ( object ) => {
- if ( object.isMesh ) {
- materials[ object.material.uuid ] = object.material;
- output += buildXform( object, buildMesh( object.geometry, object.material ) );
- }
- } );
- output += buildMaterials( materials );
- return zipSync( { 'model.usda': strToU8( output ) }, { level: 0 } );
- }
- }
- //
- function buildHeader() {
- return `#usda 1.0
- (
- doc = "Three.js"
- metersPerUnit = 1
- upAxis = "Y"
- )
- `;
- }
- // Xform
- function buildXform( object, define ) {
- const name = 'Object' + object.id;
- const transform = buildMatrix( object.matrixWorld );
- return `def Xform "${ name }"
- {
- matrix4d xformOp:transform = ${ transform }
- uniform token[] xformOpOrder = ["xformOp:transform"]
- ${ define }
- }
- `;
- }
- function buildMatrix( matrix ) {
- const array = matrix.elements;
- return `( ${ buildMatrixRow( array, 0 ) }, ${ buildMatrixRow( array, 4 ) }, ${ buildMatrixRow( array, 8 ) }, ${ buildMatrixRow( array, 12 ) } )`;
- }
- function buildMatrixRow( array, offset ) {
- return `(${ array[ offset + 0 ] }, ${ array[ offset + 1 ] }, ${ array[ offset + 2 ] }, ${ array[ offset + 3 ] })`;
- }
- // Mesh
- function buildMesh( geometry, material ) {
- const name = 'Geometry' + geometry.id;
- const attributes = geometry.attributes;
- const count = attributes.position.count;
- return `def Mesh "${ name }"
- {
- int[] faceVertexCounts = [${ buildMeshVertexCount( geometry ) }]
- int[] faceVertexIndices = [${ buildMeshVertexIndices( geometry ) }]
- rel material:binding = </_materials/Material_${ material.id }>
- normal3f[] normals = [${ buildVector3Array( attributes.normal, count )}] (
- interpolation = "faceVarying"
- )
- point3f[] points = [${ buildVector3Array( attributes.position, count )}]
- texCoord2f[] primvars:UVMap = [${ buildVector2Array( attributes.uv, count )}] (
- interpolation = "faceVarying"
- )
- uniform token subdivisionScheme = "none"
- }
- `;
- }
- function buildMeshVertexCount( geometry ) {
- const count = geometry.index !== null ? geometry.index.array.length : geometry.attributes.position.count;
- return Array( count / 3 ).fill( 3 ).join( ', ' );
- }
- function buildMeshVertexIndices( geometry ) {
- if ( geometry.index !== null ) {
- return geometry.index.array.join( ', ' );
- }
- const array = [];
- const length = geometry.attributes.position.count;
- for ( let i = 0; i < length; i ++ ) {
- array.push( i );
- }
- return array.join( ', ' );
- }
- function buildVector3Array( attribute, count ) {
- if ( attribute === undefined ) {
- console.warn( 'USDZExporter: Normals missing.' );
- return Array( count ).fill( '(0, 0, 0)' ).join( ', ' );
- }
- const array = [];
- const data = attribute.array;
- for ( let i = 0; i < data.length; i += 3 ) {
- array.push( `(${ data[ i + 0 ] }, ${ data[ i + 1 ] }, ${ data[ i + 2 ] })` );
- }
- return array.join( ', ' );
- }
- function buildVector2Array( attribute, count ) {
- if ( attribute === undefined ) {
- console.warn( 'USDZExporter: UVs missing.' );
- return Array( count ).fill( '(0, 0)' ).join( ', ' );
- }
- const array = [];
- const data = attribute.array;
- for ( let i = 0; i < data.length; i += 2 ) {
- array.push( `(${ data[ i + 0 ] }, ${ data[ i + 1 ] })` );
- }
- return array.join( ', ' );
- }
- // Materials
- function buildMaterials( materials ) {
- const array = [];
- for ( const uuid in materials ) {
- const material = materials[ uuid ];
- array.push( buildMaterial( material ) );
- }
- return `def "_materials"
- {
- ${ array.join( '' ) }
- }
- `;
- }
- function buildMaterial( material ) {
- return `
- def Material "Material_${ material.id }"
- {
- token outputs:surface.connect = </_materials/Material_${ material.id }/previewShader.outputs:surface>
- def Shader "previewShader"
- {
- uniform token info:id = "UsdPreviewSurface"
- color3f inputs:diffuseColor = ${ buildColor( material.color ) }
- color3f inputs:emissiveColor = ${ buildColor( material.emissive ) }
- float inputs:metallic = ${ material.metalness }
- float inputs:roughness = ${ material.roughness }
- token outputs:surface
- }
- }
- `;
- }
- function buildColor( color ) {
- return `(${ color.r }, ${ color.g }, ${ color.b })`;
- }
- export { USDZExporter };
|