Skip to content

Commit 5ecb825

Browse files
jtydhr88webfiltered
authored andcommitted
[3d] improve mtl support logic (#3965)
1 parent 1d9d5e5 commit 5ecb825

File tree

3 files changed

+89
-36
lines changed

3 files changed

+89
-36
lines changed

src/extensions/core/load3d.ts

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ useExtensionService().registerExtension({
266266
LOAD_3D_ANIMATION(node) {
267267
const fileInput = document.createElement('input')
268268
fileInput.type = 'file'
269-
fileInput.accept = '.fbx,glb,gltf'
269+
fileInput.accept = '.gltf,.glb,.fbx'
270270
fileInput.style.display = 'none'
271271
fileInput.onchange = async () => {
272272
if (fileInput.files?.length) {
@@ -452,31 +452,43 @@ useExtensionService().registerExtension({
452452

453453
const onExecuted = node.onExecuted
454454

455-
node.onExecuted = function (message: any) {
456-
onExecuted?.apply(this, arguments as any)
455+
useLoad3dService().waitForLoad3d(node, (load3d) => {
456+
const config = new Load3DConfiguration(load3d)
457457

458-
let filePath = message.result[0]
458+
const modelWidget = node.widgets?.find((w) => w.name === 'model_file')
459459

460-
if (!filePath) {
461-
const msg = t('toastMessages.unableToGetModelFilePath')
462-
console.error(msg)
463-
useToastStore().addAlert(msg)
464-
}
460+
if (modelWidget) {
461+
const lastTimeModelFile = node.properties['Last Time Model File']
462+
463+
if (lastTimeModelFile) {
464+
modelWidget.value = lastTimeModelFile
465+
466+
const cameraState = node.properties['Camera Info']
467+
468+
config.configure('output', modelWidget, cameraState)
469+
}
470+
471+
node.onExecuted = function (message: any) {
472+
onExecuted?.apply(this, arguments as any)
465473

466-
let cameraState = message.result[1]
474+
let filePath = message.result[0]
475+
476+
if (!filePath) {
477+
const msg = t('toastMessages.unableToGetModelFilePath')
478+
console.error(msg)
479+
useToastStore().addAlert(msg)
480+
}
467481

468-
useLoad3dService().waitForLoad3d(node, (load3d) => {
469-
const modelWidget = node.widgets?.find((w) => w.name === 'model_file')
482+
let cameraState = message.result[1]
470483

471-
if (modelWidget) {
472484
modelWidget.value = filePath.replaceAll('\\', '/')
473485

474-
const config = new Load3DConfiguration(load3d)
486+
node.properties['Last Time Model File'] = modelWidget.value
475487

476488
config.configure('output', modelWidget, cameraState)
477489
}
478-
})
479-
}
490+
}
491+
})
480492
}
481493
})
482494

@@ -526,29 +538,42 @@ useExtensionService().registerExtension({
526538

527539
const onExecuted = node.onExecuted
528540

529-
node.onExecuted = function (message: any) {
530-
onExecuted?.apply(this, arguments as any)
541+
useLoad3dService().waitForLoad3d(node, (load3d) => {
542+
const config = new Load3DConfiguration(load3d)
543+
544+
const modelWidget = node.widgets?.find((w) => w.name === 'model_file')
531545

532-
let filePath = message.result[0]
546+
if (modelWidget) {
547+
const lastTimeModelFile = node.properties['Last Time Model File']
533548

534-
if (!filePath) {
535-
const msg = t('toastMessages.unableToGetModelFilePath')
536-
console.error(msg)
537-
useToastStore().addAlert(msg)
538-
}
549+
if (lastTimeModelFile) {
550+
modelWidget.value = lastTimeModelFile
551+
552+
const cameraState = node.properties['Camera Info']
553+
554+
config.configure('output', modelWidget, cameraState)
555+
}
539556

540-
let cameraState = message.result[1]
557+
node.onExecuted = function (message: any) {
558+
onExecuted?.apply(this, arguments as any)
559+
560+
let filePath = message.result[0]
561+
562+
if (!filePath) {
563+
const msg = t('toastMessages.unableToGetModelFilePath')
564+
console.error(msg)
565+
useToastStore().addAlert(msg)
566+
}
567+
568+
let cameraState = message.result[1]
541569

542-
useLoad3dService().waitForLoad3d(node, (load3d) => {
543-
const modelWidget = node.widgets?.find((w) => w.name === 'model_file')
544-
if (modelWidget) {
545570
modelWidget.value = filePath.replaceAll('\\', '/')
546571

547-
const config = new Load3DConfiguration(load3d)
572+
node.properties['Last Time Model File'] = modelWidget.value
548573

549574
config.configure('output', modelWidget, cameraState)
550575
}
551-
})
552-
}
576+
}
577+
})
553578
}
554579
})

src/extensions/core/load3d/LoaderManager.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ export class LoaderManager implements LoaderManagerInterface {
132132
if (this.modelManager.materialMode === 'original') {
133133
const mtlUrl = url.replace(/(filename=.*?)\.obj/, '$1.mtl')
134134

135+
const subfolderMatch = url.match(/[?&]subfolder=([^&]*)/)
136+
137+
const subfolder = subfolderMatch
138+
? decodeURIComponent(subfolderMatch[1])
139+
: '3d'
140+
141+
this.mtlLoader.setSubfolder(subfolder)
142+
135143
try {
136144
const materials = await this.mtlLoader.loadAsync(mtlUrl)
137145
materials.preload()

src/extensions/core/load3d/threejsOverride/OverrideMTLLoader.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ class OverrideMTLLoader extends Loader {
3838
this.loadRootFolder = loadRootFolder
3939
}
4040

41+
setSubfolder(subfolder) {
42+
this.subfolder = subfolder
43+
}
44+
4145
/**
4246
* Starts loading from the given URL and passes the loaded MTL asset
4347
* to the `onLoad()` callback.
@@ -135,7 +139,8 @@ class OverrideMTLLoader extends Loader {
135139
const materialCreator = new OverrideMaterialCreator(
136140
this.resourcePath || path,
137141
this.materialOptions,
138-
this.loadRootFolder
142+
this.loadRootFolder,
143+
this.subfolder
139144
)
140145
materialCreator.setCrossOrigin(this.crossOrigin)
141146
materialCreator.setManager(this.manager)
@@ -155,7 +160,7 @@ class OverrideMTLLoader extends Loader {
155160
*/
156161

157162
class OverrideMaterialCreator {
158-
constructor(baseUrl = '', options = {}, loadRootFolder) {
163+
constructor(baseUrl = '', options = {}, loadRootFolder, subfolder) {
159164
this.baseUrl = baseUrl
160165
this.options = options
161166
this.materialsInfo = {}
@@ -164,6 +169,7 @@ class OverrideMaterialCreator {
164169
this.nameLookup = {}
165170

166171
this.loadRootFolder = loadRootFolder
172+
this.subfolder = subfolder
167173

168174
this.crossOrigin = 'anonymous'
169175

@@ -283,16 +289,25 @@ class OverrideMaterialCreator {
283289
/**
284290
* Override for ComfyUI api url
285291
*/
286-
function resolveURL(baseUrl, url, loadRootFolder) {
292+
function resolveURL(baseUrl, url, loadRootFolder, subfolder) {
287293
if (typeof url !== 'string' || url === '') return ''
288294

295+
if (baseUrl.endsWith('/')) {
296+
baseUrl = baseUrl.slice(0, -1)
297+
}
298+
299+
if (!baseUrl.endsWith('api')) {
300+
baseUrl = '/api'
301+
}
302+
289303
baseUrl =
290304
baseUrl +
291305
'/view?filename=' +
292306
url +
293307
'&type=' +
294308
loadRootFolder +
295-
'&subfolder=3d'
309+
'&subfolder=' +
310+
subfolder
296311

297312
return baseUrl
298313
}
@@ -302,7 +317,12 @@ class OverrideMaterialCreator {
302317

303318
const texParams = scope.getTextureParams(value, params)
304319
const map = scope.loadTexture(
305-
resolveURL(scope.baseUrl, texParams.url, scope.loadRootFolder)
320+
resolveURL(
321+
scope.baseUrl,
322+
texParams.url,
323+
scope.loadRootFolder,
324+
scope.subfolder
325+
)
306326
)
307327

308328
map.repeat.copy(texParams.scale)

0 commit comments

Comments
 (0)