Search Unity

Stencil buffer with lightmaps on Android

Discussion in 'Shaders' started by menghui0330, May 16, 2019.

  1. menghui0330

    menghui0330

    Joined:
    Jan 12, 2016
    Posts:
    4
    Hi guys, I am new to shader and recently I have been trying to make portal effect using stencil buffer, everything works fine until I started to bake lightmaps. For some reason my shader doesn't work if I try to bake lightmaps and build it on Android, but it works fine in the Editor, is there something wrong with my shader?

    Code (CSharp):
    1. Shader "Custom/MobileDiffuse" {
    2. Properties {
    3.     _MainTex ("Base (RGB)", 2D) = "white" {}
    4.     [Enum(CompareFunction)] _StencilComp("Stencil Comp", int) = 3
    5. }
    6. SubShader {
    7.     Tags { "RenderType"="Opaque" }
    8.     LOD 100
    9.  
    10.     Stencil
    11.     {
    12.         Ref 1
    13.         Comp [_StencilComp]
    14.     }
    15.  
    16. CGPROGRAM
    17. #pragma surface surf Lambert noforwardadd
    18.  
    19. sampler2D _MainTex;
    20.  
    21. struct Input {
    22.     float2 uv_MainTex;
    23. };
    24.  
    25. void surf (Input IN, inout SurfaceOutput o) {
    26.     fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
    27.     o.Albedo = c.rgb;
    28.     o.Alpha = c.a;
    29. }
    30. ENDCG
    31. }
    32.  
    33. Fallback "Mobile/VertexLit"
    34. }
    Code (CSharp):
    1. Shader "Custom/PortalMask"
    2. {
    3.     SubShader
    4.     {
    5.         Tags { "RenderType"="Opaque" }
    6.         LOD 100
    7.         Zwrite Off
    8.         ColorMask 0
    9.  
    10.         Pass
    11.         {
    12.             Stencil{
    13.                 Ref 1
    14.                 Comp always
    15.                 Pass replace
    16.             }
    17.  
    18.             CGPROGRAM
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.        
    22.             #include "UnityCG.cginc"
    23.  
    24.             struct appdata
    25.             {
    26.                 float4 vertex : POSITION;
    27.             };
    28.  
    29.             struct v2f
    30.             {
    31.                 float4 vertex : SV_POSITION;
    32.             };
    33.  
    34.             v2f vert (appdata v)
    35.             {
    36.                 v2f o;
    37.                 o.vertex = UnityObjectToClipPos(v.vertex);
    38.                 return o;
    39.             }
    40.        
    41.             fixed4 frag (v2f i) : SV_Target
    42.             {
    43.                 return fixed4(0.0, 0.0, 0.0, 0.0);
    44.             }
    45.             ENDCG
    46.         }
    47.     }
    48. }
     

    Attached Files: