最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What does object traverse mean in Three.js? - Stack Overflow

programmeradmin4浏览0评论

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

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

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

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);
});
Share Improve this question asked Aug 8, 2015 at 22:59 mfaieghimfaieghi 6102 gold badges10 silver badges26 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 18

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.

发布评论

评论列表(0)

  1. 暂无评论