问题描述
使用three.js ObjLoader 的示例也使用MTLLoader,它为mtl 文件提供了单独的路径.
Examples of using the three.js ObjLoader also use the MTLLoader which is given a separate path to the mtl file.
https://github.com/mrdoob/three.js/blob/master/examples/webgl_loader_obj_mtl.html#L91
然而,obj 文件包含这样的 mtl 文件路径:
However, obj files include the mtl file path like this:
mtllib mymaterial.mtl
我是 IIIF 社区的一员,并且要求包含在 IIIF 清单中的资源具有描述您需要的所有内容的单一路径.是否有使用 obj 文件加载网格和材质的示例?
I am part of the IIIF community, and it is a requirement that resources included in a IIIF manifest have a single path that describes everything you need. Are there any examples of using an obj file to load the mesh and materials?
推荐答案
我认为主要的问题是当前编写的 objloader 需要手头有材料才能知道在加载 obj 文件时要应用哪些材料.
I think the primary issue is that the objloader as it's currently written needs to have materials in hand to know what materials to apply when the obj file is loaded.
将 mtl 文件应用于现有网格并非易事.
Applying an mtl file to an existing mesh is non-trivial.
另一个警告是,我似乎找不到 objloader 支持多个 mtl 文件的任何地方.
The other caveat is that I can't seem to find anywhere where objloader supports more than one mtl file.
结论是 objloader 尚未准备好或不支持您想要做的事情.但由于它是开源的并接受拉取请求,因此您可能会编写自己的 objloader.如果你谷歌你会发现有一个名为 objmtlloader.js 的尝试.但显然它有问题并被删除.
Conclusion is that objloader as is is not ready or supported for what you would like to do. But since it's open source and accepting pull reqs, you could potentially write an objloader of your own. If you google you will find there was an attempt called objmtlloader.js. But apparently it was problematic and removed.
如果您对每个 obj 一个 mtllib 没问题,但是您不想编写代码,则可以使用以下模式:
If you're ok with one mtllib per obj, and you don't want to write code however, you could use the following pattern:
var loader = new THREE.OBJLoader( manager );
var objpath = 'path/to/your/object.obj';
loader.load( objpath, function ( object ) {
loadMats(object, objpath);
},
function(eve){console.log("obj progress", eve)},
function(xhr){console.log("obj error", e)}
);
var loadMats = function(obj, objpath){
imgloader = new THREE.MTLLoader(manager);
imgloader.setPath(objpath.substring(0, objpath.lastIndexOf("/")+1));
imgloader.load( obj.materialLibraries[0],
function(materials){
var objLoader = new THREE.OBJLoader(manager);
objLoader.setMaterials(materials);
objLoader.load(objpath, function(object) {
scene.add(object);
},
function(eve){console.log("obj progress", eve)},
function(xhr){console.log("obj error", e)}
);
},
function(eve){console.log("mtl progress", eve)},
function(xhr){console.log("mtl error", e)}
);
}
此模式加载 obj,允许它发现材料文件名,然后加载它们.这种模式有问题,因为它必须加载 obj 两次,但我们可以假设第二次加载它缓存在浏览器 ram 中,应该只需要几毫秒.
This pattern loads the obj, allows it to discover the material file names, and then loads them. This pattern is problematic in that it must load the obj twice, but we can assume the second load it's cached in browser ram and should take just a few milliseconds.
这是一个废话答案,也许有人可以纠正我,但由于没有人回答,我要说这就是我将如何处理从 mtllib 行获取 mtl 文件,并在未来提供更好的支持.
This is a crap answer, and maybe someone can correct me, but since no one has answered, I am going to say this is how I would handle grabbing mtl files from the mtllib lines, and work on better support going forward.
说了这么多,知道了 mtl 规范,一个 obj 文件会使用多个 MTL 文件似乎很愚蠢,除非 MTL 在许多 obj 文件之间共享.
All that said, knowing the mtl spec, it seems rather silly that an obj file would use multiple MTL files unless the MTL were shared among many obj files.
这篇关于有没有办法使用 obj 文件中的路径加载 mtl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!