Find materials referencing a texture

Find all materials in the scene that reference texture A

There is no direct relation between texture and material. You can create a texture independent of a material. If you want to check the references you will need to do it on your own when setting the material’s texture. Otherwise you can iterate over all materials, and check if one of the material’s textures in a specific texture. The material itself has a function called “hasTexture” with which you can check if a specific texture is referenced by it.

2 Likes

Correct. I could add that I like to make use of the property ‘name’ when creating such texture.
I can next pick from all of its ‘name’. By creating this name including some sort of identifier in the name, I can next pick from all textures that include this identifier (searching through ‘includes’).

So, something like this:

var picttex= new BABYLON.Texture(“textures/mypicttex.jpg”, scene);
picttex.name = “mytex0_pict_custom”;

and then access through:

scene.textures.forEach((texture)=>{
if (texture.name && texture.name.includes(“pict”)){
texture.level = 2;}
});

or of course, through mesh or material (or a combination of all).

Else, you can also use ‘uniqueId’ on texture. Except of course in this case the ID is…well, unique :wink:

1 Like
scene.materials.forEach(m => m.hasTexture(textureA));

this should do it but it can be slow has it iterates over everything. I would recommend to avoid using this every frame.

1 Like