THIS IS THE BEST TUTORIAL out there for URP / Stencil / Depth mask - for AR masking as well. Don't search further. I checked other tutorials, this one is the best. Thank you for your work!
@@pixtrick i feel the focus more on the stencil aspect was more important imo. And yet there's still a lot that i dont know about stencils like the satuates and wraps. This at least should help many get a decwnt understanding
I've been working on a feature for quite some time now, and recently came to the conclusion that shaders/stenciling was my best option. This video has brought me so close to the solution, but I fear I'm still too new to figure out the solution. Here's the context: I have a building exterior and interior. When the player enters the building, I want all exterior objects to fade into darkness, and interior objects to remain on screen (The walls move out of the way, so thats not the issue here). When the player exits, those objects fade back in. From my failed attempts and the info I got from this video, here's what I think I need to do: Separate interior objects from other objects by putting them on their own layer Have a world space blackout screen hover in front of the camera. Set the Blackout screen to its own layer "blackout" When I want to fade out all exterior objects, I call in the blackout screen Use a stencil that accomplishes the following: If an "interior" object has been rendered on this pixel, AND would be overwritten by a "blackout" pixel, use the "Interior" pixel. This seems to be very close to what you're doing in this video, so I am almost certain that this is the solution, but I can't seem to wrap my head around how this looks set up.
@@pixtrick mostly, but I realize this doesn't completely work for my use-case. I need to make a hole in an object, but only if the "hole creator" object is actually visible, i.e. not occluded. With you example, the "hole creator" can be behind the object (occluded) and it will still make that part invisible.
I will ask this here because your video is truly one of the best stencil buffer explanations I have seen over the years (especially considering read/write mask)^^ You say turn off shadow casting for the ground. That is because the shadow caster pass does not care about the stencil value. That is really not ideal. I see the workaround of setting the walls to draw shadows both sided okay but it still feels a bit broken.. the same is true for for any kind of depth pre pass unfortunately. Since the ground is disabled from the regular rendering loop it does not show up in the depth pre pass at all which will prevent us from using depth priming as optimization and the objects will also be missing in the depth texture (except depth priming is disabled, but unfortunately this only applies the the game view, scene view is always using a depth pre pass). Btw SSAO in DepthNormals mode forces a depthNormals pre pass.. so the same issue applies here as well. That is why it was not working for stylie473joker5. After Opaque mode + priming disabled copies depth from the depth written by the color pass, but that works only in very few cases. Do you have any ideas how one could fix / work around that issue? The way you show here is the way everyone seems to implement stencil buffer usage in URP but it's seems incredibly limited due to these limitation. Thanks for thinking about it :)
Hey! Thanks for the comment and the kind words I appreciate that! Now, about your question, I don't really think there's a real workaround for that, since the shadow caster pass uses the vertex data to draw shadows. Since the fake geometry created by the stencil buffer is not an actual geometry (I mean it is not some vertex data, it's just pixels on screen), the shadow caster pass cannot render there properly. Then, I don't really see any workaround for keeping the ground shadows while not rendering them into a hole. Now, if you really need your ground shadow (I assume it would be for like a platformer game or something), I would suggest not to use the stencil buffer unless you're really forced to. Use real geometries as much as you can. I think this is not (or maybe it is but it would be such a pain regarding performance) possible to draw shadows pixel by pixel, so that the shadows are not rendered into the hole but are rendered for the ground, but that is absolutely not reasonable. May I ask you what you are trying to achieve? Maybe there's a workaround in your situation and I would be glad to help :) Have a great day !
@@pixtrickThanks so much for the reply :) I think, I figured out two possible solutions by now but either way it sucks a bit that this is so exhausting to do. Beware this will be a long text, thanks for letting me rubberduck a bit. It is in fact not a plattformer, rather an isometric adventure type of game. I am making this example up but the situation is basically like this. Imagine a hill and ground mesh with a meteor crater on top. The crater is the hole I would like to stencil out. I can not build this right into the mesh because it is procedurally placed. Inside the crater I have a plane which reads the depth texture to create some fog-ish dust. Issue 1: Shadows of the hill are drawn without the stencil cutout hence everything in the crater is shadowed. Turning of the shadows would mean no shadows in the surrounding area by the hill. Issue 2: the information in the depth texture is also wrong. This is a bit harder to explain and needed a bit of digging in the frame debugger and render pipeline. The Render objects feature only writes to the cameras color target. The color target contains a depth channel but it is only used when certain conditions are met. For example in the game view, if depth priming is disabled and nothing else forces a depth pre pass (like ssao in depthNormals mode). The scene view always uses a depth pre pass not matter what we do. When we remove the ground from the regular render loop and only render it through render objects it is completely removed from the depth pre pass because the render objects feature does not set that up and the mask definition that we have set excludes it from ALL main passes (except for shadows...). I wonder why this isn't done in the render feature but that I guess that this is the reason why the feature still says experimental. Unity didnt figure out a way to make it compatible with any possible srp renderer. This is an issue what the typical "hey draw the characters silhouette" tutorials don't have because they draw the character in the regular loop too. As for solutions, one option I can make work is not using stencils at all. Render everything in the main loop, set the queue for ground materials to 1990 and create a depth mask shader + material that overwrites the depth where it is rendered with infinitely far away. Then everything that is rendering afterwards renders as usual and even shadows are correct (because the shadow passes use the depth only passes of the shaders too). The other solution is to modify the render objects feature to also create a depth pre pass or inject the missing draw calls into the existing pass with the correct stencil renderblock set and render the stencil render objects feature extremely early (before pre passes). Second solution is more flexible / cleaner and actually allows using the stencil buffer fully but it is much more work. I am at the point where stuff renders in the depth pass but not with stencils yet. I guess my issue here is simply summarized with: Render Objects does not work with depth pre pass, stencil in render objects does not work with shadows in URP, but it could, unity just didn't finish it yet 😕.
Hmmm your thinking process might be good, but I think there's a much simpler solution for such a problem. Basically, your problem is : how to generate a hole in a procedurally generated terrain. To me, this is much simpler than having to deal with depth pre passes and everything, plus it would be much more cleaner. As I said before, you have to use real geometries as much as you can, and only use the stencil buffer when it's needed. To me, you shouldn't use the stencil buffer at all for this problem since the crater will not move in your scene (I mean I assume it won't move). I don't know how you generate your terrain and how it should look, but I think you should use techniques such as the wave function collapse that allows you to design "tiles" that you can put together in numerous ways. That way, you can keep a procedurally generated terrain and would only have to make a tile where there is the crater. If you still really want to use the stencil buffer, well don't xD I cannot really help since I think it would feel broken ether way. Still, it was extremely interesting to read your different solutions, but yeah to me you should change your approach to this problem. Hope that helps !
Disabling the ground collider is fine for a single entity but you’re quite likely to have multiple entities and those will still rely on the ground entity!!
Absolutely, that was just an example, it's totally up to you to improve this ;) In this case what you would do instead is disabling the collision between the 2 colliders basically
It is indeed possible, but you won't have access to the render features. Basically, the URP allows a per-layer rendering with its render features (so you don't have to worry about the materials of each object of the layer), whereas the built-in RP don't, so you'll have to write custom shaders to get the same result, it's a bit harder but 100% doable in my opinion.
@@pixtrick thanks for the response! I finally succeeded! I think the hardest part was that i used Amplify and its stencil buffer parameters were some time buggy 😅 But you're explanation was really great and help me find the way!!
Thank you for your hard work, The hole is working as intended however The wall i put the render feature on is being rendered transparent (i can see stuff behind it) any idea what am i missing ? Edit: its the Screen space ambient occlusion affecting the wall like that (disabling it fixes the issue). but that's not a good solution. Edit 2 : Ticking "After Opaque" in the Ambient Occlusion render feature fixed the issue. I hope it helps anyone having the same issue and thanks again for the awesome tutorial
I can't seem to follow your instructions when it comes to NOT using a shader to solve it. You pop up the render object settings but I can't figure out how to set it up to work. I am trying to solve this without shaders because I am developing something that can't use normal shaders at all. It could use shader graph, but I don't know how to do this in shader graph either.
Hey ! You just have to use a render feature then. Basically, instead of using a shader for the mask material, you just have to create a render feature with a Never stencil comparison operation, so that it won't show on screen. You can use any material you want for that mask then since it won't show on screen at all. EDIT : I forgot to mention that after doing that, you'll have to use the Fail operation instead of the Pass operation, because the comparison operation will always be false, so that the Pass operation will never trigger.
Hmm, yeah, I just can't seem to figure it out. I was trying to make something for the Apple Vision Pro but you can't use normal shaders, you can only use shader graph (or other node based shading methods), because they use MaterialX which requires node based shading. Thanks for your help though! Appreciate you taking the time to respond.@@pixtrick
@@helikaon00 I struggled with the same thing and got it to work. I don't know if it will work for you or anyone else reading, but this is what I did: - Make sure that the culling mask dropdown menu of your main camera has both your walls layer and your occlusion object layer checked - Make sure the proper render pipeline asset (the one you have the stencil on) is set in the quality section of project settings - Make sure that in the "Filtering" section of the render pipeline asset inspector window, the Opaque Layer Mask dropdown has both your walls layer and your occlusion layer UNchecked - On the render feature for the walls, set Event to AfterRenderingOpaques, set the layer mask to your walls layer, enable Stencil and set its value to 1, and then go "Not Equal, Keep, Keep, Keep" for the next four dropdown menus, in that order from top to bottom. - On the render feature for the obscuring object, set Event to BeforeRenderingOpaques, set the layer mask to your obscuring object layer, enable Stencil and set its value to 1, and then go "Never, Keep, Replace, Keep" for the next four dropdown menus, in that order from top to bottom. Lastly, in case anyone wants this to only work when behind a wall, not in front, give your obscuring object a new script. Have it fire a raycast from itself to the main camera every frame. If the raycast is obstructed by any object in the wall layer, change it's scale to whatever size you want. Otherwise, change its scale to 0. I'm not a professional at this, I figured this out through trial and error. So if it doesn't work for you, I'm really sorry but I have no clue how to fix this. I just hate when people post "I figured it out" and don't say how, so hopefully this helps someone.
Great tutorial! I have a question, why do we create a new layer for each view? Can’t we modify the materials of each object and change their stencil ref values to be equal to 1 2 3 4 respectively and set the test to equal? I don’t want to clutter the project with many layers just for some effect. Same for the first ground crack effect. Follow up Q for the Mask shader, could we have said Comp Never and Fail Replace? Wouldn’t that also make the mask invisible? Would we still need Blend Zero One in that case? Another follow up Q.. for the objects that fail the stencil test and not get drawn, does Unity still write them to the depth buffer?
Only great questions here! About the layers, you can use materials that have stencil behaviours for the objects in the cube without having to modify their layer indeed. The drawback of this method is that you have to create a custom shader for these objects so that you can customise their stencil behaviour. You wouldn't be able to use any material for these objects, whereas you can with the layer method. About the mask shader: yes, Comp Never / Fail Replace is a great method for making an invisible mask. However, it won't draw at all, so it won't write into the depth buffer. It could be annoying for some edge cases, but for I tend to prefer that approach too. You don't need ZWrite Off or Blend Zero One with that technique, I find that quite elegant. About your last question, I partially answered it before but just to clarify: if a pixel fails the stencil test, it is not rendered. If it is not rendered, it cannot write into the depth buffer. Hope that helps!
The magic card example makes me think. I tested and if I let the card as Default Layer and all the content to the new layer, I can set a Render Feature with the same stencil value and using Equal to have the same effect. Without 2 render features. Did I miss something? Thanks for your tutorials
Do you mean that the effect works without a render feature for the card itself ? Interesting ! I would be curious to see that To me, the card model would overlap all the content if you render it without a render feature, but perhaps I'm wrong, I will test that soon ;)
It's true that using your example you created the hole and show the content, it's perfect. But we can have a hole modeled in the card and use the quad only to show the content. Not a really important thing...
@@marcmantra oh yeah that works as well ! I just used this technique to allow you all to resize the hole in the card as you wish I agree this is not an important thing, but I didn't think about that to be honest x)
Amazing tutorial! I'm currently having some preformance issuses in my VR project while using the solution you show us on video. Do you have any performance tips to VR projects? or any performance tips in general? thanks!!
@@pixtrick I'm currently creating a magic window, where the player can see a theater inside one window of a house. It works somehow like the magic card u show us. But the FPS drops below 50 when the player look trough that window. Do you know something I can do to improve the shader performace? Thank you so much!!
ì'm going to shoot a comment just in case, but the magic card doesn't work for me, the quad does not cut a hole through the card and does not show anything through it. I've foilowed the tutorial exactly and i did the important thing you mentioned earlier, what might be wrong? the only way the quad cuts through the card is when i set the default layer as visible, otherwise it does not work at all, what could fix this?
Hi! Did you set the mask and card layermasks correctly ? I'm not entirely sure what can be wrong, but you can send me a message on discord to show me what you're trying to do
I'd really like to learn how to do it without requiring a special layer for the ground as I'm working on a game where the layers have already been carefully setup and can't really be changed
If you want to see through the hole, yes it could be useful. However, most games use decals for that, which is basically slapping a bullet impact texture on a wall. Still, I would be curious to see that! That's kind of a good idea :)
What method should I use if I want to render mouth over beard of each character even when beard is covered mouth? I tried following your video but mouths of all characters is now rendered over every character's beard.
Great tutorial! The card effect is really useful for Augmented Reality experiences! I have only one problem, let's say I have an object that animates from inside the card/portal in and out. Currently the object is only renderd through the portal, so in case it goes outside, and you look the portal from the side the object will disappear. How do I need to set up the materials and settings to achieve the effect?
Hey mate, glad you found an interesting application for this tutorial :D The portal inside is indeed rendered only through the portal. I mean, yeah, that's intended. Now, if you want to render the objects even if they are in front of the portal without using the stencil buffer (basically looking the objects from the side when they are out of the portal) and only in that case, this gets a lot more tricky, because you'll need to use the depth buffer, but transparent materials (such as the one used for the portal) do not write into the depth buffer so you're a bit boned... You'll need to use a trick to make the portal material write into the depth buffer, then create another render feature to render the objects out of the portal. Actually, the hardest part will be to find that trick ... Hope that helps ! Good luck !
@@pixtrick yes it is disabled and the rendering path to forward, I tried different depth texture modes as well. I'll try to isolate the problem by making a new project and make some test and I'll come back to you.
Hey, I'm not very sure about that, you should try ! In any case, if the render features are not available, you can still use the stencil buffer through custom shaders
Decals are not compatible with the stencil buffer, or at least I haven't find a way to make them work together yet. We discussed about it on the discord server of the community but didn't find a solution ... Regarding SSAO, I'm not entirely sure why it doesn't work as intended ... It may be either the AfterRenderingOpaques event queue or simply because objects rendered through a render feature are not considered as "real" geometries
Hi, one of the first portal tutorials I saw relied on a mask material instead of a stencil buffer, to make the outsides of the portal invisible. It seemed way easier to me than setting up a stencil buffer. Is there a reason this is better? Thanks!
Hi ! I think that what you saw is also based on the stencil buffer, but perhaps the main difference here is the use of the URP which allows you to add stencil parameters on any rendered material. However, I agree it might be annoying to set up, so if you're only using hand-written shaders (shader graphs doesn't count), you can add these stencil behaviours way more easily. Does it answer your question? x)
Hi, this is so great. I was wondering if you could tell me what version of unity you are using? I'm finding it impossible to get transparent materials to render even after adding the renderobjects.
Hey I'm interested in holes that do not require a special layer for the ground ;) Also, for the hole collider, is it not possible to not have it convex? (I guess it would be possible to make a non-convex shape by making multiple convex shapes work together)
It is indeed possible but you'll then have to procedurally generate a concave mesh that fits the hole geometry. I'll maybe make a video on that later since it's very interesting but quite advanced too
@@ninocoillard6775 when you say enabled, do you mean when you can see through the door or do you have something that enables or disables the view through the door ?
@@pixtrick I want to move object/objects over other layers or on certain layers in 3d platform. This is is easy in 2D but in 3D it is not satisfactory to me. There is a solution in Unity docs but it is not working for URP. The second one is shadows, and i couldnt get rid off weird shadow renders for this technique. I also want to see proper shadows on other objects. :/
@@Hozgen you can use the depth buffer to do that I suppose. Maybe you can contact me on discord to go more into details because I'm not sure to get your problem to be honest :/
New sub! I have a doubt, basically in the example of the letter it's as if it were a "hologram" on a cellphone using the position of your eyes (camera). Is there a possibility to obtain that texture relative to the camera position so that on the full screen I can create this hologram effect? This is not with stencils, but I have been looking for how to do this and can't find any information :c.
Hey, thanks a lot ! I'm not sure to fully understand your request to be honest, is it about the floating text in the magic card? If that so, it's only a canvas in world space with appropriate dimensions
@@pixtrick nonono, I have an eyetracker device, I can know the user position and I want that the monitor screen act like that portal to make that card effect, It's so tricky. If you atatch the game camara to the user real position works but not that good that the card effect
@@pixtrick Not so much like a parallax, more like the monitor was a window and there were things through it. It looks the same as what the card effect looks like.
THIS IS THE BEST TUTORIAL out there for URP / Stencil / Depth mask - for AR masking as well. Don't search further. I checked other tutorials, this one is the best. Thank you for your work!
Thanks for the kind words ! :D
This is a high level video. Instead of showing what to do, you also explained how it works actually. Great tutorial.
Thanks for these kind words :D
Thank you for this. And definitely yes regarding the Advanced Hole Cutting Techniques. Will be waiting for that video too.
I'll add it on my list then ;)
11:50 you could also assign those faces as separate materials
Definitely yeah, I didn't talk about the UV mapping either but that was not the sake of the video
@@pixtrick i feel the focus more on the stencil aspect was more important imo. And yet there's still a lot that i dont know about stencils like the satuates and wraps. This at least should help many get a decwnt understanding
It is the best tutorial on the Stencil buffer. Thank you so much for the great video!
It's an honour ! Thank you so much !
Thank you so much for the amazing explanation!
You're welcome :D
I've been working on a feature for quite some time now, and recently came to the conclusion that shaders/stenciling was my best option. This video has brought me so close to the solution, but I fear I'm still too new to figure out the solution.
Here's the context:
I have a building exterior and interior. When the player enters the building, I want all exterior objects to fade into darkness, and interior objects to remain on screen (The walls move out of the way, so thats not the issue here). When the player exits, those objects fade back in.
From my failed attempts and the info I got from this video, here's what I think I need to do:
Separate interior objects from other objects by putting them on their own layer
Have a world space blackout screen hover in front of the camera.
Set the Blackout screen to its own layer "blackout"
When I want to fade out all exterior objects, I call in the blackout screen
Use a stencil that accomplishes the following:
If an "interior" object has been rendered on this pixel, AND would be overwritten by a "blackout" pixel, use the "Interior" pixel.
This seems to be very close to what you're doing in this video, so I am almost certain that this is the solution, but I can't seem to wrap my head around how this looks set up.
I guess we already figured this out ;)
presentation quality off the charts! liked and subbed
Thank you so much :D
Thanks! It's a great tutorial, worked perfectly right off the bat.
I appreciate the video but honestly this is so fn complicated. UPDATE: I got it working. Took some real concentration 😅 appreciate your help here!
Yeah, it is quite difficult to grasp. Glad you got it working though!
@@pixtrick mostly, but I realize this doesn't completely work for my use-case. I need to make a hole in an object, but only if the "hole creator" object is actually visible, i.e. not occluded. With you example, the "hole creator" can be behind the object (occluded) and it will still make that part invisible.
You would have to use the depth buffer on top of the stencil buffer for that. You can dm me on discord if you want to talk more about it
I will ask this here because your video is truly one of the best stencil buffer explanations I have seen over the years (especially considering read/write mask)^^
You say turn off shadow casting for the ground. That is because the shadow caster pass does not care about the stencil value. That is really not ideal. I see the workaround of setting the walls to draw shadows both sided okay but it still feels a bit broken.. the same is true for for any kind of depth pre pass unfortunately. Since the ground is disabled from the regular rendering loop it does not show up in the depth pre pass at all which will prevent us from using depth priming as optimization and the objects will also be missing in the depth texture (except depth priming is disabled, but unfortunately this only applies the the game view, scene view is always using a depth pre pass).
Btw SSAO in DepthNormals mode forces a depthNormals pre pass.. so the same issue applies here as well. That is why it was not working for stylie473joker5. After Opaque mode + priming disabled copies depth from the depth written by the color pass, but that works only in very few cases.
Do you have any ideas how one could fix / work around that issue? The way you show here is the way everyone seems to implement stencil buffer usage in URP but it's seems incredibly limited due to these limitation. Thanks for thinking about it :)
Hey! Thanks for the comment and the kind words I appreciate that!
Now, about your question, I don't really think there's a real workaround for that, since the shadow caster pass uses the vertex data to draw shadows. Since the fake geometry created by the stencil buffer is not an actual geometry (I mean it is not some vertex data, it's just pixels on screen), the shadow caster pass cannot render there properly. Then, I don't really see any workaround for keeping the ground shadows while not rendering them into a hole. Now, if you really need your ground shadow (I assume it would be for like a platformer game or something), I would suggest not to use the stencil buffer unless you're really forced to. Use real geometries as much as you can. I think this is not (or maybe it is but it would be such a pain regarding performance) possible to draw shadows pixel by pixel, so that the shadows are not rendered into the hole but are rendered for the ground, but that is absolutely not reasonable.
May I ask you what you are trying to achieve? Maybe there's a workaround in your situation and I would be glad to help :)
Have a great day !
@@pixtrickThanks so much for the reply :)
I think, I figured out two possible solutions by now but either way it sucks a bit that this is so exhausting to do. Beware this will be a long text, thanks for letting me rubberduck a bit.
It is in fact not a plattformer, rather an isometric adventure type of game. I am making this example up but the situation is basically like this. Imagine a hill and ground mesh with a meteor crater on top. The crater is the hole I would like to stencil out. I can not build this right into the mesh because it is procedurally placed. Inside the crater I have a plane which reads the depth texture to create some fog-ish dust.
Issue 1: Shadows of the hill are drawn without the stencil cutout hence everything in the crater is shadowed. Turning of the shadows would mean no shadows in the surrounding area by the hill.
Issue 2: the information in the depth texture is also wrong. This is a bit harder to explain and needed a bit of digging in the frame debugger and render pipeline. The Render objects feature only writes to the cameras color target. The color target contains a depth channel but it is only used when certain conditions are met. For example in the game view, if depth priming is disabled and nothing else forces a depth pre pass (like ssao in depthNormals mode). The scene view always uses a depth pre pass not matter what we do.
When we remove the ground from the regular render loop and only render it through render objects it is completely removed from the depth pre pass because the render objects feature does not set that up and the mask definition that we have set excludes it from ALL main passes (except for shadows...).
I wonder why this isn't done in the render feature but that I guess that this is the reason why the feature still says experimental. Unity didnt figure out a way to make it compatible with any possible srp renderer. This is an issue what the typical "hey draw the characters silhouette" tutorials don't have because they draw the character in the regular loop too.
As for solutions, one option I can make work is not using stencils at all. Render everything in the main loop, set the queue for ground materials to 1990 and create a depth mask shader + material that overwrites the depth where it is rendered with infinitely far away. Then everything that is rendering afterwards renders as usual and even shadows are correct (because the shadow passes use the depth only passes of the shaders too).
The other solution is to modify the render objects feature to also create a depth pre pass or inject the missing draw calls into the existing pass with the correct stencil renderblock set and render the stencil render objects feature extremely early (before pre passes).
Second solution is more flexible / cleaner and actually allows using the stencil buffer fully but it is much more work. I am at the point where stuff renders in the depth pass but not with stencils yet.
I guess my issue here is simply summarized with: Render Objects does not work with depth pre pass, stencil in render objects does not work with shadows in URP, but it could, unity just didn't finish it yet 😕.
Hmmm your thinking process might be good, but I think there's a much simpler solution for such a problem.
Basically, your problem is : how to generate a hole in a procedurally generated terrain. To me, this is much simpler than having to deal with depth pre passes and everything, plus it would be much more cleaner. As I said before, you have to use real geometries as much as you can, and only use the stencil buffer when it's needed. To me, you shouldn't use the stencil buffer at all for this problem since the crater will not move in your scene (I mean I assume it won't move). I don't know how you generate your terrain and how it should look, but I think you should use techniques such as the wave function collapse that allows you to design "tiles" that you can put together in numerous ways. That way, you can keep a procedurally generated terrain and would only have to make a tile where there is the crater.
If you still really want to use the stencil buffer, well don't xD
I cannot really help since I think it would feel broken ether way. Still, it was extremely interesting to read your different solutions, but yeah to me you should change your approach to this problem.
Hope that helps !
This helps a lot! You make my day!🤫
Trop bien ! Merci Pix !!
Disabling the ground collider is fine for a single entity but you’re quite likely to have multiple entities and those will still rely on the ground entity!!
Absolutely, that was just an example, it's totally up to you to improve this ;)
In this case what you would do instead is disabling the collision between the 2 colliders basically
I think this 3D mask system i was looking for i will definitely do some tests tomorrow.
Don't hesitate to message me on discord if you need anything
great explanation! is this possible in the build in render pipeline ? i struggle a lot to convert the technique correctly
It is indeed possible, but you won't have access to the render features.
Basically, the URP allows a per-layer rendering with its render features (so you don't have to worry about the materials of each object of the layer), whereas the built-in RP don't, so you'll have to write custom shaders to get the same result, it's a bit harder but 100% doable in my opinion.
@@pixtrick thanks for the response!
I finally succeeded!
I think the hardest part was that i used Amplify and its stencil buffer parameters were some time buggy 😅
But you're explanation was really great and help me find the way!!
Man this is some good stuff!
You should consider making a game someday! (:
Perhaps someday ... Stay tuned ;)
Thanks a lot I really needed it ! It's very clear ! Another one ?
For sure ! Stay tuned ;)
Thanks for sharing! :)
It's my pleasure !
Thank you for your hard work, The hole is working as intended however The wall i put the render feature on is being rendered transparent (i can see stuff behind it) any idea what am i missing ?
Edit: its the Screen space ambient occlusion affecting the wall like that (disabling it fixes the issue). but that's not a good solution.
Edit 2 : Ticking "After Opaque" in the Ambient Occlusion render feature fixed the issue.
I hope it helps anyone having the same issue and thanks again for the awesome tutorial
I didn't know there was such a weird interaction ! Thanks for sharing that :)
I can add a bit more information about why this wasn't working and kind of still is broken. See my other comment I left :)
Thank you so much you done great job
Nice Video, Learn A Lot
That's good news!
Just excellent.
Thank you so much !
I can't seem to follow your instructions when it comes to NOT using a shader to solve it. You pop up the render object settings but I can't figure out how to set it up to work.
I am trying to solve this without shaders because I am developing something that can't use normal shaders at all. It could use shader graph, but I don't know how to do this in shader graph either.
Hey ! You just have to use a render feature then. Basically, instead of using a shader for the mask material, you just have to create a render feature with a Never stencil comparison operation, so that it won't show on screen. You can use any material you want for that mask then since it won't show on screen at all.
EDIT : I forgot to mention that after doing that, you'll have to use the Fail operation instead of the Pass operation, because the comparison operation will always be false, so that the Pass operation will never trigger.
Hmm, yeah, I just can't seem to figure it out. I was trying to make something for the Apple Vision Pro but you can't use normal shaders, you can only use shader graph (or other node based shading methods), because they use MaterialX which requires node based shading. Thanks for your help though! Appreciate you taking the time to respond.@@pixtrick
@@helikaon00 I struggled with the same thing and got it to work. I don't know if it will work for you or anyone else reading, but this is what I did:
- Make sure that the culling mask dropdown menu of your main camera has both your walls layer and your occlusion object layer checked
- Make sure the proper render pipeline asset (the one you have the stencil on) is set in the quality section of project settings
- Make sure that in the "Filtering" section of the render pipeline asset inspector window, the Opaque Layer Mask dropdown has both your walls layer and your occlusion layer UNchecked
- On the render feature for the walls, set Event to AfterRenderingOpaques, set the layer mask to your walls layer, enable Stencil and set its value to 1, and then go "Not Equal, Keep, Keep, Keep" for the next four dropdown menus, in that order from top to bottom.
- On the render feature for the obscuring object, set Event to BeforeRenderingOpaques, set the layer mask to your obscuring object layer, enable Stencil and set its value to 1, and then go "Never, Keep, Replace, Keep" for the next four dropdown menus, in that order from top to bottom.
Lastly, in case anyone wants this to only work when behind a wall, not in front, give your obscuring object a new script. Have it fire a raycast from itself to the main camera every frame. If the raycast is obstructed by any object in the wall layer, change it's scale to whatever size you want. Otherwise, change its scale to 0.
I'm not a professional at this, I figured this out through trial and error. So if it doesn't work for you, I'm really sorry but I have no clue how to fix this. I just hate when people post "I figured it out" and don't say how, so hopefully this helps someone.
Great tutorial! I have a question, why do we create a new layer for each view? Can’t we modify the materials of each object and change their stencil ref values to be equal to 1 2 3 4 respectively and set the test to equal? I don’t want to clutter the project with many layers just for some effect. Same for the first ground crack effect.
Follow up Q for the Mask shader, could we have said Comp Never and Fail Replace? Wouldn’t that also make the mask invisible? Would we still need Blend Zero One in that case?
Another follow up Q.. for the objects that fail the stencil test and not get drawn, does Unity still write them to the depth buffer?
Only great questions here!
About the layers, you can use materials that have stencil behaviours for the objects in the cube without having to modify their layer indeed. The drawback of this method is that you have to create a custom shader for these objects so that you can customise their stencil behaviour. You wouldn't be able to use any material for these objects, whereas you can with the layer method.
About the mask shader: yes, Comp Never / Fail Replace is a great method for making an invisible mask. However, it won't draw at all, so it won't write into the depth buffer. It could be annoying for some edge cases, but for I tend to prefer that approach too. You don't need ZWrite Off or Blend Zero One with that technique, I find that quite elegant.
About your last question, I partially answered it before but just to clarify: if a pixel fails the stencil test, it is not rendered. If it is not rendered, it cannot write into the depth buffer.
Hope that helps!
@@pixtrick Amazing! Thank you
The magic card example makes me think. I tested and if I let the card as Default Layer and all the content to the new layer, I can set a Render Feature with the same stencil value and using Equal to have the same effect. Without 2 render features. Did I miss something? Thanks for your tutorials
Do you mean that the effect works without a render feature for the card itself ? Interesting ! I would be curious to see that
To me, the card model would overlap all the content if you render it without a render feature, but perhaps I'm wrong, I will test that soon ;)
It's true that using your example you created the hole and show the content, it's perfect. But we can have a hole modeled in the card and use the quad only to show the content. Not a really important thing...
@@marcmantra oh yeah that works as well ! I just used this technique to allow you all to resize the hole in the card as you wish
I agree this is not an important thing, but I didn't think about that to be honest x)
Amazing tutorial! I'm currently having some preformance issuses in my VR project while using the solution you show us on video. Do you have any performance tips to VR projects? or any performance tips in general? thanks!!
What solution are you using?
@@pixtrick I'm currently creating a magic window, where the player can see a theater inside one window of a house. It works somehow like the magic card u show us. But the FPS drops below 50 when the player look trough that window. Do you know something I can do to improve the shader performace? Thank you so much!!
@@Vinisetas I would say your theater rendering is screwing up something then. The stencil buffer does not really cost something
@@pixtrick Turns out there was a real time light one the theater scene, now the stencil is working perfectly fine, thanks a lot!!
ì'm going to shoot a comment just in case, but the magic card doesn't work for me, the quad does not cut a hole through the card and does not show anything through it. I've foilowed the tutorial exactly and i did the important thing you mentioned earlier, what might be wrong? the only way the quad cuts through the card is when i set the default layer as visible, otherwise it does not work at all, what could fix this?
Hi! Did you set the mask and card layermasks correctly ? I'm not entirely sure what can be wrong, but you can send me a message on discord to show me what you're trying to do
I'd really like to learn how to do it without requiring a special layer for the ground as I'm working on a game where the layers have already been carefully setup and can't really be changed
I'll try to make a video on that
Could this be used for making bullet holes on walls etc?
If you want to see through the hole, yes it could be useful. However, most games use decals for that, which is basically slapping a bullet impact texture on a wall.
Still, I would be curious to see that! That's kind of a good idea :)
Hi, how did you manage to make the inverted cube invisible from the side? Like the tunnel to go through? Great tutorial
Hi ! It's an inverted render culling, you can access this parameter in the material editor
What method should I use if I want to render mouth over beard of each character even when beard is covered mouth? I tried following your video but mouths of all characters is now rendered over every character's beard.
I don’t understand what is wrong
Great tutorial! The card effect is really useful for Augmented Reality experiences! I have only one problem, let's say I have an object that animates from inside the card/portal in and out. Currently the object is only renderd through the portal, so in case it goes outside, and you look the portal from the side the object will disappear. How do I need to set up the materials and settings to achieve the effect?
Hey mate, glad you found an interesting application for this tutorial :D
The portal inside is indeed rendered only through the portal. I mean, yeah, that's intended. Now, if you want to render the objects even if they are in front of the portal without using the stencil buffer (basically looking the objects from the side when they are out of the portal) and only in that case, this gets a lot more tricky, because you'll need to use the depth buffer, but transparent materials (such as the one used for the portal) do not write into the depth buffer so you're a bit boned... You'll need to use a trick to make the portal material write into the depth buffer, then create another render feature to render the objects out of the portal. Actually, the hardest part will be to find that trick ...
Hope that helps ! Good luck !
did you find a solution?
my braiiinnn
Imagine mine to make the video 💀
Is this effect somehow possible with depth priming set to auto or forced?
Not sure, but all the tests I've made so far point to the fact it is not
This works greatly in Editor for me, but in the build it doesn't function properly (neither on Android nor on PC) and I just get plain geometry.
What unity version are you using ?
2022.3.17f1@@pixtrick
You have to make sure that the depth priming is off, and I'm not entirely sure but I think setting the rendering path to forward can help as well
@@pixtrick yes it is disabled and the rendering path to forward, I tried different depth texture modes as well. I'll try to isolate the problem by making a new project and make some test and I'll come back to you.
@@TheGameLecturer all right ! By the way you can message me on discord if you prefer
Is there any chance to apply the same effect in the HD render pipeline? needed very very much
Hey, I'm not very sure about that, you should try ! In any case, if the render features are not available, you can still use the stencil buffer through custom shaders
SSAO and DECALS not working how can we fix it ?
Decals are not compatible with the stencil buffer, or at least I haven't find a way to make them work together yet. We discussed about it on the discord server of the community but didn't find a solution ...
Regarding SSAO, I'm not entirely sure why it doesn't work as intended ... It may be either the AfterRenderingOpaques event queue or simply because objects rendered through a render feature are not considered as "real" geometries
Hi, one of the first portal tutorials I saw relied on a mask material instead of a stencil buffer, to make the outsides of the portal invisible. It seemed way easier to me than setting up a stencil buffer. Is there a reason this is better? Thanks!
Hi ! I think that what you saw is also based on the stencil buffer, but perhaps the main difference here is the use of the URP which allows you to add stencil parameters on any rendered material.
However, I agree it might be annoying to set up, so if you're only using hand-written shaders (shader graphs doesn't count), you can add these stencil behaviours way more easily.
Does it answer your question? x)
@@pixtrick Thanks! Yes, that answers it:)
Hi, this is so great. I was wondering if you could tell me what version of unity you are using? I'm finding it impossible to get transparent materials to render even after adding the renderobjects.
Did you choose the right render queue for your render feature ? I'm using Unity 2021.3+ by the way
@@pixtrick hey, yep. I ended up getting it working in a new project. No idea what I messed up. Thanks for the very quick reply!
@@_TommyP maybe it was something related to depth priming or the render type then. Anyways, I'm glad you found a solution!
Hey I'm interested in holes that do not require a special layer for the ground ;) Also, for the hole collider, is it not possible to not have it convex? (I guess it would be possible to make a non-convex shape by making multiple convex shapes work together)
It is indeed possible but you'll then have to procedurally generate a concave mesh that fits the hole geometry. I'll maybe make a video on that later since it's very interesting but quite advanced too
Your brain is massive
You're flattering me x)
This is awesome however when I apply the card part to my game with a door I want to mask I can still see the shadow, do you know how to fix it ?
Hey there ! You just have to disable shadow casting in the mesh renderer
Okay, and if I want to cast just when it is enabled and then when it is masked, disable it ?@@pixtrick
@@ninocoillard6775 when you say enabled, do you mean when you can see through the door or do you have something that enables or disables the view through the door ?
can we add this stencil id to shader graph? I couldnt find any method.
Unfortunately, the stencil buffer is not supported by the shader graph ... May i ask why you want to retrieve this information ?
@@pixtrick I want to move object/objects over other layers or on certain layers in 3d platform. This is is easy in 2D but in 3D it is not satisfactory to me. There is a solution in Unity docs but it is not working for URP.
The second one is shadows, and i couldnt get rid off weird shadow renders for this technique. I also want to see proper shadows on other objects. :/
@@Hozgen you can use the depth buffer to do that I suppose. Maybe you can contact me on discord to go more into details because I'm not sure to get your problem to be honest :/
New sub!
I have a doubt, basically in the example of the letter it's as if it were a "hologram" on a cellphone using the position of your eyes (camera). Is there a possibility to obtain that texture relative to the camera position so that on the full screen I can create this hologram effect? This is not with stencils, but I have been looking for how to do this and can't find any information :c.
Hey, thanks a lot !
I'm not sure to fully understand your request to be honest, is it about the floating text in the magic card? If that so, it's only a canvas in world space with appropriate dimensions
@@pixtrick nonono, I have an eyetracker device, I can know the user position and I want that the monitor screen act like that portal to make that card effect, It's so tricky. If you atatch the game camara to the user real position works but not that good that the card effect
It's hard to explain, also is similar to the 3d wallpaper efect, but that use the acelerometer, is not that real effect
@@ToniehGaming ok i think i'm slowly getting it. Is it like a parallax effect ?
@@pixtrick Not so much like a parallax, more like the monitor was a window and there were things through it. It looks the same as what the card effect looks like.
does this work in Unity HDRP?
I think there would be a way to use the stencil buffer in hdrp, but I don't know how honestly (i mean without having to hard code it in a shader)
Can these be done in Shader Graph?
Unfortunately no, shader graph doesn't support the stencil buffer
Merci, maintenant c'est limpide