I'm facing a challenge with changing the material of a mesh using three.js's mesh loader. Although I can easily change the material within the loader, I encounter an issue where I can no longer access it from an external function. It seems to be a scoping problem that I'm struggling to resolve.
Here's an approach that works (but is not feasible for my use case):
var loader = new THREE.GLTFLoader();
loader.load('model.glb', function (gltf) {
scene.add(gltf.scene);
// Changing material below
var newMat = new THREE.TextureLoader().load(something.jpg);
gltf.scene.traverse(function (node) {
node.material = newMat;
});
});
However, this approach does not work. How can I go about fixing it?
var loader = new THREE.GLTFLoader();
loader.load('model.glb', function (gltf) {
scene.add(gltf.scene);
});
function textureSwap(){
var newMat = new THREE.TextureLoader().load(something.jpg);
gltf.scene.traverse(function (node) {
node.material = newMat;
});
}
textureSwap(); // Expected material change on call
The issue encountered is 'gltf is not defined'.