Impossible Geometry with Stencil Shaders in Unity URP

Ойындар

Games like Antichamber feature impossible geometry where multiple objects seemingly inhabit the same physical space, but only appear when viewed from certain angles. We can recreate the effect in Unity using stencil shaders and Universal Render Pipeline's special Renderer Features functionality!
------------
👇 Download the project on GitHub: github.com/daniel-ilett/shade...
📝 Read the article version here: danielilett.com/2022-01-05-tu...
------------
💖 Huge thanks to my Patreon supporters for January 2022, especially: Gemma Louise Ilett, Morrie, Pisit Praiwattana, Jack Dixon, and Underwater SUN.
------------
🎮 This project uses Unity 2020.3.21f1 and URP 10.6.0, but it should work with other versions.
📦 This tutorial uses the 'Boat Lowpoly' model by RacKrab on Sketchfab: sketchfab.com/3d-models/boat-...
------------
📚 Get a copy of my shader book here: www.dpbolvw.net/click-10074214...
✨ Grab Snapshot Shaders Pro here: assetstore.unity.com/packages...
------------
💬 Join the Discord: / discord
💖 Support me on Patreon: www.patreon.com/danielilett?f...
☕ Or throw me a one-off coffee on Ko-fi: ko-fi.com/danielilett
#gamedev #shaders #madewithunity

