Search Unity

Shader working on some Android devices but not all (Unity 2018.4)

Discussion in 'Shaders' started by Zante, Jan 8, 2021.

  1. Zante

    Zante

    Joined:
    Mar 29, 2008
    Posts:
    429
    **********
    EDIT: Turns out this is actually rendering fine on the Google Pixel devices mentioned (when on a material assigned to a mesh surface). The problem seems to be that when I use it as the material in a UI image component, it fails to render on the Pixel devices. Still not sure why though. :[

    Edit #2: FIXED - Turns out the fix was setting the render mode from screen space 'overlay' to 'camera' in the canvas. For some reason this allows it to work on Pixel devices.
    **********

    I'm using a shader to create a transparent overlay image from a RenderTexture material. This is then stretched over the UI to create a soft masking effect. The problem is that, although this works in the editor, my Samsung Chromebook and Huawei tablet, it doesn't work on any of our Google Pixel devices (2XL and 4A). In the latter, it simply doesn't render the image at all, making the scene look flat.

    The shader code is below. The image shows the actual outcome in the editor along with the overlay UI image that should be produced (on the aforementioned devices). Any ideas why this wouldn't be working on Google Pixel smartphones?



    As seen in the editor and Chromebook/Huawei Media tablet

    Shader Code:

    Code (CSharp):
    1. Shader "Custom/AlphaMaskIII" {
    2.     Properties{
    3.         _Color("Color", Color) = (1,1,1,1)//
    4.         _AlphaVal("AlphaVal", Range(0,1)) = 1.0
    5.         _MainTex("MainTex (Sprite)", 2D) = "white" {}
    6.         _AlphaTex("AlphaTex (R)", 2D) = "white" {}
    7.     }
    8.  
    9.         SubShader{
    10.             Tags {"Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent"}
    11.             LOD 100
    12.  
    13.             ZWrite Off
    14.             Blend SrcAlpha OneMinusSrcAlpha
    15.  
    16.             Pass {
    17.                 CGPROGRAM
    18.                     #pragma vertex vert
    19.                     #pragma fragment frag
    20.  
    21.                     #include "UnityCG.cginc"
    22.  
    23.                     struct appdata_t {
    24.                         float4 vertex : POSITION;
    25.                         float2 texcoord : TEXCOORD0;
    26.                         float2 texcoordA : TEXCOORD1; // alpha uv
    27.                     };
    28.  
    29.                     struct v2f {
    30.                         float4 vertex : SV_POSITION;
    31.                         half2 texcoord : TEXCOORD0;
    32.                         half2 texcoordA : TEXCOORD1; // alpha uv
    33.                     };
    34.  
    35.                     sampler2D _MainTex;
    36.                     sampler2D _AlphaTex;
    37.                     float _AlphaVal;
    38.  
    39.                     float4 _MainTex_ST;
    40.                     float4 _AlphaTex_ST; // alpha uv
    41.                     fixed4 _Color;//
    42.  
    43.                     v2f vert(appdata_t v)
    44.                     {
    45.                         v2f o;
    46.                         o.vertex = UnityObjectToClipPos(v.vertex);
    47.                         o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    48.                         o.texcoordA = TRANSFORM_TEX(v.texcoordA, _AlphaTex); // alpha uv
    49.                         return o;
    50.                     }
    51.  
    52.                     fixed4 frag(v2f i) : SV_Target
    53.                     {
    54.                         fixed4 main = tex2D(_MainTex, i.texcoord) * _Color;
    55.                         fixed4 alph = tex2D(_AlphaTex, i.texcoordA); // alpha uv
    56.              
    57.                         return fixed4(main.r, main.g, main.b, (main.a*alph.r*_AlphaVal));
    58.                     }
    59.                 ENDCG
    60.             }
    61.     }
    62.  
    63. }
     
    Last edited: Jan 12, 2021
  2. iamvideep

    iamvideep

    Joined:
    Oct 27, 2017
    Posts:
    118
  3. Zante

    Zante

    Joined:
    Mar 29, 2008
    Posts:
    429

    Turns out this is actually rendering fine to a material when placed on a mesh surface. The problem seems to be that when I use it as the material in a UI image component, it fails to render on the Pixel devices. Still not sure why though. :[
     
  4. iamvideep

    iamvideep

    Joined:
    Oct 27, 2017
    Posts:
    118
    Then you might want to check the render queue. Overlay might work and also check the ZTest values.
     
  5. Zante

    Zante

    Joined:
    Mar 29, 2008
    Posts:
    429
    Turns out the fix was setting the render mode from screen space 'overlay' to 'camera' in the canvas. For some reason this allows it to work on Pixel devices.
     
    Northmaen and iamvideep like this.
  6. rjonaitis

    rjonaitis

    Unity Technologies

    Joined:
    Jan 5, 2017
    Posts:
    115
    The shader needs "ZTest Always" to correctly work for overlay.
     
  7. iamvideep

    iamvideep

    Joined:
    Oct 27, 2017
    Posts:
    118
    Great!
     
  8. Zante

    Zante

    Joined:
    Mar 29, 2008
    Posts:
    429
    The results seemed incocnsistent over Android devices however and, as a consequence, it wasn't intuitive to figure out for me. Good to know what the best practice is : ]
     
  9. Hoorza

    Hoorza

    Joined:
    May 8, 2016
    Posts:
    45
    Hi all. I got a similar problem. But it is a bog-standard particle shader from Universal Render Pipeline. It works on Samsung phone A3 and in the editor but it doesn't work on Samsung Tablet S3. OpenGL versions are 3.1 and 3.2, same maker yet shader is grayed out not displaying properly. Any ideas? Also, how can I know that there aren't 5000 other devices that won't display it properly once I publish the game? Any advice is appreciated.
     
  10. Uniter_1234

    Uniter_1234

    Joined:
    Jan 23, 2022
    Posts:
    1
    Dear Hoorza,
    Did you find any solution to your problem...Even I came across the same issue in unity 2020.3.39 and built the apk successfully with URP but the shader was not showing desired output but complete white material instead in OpenGL es 3.1 and 3.2 devices. Please guide us with your valuable knowledge if you came across the solution. Thank you!