Reputation: 640
I need to extract the depth values for the camera screen and write to a file. I read that _CameraDepthTexture is only accessible from shaders. I am not very familiar with shaders is it possible to write to file in shader code? Or is there any other way to extract these information from the _CameraDepthTexture?
Upvotes: 3
Views: 4332
Reputation: 26
This "might" work in theory, but I have not tested it. I am assuming this is some sort of on-demand debug or test that will be called when needed? This will not be very performance friendly if you try to write out every frame to file.
1) Create a second camera that renders to texture. Put it in the same position and orientation as your main camera.
2) Enable the depth buffer on the second camera.
3) Write a simple pixel shader that takes the depth buffer values and outputs them as the color value.
4) Convert the rendered texture to png or jpg using the Texture2D facilities supplied by Unity and write to file.
You might be able to do this using just the main camera by enabling the post-process effect for one or two frames on the main camera and grabbing it. Again, I haven't tested this.
This is a complete hack, but there is no way to write a file directly in a shader (it runs on the GPU) and no way to get the final depth buffer in scripts.
This gentleman shows you the basics of this principle at work as a post-processing effect. You would have to extend his example to get what you are looking for.
Unity Shaders – Depth and Normal Textures (Part 1) - by William Chyr
Sorry I can't be more specific with a working example. I don't have access to my Unity machines right now to test this.
Upvotes: 1