Search Unity

Ambient Occlusion problem in shader

Discussion in 'Shaders' started by dmennenoh, Sep 5, 2019.

  1. dmennenoh

    dmennenoh

    Joined:
    Jul 1, 2015
    Posts:
    379


    I found a shader that allows me to adjust the brightness and contrast of the texture. It works great, however when I turn on AO the shading added to the backside of the helmet is applied over the face object.

    It's a simple shader... here is the code:

    Code (CSharp):
    1. Shader "Custom/Brightness Effect" {
    2.     Properties{
    3.         _MainTex("Base (RGB)", 2D) = "white" {}
    4.         _Brightness("Brightness", float) = 1
    5.         _Contrast("Contrast", float) = 1
    6.     }
    7.  
    8.         SubShader{
    9.            
    10.             Pass {
    11.  
    12.                 CGPROGRAM
    13.                 #pragma vertex vert_img
    14.                 #pragma fragment frag
    15.                 #include "UnityCG.cginc"
    16.  
    17.                 uniform sampler2D _MainTex;
    18.                 uniform float _Brightness;
    19.                 uniform float _Contrast;
    20.  
    21.                 fixed4 frag(v2f_img i) : SV_Target
    22.                 {
    23.                     fixed4 output = tex2D(_MainTex, i.uv);
    24.                     output = output * _Brightness;
    25.                     output = (output - 0.5) * _Contrast + 0.5;
    26.                     return output;
    27.                 }
    28.                 ENDCG
    29.             }
    30.         }
    31.  
    32.             Fallback off
    33. }
     
  2. dmennenoh

    dmennenoh

    Joined:
    Jul 1, 2015
    Posts:
    379
    I found if I set the Render Queue in the material to 3000 (Transparent) then the geometry renders over the AO, but the AO doesn't affect the geometry at all which I want it to...
     
  3. dmennenoh

    dmennenoh

    Joined:
    Jul 1, 2015
    Posts:
    379
    So, I set the render queue back to 2000 (From Shader) and changed Fallback off to Fallback "Diffuse" and it seems to have fixed the AO issue. I just have no idea why it fixed it.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    Because that shader has no shadow caster pass, which means it didn't appear in the camera depth texture which the SSAO pass uses to calculate the AO.

    Making it part of the transparent queue fixed it by moving having it draw after AO was applied.

    Adding the fallback fixed it by that adding a shadow caster pass for the camera depth texture generation to use.
     
  5. dmennenoh

    dmennenoh

    Joined:
    Jul 1, 2015
    Posts:
    379
    Thanks bgolus! I was hoping you might reply. I'm really bad at shaders.