Search Unity

Question iOS Fog Of War Shader Debug Help

Discussion in 'Shaders' started by unity_483C4357D3269025FA3B, Aug 9, 2022.

  1. unity_483C4357D3269025FA3B

    unity_483C4357D3269025FA3B

    Joined:
    Jul 2, 2022
    Posts:
    20
    Hello,

    I am using Andrew Hung's tutorial to create fog of war for a strategy game.
    https://andrewhungblog.wordpress.com/2018/06/23/implementing-fog-of-war-in-unity/

    I followed his tutorial to the letter and it works perfectly in on my computer, however when I build for iOS I can only get one of the two shrouds to show. Either the semi transparent one, or the completely dark one. My guess is there is some part of the shader that is not correctly supported by iOS, however I know little about shaders, is anyone willing to take a look? Any hints are appreciated, I am willing to try anything.

    Example on preivew on my computer:

    As you can see 3 levels of "shroud"
    1. Completely black for unexplored (right)
    2. Dark grey for previously explored but no unity present (bottom)
    3. And shroud lifted for when a unit is present (center)

    Now on mobile it looks like this:

    As you can see we only have 2 levels
    1. Either dark grey
    2. Shroud lifted (right)


    This is the shader I am using, again everything is taken from Andrew Hung's tutorial.

    Code (shader):
    1. Shader "Custom/FogProjection"
    2. {
    3.     Properties
    4.     {
    5.         _PrevTexture ("Previous Texture", 2D) = "white" {}
    6.         _CurrTexture ("Current Texture", 2D) = "white" {}
    7.         _Color ("Color", Color) = (0, 0, 0, 0)
    8.         _Blend("Blend", Float) = 0
    9.     }
    10.     SubShader
    11.     {
    12.         Tags { "Queue"="Transparent+100" } // to cover other transparent non-z-write things
    13.         Pass
    14.         {
    15.             ZWrite Off
    16.             Blend SrcAlpha OneMinusSrcAlpha
    17.             ZTest Equal
    18.             CGPROGRAM
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.             #include "UnityCG.cginc"
    22.             struct appdata
    23.             {
    24.                 float4 vertex : POSITION;
    25.                 float4 uv : TEXCOORD0;
    26.             };
    27.             struct v2f
    28.             {
    29.                 float4 uv : TEXCOORD0;
    30.                 float4 vertex : SV_POSITION;
    31.             };
    32.             float4x4 unity_Projector;
    33.             sampler2D _CurrTexture;
    34.             sampler2D _PrevTexture;
    35.             fixed4 _Color;
    36.             float _Blend;
    37.             v2f vert (appdata v)
    38.             {
    39.                 v2f o;
    40.                 o.vertex = UnityObjectToClipPos(v.vertex);
    41.                 o.uv = mul(unity_Projector, v.vertex);
    42.                 return o;
    43.             }
    44.             fixed4 frag (v2f i) : SV_Target
    45.             {
    46.                 float aPrev = tex2Dproj(_PrevTexture, i.uv).a;
    47.                 float aCurr = tex2Dproj(_CurrTexture, i.uv).a;
    48.                 fixed a = lerp(aPrev, aCurr, _Blend);
    49.                 // weird things happen to minimap if alpha value gets negative
    50.                 _Color.a = max(0, _Color.a - a);
    51.                 return _Color;
    52.             }
    53.             ENDCG
    54.         }
    55.     }
    56. }