Search Unity

Shader on IOS turns dark

Discussion in 'iOS and tvOS' started by pajamajama, Aug 28, 2013.

  1. pajamajama

    pajamajama

    Joined:
    Oct 22, 2012
    Posts:
    66
    Same post on Unity Answers: http://answers.unity3d.com/questions/525918/shader-on-ios-turns-dark.html


    I'm porting a game to IOS and one of my shaders is giving me a headache. Thank you for taking the time to help me out on this.

    The attached image is of what the shader looks like on the computer (left), and what it looks like on the IPad (right). For the IPad It seems to be losing some of its transparency, turning those pixels black. Strangely though it isn't losing all of it's transparency as you can see towards the center.



    I'm using IOS 6, Unity 4, and OpenGL ES 2 Here's the shader in question:

    Code (csharp):
    1. Shader "Shield"
    2. {
    3.  
    4. Properties
    5. {
    6.     _Color("_Color", Color) = (0.0,1.0,0.0,1.0)
    7.     _Inside("_Inside", Range(0.0,2.0) ) = 0.0
    8.     _Rim("_Rim", Range(0.0,2.0) ) = 1.2
    9.     _Texture("_Texture", 2D) = "white" {}
    10.     _Speed("_Speed", Range(0.5,5.0) ) = 0.5
    11.     _Tile("_Tile", Range(1.0,10.0) ) = 5.0
    12.     _Strength("_Strength", Range(0.0,5.0) ) = 1.5
    13. }
    14.    
    15. SubShader
    16. {
    17.     Tags
    18.     {
    19.         "Queue"="Transparent+2"
    20.         "IgnoreProjector"="True"
    21.         "RenderType"="Transparent"
    22.  
    23.     }
    24.    
    25.     Pass{
    26.         ZWrite On
    27.         ColorMask 0
    28.    
    29.     }
    30.  
    31.        
    32. Cull Back
    33. ZWrite On
    34. ZTest LEqual
    35.  
    36.  
    37. CGPROGRAM
    38. #pragma surface surf BlinnPhongEditor alpha vertex:vert
    39. //#pragma target 3.0
    40.  
    41.  
    42. fixed4 _Color;
    43. sampler2D _CameraDepthTexture;
    44. fixed _Inside;
    45. fixed _Rim;
    46. sampler2D _Texture;
    47. fixed _Speed;
    48. fixed _Tile;
    49. fixed _Strength;
    50.  
    51. struct EditorSurfaceOutput
    52.     {
    53.         half3 Albedo;
    54.         half3 Normal;
    55.         half3 Emission;
    56.         half3 Gloss;
    57.         half Specular;
    58.         half Alpha;
    59.     };
    60.            
    61. inline half4 LightingBlinnPhongEditor_PrePass (EditorSurfaceOutput s, half4 light)
    62. {
    63.     half3 spec = light.a * s.Gloss;
    64.    
    65.     half4 c;
    66.    
    67.     c.rgb = (s.Albedo * light.rgb + light.rgb * spec);
    68.    
    69.     c.a = s.Alpha + Luminance(spec);
    70.    
    71.     return c;
    72.  
    73.  
    74. }
    75.  
    76. inline half4 LightingBlinnPhongEditor (EditorSurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
    77. {
    78.     viewDir = normalize(viewDir);
    79.     half3 h = normalize (lightDir + viewDir);
    80.    
    81.     half diff = max (0, dot (s.Normal, lightDir));
    82.    
    83.     float nh = max (0, dot (s.Normal, h));
    84.     float3 spec = pow (nh, s.Specular*128.0) * s.Gloss;
    85.    
    86.     half4 res;
    87.     res.rgb = _LightColor0.rgb * (diff * atten * 2.0);
    88.     res.w = spec * Luminance (_LightColor0.rgb);
    89.  
    90.     return LightingBlinnPhongEditor_PrePass( s, res );
    91. }
    92.  
    93. struct Input
    94. {
    95.     float4 screenPos;
    96.     float3 viewDir;
    97.     float2 uv_Texture;
    98. };
    99.  
    100.  
    101. void vert (inout appdata_full v, out Input o)
    102. {
    103.  
    104. }
    105.            
    106.  
    107. void surf (Input IN, inout EditorSurfaceOutput o)
    108. {
    109.     o.Albedo = fixed3(0.0,0.0,0.0);
    110.     o.Normal = fixed3(0.0,0.0,1.0);
    111.     o.Emission = 0.0;
    112.     o.Gloss = 0.0;
    113.     o.Specular = 0.0;
    114.     o.Alpha = 1.0;
    115.     float4 ScreenDepthDiff0= LinearEyeDepth (tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(IN.screenPos)).r) - IN.screenPos.z;
    116.     float4 Saturate0=fixed4(0.3,0.3,0.3,1.0);//
    117.     float4 Fresnel0_1_NoInput = fixed4(0,0,1,1);
    118.     float4 Fresnel0=float4( 1.0 - dot( normalize( float4(IN.viewDir, 1.0).xyz), normalize( Fresnel0_1_NoInput.xyz ) ) );
    119.     float4 Step0=step(Fresnel0,float4( 1.0 ));
    120.     float4 Clamp0=clamp(Step0,_Inside.xxxx,float4( 1.0 ));
    121.     float4 Pow0=pow(Fresnel0,_Rim.xxxx);
    122.     float4 Multiply5=_Time * _Speed.xxxx;
    123.     float4 UV_Pan0=float4((IN.uv_Texture.xyxy).x,(IN.uv_Texture.xyxy).y + Multiply5.x,(IN.uv_Texture.xyxy).z,(IN.uv_Texture.xyxy).w);
    124.     float4 Multiply1=UV_Pan0 * _Tile.xxxx;
    125.     float4 Tex2D0=tex2D(_Texture,Multiply1.xy);
    126.     float4 Multiply2=Tex2D0 * _Strength.xxxx;
    127.     float4 Multiply0=Pow0 * Multiply2;
    128.     float4 Multiply3=Clamp0 * Multiply0;
    129.     float4 Multiply4=Saturate0 * Multiply3;
    130.     o.Emission = Multiply3.xyz * _Color.rgb;
    131.     o.Alpha =  Multiply3.w * _Color.a;
    132.  
    133. }
    134. ENDCG
    135. }
    136. Fallback "Diffuse"
    137. }
     
    Last edited: Sep 1, 2013
  2. Harry1960

    Harry1960

    Joined:
    May 15, 2007
    Posts:
    136
    Did you check your texture import settings for iOS? If the settings use compression you will have a reduced amount of color/aplha values.

    Harry
     
  3. pajamajama

    pajamajama

    Joined:
    Oct 22, 2012
    Posts:
    66
    Thanks for the great suggestion. I tried changes the textures to "Truecolor" and than "16Bit", but unfortunately there is no visible difference with either.
     
    Last edited: Aug 29, 2013
  4. pajamajama

    pajamajama

    Joined:
    Oct 22, 2012
    Posts:
    66
    The problem was that my alpha channel was being pre-multiplied. I was able to resolve it by using Alpha From Grayscale instead of from Alpha Channel.

    I hope this helps other people with the same problem!