本文介绍了如何将材质分配给 ColladaLoader 或 OBJLoader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我浏览了文档和许多示例,但无法找到将材料分配给 Collada .dae 或 OBJLoader .obj 文件的正确语法.

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.

在创建 Mesh 并将加载器作为几何体和材料作为设置材料时,Json 文件似乎可以正常工作.

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

这样的代码有效

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);
});

这不起作用

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);
});

这不起作用

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);
});

我也试过 geometry.material = new THREE.MeshLambertMaterial({ color: 0xff0000});function(geometry) 里面没有成功.

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

.Dae 格式似乎直接保留了来自 Cinema 等 3D 程序的材料颜色.如何为这些装载机正确设置材料?如果这是可能的.

.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.

推荐答案

对于 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 );

} );

对于ColladaLoader,模式类似.

var loader = new THREE.ColladaLoader();

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

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

} );

three.js r.62

three.js r.62

这篇关于如何将材质分配给 ColladaLoader 或 OBJLoader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 13:27