Search Unity

  1. Click here to see what's on sale for the "Best of Super Sale" on the Asset Store
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Stencil buffer for UWP? If so How?

Discussion in 'Universal Render Pipeline' started by castor76, Jan 8, 2020.

  1. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    1,927
    Maybe this was answered somewhere already, but I was wondering if it is possible to still have stencil buffer in UWP?

    If so, how do you go about implementing shader for UWP utilizing stencil buffer? Is there any official documentation about this?
     
  2. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    5,909
    stencil buffers are supported but i do not know of any official documentation.
    when it comes to writing shader using stencil options you will have to write these manually.
    you may use shader graph but have to edit the generated code and add the stencil related code.
     
  3. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    1,927
    Garhhhh
     
  4. lloydv

    lloydv

    Joined:
    Sep 15, 2015
    Posts:
    52
    Depending on what you're trying to do, you might also be able to use the custom renderer features instead.

    I was experimenting with stencils just yesterday trying to create that effect where an object is only visible through a portal/window. My research first led me to stencils and I had no luck getting that to work, but then I tried setting up some layers and custom renderer features and got exactly the effect I wanted surprisingly easily.

    Check out this video, it's for LWRP but it's still pretty followable for URP.
     
    RobertCoomber likes this.
  5. travis_unity746

    travis_unity746

    Joined:
    Nov 12, 2018
    Posts:
    9
    Hi, for anyone else that stumbles across this post like I did and is having trouble using the Stencil buffer in the new URP (or LWRP), I've written a blog post on how worked out how to update my shaders to use the new Render Features (like the video posted above) to create portals/windows etc. I hope it saves someone the few hours I spent trying to work it all out! :)

    https://bdts.com.au/tips-and-resour...s-with-the-universal-render-pipeline-urp.html

     
  6. hdtvee

    hdtvee

    Joined:
    May 7, 2016
    Posts:
    5
    Thank you for the blog post. Unfortunately it does not work for me. Does the object's material behind the window overrides maybe this behavior?
     
  7. travis_unity746

    travis_unity746

    Joined:
    Nov 12, 2018
    Posts:
    9
    Hi, Sorry I'm not sure I understand your question? The material on the objects behind the window in my example use the standard URP/Lit shader. Nothing special is done to the actual materials or shaders themselves; all the overrides are set in the Renderer Features. In both the Renderer Features the material isn't overridden either. Hope this helps!
     
  8. LW

    LW

    Joined:
    Jun 23, 2013
    Posts:
    22
    Yeah, following your blog post (2019.3.9f1) does nothing.
     
    TreeMSI likes this.
  9. slidefactory

    slidefactory

    Joined:
    Dec 12, 2016
    Posts:
    1
    Just got this working in 2019.3.9f1 with URP. So can confirm it works there. Thanks for your great post!

    The key was the Custom Mask shader rendered Opaque with ZWrite off for me. Also, make sure you remove the layers from the Forward Renderer's Opaque Layer Mask. Then make sure your mask is on top so it writes the values before your other layer tries to read them. I have both my events on Before Rendering Opaques.

    There does seem to be a weird glitch in the scene view where the masked objects look transparent, which was driving me crazy. If you turn off the unity axis gizmo preview, it looks fine.
     
  10. Why485

    Why485

    Joined:
    Jun 30, 2013
    Posts:
    41
    I kept on trying to get this to work, but it turns out Stencil Buffer overrides on Render Objects is broken in 7.3.1. If you are on that version, downgrading to 7.2.1 fixed the problem for me. I've already reported the bug to Unity, but this is what happens in 7.3.1.



    After moving to 7.2.1, the stencil values get applied and saved correctly.

     
  11. DebugLogError

    DebugLogError

    Joined:
    Jul 24, 2013
    Posts:
    41
    I ran into that issue with 7.3.1 too. However, you can get it to work in 7.3.1 by clicking on the individual renderer feature in the project tab (instead of editing it via the renderer's list of renderer features in the inspector).

    Annotation 2020-04-24 115107.jpg
     
  12. travis_unity746

    travis_unity746

    Joined:
    Nov 12, 2018
    Posts:
    9
    Sorry you weren't able to get it to work. I've upgraded my project to 2019.3.11f1 and confirmed that I'm using the Universal RP package version 7.3.1. Everything is still working OK for me.

    If I've missed anything in my notes, please let me know an I'll fix my post!
     
  13. UDN_5c806b49-d8a0-4f67-a296-c12c91aa7396

    UDN_5c806b49-d8a0-4f67-a296-c12c91aa7396

    Joined:
    Jan 9, 2017
    Posts:
    129
    Do you have the project file on GitHub or somewhere? I can't seem to get this working too.
     
  14. travis_unity746

    travis_unity746

    Joined:
    Nov 12, 2018
    Posts:
    9

    Attached Files:

    Last edited: Apr 30, 2020
  15. UDN_5c806b49-d8a0-4f67-a296-c12c91aa7396

    UDN_5c806b49-d8a0-4f67-a296-c12c91aa7396

    Joined:
    Jan 9, 2017
    Posts:
    129
  16. isira_pmt

    isira_pmt

    Joined:
    Jul 23, 2018
    Posts:
    1
    Thanks mate.. it worked
     
  17. travis_unity746

    travis_unity746

    Joined:
    Nov 12, 2018
    Posts:
    9
  18. dfkan12

    dfkan12

    Joined:
    Nov 1, 2017
    Posts:
    8
    Hej @travis_unity746, we downloaded your sample scene for URP and it works. However we are wondering whether we are missing some layermasks. Whether they got lost as we opened the scene. Does this scene use layermasks or it just works without them?
     
  19. travis_unity746

    travis_unity746

    Joined:
    Nov 12, 2018
    Posts:
    9
    I'm not sure I understand your question, but I did create two layers for the game objects - a "Window" layer and a "HiddenObjects" layer. The Forward renderer object has two renderer objects which tell it how to draw objects on those two layers.
     
  20. dfkan12

    dfkan12

    Joined:
    Nov 1, 2017
    Posts:
    8
    Thanks that answers it. I think we lost the layers on imports. But it still works dont know why. Thanks for the tutorial.
     
  21. dfkan12

    dfkan12

    Joined:
    Nov 1, 2017
    Posts:
    8
    Hey travis_unity746, how would you clear depth with this technique? Say if there is an object behind the stencil mask quad that is in front of the hidden objects and you want to clear it?
     
unityunity