Search Unity

Basic Deferred Shader

Discussion in 'Shaders' started by HWDKoblenz, Jun 18, 2018.

  1. HWDKoblenz

    HWDKoblenz

    Joined:
    Apr 3, 2017
    Posts:
    19
    Hello everyone,

    I'm quite new to Unity Shaders and want to find out how a simple deferred shader works in unity. I found some tutorials online but it's not that what i know from my studies with openGL and GLSL.

    Well I know that deferred shading works with multiple passes. First fill your g-buffer and use your results in a second pass. With my openGL setup it was quite easy that i filled the buffer and then send these textures to the second pass into my fragment shader. So in Unity it seams to be quite easy to make multiple passes in one shader file. This is a little pseudo Code I already have until now.

    Code (CSharp):
    1.  
    2. Shader "Deferred Shader"
    3. {
    4.     Properties
    5.     {
    6.         ...some properties for inspector.
    7.     }
    8.         SubShader
    9.     {
    10.         // My First Pass to fill G-Buffer
    11.         Pass
    12.     {
    13.         Tags{ "LightMode" = "Deferred" }
    14.         // Data that my fragmentshader has to safe
    15.         struct fragmentdata
    16.     {
    17.         half4 albedo : SV_Target0;
    18.         half4 specular : SV_Target1;
    19.         half4 normal : SV_Target2;
    20.         half4 position : SV_Target3;
    21.     };
    22.     vertexdata vert(appdata_base)
    23.     {
    24.         vertexdata vs;
    25.         .. do simple calcus
    26.             return vs
    27.     }
    28.     fragmentdata frag(vertexdata vs)
    29.     {
    30.         //Fill my textures for SVTarget 0 -3 with data from inspector and VS shader.
    31.     }
    32.     }
    33.  
    34.  
    35.  
    This seams to work... If i switch to deferred lighting in unity and check the single channels i can see the correct result.

    So now I want to use these textures in my second "lightpass" The following code i placed below the deferred base:

    Code (CSharp):
    1.  
    2.  
    3.                  
    4. // My second Pass to make lighting
    5. Pass
    6. {
    7.     Tags{ "LightMode" = "ForwardBase" }
    8.  
    9.  
    10.     //ini gbuffer read var.
    11.     // these vars are global and should have the result from the SV_Target 0...3 right???
    12.     sampler2D _CameraGBufferTexture0;
    13.     sampler2D _CameraGBufferTexture1;
    14.     sampler2D _CameraGBufferTexture2;
    15.     sampler2D _CameraGBufferTexture3;
    16.     struct v2f
    17.     {
    18.         .
    19.         .
    20.         .
    21.     };
    22.     v2f vert(appdata_base)
    23.     {
    24.         v2f fr
    25.             .. do simple calcus
    26.             return fr
    27.     }
    28.     fixed4 frag(v2f fr)
    29.     {
    30.         //Get GBuffers with:
    31.         float4 albedo = tex2D(_CameraGBufferTexture0, fr.uv);
    32.         .
    33.         .
    34.         .
    35.         // Calculate Light with Phong
    36.         return final color //calculated by gbuffer textures.
    37.     }
    38. }
    39.  
    40.  
    This second pass has... well no effect at all. So what is my main problem? What do I have to do to setup the light pass in unity after filling the gbuffer? I hope you can get an idea of my problem :-D.

    Thank you =).
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    The forward base pass is only used for forward rendering. If your shader has a deferred pass it'll skip the forward passes when being rendered using the deferred pipeline. All deferred lighting passes are done with an internal deferred shading shader which you can override in the graphics settings. But all deferred objects will use that shader for lighting once it's overridden.
     
    kweiming likes this.
  3. HWDKoblenz

    HWDKoblenz

    Joined:
    Apr 3, 2017
    Posts:
    19
    Hello,

    ah okay I didn't see that internal shader. Okay this makes sense. Thank you very much! Now I understand a tutorial that i found yesterday... Thanks for your hint <3.
     
  4. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    328


    Github's source code included in video description.