I've imported an OBJ model with the following code:
var loader = new THREE.OBJLoader();
loader.addEventListener('load', function (geometry) {
object = geometry.content;
scene.add(object);
});
loader.load('ship.obj');
It works fine, but whenever I try to add a material to it, it either has no effect or the model disappears. I assumed I could do it like this:
var ship = new THREE.Mesh(object, material);
But that doesn't work. does anyone know of a way to do this, or if it's even possible? I've tried using OBJMTLLoader too, but it just adds complication while still not allowing me to change the material.
EDIT: This answer is now outdated. Instead, see the answer by @mightypile.
Assuming you have properly defined material
, try this:
loader.addEventListener( 'load', function ( event ) {
var object = event.content;
object.traverse( function ( child ) {
if ( child instanceof THREE.Mesh ) {
child.material = material;
}
} );
scene.add( object );
});