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
public void MeshCut_Cut()
public void MeshCut_Cut(MDM_MeshCut_Cutter cutterInput)
public void MeshCut_Cut(Vector3 position, Vector3 normal)
public void MeshCut_Cut(Vector3 position, Vector3 normal, List<(MeshFilter f, MeshCollider c)> availableMeshes)
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;
public event Action<List<Transform>> Event_GotCut;
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;
public class SampleScript : MonoBehaviour
{
public Camera mainCam;
public MeshFilter[] targetObjectsToCut;
private MDM_MeshCut[] targetMeshCutters;
private void Start()
{
if (targetObjectsToCut == null || targetObjectsToCut.Length == 0)
return;
targetMeshCutters = new MDM_MeshCut[targetObjectsToCut.Length];
for (int i = 0; i < targetObjectsToCut.Length; i++)
{
var obj = targetObjectsToCut[i];
if (obj == null) continue;
var cut = targetMeshCutters[i] = MD_ModifierBase.CreateModifier<MDM_MeshCut>(obj.gameObject, MD_ModifierBase.MeshReferenceType.CreateNewReference);
cut.addSeparationOffset = true;
cut.addMeshColliderAfterCut = true;
cut.MeshCut_RefreshMeshCollider();
}
}
private void Update()
{
if (!mainCam)
return;
if (Input.GetKeyDown(KeyCode.Space))
foreach (MDM_MeshCut mc in targetMeshCutters)
mc.MDModifier_SubdivideMesh();
if (Input.GetKeyDown(KeyCode.S))
foreach (MDM_MeshCut mc in targetMeshCutters)
mc.MDModifier_SmoothMesh();
if (Input.GetMouseButtonDown(0))
{
Ray r = mainCam.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(r, out RaycastHit h))
{
foreach (MDM_MeshCut mc in targetMeshCutters)
{
mc.MeshCut_Cut(h.point, Vector3.right);
}
}
}
}
}