Skip to content

Commit

Permalink
mrdoob#28514 extend FBXLoader textures type
Browse files Browse the repository at this point in the history
  • Loading branch information
catalin-enache committed May 28, 2024
1 parent 1911011 commit 7e6377a
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 29 deletions.
34 changes: 9 additions & 25 deletions examples/jsm/loaders/FBXLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,31 +427,19 @@ class FBXTreeParser {

let texture;

const extension = textureNode.FileName.slice( - 3 ).toLowerCase();
const nonNativeExtensions = new Set( [ 'tga', 'tif', 'tiff', 'exr', 'dds', 'hdr', 'ktx2' ] );

if ( extension === 'tga' ) {
const extension = textureNode.FileName.split( '.' ).pop().toLowerCase();

const loader = this.manager.getHandler( '.tga' );
if ( nonNativeExtensions.has( extension ) ) {

const loader = this.manager.getHandler( `.${extension}` );
if ( loader === null ) {

console.warn( 'FBXLoader: TGA loader not found, creating placeholder texture for', textureNode.RelativeFilename );
texture = new Texture();

} else {

loader.setPath( this.textureLoader.path );
texture = loader.load( fileName );

}

} else if ( extension === 'dds' ) {

const loader = this.manager.getHandler( '.dds' );

if ( loader === null ) {

console.warn( 'FBXLoader: DDS loader not found, creating placeholder texture for', textureNode.RelativeFilename );
console.warn(
`FBXLoader: ${extension.toUpperCase()} loader not found, creating placeholder texture for`,
textureNode.RelativeFilename
);
texture = new Texture();

} else {
Expand All @@ -461,13 +449,9 @@ class FBXTreeParser {

}

} else if ( extension === 'psd' ) {

console.warn( 'FBXLoader: PSD textures are not supported, creating placeholder texture for', textureNode.RelativeFilename );
texture = new Texture();

} else {

// TextureLoader#load() returns a texture in any case which can be used as placeholder if the image failed to load.
texture = this.textureLoader.load( fileName );

}
Expand Down
Binary file added examples/models/fbx/sample_512.dds
Binary file not shown.
Binary file added examples/models/fbx/sample_512.exr
Binary file not shown.
Binary file added examples/models/fbx/sample_512.hdr
Binary file not shown.
Binary file added examples/models/fbx/sample_512.tga
Binary file not shown.
Binary file added examples/models/fbx/sample_512.tiff
Binary file not shown.
Binary file added examples/models/fbx/with_non_native_textures.fbx
Binary file not shown.
38 changes: 34 additions & 4 deletions examples/webgl_loader_fbx.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { FBXLoader } from 'three/addons/loaders/FBXLoader.js';
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
import { EXRLoader } from 'three/addons/loaders/EXRLoader.js';
import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
import { TGALoader } from 'three/addons/loaders/TGALoader.js';
import { TIFFLoader } from 'three/addons/loaders/TIFFLoader.js';
import { DDSLoader } from 'three/addons/loaders/DDSLoader.js';

export const defaultLoadingManager = THREE.DefaultLoadingManager;

export const exrLoader = new EXRLoader();
export const rgbeLoader = new RGBELoader();
export const tgaLoader = new TGALoader();
export const ddsLoader = new DDSLoader();
export const tiffLoader = new TIFFLoader();

defaultLoadingManager.addHandler( /\.tga$/i, tgaLoader );
defaultLoadingManager.addHandler( /\.dds$/i, ddsLoader );
defaultLoadingManager.addHandler( /\.exr$/i, exrLoader );
defaultLoadingManager.addHandler( /\.hdr$/i, rgbeLoader );
defaultLoadingManager.addHandler( /\.tiff$/i, tiffLoader );

let camera, scene, renderer, stats, object, loader, guiMorphsFolder;
const clock = new THREE.Clock();
Expand All @@ -43,7 +62,8 @@

const assets = [
'Samba Dancing',
'morph_test'
'morph_test',
'with_non_native_textures'
];


Expand Down Expand Up @@ -87,7 +107,7 @@
grid.material.transparent = true;
scene.add( grid );

loader = new FBXLoader( );
loader = new FBXLoader( defaultLoadingManager );
loadAsset( params.asset );

renderer = new THREE.WebGLRenderer( { antialias: true } );
Expand Down Expand Up @@ -126,8 +146,18 @@

object.traverse( function ( child ) {

if ( child.material ) child.material.dispose();
if ( child.material && child.material.map ) child.material.map.dispose();
if ( child.material ) {

const materials = Array.isArray( child.material ) ? child.material : [ child.material ];
materials.forEach( material => {

if ( material.map ) material.map.dispose();
material.dispose();

} );

}

if ( child.geometry ) child.geometry.dispose();

} );
Expand Down

0 comments on commit 7e6377a

Please sign in to comment.