Is it possible to change this at runtime?

Ok, I have a dice model with a material. I have my UV mapping setup to allow me to assign a material and texture to each face. This is working great for me.

However, my issue is I want to change the texture of one or more of the sides at runtime. Is it possible? If so how? I’m lost in the documentation and just can’t figure it out. I can change the texture0 in the shader but that applies to the entire dice. I can’t find a way to just change a single face.

You can create a mechanic like this. There are different ways to do things as always. You can create a setup where you have a single model and single set of UV’s however this would mean you need to swap your information in the texture via a render target technique where you update the rt after you change parts of the captured content. This is kinda complicated to setup and maintain. I think a much easier way to go about it would be to use a model with 6 materials as you show in the image however to swap a texture in the model you need to use API reference (model) textureN property and currently it has no way of knowing what material to apply that to so it swaps all materials using the textureN and not what we want. So you can simplify even more and just split the sides as separate models and then apply different materials for each then you can swap the textures per side as you wish for the effect I believe you are looking for.

here’s what it looks like in action.

heres a link to a sample project showing how it works:

Hope this helps cheers~

3 Likes

Thank you. Yes I already was going down the multiple material path but can’t get the syntax correct. I’ll look at the sample you provided… thanks again

BTW: if you have a buy me a beer/coffee or patreon site I’d gladly buy you a beer for your work. This has been killing me and your example is clearly what I need.

@britzl Can this be added to the samples on the defold site?

Ok it appears you use a new model for each side. I was hoping to just change a texture or material, but if this is the best way I’ll go this route. the reason why is I have some more complicated shapes where having a model for each side isn’t ideal as I have to assemble it. But if I could just change the texture on the material it would be easier (I think).

See I updated the material so I have a dedicated material per side. But I have issues trying to access the tex0 of each material and change it out.

A trick to make it easy is to make sure every piece shares the same pivot position before exporting. When you bring the exported mesh into defold it will assemble together without any work.

Yea this would be ideal, there is one thing in the way of making this happen and we may need to create a github feature request for it. So #textureN Used for getting/setting model texture for unit 0-7 and is used like so "texture0" , "texture1" etc. This is the way we change textures which was fine except now that the model component can have more than one material it applies to all materials being used. The textureN should be unique slot names (which you would set in each material) or the api for textureN needs to take into account the material you are wanting to use the property on. This would be a feature that would need to be fleshed out but it makes since to have a feature for this as textureN becomes less useful for multi-material models as of now.

1 Like

Ah, ok, so the issue is when I change the texture it changes all the textures so I end up with all sides being the same. At least that’s what I keep seeing. So I’ll ditch this for now and move to the meshes.

I’m not a guru at blender, but I got this far and remapped my UVs to get me this far. So I’ll try the pviot trick you mentioned.

1 Like

If you need help slicing up some more complicated die let me know I wouldn’t mind helping you out with that. Or any blender questions shoot them my way , cheers.

2 Likes

I’ll do that. Thank you so much for helping.

Here’s my github request. I hope I captured it correctly.
Ability to change textures and materials at runtime when a model has more than one UV mapping · Issue #9774 · defold/defold

Hey one last time thank you. I am apparently better at blender than I thought. It’s actually not hard to create separate .glb files for each face. Key is to delete all the faces you don’t want to export. Then export it. Undo. Then select all the other faces you don’t want. Export. Undo…

Repeat until the entire object is exported. Then using the game object layout you have in your github it all comes together.