Cloning a material and changing the texture does create n textures of cloned? Why?

Dears,
I never paid all too much attention to it until today. I have a material with a baseTexture.
I next clone this material n times. For each, I set a new baseTexture. Next thing that happens when I load the materials, I have n (=cloned) instances of the original baseTexture. Sounds a bit weird since I changed this baseTexture to a new one for my clones. Now I have both. Say I make 10 clones, I have 11 instances of the original baseTexture and 1 for each new baseTexture I assigned for my clone.
Why is that and any way to avoid it (apart from the trick of searching through all textures and remove all duplicates)? Thanks,

Reassigning a texture doesn’t mean that the old texture is disposed. If you set a new texture, you need to dispose the old one, otherwise you will have the old textures and the new one in memory.

I guess I got this part but I still don’t get the logic behind this when cloning a material. Thing is if I clone the material and assign a new (say) baseTexture, all information I have given at the origin for this baseTexture is removed. Means I need to enter all of it again because I change just the image/texture.
So, I guess my question is: why doesn’t it do the same for the image/texture?

Edit: I suppose the solution would be that the material to be cloned should not include the textures that will next be changed in clones. Obviously, this way it won’t duplicate and since the parameters of a replaced texture (such as scale, offset, level,etc) are not kept anyways when setting a new texture, it won’t make any difference.

When cloning any object you get everything cloned together with it. Outside of the clone function you can define whether or not classes included in this object (for example, a texture in a material) will be cloned as well or not.
So the current behavior is this:
You clone a material, which in turn clones the texture (but reuses the URL, so the image is not loaded twice). You then set a new texture, replacing the cloned texture. and then you need to dispose the cloned texture. I guess what you want is the ability to maintain the texture object, but change the actual image that is being displayed. Or am I misunderstanding? (and sorry if I am :slight_smile: )

Yes. It would have made the cloning of materials a greater advantage, I believe. But I guess I can deal without it. Thanks for the insight and reply and have a great day :sunglasses:

1 Like

A texture has an updateUrl function, you can use it to update the URL of the image you are loading without changing any other parameters on the texture. so instead of cloning or recreating, you can just update the URL :slight_smile:

1 Like

oh, yes. Silly me forgot about that. That should work. Thank you :hugs:

Edit: Confirmed. This topic is a non-topic. My apologies.

1 Like