Search Unity

How to cut a desired shape out of a mesh?

Discussion in 'Scripting' started by h00man, Aug 31, 2019.

  1. h00man

    h00man

    Joined:
    Apr 17, 2017
    Posts:
    54
    hey guys ,
    i know for the fact that it's really difficult to achieve , so i was if there is any way to cut a shape out a simple mesh at run time.
    any asset on asset store any plugin or any method would be really great.
    and here is what i like to do in unity.
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Never did this myself, but here is how i'd approach the problem:
    Similar to shooting a bullet, you'll want to track where the player (or rather gun) is looking when pressing the mouse button. When the raycast collides with a mesh, save the hit position. If it's not just a sprite (like it assumably is in the video), then you will need to save both, the entry and exit hit location of the raycast on the mesh.

    Doing this for some period of time (like cutting a hole in a fence), you end up with a ton of pairs of points on the target mesh. Now we need to detect when the process of cutting is finished. Depends a bit on what you want, but as a rule of thumb, we completed some cuttable shape when the new point is reasonably close to any other (or only the first) point we saved so far for this object. You'd potentially want to make sure the area has some minimum size too.

    To save a bit of processing later on, it would be a good idea to remove elements from this list, such that a minimal amount of pairs remains for the "cut resolution" you need.

    Now comes the actually hard part. Creating two meshes or sprites from one. For sprites this should be comparably easy, as you could recreate the shape defined by your points on a texture and use that as a sort of mask to create two new textures from the cut one. Then (replacing and) placing the two textures in the scene like they were just cut, making one of them physically simulated, should be comparably easy.

    If we are talking about a real 3d mesh however, this gets a lot worse. Imagine we want to cut a cylinder out of a cube.
    We need to split the old mesh (cube) into two new meshes: a cycliner-like shape, defined by out points, aswell as a cube from which we "subtracted" the previously mentioned shape.
    Creating the cut-out shape only should be comparably easy. We only have to connect all entry-points with triangles, such that it created a closed surface, do the same for the exit-points, then connect these two shapes along the cut edge (which should be a straight line in all cases). For out cube example this works, however in reality the cut mesh may have any kind of bumps or shapes, which you'd want to also have on the cut-out shape. So in a postprocessing step, you'd need to figure out (at least roughly) which vertices and triangles made up the mesh inside the shapes defined by your entry-point shape and exit-point shape, and then apply that to your newly created mesh.
    Creating the cube with a cyliner-like hole is a lot harder, if i'm not missing something. I'm not entirely sure what would be the best way to approach this problem. It may even be better to approach this problem first, as it may result in a comparably free solution for the cut-out shape as well. I believe i'd approach it along those lines:

    Create an algorithm that lets you insert a new vertex into any given mesh, that is then correctly connected to others with triangles. You could approach this by figuring out which triangle(s) the point ended up on, then removing that triangle and inserting multiple new ones. If you have this you can:
    Insert a new vertice for each of your saved points into the mesh. Now copy the mesh once and, on one of the copies each, remove all outer triangles, or all inner triangles. This should leave you with a shape that is a) a cube with a hole in it and b) two surfaces. For both of these meshes you will now need to insert new triangles to fix the holes along the cut, which should be the easier part of the whole procedure.

    I believe there are some assets which allow cutting through objects. However, none of these are free (to my knowledge) and i'd say it's a lot easier to cut an object in two than to cut a specific shape out of an object.
    If i had a bit more time right now this may actually be a fun project to work on, because for some sadistic reason i enjoy working on meshes with code haha.

    Hope this helps!
     
    EdGunther and h00man like this.
  3. h00man

    h00man

    Joined:
    Apr 17, 2017
    Posts:
    54
    alright thanks a lot man.. since i was looking for a way all day and didn't find anything useful, i never thought someone could ever come up with a solution.and btw i think in the fence object in the video is not a 3D mesh i think it is some kinda sprite or 2D texture .so since i'm a noob in coding it is better stick to a plugin in the asset store
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    It's most certainly not a beginner-friendly topic. The 2D version seems very doable (comparably) if you are interrested in doing something like that tho. Maybe have a look at making (coding!) some simple shaders first to get a feel for how to work with textures and applying masks and so on.
    I personally believe you can achieve anything if it's something you really want to. Just remember to take small, simple steps as learning experiences and improve your knowledge that way. Dont directly start by tackling a huge problem, try solving a similar, but way simpler problem first to get the general idea.
    I doubt anybody would look at a problem like this and be "yeah sure, it's easy just do this and that" (unless they already implemented something like that before), but you can eventually reach a point where you are able to analyse a problem, divide it into smaller problems you feel more comfortable approaching, and then put them back together to get what you want. Divide and conquer! The rest just happens as you go.. maybe your initial approach sucked, but then you gained some experience again and can approach do it better in the future :)
     
  5. h00man

    h00man

    Joined:
    Apr 17, 2017
    Posts:
    54
    yeah i remember i took the same approach when i wanted to make a physical bullet to pass through Colliders.but now unfortunately i know nothing about vertices and also creating mesh at run time.oh and btw i did some search on the asset store and didn't find anything regarding cutting through mesh
     
  6. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    There are a couple paid assets for slicing though a mesh, as in, with a sword or something. Did not find anything for cutting as you want it either. If it doesnt exist yet maybe i'll try creating something like that in the future when i have more time.
     
  7. h00man

    h00man

    Joined:
    Apr 17, 2017
    Posts:
    54
    i already tried slicing assets but the problem with all of them is that you have to start the process of cutting the mesh from outside the boundaries of the mesh.
    i wish i had the skill to create it .something that doesn't exist in the asset store
     
  8. mbaske

    mbaske

    Joined:
    Dec 31, 2017
    Posts:
    473
    Take a look at constructive solid geometry:
    https://en.wikipedia.org/wiki/Constructive_solid_geometry
    https://assetstore.unity.com/packages/tools/modeling/csg-82197
    Not sure how well this works for non-primitive shapes though...

    An alternative could be voxelizing the target object. Then group voxels depending on wether they're inside the cutter shape and reconstruct new meshes from your voxel groups.
     
  9. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    I did think about that as well, but had the same concerns you mentioned. I'm pretty sure CSG makes rather heavy use of raytracing, which, to my knowledge, is pretty expensive on triangles. So it may not be a good approach for anything more complex than the cylinder+cube example. However, as i did not properly investigate the idea it may still be a good approach to the problem - at least for some applications.

    Voxelizing the object may be another option, but comes with potential drawbacks. The problem i saw with this approach is the resolution. Depending on how important the overall cut-shape is, we'd need a resolution of voxels potentially as small as the "laser" cutting the object. This can result in very expensive and thus slow updates to the mesh at runtime, especially for larger objects. If the resolution of the cut shape is not of high importance this is definitely a way of doing it, but then i'd assume it's more about general "destructability" than cutting shapes.
    And it may also be problematic to voxelize a mesh at runtime without the viewer noticing the difference visually.

    But i agree, both of these (and probably many more) approaches are worth mentioning, at the very least for specific applications.
     
  10. h00man

    h00man

    Joined:
    Apr 17, 2017
    Posts:
    54
    someone actually has done something similar but it's too clumsy.
     
  11. Finijumper

    Finijumper

    Joined:
    Jul 12, 2016
    Posts:
    79
    The problem I have with CSG is that I want to be able to draw a line in the mesh and the method only works well with 1 or 2 holes. You can see what I tried:


    I'd like to be able to achieve this:
     
  12. Henry_Sun

    Henry_Sun

    Joined:
    Jun 1, 2019
    Posts:
    24
    Sprites or plane meshes are enough to make the look of the cut fence.
    So we might only need a roughly shaping mesh for collider.
    Here's what I came up with.
    cutmesh.png