简体   繁体   English

从对象(THREE.JS和GLTF)创建网格阵列

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

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[] ? 我将如何制作一系列children[] Each child will would then allow me to assign separate textures. 每个孩子然后将允许我分配单独的纹理。

I have made the model in Blender with separate meshes and materials. 我已经在Blender中使用单独的网格和材质制作了模型。

In THREE.JS, I want to be able to go: 我希望在THREE.JS中能够:

children[0].material.map = blueTexture;

children[1].material.map = greenTexture;

etc 等等

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM