Create an Array of Meshes from Object (THREE.JS and GLTF)

风流意气都作罢 提交于 2020-01-25 06:17:48

问题


Below is my piece of code which loads a mesh onto the object. At the moment its traversing through the entire thing.

Current code:

var loader = new THREE.GLTFLoader();
            loader.load( '../gtf/Box.gltf', function ( gltf ) {
                model = gltf.scene;

                gltf.scene.traverse( function ( child ) {
                    if ( child.isMesh ) {
                        child.material.map = texture;
                    }
                } );

                scene.add( model);
            } );

How would I go about making an array of children[]? Each child will would then allow me to assign separate textures.

I have made the model in Blender with separate meshes and materials.

In THREE.JS, I want to be able to go:

children[0].material.map = blueTexture;

children[1].material.map = greenTexture;

etc


回答1:


If I understand your question correctly, you could do the following:

var loader = new THREE.GLTFLoader();
loader.load( '../gtf/Box.gltf', function ( gltf ) {
  model = gltf.scene;

  var children = []

  // Construct array of children during traversal of gltf object
  gltf.scene.traverse( function ( child ) {
      if ( child.isMesh ) {
          children.push(child)
      }
  } );

  // Assuming array length is 2 or more, and blueTexture/greenTexture are defined
  children[0].material.map = blueTexture;
  children[1].material.map = greenTexture;

  scene.add( model);
} );


来源:https://stackoverflow.com/questions/52068620/create-an-array-of-meshes-from-object-three-js-and-gltf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!