Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Make Shader Ignore Layers?

Discussion in 'Shader Graph' started by Mashimaro7, Jun 8, 2022.

  1. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    723
    upload_2022-6-8_19-3-8.png
    I have this hole in the game I'm making, if anyone has any alternate ideas, please let me know, but i want the hole to be dug down even if the geometry is intersecting it. I wonder if i could just make a quad in the middle of the hole with a shader that ignores all layers except the hole?

    The hole is dug down a bit. The reason I don't want to just push down the terrain is this hole will not only be used on terrain(in fact, we're thinking of just using geometry for the entire level and doing away with the terrain)

    Thanks in advance!
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,593
    You can use shader, to create hole effect. You could use shader even, to extrude hole, like extruding a tube.

    Or find shader which creates hole and place game object under the terrain, which has shape of the hole and the tunnel.

    I suggest to avoid doing anything procedural, if you never done anything similar.
     
  3. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    723
    The model already has a sufficient hole. I just want a shader that masks the hole, so even if there's geometry from other objects(ie, the ground) in the hole, it only displays the hole.
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,593
    Can you please alla borate a bit more on the desired effect?
    I am unsure I do understand what you require.
    Maybe if you can sketch what you need?
     
  5. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    723
    I just want to make a shader that masks out the terrain.
    Like how right here you can see the terrain where there's supposed to be a hole
    upload_2022-6-8_20-13-54.png
    It must be possible to put a quad over that hole that makes it so it only renders the hole model, right? So i just want to put an invisible quad in the hole that masks out ANY object that isn't hole, maybe i could make it so any rays that pass through the quad only render one layer, the layer the hole is on?
     
  6. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    723
    upload_2022-6-8_20-21-43.png
    Btw, this is what it looks like without any obstructions.
     
  7. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,123
    Well, in theory you could make shader that does not write to color or writes with alpha 0, but also writes to Z buffer and if you queued all objects properly it would do what you want (I guess?)
     
  8. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    723
    Sorry for the late response. I'm not sure how to go about that. I'm not an expert in shader graph haha. How do i queue the objects?If i could make a quad that just always queues the hole's model first for anything that passes through it that would work? I wonder if I would have to put the hole on a layer and set that layer to render first? But I have no idea how to go about this haha
     
  9. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,123
    Antypodish and Mashimaro7 like this.
  10. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    723
    Thanks. I'm kinda confused, I tried doing it the way he did, the only thing is my terrain is an opaque, There's several things I need to change to make it work and I'm not really able to get it working... upload_2022-6-10_20-13-18.png
    This is what I got, the Mask is the quad I have inside of the hole(using an invisible shader), and the ground is on the terrain layer which is on the "masked objects" render feature. I imagine there's a few things I need to change to get it working despite the terrain being an opaque?

    Thanks
     
  11. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    723
  12. JontyAli

    JontyAli

    Joined:
    Feb 3, 2023
    Posts:
    1
    allah borate
     
  13. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
  14. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    723
    Thank you for your response, i believe this is what i need, but i'm having difficulties getting it working. So,
    upload_2023-2-8_18-46-28.png
    On this hole, i gave the mask a stencil material assigned to 1(i just moved it up for testing), but i'm a bit confused, for one, i'm supposed to set this mask to only render the hole, right? But if i uncheck the hole's layer, the entire hole turns invisible... and for another, the stencil mask isn't even displaying the hole through it, even though i have it assigned and have the stencil render feature assigned to hole, whether through the mask or otherwise, i cant see the hole...
    upload_2023-2-8_18-49-10.png
    Would you be able to help me figure out what im doing wrong?

    Thank you!

    Edit: I tried importing the forward renderer he used in that first video, and it has strange results...
    https://imgur.com/a/fkg4kg6
    I changed the layers to correspond with the hole, so it's only rendering the hole through the mask, so the stencil mask is working, more or less... but it's still rendering the ground even through the stencil lol
     

    Attached Files:

    Last edited: Feb 8, 2023
  15. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    A stencil mask is simply:
    - Write to a buffer from your shader
    - Read this buffer when rendering another object

    From the Docs: https://docs.unity3d.com/Manual/SL-Stencil.html

    In this case, you want your hole to write to the Stencil Buffer. Then, your floor can read from the stencil buffer, and clip (not render) when it finds that the hole has written to it.

    The 2 shaders at the bottom of the Docs-page show how one will write a value of 2 to the buffer, where the other will read from the buffer and Clip https://developer.download.nvidia.com/cg/clip.html if it finds a value >= 2
     
  16. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    723
    Oh okay, shaders aren't my specialty so forgive me, but... How do I have to do this? So I need to make the mask on the hole a stencil shader that only writes if it gets a certain value, and the hole should be a separate stencil that's assigned to 1 and the ground could be 2? Does that mean I need to make another custom shader for the ground, though? I'm a bit confused as to how i can assign these values.

    Edit: Sorry, just realized after posting this, oh okay, so, i need a stencil shader that only writes if the number is, say less than 2, and i need to use renderer features to assign these numbers to different layers?