No announcement yet.

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

  • Filter
  • Time
  • Show
Clear All
new posts

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

    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!

    I posted my code for you here
    UE4 Marketplace: Melee Weapon Plugin & Compressed Binary Save System Plugin | Rama's C++ AI Jumping Videos | Vertex Snap Editor Plugin

    ♥ Rama


      Oh wow, thanks a lot!

      Let me check out the code



        Originally posted by Rama View Post
        This doesn't handle getting SceneDepth it seems.


          You Can Use My Code,It can get the scene depth texture:

          (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?

          Click image for larger version  Name:	MyError-min.png Views:	22 Size:	249.8 KB ID:	1779950

          Can someone help me?
          Last edited by VictorHong1998; 06-26-2020, 09:11 PM.