# Pixel search on a TextureRenderTarget

**URL:** <https://forums.unrealengine.com/t/pixel-search-on-a-texturerendertarget/694716>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [November 20, 2022, 12:00am UTC](https://forums.unrealengine.com/t/pixel-search-on-a-texturerendertarget/694716 "2022-11-20T00:00:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![guitou80](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@guitou80](https://forums.unrealengine.com/u/guitou80)\
**Post date:** [November 20, 2022, 12:00am UTC](https://forums.unrealengine.com/t/pixel-search-on-a-texturerendertarget/694716/1 "2022-11-20T00:00:53Z")

</div>

Hi all,

I implemented this c++ function to check if at least one pixel of this color is on the TextureRenderTarget :

```auto
bool ASceneCaptureManager::ReadTextureRenderTargetPixels(UTextureRenderTarget2D * TextureRenderTarget, FColor TestColor) { 

	if (TextureRenderTarget == nullptr) { return false; }

	TArray<FColor> l_ColorsPixelsArray;
	FColor l_PixelColor;
	FRenderTarget *l_RenderTarget = TextureRenderTarget->GameThread_GetRenderTargetResource();
	l_RenderTarget->ReadPixels(l_ColorsPixelsArray);

	for (int32 i = 0; i < TextureRenderTarget->SizeX; i++) {
		for (int32 j = 0; j < TextureRenderTarget->SizeY; j++) {

			l_PixelColor = l_ColorsPixelsArray[i + j * TextureRenderTarget->SizeX];

			if (l_PixelColor.R == TestColor.R && l_PixelColor.G == TestColor.G && l_PixelColor.B == TestColor.B) {
				return true;
			}
		}

	}

	return false;

}

```

Here is the Blurprint :

 ![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/5/c/d5cf92611c5fd40d829dd8b0f6ab7eacd8f5977e.jpeg)

The CaptureScene function calls USceneCaptureComponent2D-\>CaptureSceneDeferred();

RenderTarget texture size is 512 x 512

The problem is that this function returns true 20% of the time when it should return true everytime (after each call i opened the RenderTarget texture and there was pixels of this color).

Do you have any idea how to solve this please ?

---

<div class="post-metadata">

**Author:** ![jwatte](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jwatte/32/744941_2.png) [@jwatte](https://forums.unrealengine.com/u/jwatte)\
**Post date:** [November 20, 2022, 12:30am UTC](https://forums.unrealengine.com/t/pixel-search-on-a-texturerendertarget/694716/2 "2022-11-20T00:30:32Z")

</div>

Are you sure that you’re rendering a perfect, 100% red, that’s not tone mapped or lit or fogged or shaded?  
You could test whether it matches the color with a little slop through something like:

```auto
    int difference = Abs(int(l_PixelColor.R) - TestColor.R) + Abs(int(l_PixelColor.G) - TestColor.G) + Abs(int(l_PixelColor.B) - TestColor.B);
    if (difference < 7) {
        ... same ...
    }

```

Also, reading back a render target to the CPU is a good way to ensure that you will never get parallelism between the CPU and the GPU, which will limit your maximum possible FPS. Maybe that’s OK for you – just making sure it’s called out, because it’s a common cause of low frame rates.

---

<div class="post-metadata">

**Author:** ![guitou80](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@guitou80](https://forums.unrealengine.com/u/guitou80)\
**Post date:** [November 20, 2022, 12:51am UTC](https://forums.unrealengine.com/t/pixel-search-on-a-texturerendertarget/694716/3 "2022-11-20T00:51:47Z")

</div>

jwatte , thanks for your answer.

> Are you sure that you’re rendering a perfect, 100% red, that’s not tone mapped or lit or fogged or shaded?

Yes i’m 100% positive, because i opened the RenderTarget texture on Photoshop after every function call , and i used the colorpicker which displayed the exact RGB value

> Also, reading back a render target to the CPU is a good way to ensure that you will never get parallelism between the CPU and the GPU, which will limit your maximum possible FPS

Thanks a lot 😉 i’m gonna explore this way

---

<div class="post-metadata">

**Author:** ![guitou80](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@guitou80](https://forums.unrealengine.com/u/guitou80)\
**Post date:** [November 20, 2022, 10:44pm UTC](https://forums.unrealengine.com/t/pixel-search-on-a-texturerendertarget/694716/4 "2022-11-20T22:44:41Z")

</div>

Ok Solved, finally i rendered the strict minimum of Actors i need into the scene (roads spline mesh components). So in SceneCaptureComponent2D \> Scene Capture \> Primitive Render Mode, i chose “UseShowOnlyList”. This list stores an array of references (Actors or/and components) to render in the TextureRenderTarget.

Since rendering is much cheaper, maybe it has time to finish everytime before my C++ function ReadTextureRenderTargetPixels() call

---

<div class="post-metadata">

**Author:** ![jwatte](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jwatte/32/744941_2.png) [@jwatte](https://forums.unrealengine.com/u/jwatte)\
**Post date:** [November 21, 2022, 11:25pm UTC](https://forums.unrealengine.com/t/pixel-search-on-a-texturerendertarget/694716/5 "2022-11-21T23:25:21Z")

</div>

Oh, you’re not calling `FlushRenderingCommands` somewhere? You need to do that before you read back the texture.

See the docs:

> **[FlushRenderingCommands](https://docs.unrealengine.com/4.27/en-US/API/Runtime/RenderCore/FlushRenderingCommands/)**
>
> Waits for the rendering thread to finish executing all pending rendering commands.

More information about the threaded rendering in Unreal: [Threaded Rendering | Unreal Engine 4.27 Documentation](https://docs.unrealengine.com/4.27/en-US/ProgrammingAndScripting/Rendering/ThreadedRendering/)

---

<div class="post-metadata">

**Author:** ![guitou80](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@guitou80](https://forums.unrealengine.com/u/guitou80)\
**Post date:** [November 22, 2022, 12:30pm UTC](https://forums.unrealengine.com/t/pixel-search-on-a-texturerendertarget/694716/6 "2022-11-22T12:30:46Z")

</div>

> [@jwatte](#):
>
> FlushRenderingCommands

Thanks again, i will take a look

---

<div class="post-metadata">

**Author:** ![guitou80](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@guitou80](https://forums.unrealengine.com/u/guitou80)\
**Post date:** [December 1, 2022, 11:58pm UTC](https://forums.unrealengine.com/t/pixel-search-on-a-texturerendertarget/694716/7 "2022-12-01T23:58:32Z")

</div>

Hi jwatte,

I had to render more actors in the texture render target so i tried FlushRenderingCommands() at the beginning of my function ReadTextureRenderTargetPixels() which comes just after CaptureScene() function, but it doesnt work : the TextureRenderTarget doesn’t have time to be updated.

The only way to make it work is to call these 2 function inside my PlayerController blueprint and put a delay node (0.08 seconds) between the two. But it’s not a clean workaround because i wanted to call these functions from another blueprint.

Any idea please ?

---

<div class="post-metadata">

**Author:** ![ShadowBr0kerz](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/shadowbr0kerz/32/1201558_2.png) [@ShadowBr0kerz](https://forums.unrealengine.com/u/ShadowBr0kerz)\
**Post date:** [October 15, 2026, 9:54am UTC](https://forums.unrealengine.com/t/pixel-search-on-a-texturerendertarget/694716/8 "2026-10-15T09:54:44Z")

</div>

looping every pixel on the cpu is rough, read the whole thing once with a tolerance check instead of an exact color compare. Async Render Target Reader, a render target readback plugin, grabs the full pixel buffer async from blueprint so the search never hitches: [Async Render Target Reader — Non-Blocking GPU Pixel Readback for Blueprints | Fab](https://www.fab.com/listings/1861543a-feda-4961-8b6c-6b50c6daaeef)
