Symptoms:
- I have two RenderTextures with format RenderTexture.Depth, Graphics.Blit is not copying the depth values from one to the other.
Cause:
Graphics.Blit draws a quad with Z-Write off, so the depth will not be copied from one RenderTexture to another.
Resolution:
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; return 0; |
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 m_DepthCopyMat.SetTexture("_MyDepthTex", m_SrcDepthTexture); // Do a Blit using the DepthCopy Material/Shader Graphics.Blit(m_SrcDepthTexture, m_DstDepthTexture, m_DepthCopyMat); |
More Information:
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:
Graphics.SetRenderTarget(rendertexture.colorBuffer, depthRT.depthBuffer); |
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.
Comments
2 comments
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.
there is too few example code here, with important details missing:
what is m_SrcDepthTexture ? a render texture usually has a DepthBuffer attached, not a texture. graphics.blit doesnt work with buffers, but with textures ... so this is unclear and needs a clearer example with 3 more detailed lines.
Please sign in to leave a comment.