Search Unity

Issue with transparency on IOS device (becomes black).

Discussion in 'Shaders' started by Louis_cortes, Mar 30, 2020.

  1. Louis_cortes

    Louis_cortes

    Joined:
    Jan 11, 2018
    Posts:
    9
    Hello.

    I made a shader for a mobile application.
    Everything it's okay on many devices but the for some IOS devices the transparency doesn't seems to work (for now the issue appear on an IPhone6).
    The parts that should be transparent are totally black , I made several attempts to correct this problem without maniage to find a proper sollution.

    I copy/paste here the code, in case of someone know why.

    Code (csharp):
    1. Shader "Name"
    2. {
    3.     //Versio 05 _ 30/03 - 15h40
    4.     Properties
    5.     {
    6.         _MainTex("Texture", 2D) = "white" {}
    7.         _si2("Jauge",Range(0, 1)) = 0
    8.         _Color("Color", Color) = (0.,0.,0.,1.)
    9.         _s("ActivationEffet",Range(0, 1)) = 0
    10.        
    11.     }
    12.         SubShader
    13.         {
    14.                 Tags {
    15.                 "Queue" = "Transparent"
    16.                 "IgnoreProjector" = "True"
    17.                 "RenderType" = "Transparent" }
    18.  
    19.                 ZTest Always
    20.                 Blend SrcAlpha OneMinusSrcAlpha
    21.                 Cull Off Lighting Off ZWrite Off
    22.             Pass
    23.             {
    24.                 CGPROGRAM
    25.                 #pragma vertex vert
    26.                 #pragma fragment frag
    27.                 #include "UnityCG.cginc"
    28.                 #pragma target 3.0
    29.                 struct appdata
    30.                 {
    31.                     float4 vertex : POSITION;
    32.                     float2 uv : TEXCOORD0;
    33.                 };
    34.                 struct v2f
    35.                 {
    36.                     float2 uv : TEXCOORD0;
    37.                     float4 vertex : SV_POSITION;
    38.                 };
    39.                 sampler2D _MainTex;
    40.                 half4 _MainTex_ST;
    41.                 half _s;
    42.                 half _si2;
    43.                 half4 _Color;
    44.                 v2f vert(appdata v)
    45.                 {
    46.                     v2f o;
    47.                     o.vertex = UnityObjectToClipPos(v.vertex);
    48.                     o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    49.                     return o;
    50.                 }
    51.                 float ov(float base, float blend) {
    52.                     return lerp((2.0 * base * blend) , (1.0 - 2.0 * (1.0 - base) * (1.0 - blend)),step(0.5,base));
    53.                 }
    54.  
    55.                 fixed4 frag(v2f i) : SV_Target
    56.                 {
    57.                     float time = _Time * 20.;
    58.                     float2 uv = (i.uv - 0.5) * 5.;
    59.                     float d = length(uv);
    60.                     float z = sqrt(1.0 - d * d);
    61.                     float val = _si2;
    62.                     float pa = (lerp(lerp(lerp(pow(frac(val * 2.), 0.18) * 0.5, (1. - pow(1. - frac(val * 2.), 0.18) * 0.5), step(0.5, val)), 10., step(0.999, val)), -10., step(val, 0.001)) - 0.5) * z * 10.;
    63.                     float si = 0.03;
    64.                     float ba = smoothstep(pa - si, pa + si, uv.x);
    65.                     float m = smoothstep(0.985, 0.975, d);
    66.                     float m2 = smoothstep(1.11, 1.09,d);
    67.                     float fn2 = lerp(1.,ba,  m);;
    68.                     float i1 = pow(_s, 10.);
    69.                     float fn = lerp(fn2, 1., i1);
    70.                     return fixed4(saturate(float3(fn,fn,fn)),m2);
    71.                 }
    72.  
    73.                 ENDCG
    74.             }
    75.         }
    76. }
    77.  
    thx.
     
  2. Louis_cortes

    Louis_cortes

    Joined:
    Jan 11, 2018
    Posts:
    9
    For more precision
    the specs of the mobile are :


    <color=red>SYSTEM INFO</color>
    [iphone generation]iPhone6
    [type]Handheld
    [os version]iOS 12.4.5
    [system memory size]989
    [graphic device name]Apple A8 GPU (version Metal)
    [graphic memory size]256
    [graphic max texSize]8192
    [graphic shader level]45
    [support compute shader]True
    [support gyro]True
    [support accelero]True
    [processor count]2
    [processor type]arm64
    [support 3d texture]True
    [support shadow]True
    [platform] IPhonePlayer
    [screen size] 750 x 1334
    [screen pixel density dpi] 326


    apple A8 / Metal
    graphic shader level 45
     
  3. Louis_cortes

    Louis_cortes

    Joined:
    Jan 11, 2018
    Posts:
    9
    Hi,

    I try to replace the float by fixed, to add a fallback and to only make math operation with variables but the problem is still there.
    I also try to pass the shader model in 2.0 / 3.0 / 3.5.
    some examples of the modifications.

    Code (CSharp):
    1. Shader "Unmaze/MiniLune"
    2. {
    3.     //Versio 13 _ 30/03 - 18h50
    4.     Properties
    5.     {
    6.  
    7.         _MainTex ("Texture", 2D) = "white" {}
    8.  
    9.         _si2("Jauge",Range(0, 1)) = 0
    10.         _Color ("Color", Color) = (0.,0.,0.,1.)
    11.         _s("ActivationEffet",Range(0, 1)) = 0
    12.        
    13.     }
    14.     SubShader
    15.     {
    16.             Tags {
    17.             "Queue" = "Overlay"
    18.             "IgnoreProjector" = "True"
    19.             "RenderType" = "Transparent" }
    20.        
    21.             ZTest Always
    22.             Blend SrcAlpha OneMinusSrcAlpha
    23.             Cull off Lighting Off ZWrite Off
    24.  
    25.         Pass
    26.         {
    27.             CGPROGRAM
    28.             #pragma vertex vert
    29.             #pragma fragment frag
    30.             #include "UnityCG.cginc"
    31.             #pragma target 3.0
    32.             #pragma fragmentoption ARB_precision_hint_fastest
    33.             struct appdata
    34.             {
    35.                 float4 vertex : POSITION;
    36.                 float2 uv : TEXCOORD0;
    37.             };
    38.  
    39.             struct v2f
    40.             {
    41.                 float2 uv : TEXCOORD0;
    42.                 float4 vertex : SV_POSITION;
    43.             };
    44.  
    45.             uniform sampler2D _MainTex;
    46.             uniform float4 _MainTex_ST;
    47.             uniform float _s;
    48.             uniform float _si2;
    49.             uniform float4 _Color;
    50.             v2f vert (appdata v)
    51.             {
    52.                 v2f o;
    53.                 o.vertex = UnityObjectToClipPos(v.vertex);
    54.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    55.                 return o;
    56.             }
    57.             float ov(float base, float blend) {
    58.                 return lerp((2.0 * base * blend) , (1.0 - 2.0 * (1.0 - base) * (1.0 - blend)),step(0.5,base));
    59.             }
    60.  
    61.             fixed4 frag(v2f i) : SV_Target
    62.             {
    63.                 fixed time = _Time.y;
    64.                 float2 uv = (i.uv - 0.5) * 5.;
    65.                 fixed d = length(uv);
    66.                 fixed z = sqrt(1.0 - d * d);
    67.                 fixed m = smoothstep(0.985, 0.975, d);
    68.                 fixed m2 = smoothstep(1.11, 1.09, d);
    69.                 fixed val = _si2;
    70.                 fixed i1 = pow(_s, 10.);
    71.                 fixed fp1 = frac(val * 2.);
    72.                 fixed fp2 = pow(fp1, 0.18) * 0.5;
    73.                 fixed fp3 = 1. - fp1;
    74.                 fixed fp4 = 1. - pow(fp3, 0.18) * 0.5;
    75.                 fixed fp5 = step(0.5, val);
    76.                 fixed fp6 = step(0.999, val);
    77.                 fixed fp7 = step(val, 0.001);
    78.                 fixed pa1 = lerp(fp2, fp4, fp5);
    79.                 fixed pa2 = lerp(pa1, 10., fp6);
    80.                 fixed pa3 = (lerp(pa2, -10., fp7) - 0.5) * z * 10.;
    81.                 fixed si = 0.03;
    82.                 fixed ba = smoothstep(pa3 - si, pa3 + si, uv.x);              
    83.                 fixed fn2 = lerp(1.,ba,  m ) ;
    84.                 fixed fn = lerp(fn2, 1., i1);
    85.                 fixed3 fin = saturate(fixed3(fn, fn, fn));
    86.                 return fixed4(fin,m2);
    87.             }
    88.                
    89.             ENDCG
    90.         }
    91.            
    92.     }
    93.             Fallback "Particles/Standard Unlit"
    94. }
    95.  
    I don't know if I missing something with some spec for IOS...
     
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,717
    If you hardcode the m2 value as, say, 0.5, does transparency then work?
     
  5. Louis_cortes

    Louis_cortes

    Joined:
    Jan 11, 2018
    Posts:
    9
    Hi,
    I try to pass m2 to 0.5 the center part get transparent but outside it's still black.

    I remplace this line :
    " float2 uv = (i.uv - 0.5) * 5.; " by :
    " float2 uv = (i.uv - float2(0.5, 0.5)) * float2(5.,5.); "

    assuming that maybe the fact that I didn't explicit all the value was making a trouble on the alpha, but the problem persist.
     
    Last edited: Apr 6, 2020
  6. Louis_cortes

    Louis_cortes

    Joined:
    Jan 11, 2018
    Posts:
    9
    If I removed the line 83 :
    - fixed fn2 = lerp(1.,ba, m ) ;
    and pass "fixed ba" directly in the result I get the transparency but it's very weird, the circle around( that is in this case is black in the editor) became whit in the Iphone.

    This is an image the differents results :

    bug.jpg

    I don't get why when I add a simple lerp whith a 1.0 value in the emmissive that destroy the transparency and why the color pass to the white.
     
  7. Louis_cortes

    Louis_cortes

    Joined:
    Jan 11, 2018
    Posts:
    9
    I tried with nodal editors, but the problems remains...
     
  8. Louis_cortes

    Louis_cortes

    Joined:
    Jan 11, 2018
    Posts:
    9
    Ok, resolved by adding a max() with a 0.001 value inside the sqrt oparation.
     
  9. Louis_cortes

    Louis_cortes

    Joined:
    Jan 11, 2018
    Posts:
    9
    Does anyone knows where I can find informations on do's and don'ts for iOS ?