Search Unity

Volume Fog shader

Discussion in 'Shaders' started by aubergine, Jan 31, 2011.

  1. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
    Here is a basic sort of fake volume fog, you can arrange the fog to disappear at altitudes.

    Code (csharp):
    1. Shader "Fog-Altitude" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (1,1,1,1)
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}
    5.         _FogNear ("Fog Near", range (0,100)) = 5
    6.         _FogFar ("Fog Far", range (0,100)) = 10
    7.         _FogAltScale ("Fog Alt. Scale", range (0,100)) = 10
    8.         _FogThinning ("Fog Thinning", range (0,100)) = 100
    9.         _FogColor ("Fog Color", Color) = (0.5,0.5,0.5,1)
    10.     }
    11.     SubShader {
    12.         Tags { "RenderType"="Opaque" }
    13.         LOD 200
    14.        
    15.         CGPROGRAM
    16.         #pragma surface surf Lambert vertex:vert
    17.  
    18.         float4 _Color;
    19.         sampler2D _MainTex;
    20.         float _FogNear, _FogFar, _FogAltScale, _FogThinning;
    21.         float4 _FogColor;
    22.  
    23.         struct Input {
    24.             float2 uv_MainTex;
    25.             float3 viewDir;
    26.             float4 pos;
    27.         };
    28.        
    29.         void vert (inout appdata_full v, out Input o) {
    30.             //o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    31.             o.pos = mul(_Object2World, v.vertex);
    32.  
    33.         }
    34.  
    35.         void surf (Input IN, inout SurfaceOutput o) {
    36.             half4 c = tex2D (_MainTex, IN.uv_MainTex);
    37.             float d = length(IN.viewDir);
    38.             float l = saturate((d - _FogNear) / (_FogFar - _FogNear) / clamp(IN.pos.y / _FogAltScale + 1, 1, _FogThinning));
    39.             //o.Albedo = c.rgb * _Color.rgb;
    40.             o.Albedo = lerp(c.rgb * _Color.rgb, _FogColor, l);
    41.             o.Alpha = c.a * _Color.a;
    42.         }
    43.         ENDCG
    44.     }
    45.     FallBack "Diffuse"
    46. }
    EDIT:
    Anything below y 0 is considered fully fogged, above is calculated according to FogAltitudeScale and fogthinning values
    All the input values about fog are real world values (fog near, fog far, fog altitude..etc.)
     

    Attached Files:

    • $1.jpg
      $1.jpg
      File size:
      33.4 KB
      Views:
      7,768
    Last edited: Jan 31, 2011
  2. Cameron_SM

    Cameron_SM

    Joined:
    Jun 1, 2009
    Posts:
    915
    Nice!

    You should add all of your shaders to the unity Wiki.
     
  3. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
    The shaders are for learning purposes at the moment (my own self learning purposes :p) i feel more comfortable with these now,and i think i will make usable sets of these shaders then i might add those.
     
  4. Cameron_SM

    Cameron_SM

    Joined:
    Jun 1, 2009
    Posts:
    915
    I've considered doing this one myself once but wasn't ever sure how to deal with transparency within a scene.

    How does this fare with transparent objects?
     
  5. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
    I dont think i will ever need alpha for such an effect, but anyways here it is:

    Code (csharp):
    1. Shader "Fog-Altitude" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (1,1,1,1)
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}
    5.         _FogNear ("Fog Near", range (0,50)) = 5
    6.         _FogFar ("Fog Far", range (0,50)) = 10
    7.         _FogAltScale ("Fog Alt. Scale", range (0,10)) = 10
    8.         _FogThinning ("Fog Thinning", range (0,100)) = 100
    9.         _FogColor ("Fog Color", Color) = (0.5,0.5,0.5,1)
    10.     }
    11.     SubShader {
    12.         Tags { "RenderType"="Opaque" }
    13.         LOD 200
    14.        
    15.         CGPROGRAM
    16.         #pragma vertex vert
    17.         #pragma surface surf Lambert alpha
    18.  
    19.         float4 _Color;
    20.         sampler2D _MainTex;
    21.         float _FogNear, _FogFar, _FogAltScale, _FogThinning;
    22.         float4 _FogColor;
    23.  
    24.         struct Input {
    25.             float2 uv_MainTex;
    26.             float3 viewDir;
    27.             float4 pos;
    28.         };
    29.        
    30.         void vert (inout appdata_full v, out Input o) {
    31.             //o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    32.             o.pos = mul(_Object2World, v.vertex);
    33.  
    34.         }
    35.  
    36.         void surf (Input IN, inout SurfaceOutput o) {
    37.             half4 c = tex2D (_MainTex, IN.uv_MainTex);
    38.             float d = length(IN.viewDir);
    39.             float l = saturate((d - _FogNear) / (_FogFar - _FogNear) / clamp(IN.pos.y / _FogAltScale + 1, 1, _FogThinning));
    40.             //o.Albedo = c.rgb * _Color.rgb;
    41.             o.Albedo = lerp(c.rgb * _Color.rgb, _FogColor, l);
    42.             o.Alpha = lerp(c.a * _Color.a, _FogColor.a, l);
    43.         }
    44.         ENDCG
    45.     }
    46.     FallBack "Diffuse"
    47. }
     
    Last edited: Feb 2, 2011
  6. wlad_s

    wlad_s

    Joined:
    Feb 18, 2011
    Posts:
    148
    Wow, that's great!! Just what I was thinking about. But I thought about if it's possible to modify Unity fog. As far as I understand you've made a shader for individual objects?

    What's the best way to use this? Can it automatically fog the whole scene?

    Sorry if I ask stupid questions :) I'm new to all this.

    Thanx
     
  7. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
    It is per object, if you want to use it in whole scene, you will need to change any shaders currently in scene and add the fog part to it. Or you may ask a feature request to unity to include a smiliar method for vertical fog.
     
  8. Gogin

    Gogin

    Joined:
    Nov 9, 2013
    Posts:
    60
    I got error with Shader "Fog-Altitude" , what I need to fill?