We retrieve an UARTexture from the blueprint function “GetARTexture()” of the ARBlueprintLibrary (having “Camera Image” selected).
From this UARTexture, we want to retrieve the raw pixel data, but until now we don’t have any success.
We found a couple solutions, that were based on UTexture2D - like for example: UE4 - Reading the pixels from a UTexture2D - Isara Tech. , but UARTexture only derives from UTexture and not from UTexture2D.
The only one that we found which should work for UTexture and therefor for UARTexture as well, uses the editor-only property “Source” and therefor also doesn’t work:
Hi, Can someone hint me how I can get raw pixel data from the UTexture I retreive from a material? I am stuck at the point where I need to get actual pixel data I have no idea where to get documentation for it, because in official docs there is not...
Best Regards,
Michael
you can see what we done in different platform.i have search for a month.
it will return the unit8* raw data.
su-020297
(su-020297)
November 16, 2022, 5:57am
3
It seems the code below works for me.
1 Like
CKBksjflsf
(CKBksjflsf)
November 14, 2023, 5:55pm
4
It works, Thanks.
But some minor changed should be applied in the code.
In my case, Texture2D->GetSizeXY() return 1*1, so I change to ARTexture->Size.XY and it works.
Here’s my code:
void UMyARLib::GetARTextureData(UARTexture* ARTexture)
{
FTexture2DRHIRef Texture2D = ARTexture->GetResource()->TextureRHI->GetTexture2D();
if(!Texture2D)
{
UE_LOG(LogTemp, Warning, TEXT("Texture2D failed"));
return;
}
TArray<FColor> OutPixels;
struct FReadSurfaceContext
{
FTexture2DRHIRef Texture;
TArray<FColor>* OutData;
FIntRect Rect;
FReadSurfaceDataFlags Flags;
};
FReadSurfaceContext ReadSurfaceContext =
{
Texture2D,
&OutPixels,
FIntRect(0, 0, ARTexture->Size.X, ARTexture->Size.Y),
FReadSurfaceDataFlags(RCM_UNorm, CubeFace_MAX)
};
FReadSurfaceContext Context = ReadSurfaceContext;
ENQUEUE_RENDER_COMMAND(ReadSurfaceCommand)(
[Context](FRHICommandListImmediate& RHICmdList)
{
RHICmdList.ReadSurfaceData(
Context.Texture,
Context.Rect,
*Context.OutData,
Context.Flags);
});
FlushRenderingCommands();
//Save to file
TArray64<uint8> ImgData;
FImageUtils::PNGCompressImageArray(Context.Rect.Width(), Context.Rect.Height(), OutPixels, ImgData);
FString FileName = FPaths::ProjectSavedDir() + "test.png";
FFileHelper::SaveArrayToFile(ImgData, *FileName);
}