简体   繁体   中英

threejs UV mapping in obj file

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!

在此处输入图片说明

In your mtl file change this line:

map_Kd uv_checker large.png

to

map_Kd large.png

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM