Hi,
I am trying to read the pixel data from a populated UTexture2D in an Unreal Engine C++ project. Before i post the question here, I tried to use the method described in this link: https://answers.unrealengine.com/questions/25594/accessing-pixel-values-of-texture2d.html. However, it doesn’t work for me. All pixel values I got from the texture are some garbage data.
Here is how i do it:
I created a utexture2d (640 * 480) and initialized 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;
}
}
// TODO Test Area
//RequireCameraDepthMap();
UpdateTextureRegions(
VideoTextureColor,
(int32)0,
(uint32)1,
VideoUpdateTextureRegionColor,
(uint32)(4 * REAL_SENSE_RGB_WIDTH),
(uint32)4,
//(uint8*)(RealSense.GetColorDataTmp()),
(uint8*)PixelDepthData.GetData(),
//reinterpret_cast<uint8*>(m_pColorRingBuffer[m_iGameThreadReadIdx].GetData()),
false,
ColorRegionData
);
Then, in my frame update function, I try to read the data back from this texture and then update it with it’s old values, just want to see if I got the correct pixel values from this texture using the following codes:
UpdateTextureRegions(
VideoTextureColor,
(int32)0,
(uint32)1,
VideoUpdateTextureRegionColor,
(uint32)(4 * REAL_SENSE_RGB_WIDTH),
(uint32)4,
//(uint8*)(RealSense.GetColorDataTmp()),
(uint8*)PixelDepthData.GetData(),
//reinterpret_cast<uint8*>(m_pColorRingBuffer[m_iGameThreadReadIdx].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();
}
);
So, the texture was blue, but after I update it with its old pixel values, the texture is in a white color. So, does anybody know how to get the data from the utexture2d? or am I missed something here?
Thanks,
Z