Mesh plane cutting algorithm

I looked at the code implementation of UKismetProceduralMeshLibrary::SliceProceduralMesh, and found that it is not as complicated as I imagined. As long as the relationship between meshes/triangles/vertices/edges is clear, and the problem is gradually decomposed, complex problems can be simplified into small problems one by one , and then each of them is broken.
The steps of the comments and code are sorted out and turned into human words:

  1. Convert the cutting plane from World to Local space
  2. For each Section(SubMesh), calculate the relationship between the bounding box and the cutting plane
    1. If it is on the back, move to the other half (newly generated Mesh)
    2. If on the front, leave it still
    3. If they intersect, create two empty Section1 and Section2
      1. For each vertex, calculate the distance to the cut surface
        1. If the vertex is in the front, add Index to Set1
        2. If the vertex is in the back, add Index to Set2
      2. for each triangle (three indices)
        1. If all three vertices are in Set1, add the triangle to Section1
        2. If all three vertices are in Set2, add the triangle to Section2
        3. If a triangle intersects a cut plane, one or two new triangles need to be created
          1. for each side of the triangle
            1. Add to SliceSet1 if the starting point is heads, otherwise add to SliceSet2
            2. If the end point is not on the same side as the start point
              1. Find the intersection of the edge and the plane, generate a new vertex
              2. Add the new vertices to the two Sections and update the bounding box
          2. Generate triangles for the vertices on both sides (no more than 4) and add them to the corresponding Section
          3. If there are two edges intersecting, then the two newly generated vertices will generate a new edge, record it
      3. If there is nothing in the Section, you can discard the Section
  3. Create section geometry from the newly generated edges recorded
    1. Project the edge of the 3D space onto the cutting surface to become the edge of the 2D space
    2. Generate a closed polygon from a set of 2D edges
    3. Triangulate polygons and generate UVs
    4. Generate two new Sections from the generated section triangles, and associate the material of the cutting surface
  4. Do a similar cutting operation for the collider to generate a new convex hull collider
  5. For the other half cut, generate a new MeshComponent

The function of the engine is very simple to use, see https://www.youtube.com/watch?v=mG55ZWv3ezw .
But after understanding the principle, you can realize the cutting of StaticMesh and SkeletalMesh by yourself, such as making arbitrary cutting effects in the whole scene, and the effect of amputation
etc. Tribute to Metal Gear Rising (although Havok is used -_-)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325446592&siteId=291194637