Get the color from a texture with UV coordinates model

Hi!
I have a model, where I assign only one single texture, so all meshInstances from the model have only one material with this texture, but every meshInstance show a different uv coordinate from the texture.
So, what I need to figure out, is how I can access to a pixel from each uv coordinate from each meshinstance, and get the color of that pixel? it’s possible??

thanks!!

Hi @mrkarate,

Just to understand correctly, how are you going to select that pixel? Are you talking about doing raycasting to find a point on the model and from there translate that to a UV coordinate to get the pixel color?

There is this example (not working on current PlayCanvas Engine, you will have to run it using an earlier version), by @FBplus:

And also this character damage PlayCanvas example: Character Damage Demo | Learn PlayCanvas

Both example do partially what you are looking for, that is to convert a world point to a UV coordinate. They paint that to a render target to apply color/damage to the model. I’m not sure if that’s useful for you here, just sharing it with you.

On the other hand if you are looking to get the color of a selected (e.g. clicked) pixel, then you can easily do that by rendering the active camera to a render target and then reading that render target at the exact screen coordinates you clicked. That will give you the pixel color.

The PlayCanvas picker does something similar to find the selected entity: Entity Picking | Learn PlayCanvas

Hope that helps.

1 Like

Hi @Leonidas, I just check that two projects, but I can confirm there’s only work with 1.35 playcanvas engine to, not with the current ones above 1.4.

Anyway, I see on this examples, this is more complex than what I really need. If you can point me in the right direction, I think what I need to do is more simple, here’s what exactly I need to do on playcanvas:

I have a model, let’s think a big cube, with a lot of mini small cubes with same size, each mini cube is a meshinstance from the big cube, and have a different uv, so If assign the same texture to a material, and assign to all the meshinstances, I will see every cube with a different uv position of the texture.

I don’t have to raycast to a specific mesh uv, that’s why I think this should be as complicated than this examples, I’m using my big cube with hierarchy, so every meshinstance is a separate entity with a render component, so every single mini cube entity has their own box collider, and when I raycast it, I know inmediatly the specific entity. So in this part, knowing the entity, and accessing to the render component, I can access to the material, and get the texture, but the complete texture, so what I need to know is:

  • get the uv from the model to get only the part from the texture from that mini cube.
  • get the color from that part of the texture, I always will get only one color from this mini cubes, so this mini cubes are working like a pixel, so I don’t need nothing to check and get more colors from this part of the texture from the mini cube.

and that’s all, I just need finally that colors, to create a equivalent big cube, but instead of using textures, I will create using material colors.
Hope I’m explain well, also what you mention about the render target pixel I think is the way to go, will check it!
thanks again leonidas!

I see, yes I now better understand what you need. I don’t have though an exact example to point you to, but in general it will involve:

You can read the UVs of the model using the Mesh API and the getUVs() method:

https://developer.playcanvas.com/api/pc.Mesh.html#getUvs

Since all vertices of the mini cube have the exact same coordinates, you can just read the values of the first vertex.

After you have the coordinates, you just need to read the pixels of your texture. You can find a number of topics in the forums about this:

https://forum.playcanvas.com/search?q=read%20pixels%20texture

The UV coordinates you will get are normalized from 0 - 1 on each axis. And they will give you the exact pixel coordinates after multiplied with the width and height. Then from the pixels colors list, you can find the exact pixel color you are looking for.

Hope that helps.

2 Likes

will test with getUVs and check how read the pixels from the texture! hope this will enougth to success!!
let you know!! thanks @Leonidas!!,

1 Like