Search Unity

Depth buffer and multiple cameras

Discussion in 'Shaders' started by raggnic, May 9, 2019.

  1. raggnic

    raggnic

    Joined:
    Sep 27, 2017
    Posts:
    13
    Hi

    I'm trying to make a viewshed effect using multiple cameras and shaders.

    I use a camera to render a depth buffer with this script attached
    Code (CSharp):
    1. public class DepthCamera : MonoBehaviour {
    2.  
    3.  
    4.      
    5.         private Camera cameraComp; // second camera
    6.         public static readonly string targetTextureName = "_TransparencyFocusMask";
    7.         public static readonly string targetDepthName = "_DepthFocusMask";
    8.  
    9.         private RenderTexture target;
    10.         private RenderTexture targetDepth;
    11.  
    12.         private void OnEnable()
    13.         {
    14.             cameraComp = GetComponent<Camera>();
    15.             cameraComp.depthTextureMode = DepthTextureMode.None;
    16.  
    17.             target = new RenderTexture(512,512, 24, RenderTextureFormat.ARGBFloat);
    18.             targetDepth = new RenderTexture(512,512, 24, RenderTextureFormat.Depth);
    19.  
    20.             Shader.SetGlobalTexture("_ColorBuffer", target);
    21.             Shader.SetGlobalTexture("_DepthBuffer", targetDepth);
    22.             Shader.SetGlobalFloat("_FarClipPlane", cameraComp.farClipPlane);
    23.             Shader.SetGlobalFloat("_NearClipPlane", cameraComp.nearClipPlane);
    24.             cameraComp.SetTargetBuffers(target.colorBuffer, targetDepth.depthBuffer);
    25.  
    26.         }
    and then I access to the buffer from my shader.
    Code (CSharp):
    1.  
    2.  
    3.             uniform  sampler2D _DepthBuffer;
    4.             uniform  sampler2D _ColorBuffer;
    5.             uniform float _FarClipPlane;
    6.             uniform float _NearClipPlane;
    7.            
    8.             fixed4 frag(v2f o) : COLOR
    9.             {
    10.                
    11.                 float depth = tex2D(_DepthBuffer, o.uv).r;
    12.                
    13.                 //depth as distance from camera in units
    14.                 depth = Linear01Depth(depth);
    15.                 depth = depth * _ProjectionParams.z;
    16.  
    17.             //tried to replace from UnityCG.cginc the computation to replace on the values from the other camera
    18. //does not work
    19.                 //depth = 1.0 / ((1 - _FarClipPlane / _NearClipPlane)* depth + (_FarClipPlane / _NearClipPlane));
    20.                 //depth = depth* _FarClipPlane;
    21.  
    22.                  return depth;
    23.             }
    For now I just display the buffer, but using the built in methods it does not work well, I assume that's because the built-in variables and functions apply to the main camera.

    I've tried to replace the builtin variables with the far/near values from the other camera, but that's even worse. I think I may be wrong somewhere.

    Any idea would be welcome, thanks for reading