![](/img/trans.png)
[英]How to read from a binary file and load the data into a 3d vector in C++?
[英]How to read data from a UTexture2D in C++
我正在嘗試從虛幻引擎C ++項目中填充的UTexture2D中讀取像素數據。 在此處發布問題之前,我嘗試使用此鏈接中描述的方法: https : //answers.unrealengine.com/questions/25594/accessing-pixel-values-of-texture2d.html 。 但是,它對我不起作用。 我從紋理獲得的所有像素值都是一些垃圾數據。
我只想從SceneCapture2D和包含SceneTexture:Depth節點的后處理材料中獲取深度值。 我需要C ++中可用的深度值,以便可以使用OpenCV進行進一步處理。 在Directx11中,分段紋理可以用於CPU讀取,但是在虛幻引擎中,我不知道如何創建Dx11所具有的“分段紋理”。 我無法從當前方法中獲取正確的像素值,這使我認為我可能會嘗試訪問非CPU可讀的紋理。
這是我的實驗代碼,用於從RGB UTexture2D讀回數據。
初始化RGB紋理:
VideoTextureColor= UTexture2D::CreateTransient(640, 480, PF_B8G8R8A8);
VideoTextureColor->UpdateResource();
VideoUpdateTextureRegionColor = new FUpdateTextureRegion2D(0, 0, 0, 0, 640, 480);
ColorRegionData = new FUpdateTextureRegionsData;
PixelDepthData.Init(FColor(0, 0, 0, 255), 640 * 480);
// Populate the texture with blue color
for (int i = 0; i < 640; i++) {
for (int j = 0; j < 480; j++) {
int idx = j * 640 + i;
PixelDepthData[idx].B = 255;
PixelDepthData[idx].G = 0;
PixelDepthData[idx].R = 0;
PixelDepthData[idx].A = 255;
}
}
UpdateTextureRegions(
VideoTextureColor,
(int32)0,
(uint32)1,
VideoUpdateTextureRegionColor,
(uint32)(4 * 640),
(uint32)4,
(uint8*)PixelDepthData.GetData(),
false,
ColorRegionData
);
然后,update將其值讀回到PixelDepthData(TArray類型)數組,並使用存儲在PixelDepthData中的值(它的舊值)更新此紋理。
UpdateTextureRegions(
VideoTextureColor,
(int32)0,
(uint32)1,
VideoUpdateTextureRegionColor,
(uint32)(4 * 640),
(uint32)4,
(uint8*)PixelDepthData.GetData(),
false,
ColorRegionData
);
ENQUEUE_UNIQUE_RENDER_COMMAND_ONEPARAMETER(
FRealSenseDelegator,
ARealSenseDelegator*, RealSenseDelegator, this,
{
FColor* tmpImageDataPtr = static_cast<FColor*>((RealSenseDelegator->VideoTextureColor)->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_ONLY));
for (uint32 j = 0; j < 480; j++) {
for (uint32 i = 0; i < 640; i++) {
uint32 idx = j * 640 + i;
RealSenseDelegator->PixelDepthData[idx] = tmpImageDataPtr[idx];
RealSenseDelegator->PixelDepthData[idx].A = 255;
}
}
(RealSenseDelegator->VideoTextureColor)->PlatformData->Mips[0].BulkData.Unlock();
}
);
我得到的只是可視化場景中的白色紋理而不是藍色紋理。
有誰知道如何讀取UTexture2D對象的數據?
我知道了。 您必須首先獲取UTexture2D的RHI紋理參考,然后使用RHILockTexture2D讀取其數據,並且必須在RenderThread中進行操作。 以下代碼只是一個示例:
FTexture2DResource* uTex2DRes = (FTexture2DResource*)(RealSenseDelegator->VideoTexturePixelDepth)->Resource;
float* cpuDataPtr = (float*)RHILockTexture2D(
uTex2DRes->GetTexture2DRHI(),
0,
RLM_ReadOnly,
destStride,
false);
for (uint32 j = 0; j < 480; j++) {
for (uint32 i = 0; i < 640; i++) {
uint32 idx = j * 640 + i;
// TODO Read the pixel data right here
}
}
RHIUnlockTexture2D(uTex2DRes->GetTexture2DRHI(), 0, false);
要在“渲染線程”中執行此操作,您必須使用ENQUEUE_UNIQUE_RENDER_COMMAND_ONEPARAMETER之類的宏//如果僅將一個參數傳遞給渲染線程,請使用此宏。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.