ubernurbs
ubernurbs

Reputation: 43

Threejs: How to export an imported obj with Draco compressor

I only have limited javascript skills, i can get basic things working in threejs. Now I am struggeling with the draco exporter.

@ Threejs.org there is an example of the Draco exporter

This example uses a generated mesh and is working perfectly:

// export mesh

            var geometry = new THREE.TorusKnotBufferGeometry( 50, 15, 200, 30 );
            var material = new THREE.MeshPhongMaterial( { color: 0x00ff00 } );
            mesh = new THREE.Mesh( geometry, material );
            mesh.castShadow = true;
            mesh.position.y = 25;
            scene.add( mesh );

I can import the .obj and this also works fine

new OBJLoader()
.setPath( '../models/mymodel/' )
.load( 'mymodel.obj', function ( mesh ) {

    mesh.traverse( function ( child ) {

        if(child.name=='mymodel_part1'){                        
                    child.material = Material1;
        }
        if(child.name=='mymodel_part2'){                        
                    child.material = Material2;
        }
        if(child.name=='mymodel_part3'){                        
                    child.material = Material3;
        }

    } );

    scene.add( mesh );

} );

Now I want to export the mesh, and this does not work, I get a "TypeError: mesh is undefined".

So the Type is not correct, but what should it be, and how do I change it?

Upvotes: 1

Views: 602

Answers (1)

Mugen87
Mugen87

Reputation: 31026

The problem is that OBJLoader.load() does not produce a group but an instance of THREE.Group. Hence, you have to first determine the correct mesh in the group's descendants. This however depends on the asset since OBJ files can contain multiple mesh definitions. I've prepared a live example that demonstrates this workflow. The important code section is:

var loader = new OBJLoader();
loader.load( 'https://threejs.org/examples/models/obj/tree.obj', function ( obj ) {

    scene.add( obj );
    mesh = obj.children[ 0 ];

} );

https://jsfiddle.net/bquxjf28/2/

Upvotes: 1

Related Questions