Is there a way to get the material of an object?

I’ve some models loaded with animation (just walking around) and collisions, my main character (which I control) has a script in which detects the collision of each object and shows a plane of the collision object has, but I don’t know if there is a way in which I can get the material o texture of that plane?

Hi @Axel_Saucedo ,

You can get the material of a specific mesh instance of a model this way:
this.entity.model.meshInstances[0].material

To get the texture/diffuse map you can do this:
this.entity.model.meshInstances[0].material.diffuseMap

This will get you the material of the first mesh instance of that model and you can loop through the meshinstances to get all the materials of that model.

Here is a simple example of switching materials:
https://developer.playcanvas.com/en/tutorials/switching-materials-at-runtime/

and in the script switching-materials.js, you can see how the materials are accessed and switched:
Line 27:
https://playcanvas.com/editor/code/437442?tabs=5625601

2 Likes