# Giving a .glb a texture in code

**URL:** <https://discourse.threejs.org/t/giving-a-glb-a-texture-in-code/15071>\
**Category:** Questions\
**Tags:** typescript, gltf-loader, textures\
**Created:** [May 10, 2020, 1:55pm UTC](https://discourse.threejs.org/t/giving-a-glb-a-texture-in-code/15071 "2020-05-10T13:55:26Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![janizki](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@janizki](https://discourse.threejs.org/u/janizki)\
**Post date:** [May 15, 2020, 1:43am UTC](https://discourse.threejs.org/t/giving-a-glb-a-texture-in-code/15071/6 "2020-05-15T01:43:12Z")

</div>

By sheer dumb luck i managed to solve the issue and its texture is now working.

```
    var mat;
    var model;
    var mixer = null;
    var geo;
    const loader = new GLTFLoader();
    loader.load
    (
        '/models/cibus_ninja.glb',
        function (gltf) 
        {   
            model = gltf.scene;
    
            gltf.scene.traverse( function( object ) 
                 {            
                   if ((object instanceof THREE.Mesh))
                    { 
                          mat = object.material;
                          geo = object.geometry;
                          mat.map = texture;
                    }
                });

            scene.add(model);
            
            //Animation activation
            mixer = new THREE.AnimationMixer(model);
            mixer.clipAction(gltf.animations[4]).play();     
        },
        (xhr) => 
        {
            console.log((xhr.loaded / xhr.total * 100) + '% loaded')
        },
        (error) => 
        {
            console.log(error);
        }
    );

```

I genuinely have no idea why but creating the variables outside the loader for some reason managed to get the texture mapping working. If someone wants to explain why be free, but thanks for the help !@Mugen87  
Now to tackle how to add movement controls to this thing xD

---

_[View the full topic](https://discourse.threejs.org/t/giving-a-glb-a-texture-in-code/15071)._
