[SOLVED] Fully load material textures before cloning

Hey Guys,

In my project i need to clone the same object a lot of times and then change their individual diffuse and emissiv textures. Other textures like lightmaps should stay the same.
Problem is, when i clone and assign my material (so the instances/clones dont share the same mat anymore) the new materials lose their other maps.
I’m pretty sure the reason for this is that the textures aren’t fully loaded yet and the material is using placeholders to function.

There have been topics in the past, but i just can’t wrap my head around their solutions or they arent applicable in my case.
All i want to do is:

  1. find out which textures are needed
  2. load them
  3. clone the material

Also, please understand i cant use “FindAssetByName” because of internal reason and i cant preload the textures!

here’s a repro i made (it just clones a material and assigns it, wherein the diffuse texture gets lost):
https://playcanvas.com/editor/scene/1408289

=> Object with normal material:

=> Object with cloned material:

Thanks :slight_smile:

Threads that could be helpful:

If you know what the materials and textures are ahead of time at Editor time, you can tag the assets.

You can get all the assets via a tag query through the asset registry and load them/check if they are loaded before you do something in the scene

Thanks for the reply!
But unfortunately we cant say WHICH objects and materials will be used. That’s decided by the user at runtime.
We have roughly 70 different objects and are still adding new ones, i think this solution just isnt suitable :confused:

There is this private property on each material that I think you can access to find all referenced texture maps on your own

I am not fully sure if it’s always and correctly populated, but from a quick test it seems so:

material._assetReferences;

image

That just seems to return undefined in my repro ._.
https://playcanvas.com/editor/scene/1408289

I think you need to try that on the material, not on the mesh instance:

console.log(sourceMaterial._assetReferences);
1 Like

Oh yeah, my bad ^^’
Thanks for the Hint i’ll check it out!

1 Like

As far as i can tell after a little testing, thats exactly what i needed!
Thank you all :smiley:

1 Like

In this case, you can map the option to a tag. Eg they select the button to use pattern A, all the textures/materials/assets related to pattern A can be tagged as such and then checked at runtime

You can also multi-select assets to tag multiple at once

Please note the method above is private API and may break in any future release of the engine

1 Like

Aw Bummer, that would’ve been such a good solution.
I’ll look into the tags again and try some things