Search Unity

Simulate fog with gradient shader

Discussion in 'Shaders' started by MaciejZi, Oct 24, 2014.

  1. MaciejZi

    MaciejZi

    Joined:
    May 19, 2014
    Posts:
    5
    Hello Everyone!

    I am having a little problem with making fog effect in my Unity game. Have a look at the picture:

    The effect is achieved by having a shader that applies color gradients to the towers. All looks good, but when I add lights to the scene, it changes to this:

    Oops. The fog effect disappeared. I am happy with the shading effect on the upper parts of the towers, but the lower part should still get solid blue. My shader looks like this:
    Code (Boo):
    1. Shader "Custom/ColorGradient" {
    2.     Properties {
    3.         _ColorA ("ColorA", Color) = (1,1,1,1)
    4.         _ColorB ("ColorB", Color) = (1,1,1,1)
    5.         _Start ("Start", Float) = 2.0
    6.         _End ("End", Float) = 1.0
    7.     }
    8.     SubShader {
    9.         Tags { "RenderType"="Opaque" }
    10.         LOD 200
    11.        
    12.         CGPROGRAM
    13.         #pragma surface surf Lambert
    14.  
    15.         float4 _ColorA;
    16.         float4 _ColorB;
    17.         float _Start;
    18.         float _End;
    19.  
    20.         struct Input {
    21.             float2 uv_MainTex;
    22.             float3 worldPos;
    23.         };
    24.  
    25.         void surf (Input IN, inout SurfaceOutput o) {
    26.             if(IN.worldPos.y>_Start){
    27.                 o.Albedo = _ColorA.rgb;
    28.                 o.Emission = 0;
    29.             }
    30.             if(IN.worldPos.y<=_Start && _End<=IN.worldPos.y) {
    31.                 float d = (IN.worldPos.y-_End)/(_Start-_End);
    32.                 o.Albedo = lerp(_ColorA, _ColorB, 1.0-d).rgb;
    33.                 o.Emission =  (1.0-d) * lerp(_ColorA, _ColorB, 1.0-d).rgb;
    34.             }
    35.             if(IN.worldPos.y<_End) {
    36.                 o.Albedo = _ColorB.rgb;
    37.                 o.Emission = _ColorB.rgb;
    38.             }
    39.         }
    40.         ENDCG
    41.     }
    42.     FallBack "Diffuse"
    43. }
    Does anyone know how to fix this? Thanks a lot in advance for any suggestions.
     
  2. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
    You should not use emission, implement your own lighting function, pass fog color in it and add to final color with lerp function.