I applied a simple UV mapping for a STL model on a single face, this is the result:
Now I export the .obj
and .mtl
files from blender. Here you can download them if you want to. When I load this obj
and mtl
files using OBJLoader
and MTLLoader
the result is the following:
Why is the mapping not the same? I though three.js
could read the UV mapping from files. Is there any other way I can export the UV map to the model or I'm doing something wrong?
This is the code I use to load the obj
and mtl
files:
this.loadOBJ = function (baseURL, objFile, mtlFile, type) {
return new Promise(function (resolve, reject) {
console.log("Adding OBJ file '" + objFile + "' with MTL file '" + mtlFile + "'");
scope.clearScene();
// OBJ file loader
function loadOBJ(materials) {
var objLoader = new THREE.OBJLoader();
if (materials) {
objLoader.setMaterials(materials);
}
objLoader.setPath(baseURL);
objLoader.load(objFile, function (object) {
loadedMesh = object.children[0];
scene.add(loadedMesh);
centerCamera(loadedMesh);
render();
resolve();
}, function (xhr) {
if (xhr.lengthComputable) {
var percentComplete = xhr.loaded / xhr.total * 100;
console.log("Loading OBJ file: " + Math.round(percentComplete, 2) + '% downloaded');
}
}, function (xhr) {
console.log("Error loading OBJ: " + JSON.stringify(xhr));
reject();
});
};
if (!mtlFile) {
console.log("No MTL file specified, just loading OBJ");
loadOBJ();
}
else {
// Try to load the materials file first
var mtlLoader = new THREE.MTLLoader();
mtlLoader.setPath(baseURL);
mtlLoader.load(mtlFile, function (materials) {
// File loaded, load the OBJ file now
materials.preload();
loadOBJ(materials);
}, function (xhr) {
if (xhr.lengthComputable) {
var percentComplete = xhr.loaded / xhr.total * 100;
console.log("Loading MTL file: " + Math.round(percentComplete, 2) + '% downloaded');
}
}, function (xhr) {
// We couldn't load the MTL file, load the OBJ anyway
console.log("Error loading MTL file: " + JSON.stringify(xhr) + ". Will load only OBJ file");
loadOBJ();
});
}
});
}
Well it was just a Blender export problem. I just exported my model with this settings and it worked perfectly!