Search Unity

Boolean operation on mesh

Discussion in 'Editor & General Support' started by Finijumper, Jan 13, 2020.

  1. Finijumper

    Finijumper

    Joined:
    Jul 12, 2016
    Posts:
    79
    Hi, I'm trying to achieve an effect like this, where a cylinder intersects a cube and it substracts its shape.

    https://i.gyazo.com/5a00cc348072e576368bb0de8efac7c2.mp4

    I tried using this port of CSG.js (https://github.com/karl-/pb_CSG) but it only seems to work okay with one intersection, not dragging your mouse, you can see my attempt here:

    https://i.gyazo.com/6ad91e32334f25e049cebcd0d50353e5.mp4

    It lags a lot and after various attempts the mesh gets destroyed. How would you approach achieving the effect in the beggining?
     

    Attached Files:

  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,632
    Yeah, you can already see in that green object how much extra geometry that punching these simple holes is generating. Doing what you suggest with CSG seems like it would end-up being ridiculous. Maybe you should try some shader-based solution instead (like a stencil buffer shader or something).
     
  3. Finijumper

    Finijumper

    Joined:
    Jul 12, 2016
    Posts:
    79
    The shape needs to have collision.
     
  4. look001

    look001

    Joined:
    Mar 23, 2017
    Posts:
    111
    CSG wont be an option if you want to run int. Besides current CSG implementations for unity are not robust enough for multiple operations.
    In your case i would probably generate an extruded circle of triangle pairs for each circle segment. To make the collision work you can point the normals inwards so objects wont fall out.
    It's a bit tricky to combine two cicles. Probably the easies way is to go through each circle segment of a new circle and shoot a raycast with the distance of the segments width to test for intersections. You can then determine the intersection point and intersecting triangles (raycastHit.triangleIndex). Using that you can generate the new mesh basically.
    Good luck!
     
    Last edited: Jan 15, 2020
  5. Finijumper

    Finijumper

    Joined:
    Jul 12, 2016
    Posts:
    79
    Thanks, I found this library that might help me achieve what I want. (http://www.angusj.com/delphi/clipper.php)
    I need to figure out how to convert the path that it returns to a mesh, do you have any idea?
     
  6. JustAnotherDude

    JustAnotherDude

    Joined:
    Oct 28, 2013
    Posts:
    279
  7. BannedImp

    BannedImp

    Joined:
    Jul 31, 2019
    Posts:
    3
    JustAnotherDude, Probuilder uses PB_CSG and as far as its boolean operations go, it would be the same (if not worse) result as OP posted.

    Don't believe me? Create a cube (using Probuilder) then a torus. Now, cut the torus out of one corner of the cube. Probuilder isn't a bad tool, just a TERRIBLE suggestion for boolean operations.