How to extract a Texture2D from a render target?

Hey,

Through many attempts and hours I have been trying to convert a render target to a texture 2d. I believe the closest I have come is converting the render target to a slate brush and attempting to get it out with the “Get brush resource as Texture 2D” however this always ends in an invalid result (potentially a bug).

Using this slate brush I can change the image on a Widget UI Image from that so i know that the brush is valid and working itself (I can’t even get the image from the widget it fails with the same issue)

I’ve searched for others with the same issue and have come across nothing and was wondering if anyone could give insight into my problem. I am not really sure why it is seeming so complicated for this minor task as the render target is already a 2d texture.

Thanks,

Jafer

Because it is the only way that I can compare two images together. I plan to loop through it and compare it with a previous version of the image in affected areas and only texture 2d gives me that ability.

why do you need it to be texture2d?

have you made a material based on render target?

Yes I tried that, can’t seem to get a Texture2d from a material either, unless I am missing some node.

I’m using UTextureRenderTarget2D.

	TArray<FColor> SurfData;
	auto RenderTarget = RenderTexture->GameThread_GetRenderTargetResource();
	RenderTarget->ReadPixels(SurfData);

Or:

CachedRenderTexture = UTexture2D::CreateTransient(RenderTexture->SizeX, RenderTexture->SizeY, PF_B8G8R8A8);// RenderTexture->GetFormat());
1 Like

I will post a slight word of caution with the ReadPixels approach - back on 4.12 it wasn’t properly cross platform (issues on iOS). However I’ve seen it suggested that this is fixed in 4.14.

This can be worked around on iOS by using the platform libraries (objective C) but if you’re new to objC be prepared to study some to get this implemented.

Your comments helped me get to my desired result but I ended up doing it a little differently. I ended up making a blueprint node with the parameters render target, x, y, radius and returned an array with the area around the point labeled as an array of FLinearColors.

I had to add a radius to it as checking the 1024x1024 texture took too long for my comparisons.

Can this be done with blueprints only?

3 Likes

if i has a archivesSystem,and each archive has a property called “Screenshot”,so I will use texture2d to store “screenshot”,and you know,the bp class"save game"can use api called “save game to slot” and “load game from slot”,these api can achieve the function"archivesSystem",so I need a variable to store a “screenshot”,

but how i get Texture2D?