Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Fjonan · Aug 23, 2019 at 08:35 PM · shadersshader programmingstencil

Stencil Shader no longer working with Lightweight Render Pipeline

I wanted to use the LWRP in my project which I successfully upgraded to 2019.2.0f1. I follow the documentation on integrating the LWRP into the project and all works well (updating shaders and materials) except for my custom stencil shader. I have to admit I do not understand very well what is happening here - I copied the shader from this source and managed to adjust it so that I can give color values to the shader by code.


What it does

https://youtu.be/uAzcKoEUN2M This is the shader in action - the yellow area has a "occluder" material and the circled "occluded" (shader code down below). The effect of part of the circle color changing when entering the yellow area is the effect that I want. In addition when the circle collides with the yellow area I set the target color I want with code on the yellow-area-object.


The Problem

When switching my graphics setting to use LWRP the occluded part (stencil ref 4) gets cut off but the second pass of the occluded shader simply does not trigger. I tried switching the passes around and now the occluded part works but the un-occluded one does not, so it only executes the first pass. I tried for hours and cannot find out why that is. When I remove LWRP from my graphics settings the shader works as intended.


The Code

This is the shader code for the "occluder":

 // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
 
 Shader "Sprites/Occluder"
 {
   Properties
   {
      [PerRendererData] _MainTex ( "Sprite Texture", 2D ) = "white" {}
       _Color ( "Tint", Color ) = ( 1, 1, 1, 1 )
      [MaterialToggle] PixelSnap ( "Pixel snap", Float ) = 0
       _AlphaCutoff ( "Alpha Cutoff", Range( 0.01, 1.0 ) ) = 0.1
   }
 
 
   SubShader
   {
       Tags
       {
           "Queue" = "Transparent"
           "IgnoreProjector" = "True"
           "RenderType" = "TransparentCutout"
           "PreviewType" = "Plane"
           "CanUseSpriteAtlas" = "True"
       }
 
       Cull Off
       Lighting Off
       ZWrite Off
       Blend One OneMinusSrcAlpha
 
       Pass
       {
           Stencil
           {
               Ref 4
               Comp Always
               Pass Replace
           }
 
       CGPROGRAM
           #pragma vertex vert
           #pragma fragment frag
           #pragma multi_compile _ PIXELSNAP_ON
           #include "UnityCG.cginc"
 
           struct appdata_t
           {
               float4 vertex   : POSITION;
               float4 color    : COLOR;
               float2 texcoord : TEXCOORD0;
           };
 
           struct v2f
           {
               float4 vertex   : SV_POSITION;
               fixed4 color    : COLOR;
               half2 texcoord  : TEXCOORD0;
           };
 
           fixed4 _Color;
           fixed _AlphaCutoff;
 
           v2f vert( appdata_t IN )
           {
               v2f OUT;
               OUT.vertex = UnityObjectToClipPos( IN.vertex );
               OUT.texcoord = IN.texcoord;
               OUT.color = IN.color * _Color;
               #ifdef PIXELSNAP_ON
               OUT.vertex = UnityPixelSnap( OUT.vertex );
               #endif
 
               return OUT;
           }
 
           sampler2D _MainTex;
           sampler2D _AlphaTex;
 
 
           fixed4 frag( v2f IN ) : SV_Target
           {
               fixed4 c = tex2D( _MainTex, IN.texcoord ) * IN.color;
               c.rgb *= c.a;
 
               // here we discard pixels below our _AlphaCutoff so the stencil buffer only gets written to
               // where there are actual pixels returned. If the occluders are all tight meshes (such as solid rectangles)
               // this is not necessary and a non-transparent shader would be a better fit.
               //clip( c.a - _AlphaCutoff );
 
               return c;
           }
       ENDCG
       }
   }
 }


