问题
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