Search Unity

problems with zbuffer shader (Solved)

Discussion in 'Shaders' started by Andreas88, Dec 1, 2016.

  1. Andreas88

    Andreas88

    Joined:
    Dec 22, 2015
    Posts:
    67
    Hi folks :)
    i´m working on a rts game and i am trying to do fog of war. i followed an tutorial where you create some shaders and do some sort of stencil thing. i got an quad in front of my camera that has an shader on that darkens the image. next i got an object in front of the quad with a shader on that blocks the other shader. making it appear. my issue here is that i tried to add alpha texture features to my stencil shader, making my object appear in any shape i choose.
    here´s my shader code, i have no idea why it´s not working. i don´t really know much about how shaders work so i tried my bedst.

    Code (CSharp):
    1. Shader "Custom/Stencil/Mask OneZLess"
    2. {
    3.     Properties
    4.     {
    5.        
    6.         _AlphaTex ("Alpha (A)", 2D) = "white" {}
    7.         _Cutoff ("Alpha cutoff",float) = 0.5
    8.     }
    9.  
    10.     SubShader
    11.     {
    12.         Tags { "RenderType"="Tranparency" "Queue"="Geometry-1" "IgnoreProjector"="No"}
    13.         ColorMask 0
    14.         ZWrite off
    15.        
    16.         Stencil
    17.         {
    18.             Ref 1
    19.             Comp always
    20.             Pass replace
    21.         }
    22.        
    23.         Pass
    24.         {
    25.             Cull Back
    26.             ZTest Less
    27.        
    28.             CGPROGRAM
    29.  
    30.             #pragma vertex vert
    31.             #pragma fragment frag
    32.  
    33.            uniform sampler2D _AlphaTex;
    34.             uniform float _Cutoff;
    35.            
    36.             struct VertexInPut
    37.             {
    38.                 float4 vertex : POSITION;
    39.                 float4 texcoord : TEXCOORDO;
    40.             };
    41.  
    42.              struct VertexOutPut
    43.             {
    44.                 float4 pos : SV_POSITION;
    45.                 float4 tex : TEXCOORDO;
    46.  
    47.             };
    48.  
    49.              VertexOutPut vert(VertexInPut input)
    50.             {
    51.                 VertexOutPut output;
    52.  
    53.                 output.tex = VertexInPut.texcoord;
    54.                 output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
    55.                    return output;
    56.             }
    57.  
    58.  
    59.             float4 frag(VertexOutPut input) : COLOR
    60.             {
    61.                 float4 aTex = tex2D(_AlphaTex,input.tex.xy);
    62.                 if(aTex.a <_Cutoff)
    63.                 {
    64.                 discard;
    65.                 }
    66.                 return aTex;
    67.                 return float4(1,1,0,1);
    68.             }
    69.          
    70.             ENDCG
    71.         }
    72.     }
    73. }
     
  2. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    826
    Hm there are a few problems here:
    - Tell us what the behaviour of the shader right now is and what it should be.
    - You labeled the Thread Problems with Z-Buffer Shader. Even though I guess that this shader won't write in the depth buffer I don't think that this is really the problem you want to tackle.
    - With the stencil shader you are writing in the stencil buffer. The values written in there can be used by other shaders to do simple conditional operations. As for my knowledge you can't do a half transparency clipping effect with stencil buffers. What you could do is either fade the shader you just posted from something to transparent, or as it seems you have already chosen: Do a cutout shader.
    Cutout shaders are quite different to transparent shaders. The only allow full transparency or no transparency for every pixel, nothing in between. If you want to go with a cutout shader, take a look at the cutout shaders from Unity itself. Here you can download the newest ones: http://download.unity3d.com/download_unity/38b4efef76f0/builtin_shaders-5.5.0f3.zip
    Everything with alphatest might be interesting for you (e.g. DefaultResourcesExtra/Alpha-VertexLit.shader)´.

    From a quick look at the shader:
    The tags should be
    Tags { "Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}

    In the frag you want to return twice?! The last one won't be executed.

    The frag should look more like this:
    Code (CSharp):
    1. fixed4 frag(VertexOutPut input) : COLOR
    2.             {
    3.                 fixed4 aTex = tex2D(_AlphaTex,input.tex.xy);
    4.                 clip(aTex.a - _Cutoff);
    5.                 return aTex;
    6.             }
     
  3. Andreas88

    Andreas88

    Joined:
    Dec 22, 2015
    Posts:
    67
    Hi, thank you for your reply :)
    well at the moment my shader goes complete pink. and i throws me an Token error, like in the pic.
    it keeps saying unexpected token in line 53 and i have no idea why.
    i changed my code like you said, however i can´t tell if it made any difference since it didn´t resolve my error.

    to clarify what i am trying to do. this stencil shader will sort of cut out the geometry that is attached to the shader.
    below here is the stencil fog shader that makes everything dark.
    Code (CSharp):
    1. Shader "Custom/Transparent stencil_fog" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.  
    6.     }
    7.     SubShader {
    8.         Tags { "RenderType"="Transparent" "Queue"="Transparent" "IgnoreProjector"="True"}
    9.         LOD 200
    10.  
    11.         Stencil
    12.         {
    13.         Ref 1
    14.         Comp notequal
    15.         pass keep
    16.         }
    17.        
    18.         CGPROGRAM
    19.         #pragma surface surf Standard alpha
    20.  
    21.         sampler2D _MainTex;
    22.         fixed4 _Color;
    23.  
    24.         struct Input
    25.         {
    26.             float2 uv_MainTex;
    27.         };
    28.  
    29.         void surf (Input IN, inout SurfaceOutputStandard o) {
    30.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    31.             o.Albedo = c.rgb;
    32.             o.Alpha = c.a;
    33.         }
    34.         ENDCG
    35.     }
    36.     FallBack "VertexLit"
    the other shader (in which i got my errors) acts as an simple mask that removes the dark pixels. i was simply trying to add a texture feature so that i could mask out parts of my geometry to make other shapes than spheres and cubes etc.
    below is the mask shader that i have modified based on your initial feedbag.
    Code (CSharp):
    1. Shader "Custom/Stencil/Mask OneZLess"
    2. {
    3.     Properties
    4.     {
    5.        
    6.         _AlphaTex ("Alpha (A)", 2D) = "white" {}
    7.         _Cutoff ("Alpha cutoff",float) = 0.5
    8.     }
    9.  
    10.     SubShader
    11.     {
    12.         Tags { "RenderType"="TransparentCutout" "Queue"="AlphaTest" "IgnoreProjector"="True"}
    13.         ColorMask 0
    14.         ZWrite off
    15.        
    16.         Stencil
    17.         {
    18.             Ref 1
    19.             Comp always
    20.             Pass replace
    21.         }
    22.        
    23.         Pass
    24.         {
    25.             Cull Back
    26.             ZTest Less
    27.        
    28.             CGPROGRAM
    29.  
    30.             #pragma vertex vert
    31.             #pragma fragment frag
    32.  
    33.            uniform sampler2D _AlphaTex;
    34.             uniform float _Cutoff;
    35.            
    36.             struct VertexInPut
    37.             {
    38.                 float4 vertex : POSITION;
    39.                 float4 texcoord : TEXCOORDO;
    40.             };
    41.  
    42.              struct VertexOutPut
    43.             {
    44.                 float4 pos : SV_POSITION;
    45.                 float4 tex : TEXCOORDO;
    46.  
    47.             };
    48.  
    49.              VertexOutPut vert(VertexInPut input)
    50.             {
    51.                 VertexOutPut output;
    52.  
    53.                 output.tex = VertexInPut.texcoord;
    54.                 output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
    55.                    return output;
    56.             }
    57.  
    58.  
    59.             fixed4 frag(VertexOutPut input) : COLOR
    60.             {
    61.                 fixed4 aTex = tex2D(_AlphaTex,input.tex.xy);
    62.                 clip(aTex.a - _Cutoff);
    63.                 return aTex;
    64.             }
    65.          
    66.             ENDCG
    67.         }
    68.     }
    69. }
    i still got the unexpected token error, what could be wrong ?
     

    Attached Files:

  4. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    826
    Hey there,

    you got three small typos in there (Sadly small typos are not acceptable in shaders ;) )

    At line 51 you tried to access the uv texture coordinates by calling VertexInPut.texcoord, but actually you need to get the data not from the data class but instance, so calling input.texcoord is the way to do it.

    The other two typos are in your structs at line 39 and 45. It is not TEXCOORDO, but rather TEXCOORD0.

    Here is the corrected shader (which does compile, I didn't check it for functionality):
    Code (csharp):
    1. Shader "Custom/Stencil/Mask OneZLess"
    2. {
    3.     Properties
    4.     {
    5.         _AlphaTex("Alpha (A)", 2D) = "white" {}
    6.         _Cutoff("Alpha cutoff",float) = 0.5
    7.     }
    8.  
    9.     SubShader
    10.     {
    11.         Tags{ "RenderType" = "TransparentCutout" "Queue" = "AlphaTest" "IgnoreProjector" = "True" }
    12.         ColorMask 0
    13.         ZWrite off
    14.         Cull Back
    15.         ZTest Less
    16.  
    17.         Stencil
    18.         {
    19.             Ref 1
    20.             Comp always
    21.             Pass replace
    22.         }
    23.  
    24.         Pass
    25.         {
    26.             CGPROGRAM
    27.  
    28.             #pragma vertex vert
    29.             #pragma fragment frag
    30.  
    31.             uniform sampler2D _AlphaTex;
    32.             uniform float _Cutoff;
    33.  
    34.             struct VertexInPut
    35.             {
    36.                 float4 vertex : POSITION;
    37.                 float4 texcoord : TEXCOORD0;
    38.             };
    39.  
    40.             struct VertexOutPut
    41.             {
    42.                 float4 pos : SV_POSITION;
    43.                 float4 tex : TEXCOORD0;
    44.  
    45.             };
    46.  
    47.             VertexOutPut vert(VertexInPut input)
    48.             {
    49.                 VertexOutPut output;
    50.  
    51.                 output.tex = input.texcoord;
    52.                 output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
    53.                 return output;
    54.             }
    55.  
    56.  
    57.             fixed4 frag(VertexOutPut input) : COLOR
    58.             {
    59.                 fixed4 aTex = tex2D(_AlphaTex,input.tex.xy);
    60.             clip(aTex.a - _Cutoff);
    61.             return aTex;
    62.             }
    63.  
    64.             ENDCG
    65.         }
    66.     }
    67. }
    68.  
     
  5. Andreas88

    Andreas88

    Joined:
    Dec 22, 2015
    Posts:
    67
    ah now it works !
    thank you so much. it´s clear that i seriously need to learn how to code shaders. most of the stuff in there is in fact copy paste from an tutorial. i would love to learn how to write shaders but it´s difficult to find any good understandable tutorials. do you know any good way to learn it ? also i find it strange that there are no autocompletion when writing shaders.

    but thank you very much for your help :)
     
  6. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    826