# Get the color from a texture with UV coordinates model

**URL:** <https://forum.playcanvas.com/t/get-the-color-from-a-texture-with-uv-coordinates-model/27466>\
**Category:** Help & Support\
**Tags:** scripting\
**Created:** [September 12, 2022, 6:48pm UTC](https://forum.playcanvas.com/t/get-the-color-from-a-texture-with-uv-coordinates-model/27466 "2022-09-12T18:48:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrkarate](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/mrkarate/32/9159_2.png) [@mrkarate](https://forum.playcanvas.com/u/mrkarate)\
**Post date:** [September 12, 2022, 6:48pm UTC](https://forum.playcanvas.com/t/get-the-color-from-a-texture-with-uv-coordinates-model/27466/1 "2022-09-12T18:48:14Z")

</div>

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!!

---

<div class="post-metadata">

**Author:** ![Leonidas](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leonidas/32/6495_2.png) [@Leonidas](https://forum.playcanvas.com/u/Leonidas)\
**Post date:** [September 13, 2022, 6:09pm UTC](https://forum.playcanvas.com/t/get-the-color-from-a-texture-with-uv-coordinates-model/27466/2 "2022-09-13T18:09:28Z")

</div>

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:

> [@Paint on Object at runtime(UV Painting)](https://forum.playcanvas.com/t/paint-on-object-at-runtime-uv-painting/12800/7):
>
> @Leonidas Hi, Leonidas smile As you mentioned here: I want to implement a shader like this but I am new to shader programming pensive,after a period of learning,I’v known how to change the color of specific pixel,but have no idea how to fill the whole object with this color.I guess it has something to do with the uv coordinates?but how?is this difficult to implement? my test project is here: [https://playcanvas.com/project/704805/overview/leran-shader](https://playcanvas.com/project/704805/overview/leran-shader) Can you give me some advice？thanks…

And also this character damage PlayCanvas example: [https://developer.playcanvas.com/en/tutorials/character-damage-demo/](https://developer.playcanvas.com/en/tutorials/character-damage-demo/)

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: [https://developer.playcanvas.com/en/tutorials/entity-picking/](https://developer.playcanvas.com/en/tutorials/entity-picking/)

Hope that helps.

---

<div class="post-metadata">

**Author:** ![mrkarate](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/mrkarate/32/9159_2.png) [@mrkarate](https://forum.playcanvas.com/u/mrkarate)\
**Post date:** [September 13, 2022, 10:36pm UTC](https://forum.playcanvas.com/t/get-the-color-from-a-texture-with-uv-coordinates-model/27466/3 "2022-09-13T22:36:42Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Leonidas](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leonidas/32/6495_2.png) [@Leonidas](https://forum.playcanvas.com/u/Leonidas)\
**Post date:** [September 13, 2022, 10:48pm UTC](https://forum.playcanvas.com/t/get-the-color-from-a-texture-with-uv-coordinates-model/27466/4 "2022-09-13T22:48:29Z")

</div>

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:

> [@mrkarate](#):
>
> get the uv from the model to get only the part from the texture from that mini cube.

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](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.

> [@mrkarate](#):
>
> 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.

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](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.

---

<div class="post-metadata">

**Author:** ![mrkarate](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/mrkarate/32/9159_2.png) [@mrkarate](https://forum.playcanvas.com/u/mrkarate)\
**Post date:** [September 13, 2022, 11:23pm UTC](https://forum.playcanvas.com/t/get-the-color-from-a-texture-with-uv-coordinates-model/27466/5 "2022-09-13T23:23:21Z")

</div>

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