如何将纹理文件一一加载到MTL加载器

时间:2019-03-27 12:49:27

标签: javascript reactjs html5 three.js

我正在建立一个网站,以便用户云可以上传他们的3d模型。他们看到了。当我上传obj-mtl-texture时,强制我使用blob url。我拥有所有纹理blob网址,一个又一个,但是我没有父网址。我想将纹理加载到mtl加载器,而没有setTexturePath()函数怎么办。

我正在尝试从Three.js导出obj模型到gltf。

mtlLoader.setTexturePath(URL.createObjectURL(textureFolder));
    mtlLoader.load(URL.createObjectURL(mtlFile), (materials) => {
      materials.preload();
      objLoader.setMaterials(materials);
      objLoader.load(URL.createObjectURL(objFile),
        // called when resource is loaded
        (object) => {
          scene.add(object);
          exporter.parse(scene, (gltf) => {
            console.log(gltf);
            gltfFile = gltf2file(gltf, file.name);
            resolve(gltfFile);
          });
        },
        // called when loading is in progresses
        (xhr) => {
          console.log(`${(xhr.loaded / xhr.total) * 100}% loaded`);
        },
        // called when loading has errors
        (error) => {
          console.log(error);
          reject(new Error('Error occurred when converting file to gltf'));
        });
    },
    // called when loading is in progresses
    (xhr) => {
      console.log(`${(xhr.loaded / xhr.total) * 100}% loaded`);
    },
    // called when loading has errors
    (error) => {
      console.log('mtl error');
      console.log(error);
      reject(new Error('Error occurred when converting file to gltf'));
    });
  });

有没有人可以向我展示不使用setTexturePath()一次又一次加载纹理的方式

感谢前进

1 个答案:

答案 0 :(得分:0)

我通过为MTLLoader设置自定义loadingManager解决了问题

示例代码

const loadingManagerMTL = new THREE.LoadingManager();
  loadingManagerMTL.setURLModifier((url) => {
    let newUrl;
    for (let i = 0; i < textureFolder.files.length; i++) {
      if (url === textureFolder.files[i].name) {
        newUrl = URL.createObjectURL(textureFolder.files[i]);
      }
    }
    return newUrl;
  });

  const mtlLoader = new MTLLoader(loadingManagerMTL);

现在,您可以根据需要自定义加载管理器...

您可以使用另一个加载器的加载管理器来喜欢OBJLoader