Search Unity

Resolved Simple Surface Stencil Buffer Shader 'Not Working' in WebGL Build

Discussion in 'Shaders' started by zipgen-jvion, Jan 27, 2021.

  1. zipgen-jvion

    zipgen-jvion

    Joined:
    Jan 27, 2021
    Posts:
    3
    Hello all,

    Seeking some input from those more experienced with ShaderLab shaders as I know this is just something I'm missing/not implementing correctly.

    Following examples I have the following two surface shaders which utilise the stencil buffer. One for walls (structures) and one for windows/doors (openings). Renders as expected in the editor (Windows), that is, openings 'cut' holes in structures but not when built for WebGL.

    Appreciate all assistance.

    Code (CSharp):
    1. //https://docs.unity3d.com/Manual/ShadersOverview.html
    2.  
    3. Shader "Surface/Structure" {
    4.     Properties {
    5.         [MainTexture] albedo("Albedo", 2D) = "white" {}
    6.         [MainColor] colour("Colour", Color) = (1.0, 1.0, 1.0, 1.0)
    7.     }
    8.  
    9.     SubShader {
    10.         Tags {
    11.             "RenderType" = "Opaque"
    12.         }
    13.    
    14.         Stencil {
    15.             Ref 1
    16.             Comp NotEqual
    17.             Pass Keep
    18.         }
    19.  
    20.         CGPROGRAM
    21.             #pragma surface calcSurface Lambert
    22.  
    23.             sampler2D albedo;
    24.             float4 colour;
    25.  
    26.             struct Input {
    27.                 float2 albedo_uv;
    28.             };
    29.  
    30.             void calcSurface (Input input, inout SurfaceOutput output) {
    31.                 output.Albedo = tex2D(albedo, input.albedo_uv) * colour;
    32.             }
    33.         ENDCG
    34.     }
    35.        
    36.     FallBack "Standard"
    37. }
    Code (CSharp):
    1. //https://docs.unity3d.com/Manual/ShadersOverview.html
    2.  
    3. Shader "Surface/Opening" {
    4.     SubShader {
    5.         Tags {
    6.             "RenderType" = "Opaque"
    7.         }
    8.  
    9.         ColorMask 0
    10.  
    11.         ZWrite Off
    12.        
    13.         Stencil {
    14.             Ref 1
    15.             Comp Always
    16.             Pass Replace
    17.         }
    18.  
    19.         CGPROGRAM
    20.             #pragma surface calcSurface Lambert
    21.  
    22.             struct Input {
    23.             };
    24.  
    25.             void calcSurface (Input input, inout SurfaceOutput output) {
    26.             }
    27.         ENDCG
    28.     }
    29.        
    30.     FallBack "Standard"
    31. }
     
  2. zipgen-jvion

    zipgen-jvion

    Joined:
    Jan 27, 2021
    Posts:
    3
    This was z-depth fighting, explicitly specifying differing render queues solved this for me.