Skip to content

Commit

Permalink
USDZ Loader - texture scale + offset + rotation (#26861)
Browse files Browse the repository at this point in the history
  • Loading branch information
GitHubDragonFly authored Sep 28, 2023
1 parent cb64471 commit f886330
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions examples/jsm/loaders/USDZLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
SRGBColorSpace,
TextureLoader,
Object3D,
Vector2
} from 'three';

import * as fflate from '../libs/fflate.module.js';
Expand Down Expand Up @@ -489,6 +490,30 @@ class USDZLoader extends Loader {

}

function setTextureParams( map, data_value ) {

// rotation, scale and translation

if ( data_value[ 'float inputs:rotation' ] ) {

map.rotation = parseFloat( data_value[ 'float inputs:rotation' ] );

}

if ( data_value[ 'float2 inputs:scale' ] ) {

map.repeat = new Vector2().fromArray( JSON.parse( '[' + data_value[ 'float2 inputs:scale' ].replace( /[()]*/g, '' ) + ']' ) );

}

if ( data_value[ 'float2 inputs:translation' ] ) {

map.offset = new Vector2().fromArray( JSON.parse( '[' + data_value[ 'float2 inputs:translation' ].replace( /[()]*/g, '' ) + ']' ) );

}

}

function buildMaterial( data ) {

const material = new MeshPhysicalMaterial();
Expand All @@ -507,6 +532,12 @@ class USDZLoader extends Loader {
material.map = buildTexture( sampler );
material.map.colorSpace = SRGBColorSpace;

if ( 'def Shader "Transform2d_diffuse"' in data ) {

setTextureParams( material.map, data[ 'def Shader "Transform2d_diffuse"' ] );

}

} else if ( 'color3f inputs:diffuseColor' in surface ) {

const color = surface[ 'color3f inputs:diffuseColor' ].replace( /[()]*/g, '' );
Expand All @@ -523,6 +554,12 @@ class USDZLoader extends Loader {
material.emissiveMap.colorSpace = SRGBColorSpace;
material.emissive.set( 0xffffff );

if ( 'def Shader "Transform2d_emissive"' in data ) {

setTextureParams( material.emissiveMap, data[ 'def Shader "Transform2d_emissive"' ] );

}

} else if ( 'color3f inputs:emissiveColor' in surface ) {

const color = surface[ 'color3f inputs:emissiveColor' ].replace( /[()]*/g, '' );
Expand All @@ -538,6 +575,12 @@ class USDZLoader extends Loader {
material.normalMap = buildTexture( sampler );
material.normalMap.colorSpace = NoColorSpace;

if ( 'def Shader "Transform2d_normal"' in data ) {

setTextureParams( material.normalMap, data[ 'def Shader "Transform2d_normal"' ] );

}

}

if ( 'float inputs:roughness.connect' in surface ) {
Expand All @@ -549,6 +592,12 @@ class USDZLoader extends Loader {
material.roughnessMap = buildTexture( sampler );
material.roughnessMap.colorSpace = NoColorSpace;

if ( 'def Shader "Transform2d_roughness"' in data ) {

setTextureParams( material.roughnessMap, data[ 'def Shader "Transform2d_roughness"' ] );

}

} else if ( 'float inputs:roughness' in surface ) {

material.roughness = parseFloat( surface[ 'float inputs:roughness' ] );
Expand All @@ -564,6 +613,12 @@ class USDZLoader extends Loader {
material.metalnessMap = buildTexture( sampler );
material.metalnessMap.colorSpace = NoColorSpace;

if ( 'def Shader "Transform2d_metallic"' in data ) {

setTextureParams( material.metalnessMap, data[ 'def Shader "Transform2d_metallic"' ] );

}

} else if ( 'float inputs:metallic' in surface ) {

material.metalness = parseFloat( surface[ 'float inputs:metallic' ] );
Expand All @@ -579,6 +634,12 @@ class USDZLoader extends Loader {
material.clearcoatMap = buildTexture( sampler );
material.clearcoatMap.colorSpace = NoColorSpace;

if ( 'def Shader "Transform2d_clearcoat"' in data ) {

setTextureParams( material.clearcoatMap, data[ 'def Shader "Transform2d_clearcoat"' ] );

}

} else if ( 'float inputs:clearcoat' in surface ) {

material.clearcoat = parseFloat( surface[ 'float inputs:clearcoat' ] );
Expand All @@ -594,6 +655,12 @@ class USDZLoader extends Loader {
material.clearcoatRoughnessMap = buildTexture( sampler );
material.clearcoatRoughnessMap.colorSpace = NoColorSpace;

if ( 'def Shader "Transform2d_clearcoatRoughness"' in data ) {

setTextureParams( material.clearcoatRoughnessMap, data[ 'def Shader "Transform2d_clearcoatRoughness"' ] );

}

} else if ( 'float inputs:clearcoatRoughness' in surface ) {

material.clearcoatRoughness = parseFloat( surface[ 'float inputs:clearcoatRoughness' ] );
Expand All @@ -614,6 +681,12 @@ class USDZLoader extends Loader {
material.aoMap = buildTexture( sampler );
material.aoMap.colorSpace = NoColorSpace;

if ( 'def Shader "Transform2d_occlusion"' in data ) {

setTextureParams( material.aoMap, data[ 'def Shader "Transform2d_occlusion"' ] );

}

}

}
Expand Down

0 comments on commit f886330

Please sign in to comment.