Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Getting pixel depth from v2f_img from OnRenderImage

Discussion in 'General Graphics' started by Newcomma, Mar 5, 2017.

  1. Newcomma

    Newcomma

    Joined:
    Feb 10, 2015
    Posts:
    89
    Hi.

    I want to write a full screen fog shader as a material using Graphics.Blit in the OnRenderImage. However the pixel pos passed from v2f_img doesn't contain distance from camera information (as presumably it's a render texture)

    So my next thought was to try to use the Z Buffer. I tried enabling the camera writing to the Z buffer, and then trying to read that Z Buffer value as noted in this post (https://forum.unity3d.com/threads/d...accurate-in-just-a-few-units-distance.187915/) like so:

    Code (CSharp):
    1.             inline float ZBufferRead(float4 projpos)
    2.             {
    3.                 return LinearEyeDepth(UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(projpos))));
    4.             }
    and calling it via

    Code (csharp):
    1. float distFromCam = ZBufferRead(i.pos);
    However I'm getting a uniform number back from the ZBufferRead call across all frags, and this value is somewhere around 2^14, so I assume I'm doing something incorrectly here.

    Is my approach wrong or am I missing something?

    thanks

    (Entire Shader::)
    Code (CSharp):
    1. // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
    2.  
    3. Shader "Custom/Fog"
    4. {
    5.     Properties
    6.     {
    7.         _MainTex ("Texture", 2D) = "white" {}
    8.         _FogCol("FogColor", COLOR) = (1,0,0,1)
    9.         _FogDistance("Fog Distance", float) = 10
    10.     }
    11.     SubShader
    12.     {
    13.         Tags { "RenderType"="Opaque" }
    14.  
    15.         Pass
    16.         {
    17.             CGPROGRAM
    18.             #pragma vertex vert_img
    19.             #pragma fragment frag
    20.            
    21.             #include "UnityCG.cginc"
    22.             uniform sampler2D _CameraDepthTexture;
    23.  
    24.             inline float ZBufferRead(float4 projpos)
    25.             {
    26.                 return LinearEyeDepth(UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(projpos))));
    27.             }
    28.  
    29.             sampler2D _MainTex;
    30.             float4 _FogCol;
    31.             float _FogDistance;
    32.            
    33.             fixed4 frag (v2f_img i) : COLOR
    34.             {
    35.                 fixed4 baseCol = tex2D(_MainTex, i.uv);
    36.                 float distFromCam = ZBufferRead(i.pos);
    37.                 float colorLerp = distFromCam / _FogDistance;
    38.                 fixed4 color = lerp(baseCol, _FogCol, colorLerp);
    39.                 return color;
    40.             }
    41.             ENDCG
    42.         }
    43.     }
    44. }
    45.  
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    You also need the vert function and v2f struct from that post you linked. That linked post passes a "projPos" to the fragment shader which is what you need to do as well.