MD Package Technical & APi Documentation
MDM_MeshDamage :: MD_ModifierBase
Namespace: MDPackage.Modifiers
Damage a mesh by external forces or rigibodies - CPU based solution with multithreading support.
Read more here (Online documentation slide)
Properties
// IMDThreading implementation

// Use multithreading feature for this modifier?
public bool ThreadUseMultithreading { get; set; }
// Is multithreading in Unity-editor supported?
public bool ThreadEditorThreadSupported { get; }
// Is a task finished for this thread?
public bool ThreadIsDone { get; }
// Sleep time for a thread in ms
public int ThreadSleep { get; set; }

// Mesh Damage

// If enabled, the damage radius will be automatically received from incoming collision object in OnCollisionEnter
public bool AutoDetectRadiusSize { get; set; }
// Custom impact radius if the property above is disabled
public float ImpactRadius { get; set; }
public float ImpactRadiusMultiplier { get; set; }
// Expotential value for impact radius
public float ImpactRadiusSoftness { get; set; }

// If enabled, the impact force will be calculated from the incoming rigidbody collision
public bool DetectForceImpact { get; set; }
public float ImpactForceAmount { get; set; }
public float ImpactForceMultiplier { get; set; }
public float ImpactForceDetectionLimit { get; set; }

// If enabled, the mesh will continue in its damaged state and won't take its initial state into account
public bool ContinuousDamage { get; set; }

// Conditions

public bool CollisionWithSpecificTag { get; set; }
public string CollideWithTag { get; set; }
Events
// Triggered whether the mesh has been damaged. Params include the world space position of the impact
public event System.Action<Vector3> OnMeshDamaged;
public UnityEvent OnMeshDamagedUnityEvent;
Public Methods
// Modify current mesh by the point (world space), radius, force and direction
public void MeshDamage_ModifyMesh(Vector3 worldSpacePoint, float radius, float force, Vector3 direction)
// Refresh and sync current mesh with the modified vertices
public void MeshDamage_RefreshMesh()
// Refresh vertices & bake mesh with the current vertice positions
public void MeshDamage_BakeVertices()
// Restore deformed mesh by the specified restoration speed. Involve deltaTime for frame independency
public void MeshDamage_RestoreMesh(float restorationSpeed = 0.1f)
Examples
The following paragraphs contain Mesh Damage modifier component used in a practical example - damage specific mesh with raycast at runtime.
using UnityEngine;
using MDPackage.Modifiers;

[RequireComponent(typeof(MeshFilter))]
public class SampleScript : MonoBehaviour
{
// Cache main camera in the inspector!
 [SerializeField] private Camera mainCam;
 [Space]
// Edit damage parameters
 [SerializeField] private float damageRadius = 0.25f;
 [SerializeField] private float damageForce = 0.5f;
 [SerializeField] private bool continuousEffect = true;

private MDM_MeshDamage damageable;

private void Start()
 {
  // Add/Create a Mesh Damage modifier to this object right after start
  damageable = MD_ModifierBase.CreateModifier<MDM_MeshDamage>(gameObject, MD_ModifierBase.MeshReferenceType.CreateNewReference);
  // Add MeshCollider for raycast detection
  damageable.gameObject.AddComponent<MDPackage.MD_MeshColliderRefresher>();
 }

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

  // Sample extension - if R is held, restore mesh to its original state by the specific restoration speed
  if (Input.GetKey(KeyCode.R))
   damageable.MeshDamage_RestoreMesh(4 * Time.deltaTime);
   
  damageable.ContinuousDamage = continuousEffect;

  if (Input.GetMouseButtonDown(0))
  {
   // Create raycast from camera to the cursor position
   Ray r = mainCam.ScreenPointToRay(Input.mousePosition);
   bool hit = Physics.Raycast(r, out RaycastHit h) && h.transform == damageable.transform;
   // Checking for potential hit - if the hit collider equals the damageable, damage the mesh
   if (hit) // Modify damageable mesh by the hit point, damage radius, damage force, direction and continuous effect
    damageable.MeshDamage_ModifyMesh(h.point, Mathf.Abs(damageRadius), -Mathf.Abs(damageForce), h.normal);
  }
 }
}