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


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:

Shader "Custom/AlphaMaskIII" {
    Properties{
        _Color("Color", Color) = (1,1,1,1)//
        _AlphaVal("AlphaVal", Range(0,1)) = 1.0
        _MainTex("MainTex (Sprite)", 2D) = "white" {}
        _AlphaTex("AlphaTex (R)", 2D) = "white" {}
    }

        SubShader{
            Tags {"Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent"}
            LOD 100

            ZWrite Off
            Blend SrcAlpha OneMinusSrcAlpha

            Pass {
                CGPROGRAM
                    #pragma vertex vert
                    #pragma fragment frag

                    #include "UnityCG.cginc"

                    struct appdata_t {
                        float4 vertex : POSITION;
                        float2 texcoord : TEXCOORD0;
                        float2 texcoordA : TEXCOORD1; // alpha uv
                    };

                    struct v2f {
                        float4 vertex : SV_POSITION;
                        half2 texcoord : TEXCOORD0;
                        half2 texcoordA : TEXCOORD1; // alpha uv
                    };

                    sampler2D _MainTex;
                    sampler2D _AlphaTex;
                    float _AlphaVal;

                    float4 _MainTex_ST;
                    float4 _AlphaTex_ST; // alpha uv
                    fixed4 _Color;//

                    v2f vert(appdata_t v)
                    {
                        v2f o;
                        o.vertex = UnityObjectToClipPos(v.vertex);
                        o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
                        o.texcoordA = TRANSFORM_TEX(v.texcoordA, _AlphaTex); // alpha uv
                        return o;
                    }

                    fixed4 frag(v2f i) : SV_Target
                    {
                        fixed4 main = tex2D(_MainTex, i.texcoord) * _Color;
                        fixed4 alph = tex2D(_AlphaTex, i.texcoordA); // alpha uv
              
                        return fixed4(main.r, main.g, main.b, (main.a*alph.r*_AlphaVal));
                    }
                ENDCG
            }
    }

}

You might want to check if those phones support opengl es 3.0?
Or add a compilation target to your code?

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. :[

Then you might want to check the render queue. Overlay might work and also check the ZTest values.

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.

2 Likes

The shader needs “ZTest Always” to correctly work for overlay.

Great!

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 : ]

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.

4 Likes

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!