Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
522 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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...