Accessing pixel values of Texture2D
I was wondering if there is a way to read/write pixel values on a UTexture2D object (e.g. read values from a heightmap or draw something on a texture).
I've seen that there are FTexture2DMipMaps stored on the Texture that contain BulkData, which might be the pixel data of the texture. My idea was to get the first mip map and access its data somehow.
Is this even possible? Maybe somebody could give me some pointers in which direction to go.
asked Apr 06 '14 at 08:38 PM in C++ Programming
First you need to understand that a texture is normally, a sum of multiple images called MipMaps. MipMaps are down-scaled versions of your images, always in steps of power of 2, so the original image, is, say, 512x512 - this would be the MipMap "0", then the engine generates the MipMap "1" which is 256x256 and then MipMap "2" which is 128x128, this continues on down to 16x16 I think. The farther away the texture is rendered, the smaller version of the image is used. This means that you need to access the mipmap 0 of your texture.
First we need to access the mipmap 0 of the texture, it is important to only access the data we need through refrences or, like I did here, pointers, either will do. We do this to directly access the original data in the memory instead of copying that data into a local variable. This saves memory and speed:
With this, we can access the BulkData which contains the raw image data:
Considering this texture data is used in the rendering thread, we need to lock it to make sure nothing else can access it while we are using it, if we didn't, we could cause memory corruptions or crashes. So now we lock the image data which will also return a pointer to it - which we can then go ahead and cast the unformated data into an array of FColor's:
The FColor array is ofc 1 dimensional. So to access certain parts of the image, we need to use the width and height of the texture to calculate the position of the pixel we are wanting to lookup. Here's a small statement that does just that:
Now, for demonstration purposes, I broke down each step. We could easily access and cast this in one line:
And now that we are done with everything, we need to unlock the memory again:
Hope this helps.
Follow this question
Once you sign in you will be able to subscribe for any updates here