Search Unity

Question Trying to get a shader to render to depth post processing effect.

Discussion in 'Scripting' started by OliverWithAnO, Jan 27, 2023.

  1. OliverWithAnO

    OliverWithAnO

    Joined:
    Sep 24, 2022
    Posts:
    4
    Hello,

    I'm a beginner with unity shaders and have come across an issue that I can't really find information on. I'm currently trying to make a "toon shader" with outlines. I have implemented a post processing shader that works out an ouline to objects based on depth, to do this, the materials of the object need a depth pass from my understanding, this is something I later discovered when using a different shader to study how lighting is rendered, I noticed that the outlines weren't rendering.

    Would anyone know how I could implement a depth pass to this shader?


    .shader file:
    Code (CSharp):
    1. Shader "Unlit/CelShader"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _DepthTex("Depth Texture", 2D) = "white" {}
    7.         _MainTex_ST("Main Texture ST", Vector) = (1,1,0,0)
    8.         _DepthTex_ST("Depth Texture ST", Vector) = (1,1,0,0)
    9.         _Gloss ("Gloss", Range(0,1)) = 1
    10.         _Color("Color", Color) = (1,1,1,1)
    11.     }
    12.     SubShader
    13.     {
    14.         Tags { "RenderType"="Opaque"  "Queue" ="Geometry"}
    15.         LOD 100
    16.  
    17.         GrabPass
    18.         {
    19.            
    20.         }
    21.  
    22.         //Base Pass
    23.         Pass
    24.         {
    25.             Tags{"LightMode" = "ForwardBase"}
    26.  
    27.             ZWrite Off
    28.  
    29.             CGPROGRAM
    30.             #pragma vertex vert
    31.             #pragma fragment frag
    32.  
    33.             #include "LightingInclude.cginc"
    34.             ENDCG
    35.         }
    36.  
    37.             //Add Pass
    38.         Pass
    39.         {
    40.             Tags{"LightMode" = "ForwardAdd"}
    41.  
    42.             ZWrite Off
    43.  
    44.             Blend One One
    45.             //src*1 + dst*1
    46.             CGPROGRAM
    47.             #pragma vertex vert
    48.             #pragma fragment frag
    49.             #pragma multi_compile_fwdadd
    50.             #include "LightingInclude.cginc"
    51.             ENDCG
    52.         }
    53.     }
    54. }
    .cginc file:
    Code (CSharp):
    1. #include "UnityCG.cginc"
    2. #include "Lighting.cginc"
    3. #include "AutoLight.cginc"
    4.  
    5. struct MeshData
    6. {
    7.     float4 vertex : POSITION;
    8.     float3 normal : NORMAL;
    9.     float2 uv : TEXCOORD0;
    10. };
    11.  
    12. struct Interpolator
    13. {
    14.     float2 uv : TEXCOORD0;
    15.     float4 vertex : SV_POSITION;
    16.     float3 normal : TEXCOORD1;
    17.     float3 wPos : TEXCOORD2;
    18.     LIGHTING_COORDS(3, 4)
    19. };
    20.  
    21. sampler2D _MainTex;
    22. float4 _MainTex_ST;
    23. float4 _Color;
    24. float _Gloss = 20;
    25.  
    26.  
    27. Interpolator vert(MeshData v)
    28. {
    29.     Interpolator o;
    30.     o.vertex = UnityObjectToClipPos(v.vertex);
    31.     o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    32.     o.normal = UnityObjectToWorldNormal(v.normal);
    33.     o.wPos = mul(unity_ObjectToWorld, v.vertex);
    34.     TRANSFER_VERTEX_TO_FRAGMENT(o);
    35.     return o;
    36. }
    37.  
    38. float4 frag(Interpolator i) : SV_Target
    39. {
    40.    
    41.  
    42.  
    43.     //Diffuse Lighting
    44.     float3 N = normalize(i.normal);
    45.     float3 L = normalize(UnityWorldSpaceLightDir(i.wPos)); //Directional light
    46.     float attenuation = LIGHT_ATTENUATION(i);
    47.     float3 lambert = saturate(dot(N, L));
    48.     float3 diffuseLight = (lambert * _LightColor0.xyz) * attenuation;
    49.  
    50.     //specular lighting
    51.     float3 V = normalize(_WorldSpaceCameraPos - i.wPos); //View Direction
    52.     //float3 R = reflect(-L, N); //phong
    53.     float3 H = normalize(L + V);
    54.  
    55.     float3 specularLight = saturate(dot(H, N)) * (lambert > 0); //blinn-phong
    56.  
    57.     float specularExponent = exp2(_Gloss * 11) + 2;
    58.     specularLight = pow(specularLight, specularExponent) * _Gloss * attenuation; //speculat exponent
    59.     specularLight *= _LightColor0.xyz;
    60.  
    61.     return float4(diffuseLight * _Color + specularLight, 1);
    62. }
    If this code looks familiar, it is because I'm following along with Freya Holmers explanation of lighting:


    Thank you
     
  2. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    OliverWithAnO likes this.
  3. OliverWithAnO

    OliverWithAnO

    Joined:
    Sep 24, 2022
    Posts:
    4
    FIX: I might seem quite stupid for this, but it was because I had Zwrite off.

    I could have sworn that I tried turning it on before to no result.
     
  4. OliverWithAnO

    OliverWithAnO

    Joined:
    Sep 24, 2022
    Posts:
    4
    Hello,

    Yes, I did realize that i had Zwrite off after posting lol. I did have it on but it didn't fix the issue on its own, so i turned it off to see what difference it would make, and forgot to turn it back on lo.

    I actually had to put a shadow caster pass in the shader for it to work, which was with info I got from this dev log: