# How to read RGBA from any texture?

**URL:** <https://forums.unrealengine.com/t/how-to-read-rgba-from-any-texture/1182578>\
**Category:** C++\
**Tags:** unreal-engine, question\
**Created:** [May 29, 2023, 5:18pm UTC](https://forums.unrealengine.com/t/how-to-read-rgba-from-any-texture/1182578 "2023-05-29T17:18:14Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlienRenders](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AlienRenders](https://forums.unrealengine.com/u/AlienRenders)\
**Post date:** [May 29, 2023, 5:18pm UTC](https://forums.unrealengine.com/t/how-to-read-rgba-from-any-texture/1182578/1 "2023-05-29T17:18:14Z")

</div>

Is there a way to read RGBA values from any Texture 2D? It seems this is something often requested, but the various methods posted so far just don’t work.

This returns null except when using certain very specific texture settings. And even when it works, the data is too small.  
RawImageData-\>Lock(LOCK\_READ\_ONLY)

RHILockTexture2D() doesn’t work. The data is always too small even on uncompressed textures.

What is the proper way to read RGBA values from a texture directly? I don’t care about speed and it can be editor only. I just want to write an editor tool that merges a bunch of tiles together into a single texture. Ironically, writing seems to be no problem.

---

<div class="post-metadata">

**Author:** ![ClockworkOcean](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/clockworkocean/32/1169999_2.png) [@ClockworkOcean](https://forums.unrealengine.com/u/ClockworkOcean)\
**Post date:** [May 29, 2023, 5:23pm UTC](https://forums.unrealengine.com/t/how-to-read-rgba-from-any-texture/1182578/2 "2023-05-29T17:23:37Z")

</div>

There’s a blueprint node, read texture 2D, or something like that…

---

<div class="post-metadata">

**Author:** ![AlienRenders](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AlienRenders](https://forums.unrealengine.com/u/AlienRenders)\
**Post date:** [May 29, 2023, 5:29pm UTC](https://forums.unrealengine.com/t/how-to-read-rgba-from-any-texture/1182578/3 "2023-05-29T17:29:12Z")

</div>

I can’t find any such node.

---

<div class="post-metadata">

**Author:** ![ClockworkOcean](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/clockworkocean/32/1169999_2.png) [@ClockworkOcean](https://forums.unrealengine.com/u/ClockworkOcean)\
**Post date:** [May 29, 2023, 6:32pm UTC](https://forums.unrealengine.com/t/how-to-read-rgba-from-any-texture/1182578/4 "2023-05-29T18:32:33Z")

</div>

I know there’s another node, but can’t find it right now. In blueprint, you have to go the long way around

> [@Reading texture color data using UV values](https://forums.unrealengine.com/t/reading-texture-color-data-using-uv-values/465888/15):
>
> Here is an solution: Notice you should have “Trace Complex” and “Support UV From Hit Result” check-boxes turned ON both! “Trace Complex” check box is in your Line Trace node. “Support UV From Hit Result” you can find in: Setting\>Physics\>Optimization\>Support UV From Hit Result.

---

<div class="post-metadata">

**Author:** ![AlienRenders](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AlienRenders](https://forums.unrealengine.com/u/AlienRenders)\
**Post date:** [May 29, 2023, 7:11pm UTC](https://forums.unrealengine.com/t/how-to-read-rgba-from-any-texture/1182578/5 "2023-05-29T19:11:51Z")

</div>

Yeah, I’m already rendering into a texture and doing ReadPixels. Problem I’m having now is that the output is blurry and I have no idea why. It shows up crisp when I first double click on the output texture. But as soon as I save, it gets blurry.

---

<div class="post-metadata">

**Author:** ![ClockworkOcean](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/clockworkocean/32/1169999_2.png) [@ClockworkOcean](https://forums.unrealengine.com/u/ClockworkOcean)\
**Post date:** [May 29, 2023, 7:12pm UTC](https://forums.unrealengine.com/t/how-to-read-rgba-from-any-texture/1182578/6 "2023-05-29T19:12:50Z")

</div>

Have you got nomips set?

---

<div class="post-metadata">

**Author:** ![AlienRenders](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AlienRenders](https://forums.unrealengine.com/u/AlienRenders)\
**Post date:** [May 29, 2023, 7:18pm UTC](https://forums.unrealengine.com/t/how-to-read-rgba-from-any-texture/1182578/7 "2023-05-29T19:18:03Z")

</div>

Makes no difference. I do use mips here though. The mip levels are way blurrier than they should be.

---

<div class="post-metadata">

**Author:** ![ClockworkOcean](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/clockworkocean/32/1169999_2.png) [@ClockworkOcean](https://forums.unrealengine.com/u/ClockworkOcean)\
**Post date:** [May 29, 2023, 7:23pm UTC](https://forums.unrealengine.com/t/how-to-read-rgba-from-any-texture/1182578/8 "2023-05-29T19:23:26Z")

</div>

And it’s the resolution you’re expecting?

---

<div class="post-metadata">

**Author:** ![AlienRenders](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AlienRenders](https://forums.unrealengine.com/u/AlienRenders)\
**Post date:** [May 29, 2023, 7:27pm UTC](https://forums.unrealengine.com/t/how-to-read-rgba-from-any-texture/1182578/9 "2023-05-29T19:27:32Z")

</div>

Yeah. It’s really weird. Sometimes when I load the texture up in the editor after it’s generated, it looks fine. But when I hit the save icon, it goes blurry.

---

<div class="post-metadata">

**Author:** ![AlienRenders](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AlienRenders](https://forums.unrealengine.com/u/AlienRenders)\
**Post date:** [May 29, 2023, 7:33pm UTC](https://forums.unrealengine.com/t/how-to-read-rgba-from-any-texture/1182578/10 "2023-05-29T19:33:54Z")

</div>

Here’s one run where one tile is the proper resolution, but the others are not.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/2/7/b271b9586ae7217e4f76e4be98a86161d89b537b.jpeg)

But when I save it, it changes to this:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/b/2/9b2a544927e4de5036275c28ab60d3edff4117df.jpeg)

The exact same code generates all the tiles. The material to render the texture uses LOD0. Oh wait… these can be streamed in, right? That’s likely the problem. Let me force them to load all LODs. Thing is I have like 50 textures. I don’t want to change them one by one.

---

<div class="post-metadata">

**Author:** ![ClockworkOcean](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/clockworkocean/32/1169999_2.png) [@ClockworkOcean](https://forums.unrealengine.com/u/ClockworkOcean)\
**Post date:** [May 29, 2023, 7:41pm UTC](https://forums.unrealengine.com/t/how-to-read-rgba-from-any-texture/1182578/11 "2023-05-29T19:41:11Z")

</div>

You might find it easier with a tool?

These are both good

> **[rdTexTools in Code Plugins - UE Marketplace](https://www.unrealengine.com/marketplace/en-US/product/rdtextools)**
>
> A UE Plugin providing tools for optimizing textures

> **[SHADERSOURCE - Texture Tools in Code Plugins - UE Marketplace](https://www.unrealengine.com/marketplace/en-US/product/shadersource-texture-tools)**
>
> Texture Tools is an editor plugin that provides expanded texture modification settings such as, Channel packing, Texture Filters, Noise Creation, and more!

---

<div class="post-metadata">

**Author:** ![AlienRenders](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AlienRenders](https://forums.unrealengine.com/u/AlienRenders)\
**Post date:** [May 29, 2023, 7:42pm UTC](https://forums.unrealengine.com/t/how-to-read-rgba-from-any-texture/1182578/12 "2023-05-29T19:42:13Z")

</div>

Yeah, it was the streaming functionality that was making it blurry. There’s an option in the settings to disable it. So I have to remember to disable streaming when generating these textures.

---

<div class="post-metadata">

**Author:** ![AlienRenders](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AlienRenders](https://forums.unrealengine.com/u/AlienRenders)\
**Post date:** [May 29, 2023, 7:45pm UTC](https://forums.unrealengine.com/t/how-to-read-rgba-from-any-texture/1182578/13 "2023-05-29T19:45:24Z")

</div>

The entire reason for having my own code to create the tiles is I have to see if I can write the mips myself including the highest resolution. If you just use tiles like that, you’ll get color bleeding from adjacent tiles. So I need to add a border. It’s similar to what virtual textures does, except the border is used for the exact opposite reason. In SVT, they copy neighbouring tiles over into the border. I don’t want that. I want to avoid going into the adjacent tiles so the border is just the outside pixels of each tile duplicated.

---

<div class="post-metadata">

**Author:** ![AlienRenders](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AlienRenders](https://forums.unrealengine.com/u/AlienRenders)\
**Post date:** [May 29, 2023, 7:54pm UTC](https://forums.unrealengine.com/t/how-to-read-rgba-from-any-texture/1182578/14 "2023-05-29T19:54:26Z")

</div>

I guess rendering to a render texture and doing read pixels works for now. Wish there was a read pixels for regular textures.

---

<div class="post-metadata">

**Author:** ![RecourseDesign](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/recoursedesign/32/1194503_2.png) [@RecourseDesign](https://forums.unrealengine.com/u/RecourseDesign)\
**Post date:** [May 29, 2023, 7:58pm UTC](https://forums.unrealengine.com/t/how-to-read-rgba-from-any-texture/1182578/15 "2023-05-29T19:58:05Z")

</div>

Hi AlienRenders,

I’m the author of rdTexTools - the way I read the pixels is:

```auto
uint8* data=(uint8*)myTexture->Source.LockMip(0);

```

That’s been working in UE4.25 and up.

---

<div class="post-metadata">

**Author:** ![AlienRenders](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AlienRenders](https://forums.unrealengine.com/u/AlienRenders)\
**Post date:** [May 29, 2023, 7:59pm UTC](https://forums.unrealengine.com/t/how-to-read-rgba-from-any-texture/1182578/16 "2023-05-29T19:59:35Z")

</div>

That doesn’t work for me. It’s null.

edit: Well, it works on _some_ textures. Like RGBA uncompressed. But not much else.

```auto
  const int SizeX = InTexture->GetSizeX();
  const int SizeY = InTexture->GetSizeY();

  const int sz = SizeX * SizeY;
  OutData.SetNum(sz);

  FTexture2DMipMap& MipMap = InTexture->GetPlatformData()->Mips[0];
  FByteBulkData* RawImageData = &MipMap.BulkData;
  FColor* FormatedImageData = static_cast<FColor*>(RawImageData->Lock(LOCK_READ_ONLY));

  for (size_t i = 0; i < sz; i++)
  {
    OutData[i] = FormatedImageData[i].R + (FormatedImageData[i].G << 8) + (FormatedImageData[i].B << 16);
  }
  RawImageData->Unlock();

```

This is what I was using, but the pointer is almost always null. I only use it to update my lookup texture, but that’s uncompressed.

And here’s another function I have that doesn’t work except for uncompressed.

```auto
const int SizeX = InTexture->GetSizeX();
const int SizeY = InTexture->GetSizeY();

const int sz = SizeX * SizeY;
OutData.SetNum(sz);

const FColor* RawImageData = reinterpret_cast<const FColor*>(InTexture->Source.LockMipReadOnly(0));
for (size_t i = 0; i < sz; i++)
{
  OutData[i] = RawImageData[i].R + (RawImageData[i].G << 8) + (RawImageData[i].B << 16);
}
RawImageData->Unlock();

```

---

<div class="post-metadata">

**Author:** ![RecourseDesign](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/recoursedesign/32/1194503_2.png) [@RecourseDesign](https://forums.unrealengine.com/u/RecourseDesign)\
**Post date:** [May 29, 2023, 8:08pm UTC](https://forums.unrealengine.com/t/how-to-read-rgba-from-any-texture/1182578/17 "2023-05-29T20:08:40Z")

</div>

Can you confirm that “InTexture” is valid? Like I said - the Source.LockMip(0) has worked 100% of the time in all test and live scenarios for quite some time.

---

<div class="post-metadata">

**Author:** ![AlienRenders](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AlienRenders](https://forums.unrealengine.com/u/AlienRenders)\
**Post date:** [May 29, 2023, 8:12pm UTC](https://forums.unrealengine.com/t/how-to-read-rgba-from-any-texture/1182578/18 "2023-05-29T20:12:55Z")

</div>

Yeah, InTexture is valid. It’s the same tile textures I’m using above. They’re assets. With dynamic textures, I have to call

```auto
  InTexture->UpdateResource();
  FTextureCompilingManager& CompilerManager = FTextureCompilingManager::Get();
  CompilerManager.FinishAllCompilation();

```

But it still doesn’t work unless it’s uncompressed.

If it’s out there, I’ve tried it. I also tried RHILockTexture2D(), but that has the problem that it gives you the compressed buffer directly, so I can’t use it unless I find a decompressor but that seems like a lot of work.

Hmmm… I wonder if the streaming functionality wasn’t interfering with it. Let me try it again with streaming disabled.

edit: Nope. Pointer is null.

---

<div class="post-metadata">

**Author:** ![RecourseDesign](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/recoursedesign/32/1194503_2.png) [@RecourseDesign](https://forums.unrealengine.com/u/RecourseDesign)\
**Post date:** [May 29, 2023, 8:17pm UTC](https://forums.unrealengine.com/t/how-to-read-rgba-from-any-texture/1182578/19 "2023-05-29T20:17:43Z")

</div>

Just a note about the compilation - for 5.1 and above, you’ll need to submit any “on the fly” shaders too:

```auto
#if ENGINE_MAJOR_VERSION>4 && ENGINE_MINOR_VERSION>0
	FWorldContext* worldctx=GEngine->GetWorldContextFromGameViewport(GEngine->GameViewport);
	if(worldctx) {
		UWorld* world=worldctx->World();
		if(world) {
			UMaterialInterface::SubmitRemainingJobsForWorld(world);
		}
	}
	FAssetCompilingManager::Get().FinishAllCompilation();
#else
	GShaderCompilingManager->FinishAllCompilation();
#endif

```

I don’t know if that will fix this though…

---

<div class="post-metadata">

**Author:** ![AlienRenders](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AlienRenders](https://forums.unrealengine.com/u/AlienRenders)\
**Post date:** [May 29, 2023, 8:25pm UTC](https://forums.unrealengine.com/t/how-to-read-rgba-from-any-texture/1182578/20 "2023-05-29T20:25:06Z")

</div>

I added that code and it compiled about 200 shaders. And the pointers weren’t null anymore, but the memory from the textures were too small. Some texture buffer look like they had repeated smaller tiles.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/8/d/08d6bc6d418619fa37fdfe3200eac0a8d54af2f0.jpeg)

Fairly certain if I ran this in debug mode, the debugger would catch memory overrun exceptions.

edit: That’s my bump map. Supposed to be completely black or white.

[Next page](https://forums.unrealengine.com/t/how-to-read-rgba-from-any-texture/1182578.md?page=2)
