# GLTF runtime dynamic textures

**URL:** <https://discourse.threejs.org/t/gltf-runtime-dynamic-textures/18905>\
**Category:** Questions\
**Tags:** textures\
**Created:** [September 21, 2020, 8:01pm UTC](https://discourse.threejs.org/t/gltf-runtime-dynamic-textures/18905 "2020-09-21T20:01:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![hpenn](https://avatars.discourse-cdn.com/v4/letter/h/8baadc/32.png) [@hpenn](https://discourse.threejs.org/u/hpenn)\
**Post date:** [September 21, 2020, 8:01pm UTC](https://discourse.threejs.org/t/gltf-runtime-dynamic-textures/18905/1 "2020-09-21T20:01:33Z")

</div>

I’m trying to change textures at runtime. I exported my models from blender to GLTF with seperate textures and binary files. How does GLTF encode which mesh the textures belong to?

---

<div class="post-metadata">

**Author:** ![mjurczyk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mjurczyk/32/10995_2.png) [@mjurczyk](https://discourse.threejs.org/u/mjurczyk)\
**Post date:** [September 21, 2020, 8:55pm UTC](https://discourse.threejs.org/t/gltf-runtime-dynamic-textures/18905/2 "2020-09-21T20:55:20Z")

</div>

Each mesh has a `material` property, and in material you can find [map](https://threejs.org/docs/#api/en/materials/MeshBasicMaterial.map) - which is probably the texture you are looking for (mind that `map` is an instance of [Texture](https://threejs.org/docs/#api/en/textures/Texture), not an url or image data.)

---

<div class="post-metadata">

**Author:** ![donmccurdy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/donmccurdy/32/33209_2.png) [@donmccurdy](https://discourse.threejs.org/u/donmccurdy)\
**Post date:** [September 21, 2020, 10:44pm UTC](https://discourse.threejs.org/t/gltf-runtime-dynamic-textures/18905/3 "2020-09-21T22:44:50Z")

</div>

Note that there’s nothing glTF-specific about changing the textures on a material: All of the three.js documentation about Scenes, Meshes, and Materials can be used regardless of the model format. THREE.TextureLoader can be used to get new textures from a URL.

---

<div class="post-metadata">

**Author:** ![hpenn](https://avatars.discourse-cdn.com/v4/letter/h/8baadc/32.png) [@hpenn](https://discourse.threejs.org/u/hpenn)\
**Post date:** [September 21, 2020, 10:59pm UTC](https://discourse.threejs.org/t/gltf-runtime-dynamic-textures/18905/4 "2020-09-21T22:59:29Z")

</div>

Thanks for your reply. So, I have loaded a texture into a material and applied that material to a mesh object. The problem is that I had to apply the texture to each mesh because I don’t know how to identify which texture belongs to which mesh. I just traversed the scene and set the material for each mesh. Which is wrong.

---

<div class="post-metadata">

**Author:** ![hpenn](https://avatars.discourse-cdn.com/v4/letter/h/8baadc/32.png) [@hpenn](https://discourse.threejs.org/u/hpenn)\
**Post date:** [September 21, 2020, 11:00pm UTC](https://discourse.threejs.org/t/gltf-runtime-dynamic-textures/18905/5 "2020-09-21T23:00:02Z")

</div>

Yes, but does the texture object have any reference to which image it came from?

---

<div class="post-metadata">

**Author:** ![donmccurdy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/donmccurdy/32/33209_2.png) [@donmccurdy](https://discourse.threejs.org/u/donmccurdy)\
**Post date:** [September 21, 2020, 11:16pm UTC](https://discourse.threejs.org/t/gltf-runtime-dynamic-textures/18905/6 "2020-09-21T23:16:40Z")

</div>

The Texture does not keep any list of which Materials it was originally attached to — if you need that, you would want to store it before you change the texture out.

Note that if you enable the “Custom Properties” option when exporting from Blender, any key/value data you put onto objects should be preserved as `object.userData` in three.js. This could be helpful for sending information from Blender to your code in three.js, e.g. to store a list of texture names to (later) download and attach to that object.

---

<div class="post-metadata">

**Author:** ![hpenn](https://avatars.discourse-cdn.com/v4/letter/h/8baadc/32.png) [@hpenn](https://discourse.threejs.org/u/hpenn)\
**Post date:** [September 21, 2020, 11:59pm UTC](https://discourse.threejs.org/t/gltf-runtime-dynamic-textures/18905/7 "2020-09-21T23:59:33Z")

</div>

That’s good to know. I’m new to all of this but my assumption was that somehow the glTF file encoded this information that I’m after (which textures belong to which meshes). I just can’t find anywhere in the spec that says how.

---

<div class="post-metadata">

**Author:** ![donmccurdy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/donmccurdy/32/33209_2.png) [@donmccurdy](https://discourse.threejs.org/u/donmccurdy)\
**Post date:** [September 22, 2020, 12:09am UTC](https://discourse.threejs.org/t/gltf-runtime-dynamic-textures/18905/8 "2020-09-22T00:09:29Z")

</div>

The only association in what GLTFLoader returns is what three.js’ API has:

- mesh.material (Mesh -\> Material reference)
- material.map, material.roughnessMap, etc… (Material -\> Texture reference)

The texture does not have references to meshes, you’ll need to create that from the information above.

The glTF spec works the same way: meshes have materials, and materials have textures. To get a list of all meshes that have materials using a particular texture, you have to work backwards.

---

<div class="post-metadata">

**Author:** ![hpenn](https://avatars.discourse-cdn.com/v4/letter/h/8baadc/32.png) [@hpenn](https://discourse.threejs.org/u/hpenn)\
**Post date:** [September 23, 2020, 3:28am UTC](https://discourse.threejs.org/t/gltf-runtime-dynamic-textures/18905/9 "2020-09-23T03:28:01Z")

</div>

Thanks man that should do it
