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?
Each mesh has a material
property, and in material you can find map - which is probably the texture you are looking for (mind that map
is an instance of Texture, not an url or image data.)
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.
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.
Yes, but does the texture object have any reference to which image it came from?
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.
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.
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.
Thanks man that should do it