- I have two RenderTextures with format RenderTexture.Depth, Graphics.Blit is not copying the depth values from one to the other.
Graphics.Blit draws a quad with Z-Write off, so the depth will not be copied from one RenderTexture to another.
Create a "DepthBlit" Shader to sample and output the Depth Texture as depth values:
|// Fragment function outputs depth from _MyDepthTex to depth buffer
half4 CopyDepthBufferFragmentShader(v2f i, out float outDepth : SV_Depth) : SV_Target
float depth = SAMPLE_DEPTH_TEXTURE(_MyDepthTex, i.uv);
outDepth = depth;
The complete Shader is also in this article as an attachment. Note: the GPU needs to support GL_FragDepth extension in order to output the depth, most GPUs do, but some older mobiles might not.
Then in C# create a Material and do the Blit using it:
|// Create a Material that uses the DepthCopy Shader
Material m_DepthCopyMat = new Material(m_DepthCopyShader);
// Set the _MyDepthTex Shader Texture to our source depth texture to be copied
// Do a Blit using the DepthCopy Material/Shader
Graphics.Blit(m_SrcDepthTexture, m_DstDepthTexture, m_DepthCopyMat);
If you do not want to copy the Depth Texture but instead want to have a valid depth buffer that can be shared between the Render Targets then you can use:
before rendering a fullscreen quad for your blit.
Manual page on how to use depth textures in Unity: https://docs.unity3d.com/Manual/SL-DepthTextures.html
Information verified accurate for Unity 5.4.3p4.
This doesn't work in the non-CG pipelines (that unity recommends) and no other solutions are easily found. The complete lack of support fixing issues that are broken in unity's recommended solutions is disheartening. I'm not certain I would have chosen unity had I known the engine often doesn't work.
Please sign in to leave a comment.