Search Unity

Mesh cutting but not just straight!

Discussion in 'Scripting' started by Gerlich, Oct 12, 2019.

  1. Gerlich

    Gerlich

    Joined:
    Nov 3, 2015
    Posts:
    37
    Hey all,

    so I am playing around a bit and I try to cut a 3d mesh on runtime but not only in a straight line, but also curved. For now a simple box would be all I would want to cut into.
    Does anybody have any hints / tutorials / websites on how to achieve this?

    For cutting in a straight line there are several assets / tutorials out there, but not a single one for complex curves etc.

    Thanks in advance!
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Dynamic mesh editing is an advanced topic. The general idea here is to make one mesh into two meshes. For that you create a couple new vertices and connect them using new triangles. You can build on this idea, by simply inserting a new vertex in the middle (on both sides obviously) to create a cut-line that's made up of 3 vertices instead of 2.
    To have any custom cut-shape, you need to sample the motion when cutting, and create new vertices at those locations, then recreate the two meshes by connecting the new and old vertices for both new shapes with triangles.
    However, since this implies that the user is in control of the cut, and also the speed of the cut, you need to either cover the previous cut path visually before separating the mesh, or do something along the lines of the following:

    Another way to do this would be to edit the mesh at runtime, so for example, if you cut with a 1cm large laser, then you could also just "indent" the mesh, effectively creating a 1cm large cut into some object. Of course, since this would be in realtime, it would need to be done very efficiently, so it adds some additional complexity. It has the benefit that you dont need to actually cut something in half, but can instead just deform the mesh as if you started cutting it and were interrupted in the process. The disadvantage is that you need to check if the mesh is still in one piece, or is now two meshes, unless you dont need them to be physically simulated after being cut apart.

    In the very slight case that you just need it as a visual effect that the player never really interacts with, then you could create very convincing visuals using a custom raymarching shader with boolean operations to create new shapes. However, this is also another layer of complexity (and GPU performance) and has the disadvantage that it's just visuals (like literally just pixels), so you cant interact with it, unless you manually implement it that way.

    None of these are particularly easy, especially once you consider performance.
     
    Bunny83 and MNNoxMortem like this.
  3. MNNoxMortem

    MNNoxMortem

    Joined:
    Sep 11, 2016
    Posts:
    723
    Do not underestimate the complexity of mesh cutting... I have yet to see a fast and feature rich AND numerically stable implementation/library. (If someone finds one - paid or free, please message me)

    To add to what Yoreki explained already very nicely: If you are just interested in playing around start with something simple and cut against an axis aligned plane, then a rotated plane, then a sphere, then a capsule. Those are the basic geometric types which allow "easy" projection/surface intersection or inside/outside checks.
     
  4. Gerlich

    Gerlich

    Joined:
    Nov 3, 2015
    Posts:
    37
    Hey guys. Thanks for the heads up. So you just confirmed, what I already feared: its by no means an easy task and most likely not very performance friendly...as I am developing mainly for mobile this is key.
    As I said, "just" cutting in a straight line won´t "cut" (lol) it here.

    Thanks for your time anyway!
     
  5. MNNoxMortem

    MNNoxMortem

    Joined:
    Sep 11, 2016
    Posts:
    723
    You can play around with the CSG tools in the Asset Store and the csg implementation by evans, but I already did try all of them and thery are incredibly numerically instable or at worst not production ready at all.

    The shared numerical stability problem is based on the fact that almost all implementations are based on the same algorithm evans used in his implementation, which is not numerically stable.

    However, doing simple cuts can be achieved with those pretty easily, but the meshes degenerate rapidly and doing something non-trivial quickly summons some evil demons.
     
  6. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    What is the usecase for your mobile application? Whether or not it's feasible also depends on how big of a feature it is. If you want to cut up enemies or meshes, but there is also a lot of other things going to, then it's probably to much to handle. However, if it's "just" cutting a hole in a wall, while nothing else happens, then it may be possible from a performance perspective. You may also get away with faking the visuals, or just using a spherical hole or something along those lines, which is why i asked for the actual usecase.
    There is also a lot you can do to improve performance, like for example using DOTS, however it does not exactly decrease complexity, and working with meshes still is a pain (even more so actually), since you cant really access most of the Unity API from anywhere but the main thread, and DOTS is basically multithreaded by nature.
     
  7. ninadpradhan

    ninadpradhan

    Joined:
    May 9, 2018
    Posts:
    9
    Anyone found a solution yet on this ? mesh cutting but not just straight but in any curves, all assets on unity asset store only cut straight