Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Flipped GrabScreenPos in Forward

Discussion in 'Shaders' started by Phantomx, Dec 16, 2015.

  1. Phantomx

    Phantomx

    Joined:
    Oct 30, 2012
    Posts:
    202
    Hey,
    I'm having trouble to display depth texture on a plane in forward rendering.
    It looks like it's flipped in Y, but I can't just flip the uv in Y because the position will be incorrect.
    It works fine in deferred, only forward causes problem.

    here's the shader code:
    Code (CSharp):
    1. Shader "Custom/Depth" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         _InvFade ("Soft Particles Factor", Range(0.01,1)) = 1.0
    6.     }
    7.     SubShader {
    8.         Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    9.         Cull Off Lighting Off ZWrite Off
    10.         Pass {
    11.      
    12.             CGPROGRAM
    13.             #pragma vertex vert
    14.             #pragma fragment frag
    15.             #include "UnityCG.cginc"
    16.          
    17.             sampler2D_float _CameraDepthTexture;
    18.             float _InvFade;
    19.          
    20.          
    21.             struct appdata_t {
    22.                 float4 vertex : POSITION;
    23.             };
    24.  
    25.             struct v2f {
    26.                 float4 vertex : SV_POSITION;
    27.                 float4 projPos : TEXCOORD2;
    28.  
    29.             };
    30.  
    31.             v2f vert (appdata_t v)
    32.             {
    33.                 v2f o;
    34.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    35.                 o.projPos = ComputeGrabScreenPos(o.vertex);
    36.                 COMPUTE_EYEDEPTH(o.projPos.z);
    37.  
    38.                 return o;
    39.             }
    40.  
    41.             fixed4 frag (v2f i) : SV_Target
    42.             {
    43.  
    44.                 float sceneZ = LinearEyeDepth (SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)));
    45.                 float partZ = i.projPos.z;
    46.                 float depth = saturate (_InvFade * (sceneZ-partZ));
    47.                  
    48.              
    49.                 return depth;
    50.             }
    51.             ENDCG
    52.         }
    53.     }
    54. }
     
    Last edited: Dec 16, 2015
  2. Phantomx

    Phantomx

    Joined:
    Oct 30, 2012
    Posts:
    202
    Also, this happens only in game view, always fine in scene view
     
  3. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    You gotta flip the Y.

    #if UNITY_UV_STARTS_AT_TOP
    if (_MainTex_TexelSize.y < 0)
    o.projPos..y = 1 - o.projPos.y;
    #endif
     
  4. Phantomx

    Phantomx

    Joined:
    Oct 30, 2012
    Posts:
    202
    yeah like I said this won't work because the position is not correct anymore.
     
  5. Nobuu-Nakata

    Nobuu-Nakata

    Joined:
    Jul 12, 2012
    Posts:
    3
    Same issue here. This is a bug on forward renderer I think.
     
  6. Phantomx

    Phantomx

    Joined:
    Oct 30, 2012
    Posts:
    202
    yes, I submitted a bug report and they said it's going to be fixed in 5.4 ...