How to read data from UTexture2D in C ++

I am trying to read pixel data from a filled UTexture2D in an Unreal Engine C ++ project. Before asking a 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, this doesn't work for me. All the pixel values โ€‹โ€‹I got from the texture are some garbage data.

I just want to get depth values โ€‹โ€‹from SceneCapture2D and post-processing stuff containing SceneTexture: Depth node. I need depth values โ€‹โ€‹available in C ++ so that I can continue processing with OpenCV. In Directx11, the placement texture can be used to read the CPU, but in an unreal engine I don't know how to create an "intermediate texture" like Dx11. I cannot get the correct pixel values โ€‹โ€‹from the current method, which is why I think I can try to access a texture that is not readable by the processor.

Here is my experimental code for reading RGB data from UTexture2D.

Initialize RGB texture:

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
);

      

Then update its value back to PixelDepthData (TArray) and update this texture with the values โ€‹โ€‹in PixelDepthData, which is its old value.

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();
    }
);

      

All I got was a white texture instead of a blue color texture in the render scene.

Does anyone know how to read the data of a UTexture2D object?

+1


source to share


1 answer


I understood that. First, you need to get a reference to the RHI UTexture2D texture structure, and then use RHILockTexture2D to read the data, and you have to do this in RenderThread. The following code is just an example:

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);

      



To do this on the render thread, you must use a macro such as ENQUEUE_UNIQUE_RENDER_COMMAND_ONEPARAMETER // If you only pass one parameter to the render thread, use this one. +

+3


source







All Articles