本文介绍了three.js:MTLLoader不会加载tga纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我导出了一些.obj文件和相应的.mtl文件.纹理在同一文件夹中为.tga格式.

I have an export of some .obj files and corresponding .mtl files. Textures are in .tga format in the same folder.

这是我用来加载所有.obj的代码:

Here is the code I use to load all my .obj:

function addCar(modelPath, modelName) {
  var mtlLoader = new THREE.MTLLoader();
  mtlLoader.setPath(modelPath);

  for (var i = 0; i <= 2; i++) {
    loadObject(mtlLoader, modelPath, modelName, i);
  }
}

function loadObject(loader, path, name, i) {
  var objectName = name + i + '.obj';

  loader.load(name + i + '.mtl', function (materials) {
    materials.preload();

    var objLoader = new THREE.OBJLoader();
    objLoader.setMaterials(materials);
    objLoader.setPath(path);
    objLoader.load(objectName, function (object) {
      scene.add(object);
    }, function (xhr) {
      onProgress(xhr, objectName)
    }, onError);
  });
}

汽车已加载,但纹理未加载.它显示为全白色,并且控制台中没有错误.我尝试添加

The car is loaded, but not the textures. It appears all white, and there is no error in the console. I tried to add

  mtlLoader.setTexturePath(modelPath);

但它没有任何改变.

我也尝试添加

THREE.Loader.Handlers.add( /\.tga$/i, new THREE.TGALoader() );

,然后再调用addCar函数.当我这样做时,控制台中会出现一些警告,但纹理仍然没有出现.

before to call addCar function. When I do that, some warning appears in the console, but texture still doesn't appear.

在我看到的所有示例中,使用OBJLoader和MTLLoader时纹理都是自动加载的,但是我没有使用OBJLoader和MTLLoader和TGA纹理的示例.所以我想知道是否有什么办法可以使它起作用.

In all examples I saw, textures are loaded automatically when using OBJLoader and MTLLoader, but I didn't any example using OBJLoader and MTLLoader with TGA textures. So I'm wondering if there is something to do to get it worked.

任何帮助将不胜感激.

PS:文件(.obj.mtl.tga)是从3D max导出的.

PS: the files (.obj, .mtl and .tga) are exported from 3D max).

推荐答案

您必须使用TGALoader加载.tga文件.您可以在此处找到它.

You'll have to use the TGALoader to load .tga files. You can find it here.

有关示例,请参见 webgl_materials_texture_tga 源.

See the webgl_materials_texture_tga source for an example.

有没有理由使用.tga纹理而不是.png?在大多数情况下,它们会给出相同的结果,.png通常较小,并且可以由浏览器本地解码,因此您将获得更好的性能.

Is there a reason why you are using a .tga texture instead of .png? For most purposes they will give identical results, and .png will generally be smaller and can be natively decoded by the browser so you'll get better performance.

这篇关于three.js:MTLLoader不会加载tga纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 13:03