The Slice
script allows meshes to be sliced into two pieces during runtime. Fragments can resliced into smaller fragments if desired.
An object should have the following components added to it. The first three are required components for the script (i.e. they are added automatically) while the Collider is not a required component since you are allowed to use any type of collider.
MeshFilter
MeshRenderer
RigidBody
Collider
(any type)
- Enable Reslicing: Set to true to enable reslicing of fragments.
- Max Reslice Count: The maximum number of times a fragment can be resliced.
- Detect Floating Fragments: If enable, a pass will be made on the resulting fragments after the slicing algorithm has executed to determine if any of the fragments contain unconnected geometry. This can occur when slicing non-convex meshes. Since the geometry of each fragment must be searched to identify these disconnected sets of vertices/faces, this option will significantly reduce the performance of the slicing.
- Inside Material: The material to use for the newly sliced faces.
- Texture Scale: Scale factor applied to the UV coordinates for the sliced faces.
- Texture Offset: Constant offset applied to the UV coordinates for the sliced faces.
- Invoke Callbacks: If enabled, slicing fragments will also trigger the callback functions. This option can be useful if you only want to trigger an action the first time an object is sliced but not when the fragments are resliced (in this case, you would set this option to false).
- OnCompleted(): This callback is triggered when the slicing has been completed. Use this to play a sound, turn on a light or any other in-game logic you require.
Unlike Fracture
and Prefracture
, Slice
requires scripting since you must pass in the normal and origin of the slice plane during runtime.
If obj
is the object to be sliced, execute the following code to slice obj
into two fragments. sliceNormal
and sliceOrigin
are the normal and origin of the slice plane in world coordinates, respectively.
var slicer = obj.GetComponent<Slice>();
var sliceNormal = Vector3.up;
var sliceOrigin = obj.transform.position;
slicer.ComputeSlice(sliceNormal, sliceOrigin);