Search Unity

Question Mask Shaders not working on Android - any idea why?

Discussion in 'Shaders' started by DanTaylor, Dec 2, 2020.

  1. DanTaylor

    DanTaylor

    Joined:
    Jul 6, 2013
    Posts:
    119
    Hi there.
    I am using a mask shader to hide certain objects in Unity.
    It works fine on all platforms apart from Android.

    What I see normally...

    Notice the well in the center of the room is not shown at the bottom of the screen... this is because it is covered by masking object using my mask shader.

    What I see on Android...

    The masking object is rendered black. The choppy artifacts appear in OpenGL... but not in Vulkan.

    Now... I am guessing that this is happening due to the Skybox getting rendered AFTER the mask.
    So I set my mask render queue to a crazy high 2610... still no joy.

    Here is my mask shader code...

    Code (CSharp):
    1.  Shader "Thunderbox/Mask" {
    2.     SubShader {
    3.         // Render the mask after regular geometry, but before masked geometry and
    4.         // transparent things.
    5.         Tags {"Queue" = "Geometry+10" }
    6.         // Don't draw in the RGBA channels; just the depth buffer
    7.         ColorMask 0
    8.         ZWrite On
    9.         // Do nothing specific in the pass:
    10.         Pass {}
    11.     }
    12. }
    Any ideas what I'm doing wrong?
    (Hmmm... looking at that shader code again, could it be a depth buffer setting?)

    Cheers,
    Dan
     
  2. unityuserunity85496

    unityuserunity85496

    Joined:
    Jan 9, 2019
    Posts:
    89
    Try rewriting this, (just look for example code) in HLSL CG shader code. Sometimes Surface shader just fails for android cause a new update to android does not yet have a fix to a new break
     
  3. DanTaylor

    DanTaylor

    Joined:
    Jul 6, 2013
    Posts:
    119
    Thanks, @unityuserunity85496 - could you give a little more info? I am not a shader expert. What is HLSL?
     
  4. DanTaylor

    DanTaylor

    Joined:
    Jul 6, 2013
    Posts:
    119
    Ah! I Googled "HLSL CG mask shader unity" and this post was the third hit!
    I will try using the masking shader from the AR Kit plug-in and see how that works on Android.
    :)
     
  5. unityuserunity85496

    unityuserunity85496

    Joined:
    Jan 9, 2019
    Posts:
    89
  6. DanTaylor

    DanTaylor

    Joined:
    Jul 6, 2013
    Posts:
    119
    Tried this shader from the Unity AR Kit plug-in... still the same issue...

    Code (CSharp):
    1. Shader "Thunderbox/MobileOcclusion"
    2. {
    3.     SubShader {
    4.             Pass {
    5.                 // Render the Occlusion shader before all
    6.                 // opaque geometry to prime the depth buffer.
    7.                 Tags { "Queue"="Geometry" }
    8.  
    9.                 ZWrite On
    10.                 ZTest LEqual
    11.                 ColorMask 0
    12.  
    13.                 CGPROGRAM
    14.                 #pragma vertex vert
    15.                 #pragma fragment frag
    16.  
    17.                 #include "UnityCG.cginc"
    18.  
    19.                 struct appdata
    20.                 {
    21.                     float4 vertex : POSITION;
    22.                 };
    23.  
    24.                 struct v2f
    25.                 {
    26.                     float4 position : SV_POSITION;
    27.                 };
    28.  
    29.                 v2f vert (appdata input)
    30.                 {
    31.                     v2f output;
    32.  
    33.                     output.position = UnityObjectToClipPos(input.vertex);
    34.                     return output;
    35.                 }
    36.  
    37.                 fixed4 frag (v2f input) : SV_Target
    38.                 {
    39.                     return fixed4(0.5, 0.3, 0.0, 1.0);
    40.                 }
    41.                 ENDCG
    42.             }
    43.     }
    44. }
     
  7. unityuserunity85496

    unityuserunity85496

    Joined:
    Jan 9, 2019
    Posts:
    89
  8. DanTaylor

    DanTaylor

    Joined:
    Jul 6, 2013
    Posts:
    119
    Drat. Changing the entire render pipeline for my project isn't really an option as we are almost about to ship! Maybe I could do something with the Stencil Buffer instead? :(
     
  9. unityuserunity85496

    unityuserunity85496

    Joined:
    Jan 9, 2019
    Posts:
    89
    Yeah, there will ALWAYS be show stopper issues. You learn to test on the device from the start at some point. Stencil buffer is a hard cut. You could also change the camera to perspective and drag the objects above each other a tiny bit and test for zfighting