Screen Space Depth

  • I would want to find the depth of every pixel on the screen space. So for every pixel, tell me how far the z distance and euclidean distance (diagonal distance) is from the camera. If it should be a shader implementation, point me to the right methods, if you may.
  • Secondly, can I get the gameobject name for each pixel (assuming it’s not pointing to a skybox or nothing). I reckon raycasting is not an option as there are many pixels.

I’m hoping someone can point me to the right direction to achieve the above two functionalities. I don’t need this to be feasible in a game setting and I don’t want an approximation (ie roughly an object is there). That said, I’m looking the computationally cheapest approach if possible as I need to run this on every frame.

Appreciate any guidance.

Any solution will be welcomed, even if it’s not ideal. Approximations included.

You could render to a RenderTexture and then retrieve the depth buffer for the current viewpoint (Unity - Manual: Cameras and depth textures)

Once you have the depth buffer you can recreate the world point of each screen pixel too. You can’t get the gameObject without raycasting though. Here’s someone else doing a similar thing: Getting world position from depth buffer... - Unity Engine - Unity Discussions

1 Like

Just a quick note: The shader example provided in Unity’s documentation does NOT work. I’ve tried to flag this with Unity’s documentation team but so far they’ve been pretty silent on this.

Here’s another site that has a working depth shader (I’ve tried it myself): http://williamchyr.com/2013/11/unity-shaders-depth-and-normal-textures/

1 Like

Thanks @tonemcbride , @BlackPete , tremendously helpful!