From 01010bef91a45008b4acdde510e2e0a77565c085 Mon Sep 17 00:00:00 2001 From: ycw Date: Mon, 6 May 2024 14:32:20 +0800 Subject: [PATCH 1/3] imports OBJ with textures --- editor/js/Loader.js | 42 ++++++++++++++++--------------- examples/jsm/loaders/MTLLoader.js | 2 ++ 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/editor/js/Loader.js b/editor/js/Loader.js index 56bfe04906a324..41dbed469b5f5c 100644 --- a/editor/js/Loader.js +++ b/editor/js/Loader.js @@ -858,6 +858,24 @@ 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' } ); + return URL.createObjectURL( blob ); + + } + + return url; + + } ); + // Poly if ( zip[ 'model.obj' ] && zip[ 'materials.mtl' ] ) { @@ -865,9 +883,11 @@ function Loader( editor ) { 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; } @@ -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 ) { diff --git a/examples/jsm/loaders/MTLLoader.js b/examples/jsm/loaders/MTLLoader.js index f277c973536ea4..b9dfc013c7aa3f 100644 --- a/examples/jsm/loaders/MTLLoader.js +++ b/examples/jsm/loaders/MTLLoader.js @@ -365,6 +365,8 @@ class MaterialCreator { } + map.sourceFile = value; // for UITexture + params[ mapType ] = map; } From fc3b6bd4d7e3467090180f8b0dedc4643f1da78a Mon Sep 17 00:00:00 2001 From: ycw Date: Mon, 6 May 2024 17:45:47 +0800 Subject: [PATCH 2/3] set map.name to filename --- editor/js/libs/ui.three.js | 2 +- examples/jsm/loaders/MTLLoader.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/editor/js/libs/ui.three.js b/editor/js/libs/ui.three.js index e5ea49ba22cb8f..657aa51b640004 100644 --- a/editor/js/libs/ui.three.js +++ b/editor/js/libs/ui.three.js @@ -200,7 +200,7 @@ class UITexture extends UISpan { if ( image !== undefined && image !== null && image.width > 0 ) { - canvas.title = texture.sourceFile; + canvas.title = texture.sourceFile || texture.name; const scale = canvas.width / image.width; if ( texture.isDataTexture || texture.isCompressedTexture ) { diff --git a/examples/jsm/loaders/MTLLoader.js b/examples/jsm/loaders/MTLLoader.js index b9dfc013c7aa3f..eed5fff2cfc45a 100644 --- a/examples/jsm/loaders/MTLLoader.js +++ b/examples/jsm/loaders/MTLLoader.js @@ -365,7 +365,7 @@ class MaterialCreator { } - map.sourceFile = value; // for UITexture + map.name = value; params[ mapType ] = map; From 8d8e1f450d612418525b948371830e0b74b3a6d6 Mon Sep 17 00:00:00 2001 From: ycw Date: Wed, 8 May 2024 18:53:09 +0800 Subject: [PATCH 3/3] no filename needed in UITexture --- editor/js/Loader.js | 8 ++++---- editor/js/libs/ui.three.js | 2 +- examples/jsm/loaders/MTLLoader.js | 2 -- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/editor/js/Loader.js b/editor/js/Loader.js index 41dbed469b5f5c..a5cea2ce2e5a5b 100644 --- a/editor/js/Loader.js +++ b/editor/js/Loader.js @@ -861,13 +861,13 @@ function Loader( editor ) { const manager = new THREE.LoadingManager(); manager.setURLModifier( function ( url ) { - const u8a = zip[ url ]; + const file = zip[ url ]; - if ( u8a ) { + if ( file ) { console.log( 'Loading', url ); - const blob = new Blob( [ u8a ], { type: 'application/octet-stream' } ); + const blob = new Blob( [ file.buffer ], { type: 'application/octet-stream' } ); return URL.createObjectURL( blob ); } @@ -884,7 +884,7 @@ function Loader( editor ) { const { OBJLoader } = await import( 'three/addons/loaders/OBJLoader.js' ); const materials = new MTLLoader( manager ).parse( strFromU8( zip[ 'materials.mtl' ] ) ); - const object = new OBJLoader( manager ).setMaterials( materials ).parse( strFromU8( zip[ 'model.obj' ] ) ); + const object = new OBJLoader().setMaterials( materials ).parse( strFromU8( zip[ 'model.obj' ] ) ); editor.execute( new AddObjectCommand( editor, object ) ); return; diff --git a/editor/js/libs/ui.three.js b/editor/js/libs/ui.three.js index 657aa51b640004..e5ea49ba22cb8f 100644 --- a/editor/js/libs/ui.three.js +++ b/editor/js/libs/ui.three.js @@ -200,7 +200,7 @@ class UITexture extends UISpan { if ( image !== undefined && image !== null && image.width > 0 ) { - canvas.title = texture.sourceFile || texture.name; + canvas.title = texture.sourceFile; const scale = canvas.width / image.width; if ( texture.isDataTexture || texture.isCompressedTexture ) { diff --git a/examples/jsm/loaders/MTLLoader.js b/examples/jsm/loaders/MTLLoader.js index eed5fff2cfc45a..f277c973536ea4 100644 --- a/examples/jsm/loaders/MTLLoader.js +++ b/examples/jsm/loaders/MTLLoader.js @@ -365,8 +365,6 @@ class MaterialCreator { } - map.name = value; - params[ mapType ] = map; }