# Access color and depth buffer of each frame?

**URL:** <https://forums.unrealengine.com/t/access-color-and-depth-buffer-of-each-frame/278347>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [March 25, 2014, 8:41am UTC](https://forums.unrealengine.com/t/access-color-and-depth-buffer-of-each-frame/278347 "2014-03-25T08:41:59Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![shader3120\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@shader3120\_1](https://forums.unrealengine.com/u/shader3120_1)\
**Post date:** [March 25, 2014, 8:41am UTC](https://forums.unrealengine.com/t/access-color-and-depth-buffer-of-each-frame/278347/1 "2014-03-25T08:41:59Z")

</div>

Hi guys

I am using UE4 engine to do a computer graphics research, and I need to access the color(RGB) buffer and depth buffer of each frame, in a FPS game

How could I do that? Do I need to modify source code, if so ,which class to modify?

Thanks!

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b9fdc589](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b9fdc589](https://forums.unrealengine.com/u/anonymous_user_b9fdc589)\
**Post date:** [March 25, 2014, 8:58am UTC](https://forums.unrealengine.com/t/access-color-and-depth-buffer-of-each-frame/278347/2 "2014-03-25T08:58:57Z")

</div>

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 - Epic Developer Community Forums](https://answers.unrealengine.com/questions/7626/question-translucent-scenecolor-black.html)

---

<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:01am UTC](https://forums.unrealengine.com/t/access-color-and-depth-buffer-of-each-frame/278347/3 "2014-03-25T09:01:44Z")

</div>

#Custom Viewport Class

I am posting for you my custom viewport class

I am accessing the color buffer to save a screenshot

Even though the code says PNG I am actually saving it to a .BMP file

#config

DefaultEngine.ini

```
; ~~~Custom Viewport Client~~~
GameViewportClientClassName=/Script/VictoryGame.VictoryGameViewport

```

#.h

```
// Copyright 1998-2013 Epic Games, Inc. All Rights Reserved.

#include "VictoryGameViewport.generated.h"

UCLASS()
class UVictoryGameViewport : public UGameViewportClient
{
	GENERATED_UCLASS_BODY()
	
//Utility
public:

	//Controller
	AVictoryGamePlayerController * VictoryPC;
	
	// ~~~~~~~~~~~~~Screen Shots to PNG~~~~~~~~~~~~~~
	
	//Triggers ScreenShot
	bool VictoryDoScreenShot;
	
	//Save Screen Shot to disk as PNG
	void SaveScreenShotPNGToDisk(FString TheFileName);
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//protected because must only be run from Draw() or causes crash
protected:
	
	void VictoryTakeScreenShot();

//OVERRIDES
public:
		virtual void Draw(FViewport* Viewport,FCanvas* SceneCanvas) OVERRIDE;

};

```

#.cpp  
// Copyright 1998-2013 Epic Games, Inc. All Rights Reserved.

```
#include "VictoryGame.h"

//////////////////////////////////////////////////////////////////////////
// UVictoryGameViewport

UVictoryGameViewport::UVictoryGameViewport(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	
}

void UVictoryGameViewport::VictoryTakeScreenShot()
{
	if (!VictoryPC) return;
	if (!Viewport) return;
	
	// ~~~~~~~~~~~~~~~~~~~~~~
	
	//get viewport size
	FVector2D viewportSize;
	GetViewportSize(viewportSize);
	
	//output viewport size, useful for debugging
	/*
	FString text = "x= ";
	text += FString::FromInt(viewportSize.X);
	text += "y= ";
	text += FString::FromInt(viewportSize.Y);
	VictoryPC->ClientMessage(text);
	*/
	
	// ~~~Invalid View Port Size~~~
	if (viewportSize.X <= 0 || viewportSize.Y <= 0) return;
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	//Section of Viewport to capture
	// entire buffer invalidated if going larger than viewport size
	// during readpixels
	
	viewportSize.X--; //actual indicies, not total size
	viewportSize.Y--;
	
	//Adjusted Image Size
	VictoryPC->BMP_Width = viewportSize.X; 
	VictoryPC->BMP_Height = viewportSize.Y; 
	
	// ~~~~~~~~~~~~~~~~~~~~~~~~
	// ~~~~~~~~~~~~~~~~~~~~~~~~
	// ~~~Empty Color Buffer~~~
	VictoryPC->VictoryScreenShot_ColorBuffer.Empty();	
	// ~~~~~~~~~~~~~~~~~~~~~~~~
	// ~~~~~~~~~~~~~~~~~~~~~~~~
	
	// ~~~Reallocate Buffer~~~
		//add uninitialized does not call constructor on the TArray type
		//do not use with non-simple data types like FColor FVector etc.
		
	// ~~~Add Zeroed To Ensure Black For No Color Data Parts~~~
	VictoryPC->VictoryScreenShot_ColorBuffer.AddZeroed( viewportSize.X * viewportSize.Y );
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	
	//Get Screen Shot!
	if (!Viewport->ReadPixels(
		VictoryPC->VictoryScreenShot_ColorBuffer,
		FReadSurfaceDataFlags(),
		FIntRect(1,1,viewportSize.X,viewportSize.Y)
	)){
		VictoryPC->Optimize("Failed to ReadPixels");
		return;
		// ~~~~~~~~~~~~~~~~~~~~~~~~~
	}
	
	//Start Processing Image
	VictoryPC->SaveScreenshotToDisk();
	
}

//Draw
void UVictoryGameViewport::Draw(FViewport * Viewport, FCanvas * SceneCanvas)
{
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//this line is reaaaaaaaaaaaaaalllly important
	Super::Draw(Viewport, SceneCanvas);
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	
	//UE_LOG(Victory,Warning,TEXT("VICTORY GAME VIEWPORT Ticking!"));
	
	//Take Screen shot?
	if (VictoryDoScreenShot)
	{
		VictoryDoScreenShot = false;
		VictoryTakeScreenShot();	
	}
}
```

---

<div class="post-metadata">

**Author:** ![shader3120\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@shader3120\_1](https://forums.unrealengine.com/u/shader3120_1)\
**Post date:** [March 25, 2014, 9:02am UTC](https://forums.unrealengine.com/t/access-color-and-depth-buffer-of-each-frame/278347/4 "2014-03-25T09:02:35Z")

</div>

Hi , thanks for the reply. I would like to save the color buffer and depth buffer of each frame into a BMP file and a HDR float image(e.g. PFM file), I thought I had to modify the source code of the renderer…

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b9fdc589](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b9fdc589](https://forums.unrealengine.com/u/anonymous_user_b9fdc589)\
**Post date:** [March 25, 2014, 9:18am UTC](https://forums.unrealengine.com/t/access-color-and-depth-buffer-of-each-frame/278347/5 "2014-03-25T09:18:02Z")

</div>

Indeed, in this case you need to dig in the C++ code. Rama posted a very cool code above ! 🙂

---

<div class="post-metadata">

**Author:** ![shader3120\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@shader3120\_1](https://forums.unrealengine.com/u/shader3120_1)\
**Post date:** [March 25, 2014, 10:04am UTC](https://forums.unrealengine.com/t/access-color-and-depth-buffer-of-each-frame/278347/6 "2014-03-25T10:04:39Z")

</div>

Dear Rama, thanks a lot for posting the code, they are very useful. Right now I am pretty sure I am able to save the RGB buffer of each frame to a BMP file; but how can I save the depth buffer? One possible solution from myself would be: modify the shader applied in the scene to display depth information on the surface and then use Viewport-\>ReadFloat16Pixels to save them? Is it doable? Thanks!

---

<div class="post-metadata">

**Author:** ![BharatSingh](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@BharatSingh](https://forums.unrealengine.com/u/BharatSingh)\
**Post date:** [March 10, 2015, 6:53pm UTC](https://forums.unrealengine.com/t/access-color-and-depth-buffer-of-each-frame/278347/7 "2015-03-10T18:53:21Z")

</div>

were you able to finally figure out how to access the depth information in the viewport?

---

<div class="post-metadata">

**Author:** ![Antidamage](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/antidamage/32/918692_2.png) [@Antidamage](https://forums.unrealengine.com/u/Antidamage)\
**Post date:** [August 20, 2015, 1:51am UTC](https://forums.unrealengine.com/t/access-color-and-depth-buffer-of-each-frame/278347/8 "2015-08-20T01:51:04Z")

</div>

Quick question:

Is /Script/VictoryGame.VictoryGameViewport the namespace? Where is the /script/VictoryGame part derived from?

---

<div class="post-metadata">

**Author:** ![RVillani](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rvillani/32/1194572_2.png) [@RVillani](https://forums.unrealengine.com/u/RVillani)\
**Post date:** [December 30, 2015, 8:45pm UTC](https://forums.unrealengine.com/t/access-color-and-depth-buffer-of-each-frame/278347/9 "2015-12-30T20:45:33Z")

</div>

I managed to take snapshots from the scene with Rama’s code. But it doesn’t capture the Widgets added to the viewport. Is there a way we can capture those as well as the 3D scene?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_46a83031](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_46a83031](https://forums.unrealengine.com/u/anonymous_user_46a83031)\
**Post date:** [October 16, 2017, 1:45pm UTC](https://forums.unrealengine.com/t/access-color-and-depth-buffer-of-each-frame/278347/10 "2017-10-16T13:45:46Z")

</div>

In the header, line 14:  
AVictoryGamePlayerController \* VictoryPC;

Where does this datatype come from ?  
I want to read a buffer (motion blur in my case) as well, but do not fully comprehend this code.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b6b43276](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b6b43276](https://forums.unrealengine.com/u/anonymous_user_b6b43276)\
**Post date:** [December 16, 2017, 1:16am UTC](https://forums.unrealengine.com/t/access-color-and-depth-buffer-of-each-frame/278347/11 "2017-12-16T01:16:24Z")

</div>

I’m also looking for a way to acess the depth,so were you able to finally figure out how to access the depth information in the viewport?

---

<div class="post-metadata">

**Author:** ![Anonymous\_45c7af3adcc306c2cea2bef1bc45ad57](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Anonymous\_45c7af3adcc306c2cea2bef1bc45ad57](https://forums.unrealengine.com/u/Anonymous_45c7af3adcc306c2cea2bef1bc45ad57)\
**Post date:** [February 12, 2019, 5:05pm UTC](https://forums.unrealengine.com/t/access-color-and-depth-buffer-of-each-frame/278347/12 "2019-02-12T17:05:55Z")

</div>

Hello！！！  
I saw many GBuffers who are asking to visit SceneRenderTarget.  
This is another way to use SceneCapu.

Source Code:

```
ENQUEUE_UNIQUE_RENDER_COMMAND_ONEPARAMETER(
	DZRenderSutioBP_InterceptSceneBaseColor,
	UTexture2D*, vTextureAsset, TextureAsset,
	{
		/*if (!IsInRenderingThread())
			return;*/
			FRHICommandListImmediate& RHICmdList = GRHICommandList.GetImmediateCommandList();
			//计数加一避免Render完成后直接清空了GBuffer,但会慢一帧，你猜
			FSceneRenderTargets::Get(RHICmdList).AdjustGBufferRefCount(RHICmdList, 1);
			static const FString ScrollingMessage(TEXT("Hello World: "));
			GEngine->AddOnScreenDebugMessage(-1, 0.2f, FColor::Red, ScrollingMessage);
			FSceneRenderTargets& SceneContext = FSceneRenderTargets::Get(RHICmdList);
			if (SceneContext.GBufferA)
			{
				FTexture2DRHIRef vTextTarget = SceneContext.GetGBufferATexture();
				FString vSiceStr = FString::Printf(TEXT("FSceneRenderTargets GBufferA Size = %d*%d"), vTextTarget->GetSizeX(), vTextTarget->GetSizeY());

				GEngine->AddOnScreenDebugMessage(-1, 0.2f, FColor::Red, vSiceStr);
			}
			//移除
			FSceneRenderTargets::Get(RHICmdList).AdjustGBufferRefCount(RHICmdList, -1);
		}
	);
```

---

<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, 6:44pm UTC](https://forums.unrealengine.com/t/access-color-and-depth-buffer-of-each-frame/278347/13 "2020-06-22T18:44:29Z")

</div>

Make that a third in glorious 2020 trying to access the depth buffer with this technique.  
Been looking at scene render capture source code to try and figure out how they narrow it down to just SceneDepth there but it looks like they fetch it using a material function in SceneCapturePixelShader.usf

---

<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, 6:45pm UTC](https://forums.unrealengine.com/t/access-color-and-depth-buffer-of-each-frame/278347/14 "2020-06-22T18:45:55Z")

</div>

What is going on here? What is this supposed to do exactly? How does this give us access to sampling the SceneDepth Buffer?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_74614d29](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_74614d29](https://forums.unrealengine.com/u/anonymous_user_74614d29)\
**Post date:** [June 23, 2020, 10:22am UTC](https://forums.unrealengine.com/t/access-color-and-depth-buffer-of-each-frame/278347/15 "2020-06-23T10:22:18Z")

</div>

Hi, how are you able to get the Type FSceneRenderTargets? on the line `FSceneRenderTargets::Get(RHICmdList)`  
i will get an error use of undefined type. I added Renderer, RenderCore and RHI to my modules and imported RendererInterface.h I think I am missing something obvious, but cant figure it out.

---

<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 23, 2020, 3:38pm UTC](https://forums.unrealengine.com/t/access-color-and-depth-buffer-of-each-frame/278347/16 "2020-06-23T15:38:40Z")

</div>

@Hannes_Z Can you explain what this is supposed to do? Does it grab SceneDepth from a Render Target?

As far as what you are after,IDK if much has changed but if you look into UnrealClient.cpp they have some examples there using ENQUEUE\_RENDER\_COMMAND.

I’d also rather use the Viewport directly rather than a Scenecapture/Render Target unless the Viewport is technically a Render Target texture anyways. IDK.

---

<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 23, 2020, 5:13pm UTC](https://forums.unrealengine.com/t/access-color-and-depth-buffer-of-each-frame/278347/17 "2020-06-23T17:13:09Z")

</div>

Got FSceneRenderTargets to work.

Need to add the modules to the private dependcies since SceneRenderTargets.h is in a private folder.

so in Build.cs need

PrivateDependencyModuleNames.AddRange(new string[] { “Renderer”, “RenderCore”, “RHI” });

then include

```
#include "Runtime/Renderer/Private/PostProcess/SceneRenderTargets.h"
```

---

<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 26, 2020, 1:29am UTC](https://forums.unrealengine.com/t/access-color-and-depth-buffer-of-each-frame/278347/18 "2020-06-26T01:29:31Z")

</div>

you can follow this:

[https://vhope.cf/zh/posts/ue/ue-depth/](https://vhope.cf/zh/posts/ue/ue-depth/)

---

<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 26, 2020, 1:30am UTC](https://forums.unrealengine.com/t/access-color-and-depth-buffer-of-each-frame/278347/19 "2020-06-26T01:30:46Z")

</div>

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?

 ![305464-myerror-min.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/9/3/e93597e88770bc551d1ef2ef7f1f78bdee985eb0.png)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_74614d29](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_74614d29](https://forums.unrealengine.com/u/anonymous_user_74614d29)\
**Post date:** [June 26, 2020, 8:38am UTC](https://forums.unrealengine.com/t/access-color-and-depth-buffer-of-each-frame/278347/20 "2020-06-26T08:38:34Z")

</div>

did you manage to get correct data out of this?  
I tried multiple various picture sizes and none produced a useful result.  
Can you also Access custom depth or stencil buffer with this method?

[Next page](https://forums.unrealengine.com/t/access-color-and-depth-buffer-of-each-frame/278347.md?page=2)
