Search Unity

Depth of field auto focus from depth texture, or how to get a float set in a shader elsewhere.

Discussion in 'Shaders' started by maxxa05, Nov 21, 2014.

  1. maxxa05

    maxxa05

    Joined:
    Nov 17, 2012
    Posts:
    186
    So, I'm trying to get the average screen depth from the depth texture in an image effect script, so I can auto focus the depth of field effect. Basically, I want to sample the depth texture and determine my focus point more accurately than by doing a bunch of physic raycasts. Since reading from a RenderTexture from the CPU is so slow, I have to find another way to process it. I know I could probably sample from the Depth texture in the vertex shader, but I can't really keep the state from frame to frame (for smoothing the auto focus).

    Is there any reasonably fast way to get/set data between 2 materials or between 2 frames, for example, setting a global variable from a shader? Otherwise, is there a faster way to send a float from the GPU to the CPU other than Texture2D.ReadPixels?