raylib»Forums
1 posts
Get pixel color from Texture2D
Edited by vushu on
Hi is it possible to get a pixel color from a Texture2D?

Thx :)

1 posts
Get pixel color from Texture2D
I'm still very new to Raylib, but as far as I can tell, you can to get the image from the texture, and then you can get the pixel data from the image, manipulate it, and upload the data to the texture again. If you're making frequent changes it may be worthwhile to keep the image at hand.

Alternatively, you can also load from a file into an image first, instead of a texture, and then create the texture from that image, and keep the image around if you need.

Relevant functions:
1
2
3
4
5
6
7
Image GetTextureData(Texture2D texture);                    // Get pixel data from GPU texture and return an Image
Color *GetImageData(Image image);                           // Get pixel data from image as a Color struct array
Vector4 *GetImageDataNormalized(Image image);               // Get pixel data from image as Vector4 array (float normalized)
void UpdateTexture(Texture2D texture, const void *pixels);  // Update GPU texture with new data

Image LoadImage(const char *fileName);                      // Load image from file into CPU memory (RAM)
Texture2D LoadTextureFromImage(Image image);                // Load texture from image data


And just by the way:
1
Image GetScreenData(void);                                  // Get pixel data from screen buffer and return an Image (screenshot)



Ray
62 posts / 1 project
I like videogames/gametools development.
Get pixel color from Texture2D
Yes, that's it.

You can use GetTextureData(texture) to get the Texture as Image and GetImageData() to get an array of Color from the Image, but actually, once you get the Image, all data is already available as image.data, just need to take into account the image.format to access that data.

In any case, retrieving Texture data is a costly process to be done in a frame-basis, why you need it? What is the use case?