Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Fake radial fog on vert / frag glow shader

Discussion in 'Shaders' started by pixelknight, Sep 3, 2016.

  1. pixelknight

    pixelknight

    Joined:
    Jul 12, 2011
    Posts:
    93
    Hi guys/gals,

    I'm working on a y ground "fog" enabled unlit glow shader with an enhancement to have a spherical or cylendrical "fog" coloring at xz distance. Working on Unity 5.3.5.

    For technical reasons, we cannot use the the Unity fog as a part of the shader so manually coloring the objects.

    Expecting to get a fog all around us at a constant tone, but getting different fog saturation depending on the camera angle I look at it. Anyone know what I might be doing incorrect?

    Almost seems the closer I am to the origin, anything that is located on the other side of an axis to look at appears differently than once the camera crosses that axis.

    Code (Shader):
    1.  
    2. Shader "UnlitGlow"
    3. {
    4.  
    5.     Properties
    6.     {
    7.         _MainTex("Base (RGB)", 2D) = "white" {}
    8.         _EffectBottom("Start of Glow/Fog", Float) = 0
    9.         _EffectHeight("Distance of Glow/Fog", Float) = -30
    10.         _FogColor("Fog Color", Color) = (0.6,0.5,0.4,1)
    11.  
    12.         _FogStart("Start of Horizontal Glow/Fog", Float) = 0
    13.         _FogEnd("End of Horizontal Glow/Fog", Float) = 0
    14.     }
    15.  
    16.     SubShader
    17.     {
    18.         Tags{ "RenderType" = "SimpleGlow" "Queue" = "Transparent" }
    19.         LOD 100
    20.  
    21.         Pass
    22.             {
    23.             CGPROGRAM
    24.             #pragma vertex vert
    25.             #pragma fragment frag
    26.  
    27.             #include "UnityCG.cginc"
    28.  
    29.             struct appdata_t
    30.             {
    31.                 float4 vertex : POSITION;
    32.                 float2 texcoord : TEXCOORD0;
    33.             };
    34.  
    35.             struct v2f
    36.             {
    37.                 float4 vertex : SV_POSITION;
    38.                 fixed4 color : COLOR;
    39.                 half2 texcoord : TEXCOORD0;
    40.                 float3 worldPos: TEXCOORD4;
    41.             };
    42.  
    43.             sampler2D _MainTex;
    44.             float4 _MainTex_ST;
    45.             half _EffectBottom;
    46.             half _EffectHeight;
    47.             half _FogStart;
    48.             half _FogEnd;
    49.             fixed4 _FogColor;
    50.  
    51.             v2f vert(appdata_t v)
    52.             {
    53.                 v2f o;
    54.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    55.                 o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    56.                 o.worldPos = mul(_Object2World, v.vertex).xyz;
    57.  
    58.                 //alpha of 0 indicates no glow -- we need no glow at the (low y) start of the effect
    59.                 //and max glow at top (bottom + height) of the effect
    60.  
    61.                 o.color.a = clamp((o.worldPos.y - _EffectBottom) / _EffectHeight, 0, 1);
    62.                 return o;
    63.             }
    64.  
    65.             fixed4 frag(v2f i) : COLOR
    66.             {
    67.                 fixed4 col = tex2D(_MainTex, i.texcoord);
    68.                //as vertex world pos gets higher (greater y), the alpha also gets higher
    69.                //thus we fade into the tex color the greater alpha value we have
    70.  
    71.                float zpos = mul(UNITY_MATRIX_MVP, i.vertex).z;
    72.                float fogVar = saturate(1.0 - (_FogEnd - zpos) / (_FogEnd - _FogStart));
    73.                col.rgb = lerp(col.rgb, _FogColor, fogVar);
    74.  
    75.                col.rgb = lerp(_FogColor.rgb, col.rgb, i.color.a);
    76.                col.a = i.color.a;    //sets the glow fade to match the fog fade
    77.                return col;
    78.             }
    79.             ENDCG
    80.         }
    81.     }
    82. }
    83.  
    84.  
    85.  
    86.  
     
    Last edited: Sep 3, 2016
  2. pixelknight

    pixelknight

    Joined:
    Jul 12, 2011
    Posts:
    93
    Martin_H likes this.