Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
515 views
in Technique[技术] by (71.8m points)

javascript - How to assign a material to ColladaLoader or OBJLoader

I have looked through the documentation as well as many examples and wasn't able to find correct syntax for assigning a material to either a Collada .dae or OBJLoader .obj file.

Json files seem to properly work when creating a Mesh and having the loader as geometry and material as the set material.

Code like this works

var loader = new THREE.JSONLoader();
loader.load( "modelPath.js", function(geometry){
  var material = new THREE.MeshLambertMaterial({color: 0x666666});
  mesh = new THREE.Mesh(geometry, material);
  scene.add(mesh);
});

This does not work

var loader = new THREE.ColladaLoader();
loader.load( "modelPath.dae", function(geometry){
  var material = new THREE.MeshLambertMaterial({color: 0x666666});
  mesh = new THREE.Mesh(geometry, material);
  scene.add(mesh);
});

And this does not work

var loader = new THREE.OBJLoader();
loader.load( "modelPath.obj", function(geometry){
  var material = new THREE.MeshLambertMaterial({color: 0x666666});
  mesh = new THREE.Mesh(geometry, material);
  scene.add(mesh);
});

I have also tried geometry.material = new THREE.MeshLambertMaterial({ color: 0xff0000}); inside function(geometry) without success.

.Dae format seems to keep material color directly from 3D programs like Cinema. How can I set a material to these loaders properly? If this is even possible.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Follow this pattern for OBJLoader:

var loader = new THREE.OBJLoader();

loader.load( 'model.obj', function ( object ) {

    var material = new THREE.MeshLambertMaterial( { color: 0x666666 } );

    object.traverse( function ( child ) {

        if ( child instanceof THREE.Mesh ) {

            child.material = material;

        }

    } );

    scene.add( object );

} );

For ColladaLoader, the pattern is similar.

var loader = new THREE.ColladaLoader();

loader.load( 'model.dae', function ( collada ) {

    var dae = collada.scene;
    scene.add( dae );

} );

three.js r.62


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...