Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 41 additions & 8 deletions examples/jsm/exporters/USDZExporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class USDZExporter {
ar: {
anchoring: { type: 'plane' },
planeAnchoring: { alignment: 'horizontal' }
}
},
quickLookCompatible: false,
}, options );

const files = {};
Expand Down Expand Up @@ -68,7 +69,7 @@ class USDZExporter {

output += buildSceneEnd();

output += buildMaterials( materials, textures );
output += buildMaterials( materials, textures, options.quickLookCompatible );

files[ modelFileName ] = fflate.strToU8( output );
output = null;
Expand Down Expand Up @@ -404,15 +405,15 @@ function buildPrimvars( attributes, count ) {

// Materials

function buildMaterials( materials, textures ) {
function buildMaterials( materials, textures, quickLookCompatible = false ) {

const array = [];

for ( const uuid in materials ) {

const material = materials[ uuid ];

array.push( buildMaterial( material, textures ) );
array.push( buildMaterial( material, textures, quickLookCompatible ) );

}

Expand All @@ -425,7 +426,7 @@ ${ array.join( '' ) }

}

function buildMaterial( material, textures ) {
function buildMaterial( material, textures, quickLookCompatible = false ) {

// https://graphics.pixar.com/usd/docs/UsdPreviewSurface-Proposal.html

Expand All @@ -447,6 +448,38 @@ function buildMaterial( material, textures ) {
1002: 'mirror' // MirroredRepeatWrapping
};

const repeat = texture.repeat.clone();
const offset = texture.offset.clone();
const rotation = texture.rotation;

// rotation is around the wrong point. after rotation we need to shift offset again so that we're rotating around the right spot
const xRotationOffset = Math.sin( rotation );
const yRotationOffset = Math.cos( rotation );

// texture coordinates start in the opposite corner, need to correct
offset.y = 1 - offset.y - repeat.y;

// turns out QuickLook is buggy and interprets texture repeat inverted/applies operations in a different order.
// Apple Feedback: FB10036297 and FB11442287
if ( quickLookCompatible ) {

// This is NOT correct yet in QuickLook, but comes close for a range of models.
// It becomes more incorrect the bigger the offset is

offset.x = offset.x / repeat.x;
offset.y = offset.y / repeat.y;

offset.x += xRotationOffset / repeat.x;
offset.y += yRotationOffset - 1;

} else {

// results match glTF results exactly. verified correct in usdview.
offset.x += xRotationOffset * repeat.x;
offset.y += ( 1 - yRotationOffset ) * repeat.y;

}

return `
def Shader "PrimvarReader_${ mapType }"
{
Expand All @@ -460,9 +493,9 @@ function buildMaterial( material, textures ) {
{
uniform token info:id = "UsdTransform2d"
token inputs:in.connect = </Materials/Material_${ material.id }/PrimvarReader_${ mapType }.outputs:result>
float inputs:rotation = ${ texture.rotation * ( 180 / Math.PI ) }
float2 inputs:scale = ${ buildVector2( texture.repeat ) }
float2 inputs:translation = ${ buildVector2( texture.offset ) }
float inputs:rotation = ${ ( rotation * ( 180 / Math.PI ) ).toFixed( PRECISION ) }
float2 inputs:scale = ${ buildVector2( repeat ) }
float2 inputs:translation = ${ buildVector2( offset ) }
float2 outputs:result
}

Expand Down