Пікірлер: 69

  • @gleetch_yt
    @gleetch_yt Жыл бұрын

    Great tutorial! For anybody who's having issues with Occlusion being rendered above opaque items of the stencil just turn on the toggle "After Opaque" inside the SSAO render feature panel of the render settings.

  • @kylebanks
    @kylebanks2 жыл бұрын

    Great tutorial! I always find the stencil buffer a bit tricky to reason about, but it's so useful and you explain it really well here.

  • @infokubarcade
    @infokubarcade Жыл бұрын

    This was incredible. It's very hard to find high quality content that are not especially for begginers. Im glad i found you. Thanks for sharing !

  • @2ksp235
    @2ksp2352 жыл бұрын

    OMG U just saved my life i was looking FOR this IN the URP pipeline God bless you OMG!!!!

  • @yoyopei3793
    @yoyopei37932 ай бұрын

    Thank you Daniel, still helping great after 2 years

  • @DevDunkStudio
    @DevDunkStudio Жыл бұрын

    I keep sharing this guide to so many game devs, especially for AR

  • @glorioustoast2717
    @glorioustoast27172 жыл бұрын

    This tutorial was a life saver, if only I had found it sooner instead of spending hours trying to solve one problem. Thanks!

  • @WaenglTaengl
    @WaenglTaengl Жыл бұрын

    That, my friend, was a superb tutorial! 🚀♥ I really enjoyed watching it and using it for my character selection in foil trading card style! :)

  • @danielilett

    @danielilett

    Жыл бұрын

    That's a really good application of the effect for sure!

  • @sleep0
    @sleep0Ай бұрын

    Thanks for the short and clear tutorial!

  • @ChakravarthyElumalai
    @ChakravarthyElumalai4 ай бұрын

    Absolute lovely tut.

  • @danielilett

    @danielilett

    4 ай бұрын

    Thanks! Glad it helped you :D

  • @dorothy9892
    @dorothy98928 ай бұрын

    A whole week of trying, crying and chatgpt-ing and you solved my problem in 7 minutes T_T Thank you!

  • @MaxIzrin
    @MaxIzrin2 жыл бұрын

    Great video.

  • @favouredfool1363
    @favouredfool1363 Жыл бұрын

    FOR NEWER VERSIONS turn off "Depth Priming Mode" under "Rendering Path" in the URP Renderer Settings. Otherwise your shader is required to have a depth-normals pass to function which will make Daniels shader non-functioning.

  • @lukeyager5883

    @lukeyager5883

    9 ай бұрын

    Thank you! I did this but unfortunately my objects are now transparent. I've seen a couple other people say to turn on "After Opaque" under SSAO, but that has not worked for me. Did you ever find a solution to this?

  • @lukeyager5883

    @lukeyager5883

    9 ай бұрын

    Actually funnily enough it seems to only be an issue in the scene view. Runtime looks fin, but if you have any tips let me know!

  • @meganweber5057
    @meganweber5057 Жыл бұрын

    If you wanted to do it without any shader code or extra materials, you could give each quad its own layer, and put a renderer feature in for its layer, before the one for the corresponding object's layer, where the comp function is never, the value is the id, and the fail operation is replace.

  • @danielilett

    @danielilett

    Жыл бұрын

    Yeah, that's a really good way of doing it!

  • @helikaon00

    @helikaon00

    5 ай бұрын

    Can you describe in more detail what you mean? Thanks

  • @helikaon00

    @helikaon00

    5 ай бұрын

    Can you describe what she meant? I want to achieve this result but I can't use shaders. I CAN use a shader graph, but I don't know how to do this in shader graph. If I could avoid shaders all together, that might be best. Thanks!@@danielilett

  • @sealsharp
    @sealsharp6 күн бұрын

    Very good!

  • @moonroll1985
    @moonroll19852 жыл бұрын

    Good tutorial, thx a lot ;)

  • @cryingpsycho
    @cryingpsycho2 жыл бұрын

    Holy shit. Fucking brilliant. Give us more LSD type impossible shader things in the future pls

  • @alrightytighty1572
    @alrightytighty15722 жыл бұрын

    Honestly, knowing almost nothing about shaders, this tutorial helped a lot. There was never a point in which I didn't know why you were writing something because of your excellent explanations. Two thumbs up from me!

  • @galamotshaku
    @galamotshakuАй бұрын

    Thanks Daniel

  • @JacazzaJade
    @JacazzaJade2 жыл бұрын

    This is such a cool effect!! I was wondering do stencil buffers work in 2D renderers too?

  • @techtalkwithsubho6176
    @techtalkwithsubho61762 жыл бұрын

    op sir I liked it

  • @spyschannel8000
    @spyschannel80008 ай бұрын

    You're amazing

  • @MetabaronX
    @MetabaronX2 жыл бұрын

    Hi, I just found your channel and I love it. I am amazed of the simplicity in the solutions. Not that they are simple! Great work. I have a small question - Is there an easy way to exclude all layers but the stencil layer. So only the stencil 1 layer object are displayed in the box!?

  • @user199x

    @user199x

    Жыл бұрын

    Sorry to budge in, if you ever get an answer I'd love to know too.

  • @underbelly69
    @underbelly695 ай бұрын

    nice! would you consider making a tutorial for a virtual door / stencil the player can step thru in VR in passthrough on Quest3 ? eg. a door appears in your lounge that leads from your real lounge to a virtual space beyond.. and when you are inside the VR space - you can see back through the door to your real lounge again

  • @jamesuniverss
    @jamesuniverss Жыл бұрын

    Hi dude ! amazing tutorial, ty for this. But I have a question, how can I use Transparent shader with your stencil method ? 🤔

  • @izzynab332
    @izzynab3322 жыл бұрын

    Nice

  • @lecarno_
    @lecarno_6 ай бұрын

    Hi this video has helped out alot, but I was wondering if theres a way to also include shadows into this?

  • @clifflin7149
    @clifflin7149 Жыл бұрын

    Problem: what if you see from the back-side, your shader still write to stencil buffer -> you still see the object, which is not expected.

  • @siamakrudyani3310
    @siamakrudyani33103 ай бұрын

    Great content. Is it possible to use this technique for making a portal for vr & ar ? thank you

  • @timarlow8007
    @timarlow80072 жыл бұрын

    Very cool, could this be used to create an interior mapping / fake interior shader to use for an apartment building for example?

  • @danielilett

    @danielilett

    2 жыл бұрын

    Potentially? Interior mapping is one of those subjects I keep meaning to look into in more detail, because I think there are some interesting nuances if you want to keep the effect performant. I do remember seeing people talking about it a while back - here's one of the articles I remember reading which serves as a decent overview of techniques: www.alanzucconi.com/2018/09/10/shader-showcase-9/

  • @WelshGuitarDude
    @WelshGuitarDude Жыл бұрын

    Been trying to do this for masking out water in a volume mesh it's giving headaches because I also have transparent windows in volume like subnautica bases, and I cannot get it working correctly because I'm masking a transparent layer of water and so if I'm in the volume looking through a window I am still masking the water because the camera is inside the volume mask looking out. Haven't been able to solve this issue in urp.

  • @SiyuChen-xr
    @SiyuChen-xr6 ай бұрын

    Thanks for your awesome tutorial. I was able to have my gameobject showing throw the stencil. However, the particle systems didn't show through the stencil. Is there other modification needed for particle system, other than layer name? Thanks!

  • @sickflow_makes
    @sickflow_makes2 жыл бұрын

    Edit: I just noticed you have source code available, I will look into the source code, will find my answer :) First thanks for the tutorial.. I find this really interesting and trying to experiment with this right now, I have a question regarding the demo scene.. for each "impossible cube" did you have to create 4 stencil faces and layers for each cube and then added a renderer feature in the pipeline for each stencil layer? I am missing around with this concept to create cards with full assets/models inside them... but don't want to do this for each card

  • @jansentee3d939
    @jansentee3d93911 ай бұрын

    Thanks, works fine. Is it possible to use this as a red dot? As soon as I drag the object with the stencil layer into the red dot PFB, it is always visible, even if the gun and the red dot have the stencil layer, can you help me further?

  • @WelshGuitarDude
    @WelshGuitarDude Жыл бұрын

    How do you read the value in the fragment shader to use to display what you like? You're comparing but not getting the value to use say to set a colour in fragment which is what I can't figure out

  • @darkdoom907
    @darkdoom907 Жыл бұрын

    What objects are set to stencil layer 1 andlayer 2. I understand that you have set two quads as stencil layer1 and 2. But what about the objects that are to be seen through those quads??What layer are they set to??

  • @darkdoom907

    @darkdoom907

    Жыл бұрын

    I really like how easy you have made this, but I cannot grasp what is the scene setup, or how objects are placed

  • @Wojmanp123
    @Wojmanp123 Жыл бұрын

    Make sure you have your Quads facing the right direction! Or else the stenciled objects won't show at all

  • @stylie473joker5

    @stylie473joker5

    Жыл бұрын

    You mean Correct normals facing outside ?

  • @fukukyun78
    @fukukyun78 Жыл бұрын

    I'm confused, i'm trying to get this to work with my rabbit hole, it's like a dirt hole that digs into the ground, but i want to put a mask on top and if, say, the ground is intersecting in the hole, it will only render the rabbit hole and ignore that ground... I couldn't get it working with my forward renderer for some reason, so i tried just straight up importing yours. It works in that it shows the object only through the mask, but it doesn't ignore the ground...so i end up with only being able to see the hole through the mask, and still not being able to mask out the land...

  • @ederjairdejesushernandezto8891
    @ederjairdejesushernandezto88912 жыл бұрын

    Could someone teach me how to cut out a mesh with another mesh ? I have a simple plane, and I want to place a cube that creates a hole in the plane as it intersects it, I also want to resize the cube by picking on of its vertices, kind of resizing a panel but in 3d. I'm new on unity so I would really appreciate if someone can help my out with that

  • @sammraz1990
    @sammraz1990 Жыл бұрын

    Very cool, I just have a small problem and don't know how to fix it :(. In Unity 2022.1.16f, ambient occlusion of objects behind is projected in front of objects that are in the stencil layer. Is there a way to fix it? Thanks in advance

  • @sammraz1990

    @sammraz1990

    Жыл бұрын

    nevermind, fixed it by setting ambient occlusion in UPR Renderer Settings to After Opaque

  • @nickgreenex

    @nickgreenex

    Жыл бұрын

    @@sammraz1990 thanks! you saved my time

  • @drkcarva9477
    @drkcarva9477 Жыл бұрын

    hi can someone help me .ı have issues about shadder ı write a shadder it makes object invisible but dont render behind objects .the issue is when ı add unity terrain shadder dont work right it makes object invisible but if behind object is unity terrain it still render unity terrain. ı thinks its about unity terrains mesh renderer .btw sorry my english

  • @chunchaulam9925
    @chunchaulam99259 ай бұрын

    Would like to know if this works on HDRP too?

  • @molukkenberg9321
    @molukkenberg9321 Жыл бұрын

    It may not be your area of expertise, but do you know how to make this work in altpsace?

  • @maxiaohuimaxiao
    @maxiaohuimaxiao2 жыл бұрын

    好厉害啊!这比我看到的其他的效果更好!

  • @_TommyP
    @_TommyP9 ай бұрын

    has anyone gotten this to work with Water shaders?

  • @Emanes32
    @Emanes324 ай бұрын

    How would you make walking into it?

  • @danielilett

    @danielilett

    4 ай бұрын

    Assuming both 'versions' of the room need collision that matches the objects you can see, I'd detect when the player crosses one of the boundaries into the room, and at that point, turn on the colliders for only that 'version'. As for rendering properly, if your game is third-person (and assuming the room is small enough that the camera can always stay outside the room), you can move the player model into the correct layer for that room version so they render properly too. Then put them back into the default layer when they exit. If your game is first-person, then I suppose that's trickier. When you approach really close to one of the boundaries, assuming by that point you can only actually see one of the sides of the room and therefore technically don't need the 'impossible' effect to be active, I would move all of that version's objects into a regular layer so it gets rendered normally. Then, when you exit, I'd swap the objects back into the special layer for that room version. Mind you, I haven't tried this out or tested it, so this is just me guessing.

  • @Emanes32

    @Emanes32

    2 ай бұрын

    @@danielilett I made a version for VR, where there is a model of a person and I applied that stencil shader to it and made it double sided. When player triggers collision I just scale up that model so it makes cool transition into the other space when you walk through the person shape portal... and as its double sided, you see the correct models from the inside :) Thanks so much for the video by the way

  • @Emanes32

    @Emanes32

    2 ай бұрын

    This is it in VR for curiosity kzread.info6K48MqFuCIw

  • @manai2683
    @manai26832 жыл бұрын

    Why the hell is this still not implemented in Shader Graph or am I bad at searching for info

  • @danielilett

    @danielilett

    2 жыл бұрын

    Stencils are still not in Shader Graph, no. I would love for them to be added, but I don't know if it's planned?

  • @Need2pee

    @Need2pee

    2 жыл бұрын

    @@danielilett Unreal has all this stuff with their blueprints from so many years it's funny how unity is still lacking behind

  • @carwashh
    @carwashh Жыл бұрын

    Any idea why this no longer works on PC? In Unity 2021.3.16 (probably just 2021.3..?) and URP 12.1.8 the stencil no longer works when the platfor is set to Windows/ PC/ Mac. Swap to Android or iOS and it works again.

  • @carwashh

    @carwashh

    Жыл бұрын

    Updating to 2021.3.22 sorted it, must have been a bug in the version I was using.

  • @lukeyager5883
    @lukeyager5883 Жыл бұрын

    My objects become slightly transparent after setting the layer to a stencil layer. Any advice? Thanks!

  • @xXRubenzgzXx

    @xXRubenzgzXx

    Жыл бұрын

    I believe that might be Unity's default SSAO acting up. You can go to the forward renderer > Renderer features and uncheck SSAO. Or if you want to keep the SSAO effect then check the "After Opaque" box inside the SSAO renderer feature.

Келесі