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!
Rama
(Rama EverNewJoy)
March 25, 2014, 9:04am
2
I posted my code for you here
Oh wow, thanks a lot!
Let me check out the code
Thanks!
Nonlin
(Nonlin)
June 22, 2020, 7:29pm
4
This doesn’t handle getting SceneDepth it seems.
You Can Use My Code,It can get the scene depth texture: https://vhope.cf/zh/posts/ue/ue-depth/
(in any tick() function)
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?
Can someone help me?