API reference

DestructibleMeshComponent Class

Extends MonoBehaviour
The DestructibleMeshComponent handles the segmentation and manipulation of a mesh to create a segmented version of the original.
This class is automatically instantiated by DestructibleGlobalMeshSpawner on each destructible mesh game object. Used mainly to build destructible environments in conjunction with a global mesh. It uses asynchronous tasks to perform the computations for mesh segmentation based on specified parameters and reserved areas.
This example shows how to add mesh colliders to the mesh segments:
 private void OnDestructibleMeshCreated(DestructibleMeshComponent destructibleMeshComponent)
{
    _destructibleMeshComponent = destructibleMeshComponent;
    destructibleMeshComponent.GetDestructibleMeshSegments(_globalMeshSegments);
    foreach (var globalMeshSegment in _globalMeshSegments)
    {
        globalMeshSegment.AddComponent<MeshCollider>();
    }
}

Fields

OnDestructibleMeshCreated : UnityEvent< DestructibleMeshComponent >
Event triggered when a destructible mesh is successfully created.
Is automatically set to the DestructibleGlobalMeshSpawner.OnDestructibleMeshCreated event
Signature
UnityEvent<DestructibleMeshComponent> OnDestructibleMeshCreated
OnSegmentationCompleted : Func< MeshSegmentationResult, MeshSegmentationResult >
Function delegate that processes the segmentation result.
It can be used to modify the segmentation results before they are instantiated.
Signature
Func<MeshSegmentationResult, MeshSegmentationResult> OnSegmentationCompleted

Properties

GlobalMeshMaterial : Material
[Get][Set]
Gets or sets the material used for the mesh.
This material is applied to the mesh segments that are created during the segmentation process.
Signature
Material GlobalMeshMaterial
ReservedBottom : float
[Get][Set]
Gets or sets the reserved space at the bottom of the mesh.
TThis space is not included in the destructible area, allowing for controlled segmentation.
Signature
float ReservedBottom
ReservedSegment : GameObject
[Get]
Gets the game object associated to the reserved segment defined as the portion of the mesh that should be indestructible.
Can be null if no segment has been reserved.
Signature
GameObject ReservedSegment
ReservedTop : float
[Get][Set]
Gets or sets the reserved space at the top of the mesh.
This space is not included in the destructible area, allowing for controlled segmentation.
Signature
float ReservedTop

Methods

DebugDestructibleMeshComponent ()
Debugging method to color each segment with a unique color.
Signature
void DebugDestructibleMeshComponent()
Returns
void
DestroySegment ( segment )
Cleans up created mesh segments when the component is destroyed or reset.
Signature
void DestroySegment(GameObject segment)
Parameters
segment: GameObject  The game object associated to the segment to be destroyed.
Returns
void
GetDestructibleMeshSegments ( segments )
Populates an array with GameObjects representing the destructible mesh segments.
The array should be provided with enough space to hold all segments. see GetDestructibleMeshSegmentsCount.
Signature
void GetDestructibleMeshSegments(GameObject[] segments)
Parameters
segments: GameObject[]  The array to populate with mesh segment GameObjects.
Returns
void
GetDestructibleMeshSegments< T > ( segments )
Populates a provided container with GameObjects representing the destructible mesh segments.
The container will be cleared before being populated.
Signature
void GetDestructibleMeshSegments< T >(T segments)
Parameters
segments: T  The list to populate with mesh segment GameObjects.
Returns
void
GetDestructibleMeshSegmentsCount ()
Returns the count of destructible mesh segments currently managed by this component.
Signature
int GetDestructibleMeshSegmentsCount()
Returns
int  The number of child GameObjects representing mesh segments.
SegmentMesh ( meshPositions , meshIndices , segmentationPoints )
Initiates the mesh segmentation process asynchronously based on the provided mesh positions, indices, and segmentation points.
It uses native functions to perform the segmentation and updates the mesh accordingly.
Signature
unsafe void SegmentMesh(Vector3[] meshPositions, uint[] meshIndices, Vector3[] segmentationPoints)
Parameters
meshPositions: Vector3[]  Array of mesh vertex positions.
meshIndices: uint[]  Array of mesh indices.
segmentationPoints: Vector3[]  Array of points used for segmenting the mesh.
Returns
unsafe void

Inner Structs

MeshSegment Struct

Represents a single mesh segment with position data, indices for mesh topology, UVs for texturing, tangents for normal mapping and colors.
This struct is essential for defining the geometric and visual properties of a mesh segment.The DestructibleGlobalMeshSpawner uses this struct to create and manage mesh segments.

Fields

colors : Color[]
The colors applied to each vertex of the mesh segment.
Vertex colors can be used for a variety of effects, including vertex-based shading and color blending.
Signature
Color [] colors
indices : int[]
The indices that define the mesh topology.
\ Use the MeshSegmentationResult struct to create and manage mesh segments.
Signature
int [] indices
positions : Vector3[]
The vertex positions of the mesh segment.
Use the MeshSegmentationResult struct to create and manage mesh segments.
Signature
Vector3 [] positions
tangents : Vector4[]
The tangents used for normal mapping.
Tangents are necessary for advanced lighting effects, such as bump mapping.
Signature
Vector4 [] tangents
uv : Vector2[]
The UV coordinates for texturing the mesh segment.
UVs map the 2D texture to the 3D surface of the mesh.
Signature
Vector2 [] uv

MeshSegmentationResult Struct

Contains the results of a mesh segmentation operation, including a list of mesh segments and a specially reserved segment.
The reserved segment is a portion of the mesh that will be kept as one segment and should be indestructible. The DestructibleGlobalMeshSpawner.OnSegmentationCompleted event is triggered when this data is available and it can be used to modify the segmentation results before they are instantiated by accessing the MeshSegmentationResult.segments and MeshSegmentationResult.reservedSegment properties.
This example shows how to modify the segmentation results before they are instantiated:
 private static DestructibleMeshComponent.MeshSegmentationResult ModifySegmentationResult(
    DestructibleMeshComponent.MeshSegmentationResult meshSegmentationResult)
{
    var newSegments = new List<DestructibleMeshComponent.MeshSegment>();
    foreach (var segment in meshSegmentationResult.segments)
    {
        var newSegment = ModifyMeshSegment(segment);
        newSegments.Add(newSegment);
    }
    var newReservedSegment = ModifyMeshSegment(meshSegmentationResult.reservedSegment);
    return new DestructibleMeshComponent.MeshSegmentationResult()
    {
        segments = newSegments,
        reservedSegment = newReservedSegment
    };
}

Fields

reservedSegment : MeshSegment
A specially reserved segment that remains indestructible.
This segment is kept intact and is not subject to the usual segmentation process.
Signature
MeshSegment reservedSegment
segments : List< MeshSegment >
A list of resulting from the segmentation operation.
Each MeshSegment represents a distinct part of the original mesh.
Signature
List<MeshSegment> segments