# How to access color and depth buffer of a UE4 game?

**URL:** <https://forums.unrealengine.com/t/how-to-access-color-and-depth-buffer-of-a-ue4-game/884>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [March 25, 2014, 7:58am UTC](https://forums.unrealengine.com/t/how-to-access-color-and-depth-buffer-of-a-ue4-game/884 "2014-03-25T07:58:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_5be21b17](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_5be21b17](https://forums.unrealengine.com/u/anonymous_user_5be21b17)\
**Post date:** [March 25, 2014, 7:58am UTC](https://forums.unrealengine.com/t/how-to-access-color-and-depth-buffer-of-a-ue4-game/884/1 "2014-03-25T07:58:27Z")

</div>

Hello guys

I am using UE4 engine to do a computer graphics research project

I want to access the color (RGB) and depth buffer(float) of each frame, and my application is based on the template of FPS

How can I do that? Which class should I use?

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [March 25, 2014, 9:04am UTC](https://forums.unrealengine.com/t/how-to-access-color-and-depth-buffer-of-a-ue4-game/884/2 "2014-03-25T09:04:24Z")

</div>

I posted my code for you here

> **[Access color and depth buffer of each frame?](https://forums.unrealengine.com/questions/17862/access-color-and-depth-buffer-of-each-frame.html)**
>
> In which way do you need to access it ? If it’s for a shader purpose, you can use a SceneColor node inside your Material. Here is an example on how to setup this kind of material : \[Question\] Translucent + SceneColor = black ? - Rendering - Unreal...

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_5be21b17](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_5be21b17](https://forums.unrealengine.com/u/anonymous_user_5be21b17)\
**Post date:** [March 25, 2014, 9:06am UTC](https://forums.unrealengine.com/t/how-to-access-color-and-depth-buffer-of-a-ue4-game/884/3 "2014-03-25T09:06:40Z")

</div>

Oh wow, thanks a lot!

Let me check out the code

Thanks!

---

<div class="post-metadata">

**Author:** ![Nonlin](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Nonlin](https://forums.unrealengine.com/u/Nonlin)\
**Post date:** [June 22, 2020, 7:29pm UTC](https://forums.unrealengine.com/t/how-to-access-color-and-depth-buffer-of-a-ue4-game/884/4 "2020-06-22T19:29:37Z")

</div>

> [@Rama](#):
>
> I posted my code for you here
> 
> [Access color and depth buffer of each frame? - Programming & Scripting - Epic Developer Community Forums](https://answers.unrealengine.com/questions/17862/access-color-and-depth-buffer-of-each-frame.html)

This doesn’t handle getting SceneDepth it seems.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_2ee04ade](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_2ee04ade](https://forums.unrealengine.com/u/anonymous_user_2ee04ade)\
**Post date:** [June 24, 2020, 6:43am UTC](https://forums.unrealengine.com/t/how-to-access-color-and-depth-buffer-of-a-ue4-game/884/5 "2020-06-24T06:43:00Z")

</div>

You Can Use My Code，It can get the scene depth texture: [https://vhope.cf/zh/posts/ue/ue-depth/](https://vhope.cf/zh/posts/ue/ue-depth/)

（in any tick() function）

```auto

struct DepthPixel //定义深度像素结构体
{
float depth;
char stencil;
char unused1;
char unused2;
char unused3;
};

float* cpuDataPtr; // Texture深度值数组首地址
TArray<DepthPixel> mydata; //最终获取色深度值数据
FIntPoint buffsize; //深度长宽大小X和Y

ENQUEUE_RENDER_COMMAND(ReadSurfaceFloatCommand)( // 将读取深度数据的命令推给渲染线程进行执行
&cpuDataPtr, &mydata, &buffsize](FRHICommandListImmediate& RHICmdList) //&cpuDataPtr, &mydata, &buffsize为传入的外部参数
{
FSceneRenderTargets::Get(RHICmdList).AdjustGBufferRefCount(RHICmdList, 1);
FTexture2DRHIRef uTex2DRes = FSceneRenderTargets::Get(RHICmdList).GetSceneDepthSurface();
buffsize = uTex2DRes->GetSizeXY();
uint32 sx = buffsize.X;
uint32 sy = buffsize.Y;
mydata.AddUninitialized(sx * sy);
uint32 Lolstrid = 0;
cpuDataPtr = (float*)RHILockTexture2D(uTex2DRes,0,RLM_ReadOnly,Lolstrid,true); // 加锁 获取可读depth Texture深度值数组首地址
memcpy(mydata.GetData(), cpuDataPtr, sx * sy * sizeof(DepthPixel)); //复制深度数据
RHIUnlockTexture2D(uTex2DRes, 0, true); //解锁
FSceneRenderTargets::Get(RHICmdList).AdjustGBufferRefCount(RHICmdList, -1);

});
FlushRenderingCommands(); //等待渲染线程执行

mydata; //最终获取深度数据

```

But I have a problem，the depth textture I get is not the same size as my game viewport. How do I map the depth texture data from the screen coordinates?

 ![MyError-min.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/3X/2/7/27de8487ca4967125c0fa8ae9d1dc2794655ec4d.jpeg)

Can someone help me?
