0

i'm trying to import an obj with OBJLoader but it isn't importing properly

The obj is like this - Obj img
Obj img

And it's importing this - Obj in three js
Obj in three js

What happens is that the whole obj isn't importing well.

What can i do about it ?

The code that i'm doing is

var objLoader = new THREE.OBJLoader();
        var mtlLoader = new THREE.MTLLoader();
            mtlLoader.setTexturePath("obj2/");
            mtlLoader.setPath(  "obj2/"  );
            mtlLoader.load( "Mules/Base_10.mtl", function( materials ) {
                materials.preload();
                objLoader.setMaterials( materials );
                objLoader.load( 'obj2/Mules/Base_10.obj', function ( object ) {

                        object.traverse( function ( child )
                        {
                            if ( child instanceof THREE.Mesh )
                            {
                                meshes.push(child);
                            }
                        });
                        var object = meshes[meshes.length-1];
                        object.position.y = -0.05;
                        object.position.x = 0;
                        object.position.z = 0;

                        object.name = "salto";
                        scene.add(object);
                    }, onProgress, onError );
            }); 

Thank you.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • You can see the diference between the image one and the image two. in image won i'm making a preview of the obj file, and in image two it is in the browser . sorry for the bad english, thank tou – Celso Madeira Aug 22 '18 at 11:42
  • 1
    Looking at your code, when an OBJ file is loaded, you collect all `THREE.Mesh` objects from it, and then you _only add the last mesh_ to the scene. Is that intentional? – Petr Broz Aug 22 '18 at 13:13
  • Well i adapted this code from [link](https://github.com/BrOrlandi/bb8) . I commented that part and it looks good now :) Thanks :D – Celso Madeira Aug 22 '18 at 13:40

1 Answers1

1

The problem is:

                object.traverse(...);
                var object = meshes[meshes.length-1]; //<-- you overriding the object, with the last mesh. 
                object.position.y = -0.05;
                object.position.x = 0;
                object.position.z = 0;

                object.name = "salto";
                scene.add(object); //<-- than you add the object to your scene.

do not override the object. Also you don't need to traverse through the objects, as you will add the whole thing to your scene. and you do nothing with your meshes anyway :)

so try this:

                object.position.y = -0.05;
                object.position.x = 0;
                object.position.z = 0;

                object.name = "salto";
                scene.add(object);