MD Package Technical & APi Documentation
MDM_MeshCut :: MD_ModifierBase
Namespace: MDPackage.Modifiers
Simple and straight-two-sided mesh cut solution with custom cut settings and events. Mesh cutter source written by hugoscurti (MIT License - Github).
Read more here (Online documentation slide)
Public Methods
// Default mesh cutting (the most preferred method) - call this method to process the cut feature with properly assigned cutter source
public void MeshCut_Cut()
// Process mesh cut with required cutter input
public void MeshCut_Cut(MDM_MeshCut_Cutter cutterInput)
// Process mesh cut with required point-location & normal vector. Related just for this mesh and its chunks
public void MeshCut_Cut(Vector3 position, Vector3 normal)
// Process mesh cut with required point-location, normal vector & available meshes to process. Available meshes can be any meshes that might be related to this cutting object
public void MeshCut_Cut(Vector3 position, Vector3 normal, List<(MeshFilter f, MeshCollider c)> availableMeshes)
// Refresh mesh collider to all created chunks (if possible)
public void MeshCut_RefreshMeshCollider()
Public Fields
public MDM_MeshCut_Cutter cutterSource;

public bool addRigidbodyAfterCut = true;
public float separationForce = 2.0f;
public float defaultMass = 1.0f;
public float angularDrag = 5.0f;
public float drag = 4.0f;
public bool addSeparationOffset = false;
public float separationOffset = 0.05f;

public bool addMeshColliderAfterCut = true;

public bool automaticallyCut = false;
[Range(0.05f, 10.0f)] public float automaticallyCutDelay = 0.5f;

public MeshCollider MyMeshCollider { get; }
public readonly List<(MeshFilter f, MeshCollider c)> createdChunks;

// Custom OnCut event - when mesh gets cut - list of transforms contain a created chunks from the cut
public event Action<List<Transform>> Event_GotCut;

// What will happen when the mesh got cut?
public UnityEvent UEvent_OnCut;
Examples
The following paragraphs contain Mesh Cut modifier used in a practical example - cut specific meshes with raycast at runtime.
using UnityEngine;
using MDPackage.Modifiers;

// Sample script for Mesh Cut modifier - cut a mesh with custom raycast - apply this script to any empty object and assign gameObjects you would like to cut
public class SampleScript : MonoBehaviour
{
// Cache main camera in the inspector!
public Camera mainCam;

// Assign single or multiple gameObjects with MeshFilter
public MeshFilter[] targetObjectsToCut;
private MDM_MeshCut[] targetMeshCutters;

private void Start()
 {
  if (targetObjectsToCut == null || targetObjectsToCut.Length == 0)
   return;
  targetMeshCutters = new MDM_MeshCut[targetObjectsToCut.Length];
  // Add/Create a Mesh Cut modifier to each object right after start
  for (int i = 0; i < targetObjectsToCut.Length; i++)
  {
   var obj = targetObjectsToCut[i];
   if (obj == null) continue;
   // Add a Mesh Cut modifier to all the assigned gameObjects with MeshFilter
   var cut = targetMeshCutters[i] = MD_ModifierBase.CreateModifier<MDM_MeshCut>(obj.gameObject, MD_ModifierBase.MeshReferenceType.CreateNewReference);
   // Add separation offset if desired
   cut.addSeparationOffset = true;
   cut.addMeshColliderAfterCut = true;
   // Refresh mesh collider for raycast detection
   cut.MeshCut_RefreshMeshCollider();
  }
 }

private void Update()
 {
  // Main camera must be initialized
  if (!mainCam)
   return;

  // Additional extensions for meshes
  // If Space is pressed, subdivide meshes if possible
  if (Input.GetKeyDown(KeyCode.Space))
   foreach (MDM_MeshCut mc in targetMeshCutters)
    mc.MDModifier_SubdivideMesh();
  // If S is pressed, smooth meshes
  if (Input.GetKeyDown(KeyCode.S))
   foreach (MDM_MeshCut mc in targetMeshCutters)
    mc.MDModifier_SmoothMesh();

  if (Input.GetMouseButtonDown(0))
  {
   // Create raycast from camera to the cursor position
   Ray r = mainCam.ScreenPointToRay(Input.mousePosition);
   // Checking for potential hit - if the hit collider equals any of the cutters, the hit mesh will be cut
   if (Physics.Raycast(r, out RaycastHit h))
   {
    foreach (MDM_MeshCut mc in targetMeshCutters)
    {
     // Params: Hit point and cutting normal direction
     mc.MeshCut_Cut(h.point, Vector3.right);
    }
   }
  }
 }
}