Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Shader Graph: How to make a simple clipping shader?

Discussion in 'Graphics Experimental Previews' started by jariwake, Nov 14, 2018.

  1. jariwake

    jariwake

    Joined:
    Jun 2, 2017
    Posts:
    100
    I´m new to shader graph and I like the fact that it allows creating shaders without memorizing the bizarre HLSL syntax by heart =)

    However, I stumbled upon a problem: I have an old clipping shader that just discards all pixels below a certain plane in the world (the clipping plane is defined as position and normal vectors). Unity could not convert my shader automatically, so I guess I need to create a new lightweight rendering pipeline (which my project uses) compatible shader then.

    My question is: How can I create a similar clipping shader using shader graph? I tried playing with the position, step and comparison nodes, but only got weird results. Is it even possible with the built-in nodes or do I need to create a custom node for the clipping/discarding?

    Any help for a shader graph newbie would be appreciated. I did not find such shader by searching the net/docs.

    Or if someone can point me to a LWRP compatible clipping shader, that would be awesome as well!
     
    Last edited: Nov 14, 2018
  2. Andy-Touch

    Andy-Touch

    A Moon Shaped Bool Unity Legend

    Joined:
    May 5, 2014
    Posts:
    1,485
    Did you try using the Alpha Clip Threshold output on the Master Node?
     
    marcospgp likes this.
  3. Ehredt

    Ehredt

    Joined:
    Jun 20, 2018
    Posts:
    79
    Would Alpha Clip Threshold + something position-based work for a clipping shader? I've had good results using the Alpha Clip Threshold for other things so I'm assuming it could work well for a clipping shader.

    Speaking of, I need to figure out how to make a cutaway shader, i.e. a clipping shader but the "empty" part is filled in with a solid color or texture. Is there a way to do that using Shader Graph?
     
  4. jariwake

    jariwake

    Joined:
    Jun 2, 2017
    Posts:
    100
    Thanks for the answers @Andy-Touch and @Knipscher. I tried toying with the Alpha Clip Threshold in the PBR Master node, but could not get any clipping to happen, but thats probably due to my very limited skills on shader graph.

    This is what I thought would do the trick (on X axis), but no luck:
    upload_2018-11-21_19-0-35.png

    The docs only say "Fragments with an alpha below this value will be discarded. Requires a node connection. Expected range 0 - 1." So I am not sure how to make this work.

    Would it be possible to guide me a little bit here? Or even post an example?
     
  5. jariwake

    jariwake

    Joined:
    Jun 2, 2017
    Posts:
    100
    Ok like 3 nanoseconds after posting the above I got it - I have to set the position input to Alpha input and then Clip property to the Alpha Clip Threashold, like this:

    upload_2018-11-21_19-11-48.png

    Yay, I have a very primitive clipping shader made with shader graph. Of course this works only on one axis and in world position.

    Any tips how to improve this are welcome.
     
    MrKsi, Franceppo and Jade72 like this.
  6. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    lwrp_sg_cp.png

    This is a more advanced one.
     
    DenisIsDenis and Franceppo like this.
  7. TimCoster

    TimCoster

    Joined:
    May 19, 2016
    Posts:
    16
    Apperantly, atm the alpha clip threshold only works when a property node is inserted
     
    keepmove likes this.
  8. zezba9000

    zezba9000

    Joined:
    Sep 28, 2010
    Posts:
    992
    Why not just expose the clip method.... writing shaders like this takes way more time and so much more bloat.
     
    LaireonGames and hippocoder like this.
  9. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    It is a matter of choice and depends on user case.
    The simplest case is a single material and in this case you are right - the clip keyword and plane properties should be exposed and you can manage the clipping properties locally on material values.
    But when using a number of materials with the same clipping plane and keyword - it might be more convenient to hide them on shaders and materials so you could manage them globally by shader globals.
     
  10. zezba9000

    zezba9000

    Joined:
    Sep 28, 2010
    Posts:
    992
    Unity just messed up their shader developer tools is whats so annoying.

    I wish they would have done it like the points below as it would have solved more issues in a more elegant way.
    * Make an agnostic C# => shader lang transpiler using Roslyn syntax analyzers.
    * Make Shader-Graph compile to C# shaders.
    * This allows people who know what they're doing to write more optimized shaders in C# while people who don't to use shader graph.

    But whatever stuff in Unity is pretty hacky atm.
     
  11. VirtualPierogi

    VirtualPierogi

    Joined:
    Sep 3, 2012
    Posts:
    54
    Thats one of the best ideas ive ever seen about shaders
     
  12. zezba9000

    zezba9000

    Joined:
    Sep 28, 2010
    Posts:
    992
    CS2X will eventually support this "C# => native shader lang" concept later: https://github.com/reignstudios/CS2X
    Or course its not a Unity3D thing but something I've always wanted.
     
  13. Josepht45

    Josepht45

    Joined:
    Oct 20, 2014
    Posts:
    13
    Hey, I know it's been a couple years, but do you know how I could add a rotation to the clipping plane? I'm SO close to having this effect being how i need it, all I need is the rotation.

    Thanks ahead of time!
     
  14. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    Just rotate the SectionPlane and then update it on the material, so this way you will get the section plane rotating around SectionPoint.
     
  15. MrKsi

    MrKsi

    Joined:
    Aug 30, 2020
    Posts:
    139
    Thanks for your brain, man, I couldn't do it for 2 weeks) Although I knew it, this is a banal thing, only I used R for the place. A in Split
     
  16. DenisIsDenis

    DenisIsDenis

    Joined:
    Aug 11, 2020
    Posts:
    2
    scr_20230118_190918_501.png
    I improved to fit my needs, thanks.
    Now it works like this: clipping occurs at a point relative to the object and along the normal of the plane.