Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Shader for Clipping plane

Discussion in 'Shaders' started by Michele, Jul 9, 2008.

  1. Michele

    Michele

    Joined:
    Apr 28, 2008
    Posts:
    73
    Hi all,
    I don't know nothing about shaders :( and in a topic suggest me to make a custom shader, I hope someone of you could help me.
    I need to realize a clipping plane, like the Near clip of the camera.

    I have some geometry and I want a plane (or cube) that slice on it and hide all there is before (or inside if it is more simple)

    It's possible? Could you help me?

    Thanks
     

    Attached Files:

  2. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    So you want two clipping planes in fact?
     
  3. Michele

    Michele

    Joined:
    Apr 28, 2008
    Posts:
    73
    Just one could be usefull :)
    I thought that adding the shader on the cube (fig. 2) it could be simple.

    Just to explain, I attached some picture of the resuls
     

    Attached Files:

  4. Alpha-Loup

    Alpha-Loup

    Joined:
    Jun 23, 2006
    Posts:
    797
    Hmm... looks like a boolean operation is needed here and not a clipping shader.

    I dont know though, if this can be achieved with a shader, but my guess would be, that you have to do the cut inside your 3D App and then export to Unity...
     
  5. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Here's something quite simple limited I cook up (attached whole Unity project). Press Play or just move the Clipper cube around.

    It has some limitations though:
    * It just clips whatever is behind the planes (does not "cap" the holes)
    * It only supports one directional light for illumination
    * ...something else :)

    The way it works is quite simple in fact: there's a clipping texture, all white with black borders. Then a vertex shader computes UVs for that texture in such a way that they would relate to the clipping planes. Alpha testing is done in the shader on that texture. There's a script attached to the clipper object that passes some matrices for the vertex shader.
     

    Attached Files:

  6. Michele

    Michele

    Joined:
    Apr 28, 2008
    Posts:
    73
    eheheh, right, but I need to move the plane in Unity and see the different cutting, I can't export all fases of the boolean operation
     
  7. Michele

    Michele

    Joined:
    Apr 28, 2008
    Posts:
    73

    Your work is appretiated, I don't know how to thank you.
    Your scene works perfectly, but I don't know why, when I insert my models in your scene it doesn't clip my models.
    I Should add "VertexLitClip" material to my models to let it works.
    There is another way with the other material?

    Thanks again

    You are my hero :)
     
  8. Alpha-Loup

    Alpha-Loup

    Joined:
    Jun 23, 2006
    Posts:
    797
    :D Impressive.

    This might come in handy. Thanks Aras!
     
  9. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Your materials should use this shader, yes.
     
  10. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    I've always wanted realtime boolean effects (subtraction and intersection) in Untiy. Aras, is this theoretically possible to do in a Shader with arbitrary geometry?
     
  11. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    I guess so. Never tried :)
     
  12. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,598
    Did anyone manage to take this any further and get caps working on the ends?

    There's an interesting link here to some OpenGL libraries that do it, I wonder if this can be reproduced in unity?

    http://www.opencsg.org/

    I wonder too if this is now possible in 2.1 using a two pass process, render the normals of the clipped objects in the first pass and then process that render to look for any back facing normals those that are get rendered with the texture applied to the clipping plane.
     
  13. metervara

    metervara

    Joined:
    Jun 15, 2006
    Posts:
    203
    Trying to work out some of the quirks of this to get it to work for a project i'm doing ->The original work behaves strangely when scaling the clipping object, and also I need 4 clip-planes.

    To get 4 clip-planes i edited the texture to be square with a black border around the whole thing, but it doesn't work as is with Aras's clipping matrix setup so I replaced the box with an orthographic camera (non-active) and take the projectionMatrix from that. This gives me the four clip-planes, but unfortunately the clipping is not aligned to the camera projection and it also moves the opposite direction in y compared to the camera.

    There's a lot of things going on that I don't have a clue about, e.g the shader clipping changes when changing the near and far clipplanes of the camera.

    I'll keep working on it and if anyone want's to take a look I've attached a package of the thing.
     

    Attached Files:

  14. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    Hey Patrik,

    Just wanted to thank you for sharing :wink:

    The projection matrix is a little funky, but even in it's current state this is really cool to use for some interesting effects.

    Ethan
     
  15. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,598
    I've been playing with this a bit and modified the original test to include basic capping. It only works for non intersecting closed objects but maybe still useful.
    I'm sure someone with more knowledge of shaderLab could optimise this into a single pass at the moment I use 2 for simplicity.
     

    Attached Files:

  16. ratamorph

    ratamorph

    Joined:
    Sep 2, 2007
    Posts:
    458
    I've been playing around with this examples trying to get them to behave the way I need for a project I'm working on.

    If I understand right the code doesn't actually use a clip planes, it uses a texture to emulate how a couple of clip planes would behave, right?

    If so, is there a way to increase the precision of where the clip planes are placed so it starts clipping say at the origin of the volume or the exact side of it?

    As I scale the volume this positions get offset, I'm thinking this is because of the stretching in the clip texture and the offset being where the black pixel starts.

    Any way of just using a single clip plane instead of the clip texture?
     
  17. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,598
    I think if you took the example i did, which I reduced to just one clipping plane and then modified the texture so that the black to white transition was exactly in the center then the clip plane would exactly align with the center of the clipping box. That would allow you to precisely position it.
     
  18. founderio

    founderio

    Joined:
    Aug 20, 2009
    Posts:
    17
    I just wanted to ask if there is a possible way to get just one plane which cuts sth. in half by just hiding the second part, but dynamic.

    I looked at the results in this thread an tested them, but unfortunately these scripts/shaders don't work as I want them to do... So I just had the urge feeling to ask you if and how I can modify the shader/script so that it does what I described above, just hides one half of an object at a given plane...
     
  19. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
  20. founderio

    founderio

    Joined:
    Aug 20, 2009
    Posts:
    17
    Thanks for that Link. that is what I was looking for.

    But can anyone please tell me how I can set the Position/Rotation of the cutting plane with a script?

    I tried but failed...

    Code (csharp):
    1.  
    2. var Obj : Transform;
    3. var vec : Vector3;
    4. @script ExecuteInEditMode()
    5. function OnDrawGizmos() {
    6.     var upperLeft : Vector3 = transform.position + transform.up * 5 + transform.right * -5;
    7.     var upperRight : Vector3 = transform.position + transform.up * 5 + transform.right * 5;
    8.     var lowerRight : Vector3 = transform.position + transform.up * -5 + transform.right * 5;
    9.     var lowerLeft : Vector3 = transform.position + transform.up * -5 + transform.right * -5;
    10.     Gizmos.DrawLine(upperLeft,upperRight);
    11.     Gizmos.DrawLine(upperRight,lowerRight);
    12.     Gizmos.DrawLine(lowerRight,lowerLeft);
    13.     Gizmos.DrawLine(lowerLeft,upperLeft);
    14. }
    15. function Update() {
    16.     vec = transform.forward;
    17.     Obj.renderer.materials[0].shader.SetGlobalVector("section_depth", Vector4(transform.forward.x, transform.forward.y, transform.forward.z, 0));
    18. }
    19.  
     
  21. muffinman

    muffinman

    Joined:
    Feb 12, 2009
    Posts:
    6
    Would it be possible to modify the shader in order it to outline of the shape that has been cut?
    How could this be implemented ?
     
  22. Peter Hou

    Peter Hou

    Joined:
    Oct 12, 2010
    Posts:
    66
    This is very impressive. Has any progress been made or was the development of this shader shelved?
    Importing the unitypackage in Unity 3.1 doesn't appear to get the same result as in the screenshot. Maybe some changes or manual setup is required?
     
  23. qholmes

    qholmes

    Joined:
    Sep 17, 2010
    Posts:
    162
    Yes i would love to have an updated version of this shader... I have been looking for an easy way to do clipping of objects to show details underneath...

    Does anyone have an updated shader or ideas?

    Q
     
  24. solmyr-fr

    solmyr-fr

    Joined:
    Jun 18, 2009
    Posts:
    24
  25. Hummelwalker

    Hummelwalker

    Joined:
    Oct 28, 2010
    Posts:
    119
    Hello everybody,

    have anybody a workable solution for a cross section in Unity 3.x ?

    The way above don't works well in the current Unity version. And I don't understand the solution of solmyr. I'm not very familar with shaders, so help would be awesome :)

    Carsten
     
  26. joshstiemsma

    joshstiemsma

    Joined:
    Oct 18, 2013
    Posts:
    4
    hey guys lol this is a super old thread but it still pops up relatively high on google search for clipping as well as cross section cutting so i thought i'd share with you guys an awesome unity asset that I found that works very well.

    In the asset store look up cross section shaders and there is one by abdullah aldandarawy that works super simple and very very well even with multiple slicing planes. Not only that but I wanted to get some very specific functions out my cross section and was able to change his very clean scripts and shader code to fit my situation. I can suggest it enough and I wouldn't pay for any other one, plus its free!

    I don't even know the guy I just think this is exactly what people are looking for when they find themselves here.