javascript Three.js中的对象遍历是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31899398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does object traverse mean in Three.js?
提问by mfaieghi
I am trying to upload obj files into a WebGL scene using Three.js. I saw some sample codes like the one below which works great, but I want to know what does the command
我正在尝试使用 Three.js 将 obj 文件上传到 WebGL 场景中。我看到了一些示例代码,如下所示,效果很好,但我想知道命令是什么
object.traverse();
do? What will happen if we don't do traversing? Thank you.
做?如果我们不做遍历会发生什么?谢谢你。
// prepare loader and load the model
var oLoader = new THREE.OBJLoader();
oLoader.load('models/chair.obj', function(object, materials) {
// var material = new THREE.MeshFaceMaterial(materials);
var material2 = new THREE.MeshLambertMaterial({ color: 0xa65e00 });
object.traverse( function(child) {
if (child instanceof THREE.Mesh) {
// apply custom material
child.material = material2;
// enable casting shadows
child.castShadow = true;
child.receiveShadow = true;
}
});
object.position.x = 0;
object.position.y = 0;
object.position.z = 0;
object.scale.set(1, 1, 1);
lesson6.scene.add(object);
});
回答by Robert Cepa
It is basically the iterator through your loaded object. You can pass the function to the traverse() function which will be called for every child of the object being traversed. If you call traverse() on scene. you traverse through the complete scene graph.
它基本上是通过您加载的对象的迭代器。您可以将该函数传递给 traverse() 函数,该函数将为正在遍历的对象的每个子对象调用。如果您在现场调用 traverse() 。您遍历完整的场景图。