Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editor: Fixed assets lost when importing zipped OBJ/MTL #28292

Merged
merged 3 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
imports OBJ with textures
  • Loading branch information
ycw committed May 6, 2024
commit 01010bef91a45008b4acdde510e2e0a77565c085
42 changes: 22 additions & 20 deletions editor/js/Loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -858,16 +858,36 @@ function Loader( editor ) {

const zip = unzipSync( new Uint8Array( contents ) );

const manager = new THREE.LoadingManager();
manager.setURLModifier( function ( url ) {

const u8a = zip[ url ];

if ( u8a ) {

console.log( 'Loading', url );

const blob = new Blob( [ u8a ], { type: 'application/octet-stream' } );
ycw marked this conversation as resolved.
Show resolved Hide resolved
return URL.createObjectURL( blob );

}

return url;

} );

// Poly

if ( zip[ 'model.obj' ] && zip[ 'materials.mtl' ] ) {

const { MTLLoader } = await import( 'three/addons/loaders/MTLLoader.js' );
const { OBJLoader } = await import( 'three/addons/loaders/OBJLoader.js' );

const materials = new MTLLoader().parse( strFromU8( zip[ 'materials.mtl' ] ) );
const object = new OBJLoader().setMaterials( materials ).parse( strFromU8( zip[ 'model.obj' ] ) );
const materials = new MTLLoader( manager ).parse( strFromU8( zip[ 'materials.mtl' ] ) );
const object = new OBJLoader( manager ).setMaterials( materials ).parse( strFromU8( zip[ 'model.obj' ] ) );

editor.execute( new AddObjectCommand( editor, object ) );
return;

}

Expand All @@ -877,24 +897,6 @@ function Loader( editor ) {

const file = zip[ path ];

const manager = new THREE.LoadingManager();
manager.setURLModifier( function ( url ) {

const file = zip[ url ];

if ( file ) {

console.log( 'Loading', url );

const blob = new Blob( [ file.buffer ], { type: 'application/octet-stream' } );
return URL.createObjectURL( blob );

}

return url;

} );

const extension = path.split( '.' ).pop().toLowerCase();

switch ( extension ) {
Expand Down
2 changes: 2 additions & 0 deletions examples/jsm/loaders/MTLLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ class MaterialCreator {

}

map.sourceFile = value; // for UITexture
ycw marked this conversation as resolved.
Show resolved Hide resolved

params[ mapType ] = map;

}
Expand Down
Loading