Search Unity

Question Novice Shader Question

Discussion in 'Universal Render Pipeline' started by MizterRaven, Jun 9, 2020.

  1. MizterRaven

    MizterRaven

    Joined:
    Apr 28, 2015
    Posts:
    8
    Hello. I am trying to learn to write my own shaders and I've gotten to where I want to start using _CameraDepthTexture. In particular, I want to write my own logic to do an outline and I'd like to use that texture among others.

    What I want to do right now: render specific layer of objects as their depth.

    Where I am at: (The cube is the only object on the layer to be displayed as depth)
    Result A.png
    As you can see, the cube is taking the entire depth texture and painting it on each face, but I only want the cube's portion of that texture to be painted.

    Relevant Settings:
    ForwardRenderer.png
    Settings.png

    The Shader Code:
    Code (CSharp):
    1.  
    2. Shader "Unlit/DepthShade"
    3. {
    4.     Properties
    5.     {
    6.         _MainTex ("Texture", 2D) = "white" {}
    7.     }
    8.  
    9.     SubShader
    10.     {
    11.         Tags { "RenderType"="Opaque" }
    12.         LOD 100
    13.  
    14.         Pass
    15.         {
    16.             CGPROGRAM
    17.             #pragma vertex vert
    18.             #pragma fragment frag
    19.             // make fog work
    20.             #pragma multi_compile_fog
    21.  
    22.             #include "UnityCG.cginc"
    23.  
    24.             struct appdata
    25.             {
    26.                 float4 vertex : POSITION;
    27.                 float2 uv : TEXCOORD0;
    28.             };
    29.  
    30.             struct v2f
    31.             {
    32.                 float2 uv : TEXCOORD0;
    33.                 float4 vertex : SV_POSITION;
    34.             };
    35.  
    36.             sampler2D _MainTex;
    37.             float4 _MainTex_ST;
    38.             sampler2D _CameraDepthTexture;
    39.             float4 _CameraDepthTexture_ST;
    40.  
    41.             v2f vert (appdata v)
    42.             {
    43.                 v2f o;
    44.                 o.vertex = UnityObjectToClipPos(v.vertex);
    45.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    46.  
    47.                 return o;
    48.             }
    49.  
    50.             fixed4 frag (v2f i) : SV_Target
    51.             {
    52.                 // sample the texture
    53.                 fixed4 col = tex2D(_CameraDepthTexture, i.uv);
    54.                
    55.                 return col.r;
    56.             }
    57.             ENDCG
    58.         }
    59.     }
    60. }
    61.  
    I am assuming I should transform the UV coordinates but I just don't know what to do to them.
    Any help is appreciated.
     
  2. MizterRaven

    MizterRaven

    Joined:
    Apr 28, 2015
    Posts:
    8
    ...for anybody else who ends up here - what you want to do is use ComputeGrabScreenPos to get the correct coordinates to sample the depth texture.

    Here are a couple of links that helped me.
    https://forum.unity.com/threads/how...en-space-in-framgment-shader-function.219843/

    https://docs.unity3d.com/Manual/SL-GrabPass.html

    and the shader code I used that produced the result I wanted.

    Code (CSharp):
    1. Shader "Unlit/DepthShade"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.     }
    7.  
    8.     SubShader
    9.     {
    10.         Tags { "RenderType"="Opaque" }
    11.         LOD 100
    12.  
    13.         Pass
    14.         {
    15.             CGPROGRAM
    16.             #pragma vertex vert
    17.             #pragma fragment frag
    18.             // make fog work
    19.             #pragma multi_compile_fog
    20.  
    21.             #include "UnityCG.cginc"
    22.  
    23.             struct appdata
    24.             {
    25.                 float4 vertex : POSITION;
    26.                 float2 uv : TEXCOORD0;
    27.             };
    28.  
    29.             struct v2f
    30.             {
    31.                 float2 uv : TEXCOORD0;
    32.                 float4 grabPos : TEXCOORD1;
    33.                 float4 vertex : SV_POSITION;
    34.             };
    35.  
    36.             sampler2D _MainTex;
    37.             float4 _MainTex_ST;
    38.             sampler2D _CameraDepthTexture;
    39.             float4 _CameraDepthTexture_ST;
    40.  
    41.             v2f vert (appdata v)
    42.             {
    43.                 v2f o;
    44.                 o.vertex = UnityObjectToClipPos(v.vertex);
    45.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    46.                
    47.                 o.grabPos = ComputeGrabScreenPos(o.vertex);
    48.  
    49.                 return o;
    50.             }
    51.  
    52.             fixed4 frag (v2f i) : SV_Target
    53.             {
    54.                 // sample the texture
    55.                 fixed4 col = tex2Dproj(_CameraDepthTexture, i.grabPos);
    56.                
    57.                 return col.r;
    58.             }
    59.             ENDCG
    60.         }
    61.     }
    62. }
    63.