Search Unity

Simple Cross Section Shader

Discussion in 'Shaders' started by apple_motion, Nov 14, 2009.

  1. Josh-Armour

    Josh-Armour

    Joined:
    Aug 8, 2014
    Posts:
    6
    Nevermind, thank you, this worked great with a bunch of wiggling to figure out the right numbers
     
    Last edited: Mar 25, 2015
  2. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
    I'm probably just being stupid here, but do I apply this shader to a plane in the scene which then intersects the object to be sectioned, or do I somehow apply it to the target object as an extra material? Will it work with objects with multiple submeshes and materials?

    I can get the little preview-sphere in Inspector to show a section but I tried applying it as an additional material on my mesh and I'm getting no effect. Possibly more coffee is required.
     
  3. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
    Well, you could rip the part of the shader that performs the cut and integrate it in your shader.
    It is not designed to work as a second material.
     
  4. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
    I see the effect in the material preview window, but not in the scene or running game view (apart from the artifacts around the edge).
     

    Attached Files:

    • 1.jpg
      1.jpg
      File size:
      35.5 KB
      Views:
      3,482
  5. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
    Yup, I understand that. I suggest to either
    a. write your own shader, perhaps using the one I shared as an example
    b. use the shader I shared, but not in a second material
     
  6. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
    Just to be clear, that's just a simple sphere, not the multi-mesh object I tried first. Not a big deal though. I don't really have time to dive into the rabbit hole of shader writing just yet. :) I ended up buying this Asset Store package that arose from tomekkie2's earlier post. Ten bucks saves me a lot of time and his properties look easier to work with (separate plane and normal).

    I'll circle back to this when I have time to tackle shaders, though. Thanks for taking the time to reply.
     
  7. BioeJD

    BioeJD

    Joined:
    Mar 11, 2016
    Posts:
    6
    As I suspected. No, he won't post code, because he came here and then put it on the market. It appears to me that he used the slices technique (that he referenced above), and then just colored the internal faces as suggested by @Cascho01 ... just a guess though based on his image of the house shown in the asset store.
     
  8. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
    The nice thing about tomekkie's version is that clipping is controlled by a vector which defines the plane centerpoint, another vector that defines the clipping normal, and an offset float which moves the clipping plane along the normal. This makes it very easy to drop empty GameObjects into prefabs to use their position and orientation to define a clipping plane. Here is the Update code from the test project I wrote after buying the asset along with my notes to myself in comments. It's definitely easier to use than any of the other clipping shaders I've found.

    Code (csharp):
    1.     void Update ()
    2.     {
    3.         // Clipping offset from centerpoint along the normal.
    4.         // Prefab objects have a thickness of 1 in this demo
    5.         // so if the marker is at the rear edge (nearest the
    6.         // camera, z=-0.5), an offset of 0.9f is 90% visible.
    7.         float sectionOffset = 0.5f;
    8.  
    9.         // Apply the clipping materials
    10.         clipper.Clip(true, prefab, instance);
    11.  
    12.         // The normal can be defined two ways; this demo uses the second method.
    13.         // 1. set the Euler angles in a Vector3 and convert that to a Quaternion,
    14.         //    then multiply the resulting rotation by a Vector3 unit direction
    15.         //    which represents the normal at (0,0,0) degrees of rotation, or...
    16.         // 2. leave the Euler angles at (0,0,0) which is the same thing as
    17.         //    Quaternion.identity and use the transform of a gameobject that has
    18.         //    been rotated to point along the desired normal.
    19.         Vector4 sectionPlane = Quaternion.identity * marker.forward;
    20.  
    21.         // Centerpoint of clipping plane (called SectionPoint)
    22.         Vector4 sectionPoint = new Vector4(marker.position.x, marker.position.y, marker.position.z, 1f);
    23.  
    24.         // Apply the changes
    25.         clipper.SetClippingPlane(sectionPoint, sectionPlane, sectionOffset);
    26.  
    27.     }
     
  9. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
    @MV10 It is great to see that the solution you found works for you

    @BioeJD If tomekkie2 created a shader that is on sale, good for him. It is not a bad thing to take an idea, polish it, sell and support it as a product. It is called innovation.
     
    MV10 likes this.
  10. BioeJD

    BioeJD

    Joined:
    Mar 11, 2016
    Posts:
    6
    I never said it was a bad thing... where did you get that idea? You shouldn't be assuming I implied that at all. I just told him to not expect him to come back and help.
     
    Last edited: Mar 28, 2016
  11. BioeJD

    BioeJD

    Joined:
    Mar 11, 2016
    Posts:
    6
    But thanks for being a snob and giving me the definition of innovation. You don't have to be condescending. Way to be supportive in the community.
     
  12. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
    You are welcome.
    Indeed, this is what you did, after having 3 posts in total in the forums and zero contributions under this id.
     
  13. w_adams

    w_adams

    Joined:
    Apr 7, 2016
    Posts:
    34
    I just purchased the cross section tool, and I'd like to be able to use it in exactly the way you're describing, @MV10. I'm a bit confused on your implementation. I'll apologize for my ignorance in advance, but how did you configure your objects with the example you gave? From what I can gather, you maybe gutted and then renamed HorizontalClippingSection to clipper, and then you have another script attached to your clipping gameGameobject, which the update function you posted is part of?
     
    Last edited: Apr 7, 2016
  14. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
    I'll just attach my entire test project. Probably easier than trying to write an explanation.

    Edit: Apologies to @tomekkie2, this new zip doesn't include the shader you're selling. :eek:
     

    Attached Files:

    Last edited: Apr 12, 2016
    w_adams likes this.
  15. w_adams

    w_adams

    Joined:
    Apr 7, 2016
    Posts:
    34
    Thank you very much. This is so helpful. Your code is so diligently commented!
     
  16. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,347
  17. Drowning-Monkeys

    Drowning-Monkeys

    Joined:
    Mar 6, 2013
    Posts:
    328
    is it possible, or has anyone made a shader where the polygons are clipped against a bounding volume, like a cube or sphere?
     
  18. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    That is absolutely no problem in doing that.
    Then for a sphere that would be an issue of passing the centre and radius.
    A bounding volume would require more parameters like centre, size and rotation.
     
  19. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    I have just made clipping against sphere in the newest update of my asset.
    https://www.assetstore.unity3d.com/en/#!/content/14296
    Wouldn't be much problem to add cube, box or cylinder options.
     
  20. Stevepunk

    Stevepunk

    Joined:
    Oct 20, 2013
    Posts:
    205
    Is it possible to set a different colour/texture for each object's clipping plane?

    eg. green for leaves, brown for trunk, grey for rocks, etc?
     
  21. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    Absolutely yes. You mean something like on the image below?

    spherical_clipping.jpg
     
  22. seltar_

    seltar_

    Joined:
    Apr 16, 2015
    Posts:
    15
    Are you coloring the inside triangles so that complex meshes still look wonky, or are you actually capping off the discarded mesh?
     
  23. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    Colouring the inside triangles with unlit color + fog, no shadows.
     
  24. Stevepunk

    Stevepunk

    Joined:
    Oct 20, 2013
    Posts:
    205
    So are you creating new geometry to which we can apply a texture, or just showing the insides of the existing geometry with no shading?
     
  25. seltar_

    seltar_

    Joined:
    Apr 16, 2015
    Posts:
    15
    He's not creating new geometry as far as I understood his reply to my question.

    But I found a project on github the other day (https://github.com/daign/clipping-with-caps) which does exactly what we're trying to do. Does anybody know how to translate this into something working in unity? I had no luck, but I'm pretty new to shaders.
     
  26. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    clip_img.png That sounds very interesting, and it doesn't modify any geometry as well.

    This is a very clever idea and I haven't really thought that it could be made that way. This could be a very good fix when there are more clippling planes paralell or intersecting.
    But - at least with this code - not for the cases with intersecting meshes. I made a short test on other mesh setup, without bothering to adjust the bounding box properly.

    Clipped (+capped) meshes on the left, - originals before exporting to .dae on the right.
     
    Last edited: Aug 1, 2016
  27. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    Finally I made an attempt and managed get that idea into Unity
    hatch.jpg
    WebGL demo link
     
  28. StaffanEk

    StaffanEk

    Joined:
    Jul 13, 2012
    Posts:
    380
    That is fantastic. Did you get the stencils to work in deferred rendering?
     
  29. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    Yes, I did.
    It works in deferred in Editor, it works in deferred on Android, it works in deferred on standalone Windows. I suspect that on browser WebGL the camera doesn't actually switch mode from code.
    The screenshot is actually taken in forward mode, because you can see the fog from scene lighting - the scene lighting fog doesn't work in deferred.
    (In deferred fog can work as camera effect only - afaik)
     
  30. StaffanEk

    StaffanEk

    Joined:
    Jul 13, 2012
    Posts:
    380
    @tomekkie2
    Awesome.

    I have an archviz project that uses deferred rendering and I haven't been able to get a stenciled plane drawn.
     
    Last edited: Mar 1, 2017
  31. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    I am not a very experienced shader&rendering expert, but you can try the doc's examples here:
    https://docs.unity3d.com/Manual/SL-Stencil.html and try it yourself.
    Afaik there are some limitations in deferred.
    I was testing these examples in editor few days ago. Now I have just looked again and the rendering path doesn't seem to have any effect in the first three spheres example, while the second example with hole shader doesn't seem to work in the deffered mode.
    But stencils seems to work in my pre-update version of crossSection asset, which I am going to resubmit it in few days.
    My webgl and apk demos are updated, while the asset itself not yet, new features not yet included are marked with asterisk.
     
  32. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    I have just discovered an interesting postprocessing option for cross section shader.
    outline_post_effect2.jpg
    That is just the edge effect from here:
    https://www.assetstore.unity3d.com/en/#!/content/83913

    I have just modified the normal depth texture to limit the effect to work on backface boundaries only.
    I could be applied to any cross section shader.

    I wouldn't do that without the help of bgolus, who kept on explaining all my questions in this thread: https://forum.unity3d.com/threads/d...-relative-to-camera-viewing-direction.485204/
     
    Last edited: Jul 29, 2017
    tapawafo likes this.
  33. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    Still experimenting with tweaks on clipping shaders.
    Another cute effect from remaking the transparent shader. fading.jpg
     
    tapawafo likes this.
  34. peter_field

    peter_field

    Joined:
    Sep 4, 2013
    Posts:
    6
    Hi Tomekkie2, In this image you have used a sphere instead of a plane as the cross section. Is it easy to change the shape that is used to clip / cut away geo?

    Cheers,
    Peter
     

    Attached Files:

  35. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    As long as your geo could be given by equation - it should be no problem.
    Specifically if the geo could be approximated by ellipsoid.
    If the sphere could be expressed as the collection of points, where
    dot((wPos - centre)/R,(wPos - centre)/R) = 1

    For the ellipsoid we would have more radiuses, assume Rx, Ry, Rz.

    Instead (wPos - centre)/R we would need:

    float3((wPos - centre).x/Rx, (wPos - centre).y/Ry, (wPos - centre).z/Rz)
     
    Last edited: Aug 18, 2017