Search Unity

Customed shader for uGUI shows nothing on iOS,but is correct on Android and Windows

Discussion in 'Shaders' started by Aldrick, Jan 19, 2017.

  1. Aldrick

    Aldrick

    Joined:
    Feb 19, 2014
    Posts:
    64
    I write a shader to display a scroll-like image with transparent alpha on uGUI.It shows absolutely good on Windows and Android platform.But it show nothing on iOS.I don't know what part of it is incompatible with iOS,or does it has something to do with iOS version?

    Code (CSharp):
    1. Shader "AB/GFX/soft alpha scroll"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex("Main Texture (RGB)", 2D) = "white" {}
    6.         //_AlphaTex("Alpha Texture (Gray)", 2D) = "white" {}
    7.         _Slider("Slider",Range(0,1)) = 1
    8.         _GradientWidth("Gradient Width",Range(0,200)) = 30
    9.     }
    10.  
    11.     SubShader
    12.     {
    13.         Tags
    14.         {
    15.             "Queue" = "Transparent+999"
    16.             "ForceNoShadowCasting" = "True"
    17.             "IgnoreProjector" = "True"
    18.             "RenderType" = "Transparent"
    19.             "PreviewType"="Plane"
    20.             "CanUseSpriteAtlas"="True"
    21.         }
    22.  
    23.         Pass
    24.         {
    25.             ColorMask RGBA
    26.             Lighting Off
    27.             Fog {Mode Off}
    28.             Cull Off
    29.             ZWrite Off
    30.             ZTest LEqual
    31.             AlphaTest Off
    32.             Blend SrcAlpha OneMinusSrcAlpha
    33.  
    34.             CGPROGRAM
    35.             #pragma vertex vert
    36.             #pragma fragment frag
    37.             #pragma multi_compile RIGHT_2_LEFT LEFT_2_RIGHT UP_2_BOTTOM BOTTOM_2_UP
    38.             #include "UnityCG.cginc"
    39.  
    40.             struct appdata_t
    41.             {
    42.                 float4 vertex : POSITION;
    43.                 float2 texcoord : TEXCOORD0;
    44.             };
    45.  
    46.             struct v2f
    47.             {
    48.                 float4 vertex : POSITION;
    49.                 float2 texcoord : TEXCOORD0;
    50.             };
    51.  
    52.             sampler2D _MainTex;
    53.             half4 _MainTex_ST;
    54.             //sampler2D _AlphaTex;
    55.             fixed _Slider;
    56.             half _GradientWidth;
    57.  
    58.             v2f vert(appdata_t v)
    59.             {
    60.                 v2f o;
    61.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    62.                 o.texcoord = v.texcoord;
    63.                 o.texcoord = TRANSFORM_TEX (v.texcoord, _MainTex);
    64.                 return o;
    65.             }
    66.  
    67.             half4 frag(v2f i) : SV_Target
    68.             {
    69.                 float4 color = tex2D(_MainTex, i.texcoord);
    70.                 fixed alpha = 1;
    71.                 #if RIGHT_2_LEFT
    72.                     if((i.vertex.x > _ScreenParams.x * (1 - _Slider)) && (i.vertex.x < (_ScreenParams.x * (1 - _Slider) + _GradientWidth)))
    73.                     {
    74.                         alpha *=  ( i.vertex.x - _ScreenParams.x  * (1 - _Slider)) / _GradientWidth;
    75.                     }
    76.                    
    77.                     if( i.vertex.x >= _ScreenParams.x * (1- _Slider) + _GradientWidth)
    78.                     {
    79.  
    80.                         alpha *= 1;
    81.                     }
    82.  
    83.                     if( i.vertex.x <= _ScreenParams.x * (1- _Slider))
    84.                     {
    85.  
    86.                         alpha *= 0;
    87.                     }
    88.                 #endif
    89.                 color.a *= alpha;
    90.                 //_Slider
    91.                 return color;
    92.             }
    93.             ENDCG
    94.         }
    95.     }
    96.  
    97.     Fallback "Mobile/Particles/Additive"
    98.     CustomEditor "UIScrollMaterialInspector"
    99. }