How to get texture pixels using UTextureRenderTarget2D class?

This code copies the UTextureRenderTarget2D content into a new UTexture2D and works on published game.

// Creates Texture2D to store TextureRenderTarget content
UTexture2D *Texture = UTexture2D::CreateTransient(TextureRenderTarget->SizeX, TextureRenderTarget->SizeY, PF_B8G8R8A8);
#if WITH_EDITORONLY_DATA
Texture->MipGenSettings = TMGS_NoMipmaps;
#endif
Texture->SRGB = TextureRenderTarget->SRGB;

// Read the pixels from the RenderTarget and store them in a FColor array
TArray<FColor> SurfData;
FRenderTarget *RenderTarget = TextureRenderTarget->GameThread_GetRenderTargetResource();
RenderTarget->ReadPixels(SurfData);

// Lock and copies the data between the textures
void* TextureData = Texture->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
const int32 TextureDataSize = SurfData.Num() * 4;
FMemory::Memcpy(TextureData, SurfData.GetData(), TextureDataSize);
Texture->PlatformData->Mips[0].BulkData.Unlock();
// Apply Texture changes to GPU memory
Texture->UpdateResource();

If you only want to access pixels from RenderTarget, just use this part of the code and an index formula.

// Read the pixels from the RenderTarget and store them in a FColor array
TArray<FColor> SurfData;
FRenderTarget *RenderTarget = TextureRenderTarget->GameThread_GetRenderTargetResource();
RenderTarget->ReadPixels(SurfData);
// Index formula
FColor PixelColor = SurfData[x + y * TextureRenderTarget->SizeX];
5 Likes