# Accessing pixel values of Texture2D

**URL:** <https://forums.unrealengine.com/t/accessing-pixel-values-of-texture2d/279798>\
**Category:** Asset Creation\
**Tags:** unreal-engine, pixel, question\
**Created:** [April 6, 2014, 8:38pm UTC](https://forums.unrealengine.com/t/accessing-pixel-values-of-texture2d/279798 "2014-04-06T20:38:40Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![TaurusI76](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TaurusI76](https://forums.unrealengine.com/u/TaurusI76)\
**Post date:** [April 6, 2014, 8:38pm UTC](https://forums.unrealengine.com/t/accessing-pixel-values-of-texture2d/279798/1 "2014-04-06T20:38:40Z")

</div>

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.

Thanks!

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b65ae2a21](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b65ae2a21](https://forums.unrealengine.com/u/anonymous_user_b65ae2a21)\
**Post date:** [July 23, 2014, 7:16am UTC](https://forums.unrealengine.com/t/accessing-pixel-values-of-texture2d/279798/2 "2014-07-23T07:16:08Z")

</div>

_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:

```
FTexture2DMipMap* MyMipMap = &MyTexture2D->PlatformData->Mips[0];

```

With this, we can access the BulkData which contains the raw image data:

```
FByteBulkData* RawImageData = &MyMipMap->BulkData;

```

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:

```
FColor* FormatedImageData = static_cast<FColor*>( RawImageData->Lock( LOCK_READ_ONLY ) );

```

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:

```
uint8 PixelX = 5, PixelY = 10;
uint32 TextureWidth = MyMipMap->SizeX, TextureHeight = MyMipMap->SizeY;
FColor PixelColor;

if( PixelX >= 0 && PixelX < TextureWidth && PixelY >= 0 && PixelY < TextureHeight )
{
	PixelColor = FormatedImageData[PixelY * TextureWidth + PixelX];
}

```

Now, for demonstration purposes, I broke down each step. We could easily access and cast this in one line:

```
FColor* FormatedImageData = static_cast<FColor*>( MyTexture2D->PlatformData->Mips[0].BulkData.Lock( LOCK_READ_ONLY ) );

```

And now that we are done with everything, we need to unlock the memory again:

```
RawImageData->Unlock();

```

or

```
MyTexture2D->PlatformData->Mips[0].BulkData.Unlock();

```

* * *

Hope this helps.

---

<div class="post-metadata">

**Author:** ![Grogger](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Grogger](https://forums.unrealengine.com/u/Grogger)\
**Post date:** [January 22, 2015, 4:35pm UTC](https://forums.unrealengine.com/t/accessing-pixel-values-of-texture2d/279798/3 "2015-01-22T16:35:44Z")

</div>

I’ve tried this without success. A completely black image (32x32 DXT1) is returning values like:

```
(R=0,G=0,B=0,A=0)
(R=170,G=170,B=170,A=170)
(R=0,G=0,B=0,A=0)
(R=170,G=170,B=170,A=170)
etc...

```

Does the mip map raw data already account for texture pitch?

Edit: I just realized this post was old, hopefully you (or anyone) is still around to answer the question though.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_fb70b0ae1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_fb70b0ae1](https://forums.unrealengine.com/u/anonymous_user_fb70b0ae1)\
**Post date:** [January 28, 2015, 7:05pm UTC](https://forums.unrealengine.com/t/accessing-pixel-values-of-texture2d/279798/4 "2015-01-28T19:05:47Z")

</div>

The given solution seems to work fine if the following settings are used for the texture;

- Mip Gen Settings: NoMipmaps
- sRGB: false
- Compression Settings: TC Vector Displacementmap

[This article might also be of use.](https://wiki.unrealengine.com/Procedural_Materials)

---

<div class="post-metadata">

**Author:** ![PowerDesign](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@PowerDesign](https://forums.unrealengine.com/u/PowerDesign)\
**Post date:** [February 17, 2016, 6:03am UTC](https://forums.unrealengine.com/t/accessing-pixel-values-of-texture2d/279798/5 "2016-02-17T06:03:09Z")

</div>

It works for me. Is it possible to **sample the texture with bilinear filter**? I don’t want the exact pixel value, I need the interpolated value.

Does **locking/unlocking frequently affects performance?**

Thanks!

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_2e70cb911](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_2e70cb911](https://forums.unrealengine.com/u/anonymous_user_2e70cb911)\
**Post date:** [May 13, 2016, 5:08pm UTC](https://forums.unrealengine.com/t/accessing-pixel-values-of-texture2d/279798/6 "2016-05-13T17:08:02Z")

</div>

Can you answer this question in bluepprints, Thanks!

---

<div class="post-metadata">

**Author:** ![Dovlahkin](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Dovlahkin](https://forums.unrealengine.com/u/Dovlahkin)\
**Post date:** [July 30, 2016, 12:55pm UTC](https://forums.unrealengine.com/t/accessing-pixel-values-of-texture2d/279798/7 "2016-07-30T12:55:10Z")

</div>

Hey guys! I’ve done the exact same thing and my editor keeps crashing when I hit play. What I did was make a public Texture2D reference variable in my blueprint class created from my c++ class, asigned a image with NoMipmaps, sRGB: false and VectorDisplacementmap (RGBA8). I’ve then called this code via ReadPixel function I’ve exposed to ue4, but it crashes. What I’ve managed to do is locate that PlatformData-\>Mips[0] crashes the editor for some reason. Any ideas?

---

<div class="post-metadata">

**Author:** ![MaxPower42](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MaxPower42](https://forums.unrealengine.com/u/MaxPower42)\
**Post date:** [August 5, 2016, 5:45pm UTC](https://forums.unrealengine.com/t/accessing-pixel-values-of-texture2d/279798/8 "2016-08-05T17:45:30Z")

</div>

Hi. I used this code to read layer-info from landscape weight map textures.

But what if I want to modify the texture? I changed the lock mode to read/write and wrote colors to the pixel-array, but I didn’t see any effect afterwards ☹

---

<div class="post-metadata">

**Author:** ![MaxPower42](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MaxPower42](https://forums.unrealengine.com/u/MaxPower42)\
**Post date:** [September 6, 2016, 11:50pm UTC](https://forums.unrealengine.com/t/accessing-pixel-values-of-texture2d/279798/9 "2016-09-06T23:50:09Z")

</div>

Help please!!

I was able to dynamically change textures by procedurally editing their bulk-data, but the next time I restart my project/editor all the changes are reverted.

So how do I save the modified bulkdata to disk? I saved the texture asset after modifying it, but that’s not enough.

edit: actually, nevermind. I found the “persistent” data under UTexture2D::Source.

---

<div class="post-metadata">

**Author:** ![Teapot\_Creation](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Teapot\_Creation](https://forums.unrealengine.com/u/Teapot_Creation)\
**Post date:** [September 29, 2016, 2:55pm UTC](https://forums.unrealengine.com/t/accessing-pixel-values-of-texture2d/279798/10 "2016-09-29T14:55:01Z")

</div>

Hi, any idea about how to do this using blueprints ?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_e10a6464](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_e10a6464](https://forums.unrealengine.com/u/anonymous_user_e10a6464)\
**Post date:** [October 13, 2016, 6:35am UTC](https://forums.unrealengine.com/t/accessing-pixel-values-of-texture2d/279798/11 "2016-10-13T06:35:58Z")

</div>

What about on Android? I set texture settings to VectorDisplacementmap (RGBA8) and it works on WIndows desktop. However, it doesn’t work on Android. I think maybe that’s because UE4 on Android uses a different texture format maybe?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_82deb49f](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_82deb49f](https://forums.unrealengine.com/u/anonymous_user_82deb49f)\
**Post date:** [February 22, 2017, 8:39pm UTC](https://forums.unrealengine.com/t/accessing-pixel-values-of-texture2d/279798/12 "2017-02-22T20:39:19Z")

</div>

On the off chance anybody reads this: TC Vector won’t appear in your editor if you set the texture group to UI. Instead do-

Compression Settings: UserInterface2D (RGBA)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_9012b75d](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_9012b75d](https://forums.unrealengine.com/u/anonymous_user_9012b75d)\
**Post date:** [March 12, 2017, 9:59pm UTC](https://forums.unrealengine.com/t/accessing-pixel-values-of-texture2d/279798/13 "2017-03-12T21:59:08Z")

</div>

Thank you so much, the answer I needed in the smallest of corners.

---

<div class="post-metadata">

**Author:** ![invrsion](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@invrsion](https://forums.unrealengine.com/u/invrsion)\
**Post date:** [March 16, 2017, 1:51pm UTC](https://forums.unrealengine.com/t/accessing-pixel-values-of-texture2d/279798/14 "2017-03-16T13:51:47Z")

</div>

I’ve tried your method but my result is always the same color (205, 205, 205) for each pixel, that doesn’t match the visual content of the same texture applied to some mesh in game, have you any ideas?

Thanks.

---

<div class="post-metadata">

**Author:** ![resul4e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@resul4e](https://forums.unrealengine.com/u/resul4e)\
**Post date:** [March 21, 2017, 11:46am UTC](https://forums.unrealengine.com/t/accessing-pixel-values-of-texture2d/279798/15 "2017-03-21T11:46:23Z")

</div>

I’ve got the same issue as invrsion. Does anybody know how to fix this? Or did you fix it invrsion?

---

<div class="post-metadata">

**Author:** ![Two-faced](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Two-faced](https://forums.unrealengine.com/u/Two-faced)\
**Post date:** [March 23, 2017, 12:36pm UTC](https://forums.unrealengine.com/t/accessing-pixel-values-of-texture2d/279798/16 "2017-03-23T12:36:19Z")

</div>

Such a hot topic!

I’m trying to find a BP solution too, so there is a Image (Texture2D) to Bytes (PNG) node from Low Entry Extended Standart Library.

---

<div class="post-metadata">

**Author:** ![Two-faced](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Two-faced](https://forums.unrealengine.com/u/Two-faced)\
**Post date:** [March 24, 2017, 12:57pm UTC](https://forums.unrealengine.com/t/accessing-pixel-values-of-texture2d/279798/17 "2017-03-24T12:57:05Z")

</div>

And here is the answer: Victory Plugin.

---

<div class="post-metadata">

**Author:** ![renel\_degazon](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@renel\_degazon](https://forums.unrealengine.com/u/renel_degazon)\
**Post date:** [November 11, 2017, 8:26pm UTC](https://forums.unrealengine.com/t/accessing-pixel-values-of-texture2d/279798/18 "2017-11-11T20:26:13Z")

</div>

How would you go about getting the center of those pixels?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_8815b898](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_8815b898](https://forums.unrealengine.com/u/anonymous_user_8815b898)\
**Post date:** [December 7, 2017, 9:00pm UTC](https://forums.unrealengine.com/t/accessing-pixel-values-of-texture2d/279798/19 "2017-12-07T21:00:45Z")

</div>

Same issue here, I can’t UpdateTextureRegions also ☹

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_5917758f](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_5917758f](https://forums.unrealengine.com/u/anonymous_user_5917758f)\
**Post date:** [March 19, 2018, 10:51pm UTC](https://forums.unrealengine.com/t/accessing-pixel-values-of-texture2d/279798/20 "2018-03-19T22:51:59Z")

</div>

If it’s crashing for you, try changing ‘FByteBulkData’ to ‘FUntypedBulkData’. It worked for me!

Hope this helped someone!

[Next page](https://forums.unrealengine.com/t/accessing-pixel-values-of-texture2d/279798.md?page=2)
