4

I have a model loaded using THREE.OBJMTLLoader.

var loader = new THREE.OBJMTLLoader();
loader.addEventListener('load', function(event) {
    var mesh = event.content;
    scene.add(mesh);
});
loader.load('model/machine.obj', 'model/machine.mtl');

I need to apply a vertex and fragment shader to this model. How to do this?

2 Answers 2

5
+50

In addition to Gero3's answer, this would ensure that all the meshes in the content will get the right material:

var material = new THREE.ShaderMaterial( {

    uniforms: shader.uniforms,
    fragmentShader: shader.fragmentShader,
    vertexShader: shader.vertexShader

} );

var object = event.content;
object.traverse( function ( child ) {

    if ( child instanceof THREE.Mesh ) {

        child.material = material;

    }

} );
scene.add( object );
Sign up to request clarification or add additional context in comments.

2 Comments

It works! Glad to have this question answered by THREE.js's author! =D
Hi @mrdoob it says shader is not defined
0

You need a shadermaterial for adding a vertex and fragment shader.

            var material = new THREE.ShaderMaterial( {

                fragmentShader: shader.fragmentShader,
                vertexShader: shader.vertexShader,
                uniforms: shader.uniforms

            } ),

1 Comment

I know this. It works for a box or sphere but for model, event.content don't have a material and if set the shader material to it, there's still nothing changed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.