How to read data from a UTexture2D in C++
<p>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: <a href="https://answers.unrealengine.com/questions/25594/accessing-pixel-values-of-texture2d.html" rel="nofollow">https://answers.unrealengine.com/questions/25594/accessing-pixel-values-of-texture2d.html</a>. However, it doesn't work for me. All pixel values I got from the texture are some garbage data.</p>

<p>I just want to get the depth values from the SceneCapture2D and a post-processing material that contains SceneTexture: Depth node. I need the depth values available in C++ so that I can do further processing with OpenCV. In Directx11, staging texture can be used for CPU read, but in the unreal engine, I don't know how to create a 'staging texture' like Dx11 has. I can't get the correct pixel values from the current method which makes me think I may try to access a no-CPU-readable texture. </p>

<p>Here is my experimental code for reading data back from an RGB UTexture2D. </p>

<p>Initialize the RGB Texture:</p>

<pre><code>VideoTextureColor= UTexture2D::CreateTransient(640, 480, PF_B8G8R8A8);
VideoTextureColor-&gt;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 &lt; 640; i++) {
    for (int j = 0; j &lt; 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
);
</code></pre>

<p>Then, update read its value back to the PixelDepthData (TArray type) array and update this texture with values storing in the PixelDepthData, which is its old value.</p>

<pre><code>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&lt;FColor*&gt;((RealSenseDelegator-&gt;VideoTextureColor)-&gt;PlatformData-&gt;Mips[0].BulkData.Lock(LOCK_READ_ONLY));
        for (uint32 j = 0; j &lt; 480; j++) {
            for (uint32 i = 0; i &lt; 640; i++) {
                uint32 idx = j * 640 + i;
                RealSenseDelegator-&gt;PixelDepthData[idx] = tmpImageDataPtr[idx];
                RealSenseDelegator-&gt;PixelDepthData[idx].A = 255;
            }
        }
        (RealSenseDelegator-&gt;VideoTextureColor)-&gt;PlatformData-&gt;Mips[0].BulkData.Unlock();
    }
);
</code></pre>

<p>All I got is a white color texture instead of a blue color texture in the visualization scene.</p>

<p>Does anyone know how to read the data of the UTexture2D Object?</p>