0

I want to put a texture on my obj file, but I'm getting an error message. This is my code:

    new THREE.MTLLoader()
            .setPath( 'models/cool' )
                .load( 'CobbleStones.mtl', function ( mat ) {
                    mat.preload();

            var loader = new THREE.OBJLoader( loadingManager );

            loader.load( 'models/cool.obj', function ( object ) {
                object.traverse( function ( child ) {
                    if ( child instanceof THREE.Mesh ) {
                        child.material = gemBackMaterial;
                        child.material = material;

                        loader.setMaterials(mat);

                        var second = child.clone();
                        second.material = gemFrontMaterial;
                        var parent = new THREE.Group();
                        parent.add( second );
                        parent.add( child );
                        scene.add( parent );
                        objects.push( parent );
                    }
                } );
            } );
        });

The error message is:

GET http://localhost/alpha/Alpha/models/coolCobbleStones.mtl 404 (Not Found)

I would be very thankful if anybody could help me with my problem.

Boby
  • 77
  • 3
  • 10

1 Answers1

0

Your path is wrong. You're setting it to:

models/coolCobbleStones.mtl

But you need

models/cool/CobbleStones.mtl

Just add a / in .setPath( 'models/cool/' )

M -
  • 26,908
  • 11
  • 49
  • 81
  • Hello, thank you, I tried another mtl file and your solution worked, but there is still no texture on my obj file, do you know why? – Boby May 02 '18 at 17:23
  • Hard to tell with your code sample. It feels like `gemBackMaterial`, `material`, and `gemFrontMaterial` are coming out of nowhere. I don't know where theyr'e being declared, and the parameter `mat` that's being passed into the outermost function isn't being assigned to any mesh. – M - May 02 '18 at 17:38
  • Hello, Im working on something like a configurator and I need gemBackMaterial and gemFrontMaterial to change the color of the obj file on runtime. And mat is not assigned? I thought loader.setMaterials(mat) is enough, that's what Ive seen in other examples, do you know what I can improve there? – Boby May 04 '18 at 00:28
  • @Boby Yeah, it looks like you're calling `loader.setMaterials()` multiple times, since it's being triggered within the `object.traverse()` loop. I really don't know what else to tell you, since it all depends on the context in which your code is running, the assets it's loading, etc. My best advice is to follow the example as close as possible: https://github.com/mrdoob/three.js/blob/master/examples/webgl_loader_obj_mtl.html#L94 and once you have the loader working, then start modifying it to fit your specific needs. – M - May 04 '18 at 00:33