accessing depth information of rendertexture
Hey,
I can create a RenderTexture and have the cam render into it.
depth = new RenderTexture(Width, Height, 16);
i have specified a depth buffer of 16 bit. How can I access the depth values of this 16 bit depth buffer via scripting?
(i need both color and depth values, if possible with only one draw call)
Have you seen this page:
http://unity3d.com/support/documentation/ScriptReference/RenderTextureFormat.Depth.html
You specify it when you create your rendertexture how it will be used.
That doc seems to tell how a shader might access it. How would a c# script access it? I'm assu$$anonymous$$g that was what the original question was about. If not, will open new question.
Answer by weatxyz · Dec 24, 2016 at 11:48 PM
I'm going to resurrect this post since I have the same question, and think the original commentor didn't quite understand the question.
I know a render texture is a texture that you can render to instead of the frame buffer. You can treat that texture the same as any other texture. This means it's a Texture2D and it can be bound to a 2D texture property to be used in a shader.
The OP is asking where the Depth Texture2D is. I did some OpenGL programming a long time ago and thought that you could access the depth buffer somehow, or maybe it's just an option you pass into the binding statement that enables the z buffer for when the colors are drawn, and that's it. After that it's gone. Is that the case, or can the depth values be accessed like the colors in a render texture?
Thanks :)
O$$anonymous$$, found this at gamedev.net, although he's only setting up the FBO for depth.
glGenFramebuffers(1, &m_shadow$$anonymous$$apFboId);
glGenTextures(1, &m_shadow$$anonymous$$apTextureId);
// Allocate GPU-memory for the depth-texture.
glBindTexture(GL_TEXTURE_2D, m_shadow$$anonymous$$apTextureId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_CO$$anonymous$$PONENT32,
m_shadow$$anonymous$$apSize, m_shadow$$anonymous$$apSize, 0, GL_DEPTH_CO$$anonymous$$PONENT, GL_FLOAT, 0);
// Bind the texture to the framebuffers depth-attachment.
glBindFramebuffer(GL_FRA$$anonymous$$EBUFFER, m_shadow$$anonymous$$apFboId);
glFramebufferTexture(GL_FRA$$anonymous$$EBUFFER, GL_DEPTH_ATTACH$$anonymous$$ENT, m_shadow$$anonymous$$apTextureId, 0);
// Tell the Framebuffer we won't provide any color-atachments.
glDrawBuffer(GL_NONE); // For depth-only-renderings, if you need also frag-color don't use this.
if (glCheckFramebufferStatus(GL_FRA$$anonymous$$EBUFFER) != GL_FRA$$anonymous$$EBUFFER_CO$$anonymous$$PLETE)
{ ... }
So in this example he's only generating and then binding a 32 bit depth texture to be used for shadow maps, but in this code snippet it's clear that the depth is a texture, so it's possible to access it...somehow. He does mention the color buffer in there, he's just not setting up the FBO in that way is all.
Is this a feature request?
Here's a sample code snippet of what I'm doing:
// render texture is set up as an ARGB32 with 32 bit depth
CameraRef.targetTexture = renderTexture;
// render the colors to ARGB and should automatically render to 32 bit depth
CameraRef.Render();
// assign the awesomeEffectShader to the camera that renders the awesome effect
CameraAwesomeEffect.SetReplacementShader(awesomeEffectShader, "RenderType");
// set renderTexure like usual to be used as an ARGB texture
Shader.SetGlobalTexture("_$$anonymous$$ainTex", renderTexture);
// I need access to the depth texture so I can use it in my shader, but where is it?
Shader.SetGlobalTexture("_DepthTex", ???);
// render awesome effect
CameraAwesomeEffect.Render();