Search Unity

Custom stencil shader not working on Linux

Discussion in 'Linux' started by Teku-Studios, Oct 31, 2016.

  1. Teku-Studios

    Teku-Studios

    Joined:
    Sep 29, 2012
    Posts:
    257
    Hi there,

    Okay, here's the thing. We have a puzzle in our game that requires the player to use some "magic light" that, when moved, makes him see a hidden painting on a wall. For that purpose, we created a stencil shader that's applied to an object (the one that is moved) to use it to reveal what's under the wall sprite.

    So far, it is working like a charm in both Windows and OSX systems, but for some reason it is not working on Linux, but I don't really know why. Let me show you our code to see if you can spot some error there:

    This is applied to the material of the wall sprite, the one that hides the secret painting beneath:

    Code (CSharp):
    1. Shader "Game/BlendVertexColorStencil-1"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    6.     }
    7.  
    8.     SubShader
    9.     {
    10.         Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    11.         ZWrite Off
    12.         Lighting Off
    13.         Cull Off
    14.         Fog { Mode Off } Blend SrcAlpha OneMinusSrcAlpha
    15.         //LOD 100
    16.  
    17.         Pass
    18.         {
    19.             Stencil {
    20.                 Ref 255
    21.                 Comp notequal
    22.                 Pass keep
    23.             }
    24.  
    25.             CGPROGRAM
    26.             #pragma glsl
    27.             #pragma vertex vert_vct
    28.             #pragma fragment frag_mult
    29.             #pragma fragmentoption ARB_precision_hint_fastest
    30.             #include "UnityCG.cginc"
    31.             #include "UnityCG.glslinc"
    32.  
    33.             sampler2D _MainTex;
    34.             float4 _MainTex_ST;
    35.  
    36.             struct vin_vct
    37.             {
    38.                 float4 vertex : POSITION;
    39.                 float4 color : COLOR;
    40.                 float2 texcoord : TEXCOORD0;
    41.             };
    42.  
    43.             struct v2f_vct
    44.             {
    45.                 float4 vertex : POSITION;
    46.                 fixed4 color : COLOR;
    47.                 float2 texcoord : TEXCOORD0;
    48.             };
    49.  
    50.             v2f_vct vert_vct(vin_vct v)
    51.             {
    52.                 v2f_vct o;
    53.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    54.                 o.color = v.color;
    55.                 o.texcoord = v.texcoord;
    56.                 return o;
    57.             }
    58.  
    59.             fixed4 frag_mult(v2f_vct i) : COLOR
    60.             {
    61.                 fixed4 col = tex2D(_MainTex, i.texcoord) * i.color;
    62.                 return col;
    63.             }
    64.      
    65.             ENDCG
    66.         }
    67.     }
    68.     SubShader
    69.     {
    70.         Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    71.         ZWrite Off Blend SrcAlpha OneMinusSrcAlpha Cull Off Fog { Mode Off }
    72.         //LOD 100
    73.  
    74.         BindChannels
    75.         {
    76.             Bind "Vertex", vertex
    77.             Bind "TexCoord", texcoord
    78.             Bind "Color", color
    79.         }
    80.  
    81.         Pass
    82.         {
    83.             Stencil {
    84.                 Ref 255
    85.                 Comp notequal
    86.                 Pass keep
    87.             }
    88.      
    89.             Lighting Off
    90.             SetTexture [_MainTex] { combine texture * primary }
    91.         }
    92.     }
    93. }

    And this one is the shader that carries the moving object that "reveals" the hidden painting:

    Code (CSharp):
    1. Shader "Game/BlendVertexColorStencilMASK-1"
    2. {
    3.     Properties
    4.     {
    5.     }
    6.  
    7.     SubShader
    8.     {
    9.         Tags {"Queue"="Geometry" "IgnoreProjector"="True" "RenderType"="Transparent"}
    10.         ZWrite Off
    11.         Lighting Off Cull Off Fog { Mode Off } Blend SrcAlpha OneMinusSrcAlpha
    12.         //LOD 100
    13.  
    14.         Pass
    15.         {
    16.             Stencil {
    17.                 Ref 255
    18.                 Comp always
    19.                 Pass replace
    20.             }
    21.  
    22.             CGPROGRAM
    23.             #pragma glsl
    24.             #pragma vertex vert
    25.             #pragma fragment frag
    26.             #pragma fragmentoption ARB_precision_hint_fastest
    27.             #include "UnityCG.cginc"
    28.             #include "UnityCG.glslinc"
    29.  
    30.             sampler2D _MainTex;
    31.             float4 _MainTex_ST;
    32.  
    33.             struct appdata {
    34.                 float4 vertex : POSITION;
    35.             };
    36.             struct v2f {
    37.                 float4 pos : SV_POSITION;
    38.             };
    39.  
    40.              v2f vert(appdata v) {
    41.                 v2f o;
    42.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    43.                 return o;
    44.             }
    45.             half4 frag(v2f i) : Color {
    46.                 return half4(0,0,0,0);
    47.             }
    48.      
    49.             ENDCG
    50.         }
    51.     }
    52. }

    They're pretty simple shaders, as you see. Is there any changes that should be made to them so they work on Linux?


    Thanks in advance.
     
    Last edited: Oct 31, 2016
  2. Tak

    Tak

    Joined:
    Mar 8, 2010
    Posts:
    1,001
    Hi, what version of Unity are you using? Is this also 5.1.5f1?
     
  3. Teku-Studios

    Teku-Studios

    Joined:
    Sep 29, 2012
    Posts:
    257
    Yep! v5.1.5f1
     
  4. Teku-Studios

    Teku-Studios

    Joined:
    Sep 29, 2012
    Posts:
    257
    Just to clarify, there are no compilation errors or that kind of stuff. It just does not show the sprite that's beneath, it keeps showing the wall image.

    We're using 2DToolkit, not Unity sprites.