How to read pixels form a UTexture?

My question is as the title says. I have seen examples of how to read pixels from a Utexture2D,
but cant find a way to get the pixel data from a UTexture.

PS. The reason i want to do this, is because i want to convert a UTexture to UTexture2D.

Well, UTexture is an abstract class that cannot be instantiated. It relies on it’s concrete subclasses UTexture2D, UTextureCube, UTexture2DArray, etc to be instantiated. If you have a pointer to a UTexture, then it actually IS a UTexture2D, or UTextureCube, or UTexture2DArray etc.

So what you can do i believe, is simply cast it like so.

if(const auto Texture2D = Cast<UTexture2D>(Texture))
{
    //If This block is reached, the texture was a Texture2D.  Lets do our work with it

	// Make Sure The Texture Is Up To Date.
	Texture2D->UpdateResource();

	// Get Platform Data.
	const auto PlatformData = Texture2D->GetPlatformData();
	
	// Get The Bulk Data
	const auto TextureData = PlatformData->Mips[0].BulkData;

	// Array Pixel Data Will Be Copied To
	TArray<FColor> DestPixels{};

	// Initialize The Array With The Correct Pixel Size;
	DestPixels.AddDefaulted(PlatformData->SizeX * PlatformData->SizeY);

	// Lock The Pixel Data For CPU Read Access.
	const auto PixelData = static_cast<const FColor*>(TextureData.LockReadOnly());

	// Copy The Pixel Data.
	FMemory::Memcpy(DestPixels.GetData(), &PixelData, sizeof(FColor) * DestPixels.Num());

	// Unlock So The GPU Can Access The Data Again.
	TextureData.Unlock();

	// We Now Have All Pixel Data For The Texture And We Can Do What We Want Without Holding The GPU Up.
	DestPixels
}
1 Like

oh wow, thanks! I didnt know i could simply cast it.

Yeah, UTexture is just an abstract base class of all UTextureXX classes

1 Like

I have a similar issue, and your solution makes sense, but for some reason, after unlocking the texture data, when trying to access the DestPixels I get the following error:
Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x000000bcbb580000
to my knowledge, the Memcpy creates a copy of the data, so why would it be affected by the unlocking of the GPU?

it’s possible you are calculating the memcpy addresses wrong and are actually copying protected memory, i would give it another hard look.