This is the shader coder for the "occluded":

 // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
 
 Shader "Sprites/Occluded"
 {
   Properties
   {
      [PerRendererData] _MainTex ( "Sprite Texture", 2D ) = "white" {}
       _Color ( "Tint", Color ) = ( 1, 1, 1, 1 )
      [MaterialToggle] PixelSnap ( "Pixel snap", Float ) = 0
      [PerRendererData] _OccludedColor ( "Occluded Tint", Color ) = ( 0, 0, 0, 0.5 )
   }
 
 
 CGINCLUDE
 
 // shared structs and vert program used in both the vert and frag programs
 struct appdata_t
 {
   float4 vertex   : POSITION;
   float4 color    : COLOR;
   float2 texcoord : TEXCOORD0;
 };
 
 struct v2f
 {
   float4 vertex   : SV_POSITION;
   fixed4 color    : COLOR;
   half2 texcoord  : TEXCOORD0;
 };
 
 
 fixed4 _Color;
 sampler2D _MainTex;
 
 
 v2f vert( appdata_t IN )
 {
   v2f OUT;
   OUT.vertex = UnityObjectToClipPos( IN.vertex );
   OUT.texcoord = IN.texcoord;
   OUT.color = IN.color * _Color;
   #ifdef PIXELSNAP_ON
   //OUT.vertex = UnityPixelSnap( OUT.vertex );
   #endif
 
   return OUT;
 }
 
 ENDCG
 
 
 
   SubShader
   {
       Tags
       {
           "Queue" = "Transparent"
           "IgnoreProjector" = "True"
           "RenderType" = "Transparent"
           "PreviewType" = "Plane"
           "CanUseSpriteAtlas" = "True"
       }
 
       Cull Off
       Lighting Off
       ZWrite Off
       Blend One OneMinusSrcAlpha
 
       Pass
       {
           Stencil
           {
               Ref 4
               Comp NotEqual
           }
 
 
       CGPROGRAM
           #pragma vertex vert
           #pragma fragment frag
           #pragma multi_compile _ PIXELSNAP_ON
           #include "UnityCG.cginc"
 
 
           fixed4 frag( v2f IN ) : SV_Target
           {
               fixed4 c = tex2D( _MainTex, IN.texcoord ) * IN.color;
               c.rgb *= c.a;
               return c;
           }
       ENDCG
       }
 
 
       // occluded pixel pass. Anything rendered here is behind an occluder
       Pass
       {
           Stencil
           {
               Ref 4
               Comp Equal
           }
 
       CGPROGRAM
           #pragma vertex vert
           #pragma fragment frag
           #pragma multi_compile _ PIXELSNAP_ON
           #include "UnityCG.cginc"
 
           fixed4 _OccludedColor;
 
 
           fixed4 frag( v2f IN ) : SV_Target
           {
               fixed4 c = tex2D( _MainTex, IN.texcoord );
               if (c.r < .5 && c.g < .5 && c.b < .5 ){
                 return _OccludedColor * c;
               } else {
                 return _OccludedColor * c.a * _OccludedColor.a;
               }
           }
       ENDCG
       }
   }
 }

So I am at a loss right now. I really wanna use shader graph for my project but this stencil shader is very important so I need it to work but I don't understand why it broke. Thanks for your time.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Fjonan · Aug 26, 2019 at 07:15 PM

I got it to work with LWRP! So to not go DenverCoder9 on you, here is how I did it in the hopes, that it helps someone who wants to transfer their legacy multi pass shader and/or stencil shader to the Lightweight Render Pipeline.

First, you have to know, that old multi pass shaders do not work by design.

I tried to transfer from what I understood the shader does to the overrides:

Create a custom renderer for LWRP: Custom Renderer for LWRP

Deselect the layer for which to use the "occluded" shader (in my case player): Deselect target layer for your stencil shader

Create a "renderer feature", select target layer to be rendered. Since I use sprites I need to select Queue: Transparent and Event: After Rendering Transparents this depends on the target game object. Override the "stencil", value 4, Compare function "not equal" and "Keep" in all three cases. This one makes sure you render the layer again since we de-selected it from the default layer mask. StencilNotReplaced Renderer Feature

Create a second "renderer feature", select everything the same except the compare function, here "equal". For this renderer feature we also override the material with our "occluded" material that uses the shader. StencilReplaced Renderer Feature

You need to apply the "occluder" shader to an object so when both shaders are used at the same time, the stencil comparison will be triggered.

Lastly change the "occluded" shader from the question by deleting the first pass, only keep the second one after line 95 (starting with "occluded pixel pass. Anything rendered here is behind an occluder").

Here are some helpful links I found along the way to illuminate the stencil shader a bit and might include more reference points:

  • Stencil Buffer tutorial by Ronja Böhringer (check out the other shader tutorials on the page as well!)

  • Forum thread on multipass effects that is very much ongoing

  • the original stencil shader I used

  • some changes to the shader I made to use some custom properties

Leeloo Dallas Multi-pass

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image moustachecabal · Oct 24, 2019 at 08:25 AM 0
Share

Thank very much for your answer. I spent so much time banging my head against the wall because of the LWRP as I was trying to make the stencil checks work. I could not crack it until I reread your answer. Thanks!

avatar image irenya · Jan 27, 2020 at 04:48 AM 0
Share

Thank you for taking the time to give such a complete explanation and the shaders! Was going crazy trying to find a way to occlude part of a mesh with a transparent object and between the instructions and just a small modification of one of the shaders, got it to work within $$anonymous$$utes. Thanks a lot!!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

121 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Shader that renders pixel with highest alpha value? 0 Answers

Multi-shader Stencil Buffer not working as expected,Multi-shader Stencil not working as expected 0 Answers

Help me understand stencil shader logic 1 Answer

My object keeps rendering the object in the back first using stencil buffer 0 Answers

Limit Amount of Light Applied to Sprite 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges