Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Windows phone fog shader

Discussion in 'Shaders' started by Gnimmel, Jun 22, 2014.

  1. Gnimmel

    Gnimmel

    Joined:
    Apr 21, 2010
    Posts:
    358
    I've been trying to get fog working on windows phone and recently read it's not supported at the moment. The solution was to use a shader found here;

    http://www.software7.com/blog/unity-custom-fog-shader-on-windows-phone-8/

    I tried this and it works great on my Nokia Lumia 1520, however it means I get double fog in the editor.

    This shader takes the unity fog settings and uses them, which means the fog is turned on and works when I run the game in the editor, but its also adding in the fog from the shader. I also have objects I don't want fog on, which works on my phone, but again it gets fog in the editor.

    I've been trying to remove the connection in the shader to the unity fog settings, but I don't know a lot about shader code and I've made lots of nice purple shaders ;)

    I added some extra vars to the shader to input color and fog start and end, but so far I've only managed to get the color to work, plus it still needs the unity fog turned on in the settings.

    Can anyone give me a little help with this shader?

    Code (CSharp):
    1. Shader "Custom/Unlit_Fog" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white" {}
    4.         _Color ("Fog Color", Color) = (1,1,1,1)
    5.         _Fog_Start ("Fog Start", float) = 40
    6.         _Fog_End ("Fog Start", float) = 200
    7.  
    8.     }
    9.     SubShader {
    10.         Tags { "RenderType"="Opaque" }
    11.         LOD 200
    12.        
    13.         CGPROGRAM
    14.         #pragma surface surf Lambert finalcolor:mycolor vertex:myvert
    15.  
    16.         sampler2D _MainTex;
    17.         uniform half4 _Color;
    18.         uniform half4 unity_FogStart;
    19.         uniform half4 unity_FogEnd;
    20.  
    21.         float4 color : COLOR;
    22.         struct Input
    23.         {
    24.             float2 uv_MainTex;
    25.             half fog;
    26.         };
    27.  
    28.         void myvert (inout appdata_full v, out Input data)
    29.         {
    30.             UNITY_INITIALIZE_OUTPUT(Input,data);
    31.             float pos = length(mul (UNITY_MATRIX_MV, v.vertex).xyz);
    32.  
    33.             float diff = unity_FogEnd.x - unity_FogStart.x;
    34.             float invDiff = 1.0f / diff;
    35.             data.fog = clamp ((unity_FogEnd.x - pos) * invDiff, 0.0, 1.0);
    36.         }
    37.         void mycolor (Input IN, SurfaceOutput o, inout fixed4 color)
    38.         {
    39.             fixed3 fogColor = _Color.rgb;
    40.             #ifdef UNITY_PASS_FORWARDADD
    41.             fogColor = 0;
    42.             #endif
    43.             color.rgb = lerp (fogColor, color.rgb, IN.fog);
    44.         }
    45.  
    46.         void surf (Input IN, inout SurfaceOutput o) {
    47.             half4 c = tex2D (_MainTex, IN.uv_MainTex);
    48.             o.Albedo = c.rgb;
    49.             o.Alpha = c.a;
    50.         }
    51.         ENDCG
    52.     }
    53.     FallBack "Diffuse"
    54. }
    55.  
     
  2. kaz2057

    kaz2057

    Joined:
    Jan 18, 2011
    Posts:
    326
    Hei Gnimmel,

    I tryied your shader but I cannot see any double fx in editor view ...

    Can you explain me better your issue with a screenshot? :)
     
  3. Gnimmel

    Gnimmel

    Joined:
    Apr 21, 2010
    Posts:
    358
    Thanks for taking the time to reply to this thread. I did work out my problem in the end, and probably should have posted a update here. I just wasn't familiar with shader code and was having problems adding varibles to the shader.
    This is what I am presently using and it seems to work fine on my windows phone;

    Code (CSharp):
    1. Shader "Custom/Unlit_Fog" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white" {}
    4.         _Color ("Fog Color", Color) = (1,1,1,1)
    5.         _FogStart("Fog Start", float) = 40
    6.         _FogEnd("Fog End", float) = 150
    7.  
    8.     }
    9.     SubShader {
    10.         Tags { "RenderType"="Opaque" }
    11.         LOD 200
    12.        
    13.         CGPROGRAM
    14.         #pragma surface surf Lambert finalcolor:mycolor vertex:myvert
    15.  
    16.         sampler2D _MainTex;
    17.         uniform half4 _Color;
    18.         uniform half _FogStart;
    19.         uniform half _FogEnd;
    20.  
    21.         float4 color : COLOR;
    22.         struct Input
    23.         {
    24.             float2 uv_MainTex;
    25.             half Myfog;
    26.         };
    27.  
    28.         void myvert (inout appdata_full v, out Input data)
    29.         {
    30.             UNITY_INITIALIZE_OUTPUT(Input,data);
    31.             float pos = length(mul (UNITY_MATRIX_MV, v.vertex).xyz);
    32.  
    33.             float diff = _FogEnd - _FogStart;
    34.             float invDiff = 1.0f / diff;
    35.             data.Myfog = clamp ((_FogEnd - pos) * invDiff, 0.0, 1.0);
    36.         }
    37.         void mycolor (Input IN, SurfaceOutput o, inout fixed4 color)
    38.         {
    39.             fixed3 fogColor = _Color.rgb;
    40.             #ifdef UNITY_PASS_FORWARDADD
    41.             fogColor = 0;
    42.             #endif
    43.             color.rgb = lerp (fogColor, color.rgb, IN.Myfog);
    44.         }
    45.  
    46.         void surf (Input IN, inout SurfaceOutput o) {
    47.             half4 c = tex2D (_MainTex, IN.uv_MainTex);
    48.             o.Albedo = c.rgb;
    49.             o.Alpha = c.a;
    50.         }
    51.         ENDCG
    52.     }
    53.     FallBack "Diffuse"
    54. }
    55.  

    This is basically the same as the one from Software 7, linked in my original post, but I've added the fog settings to the shader so I can control it without using unity's fog settings. Personally I prefer this because I know how it will look on all devices.

    Here is an example of how it looks in my game at the moment;