├── MeshTriangulator.cs
└── README.md
/MeshTriangulator.cs:
--------------------------------------------------------------------------------
1 | using UnityEngine;
2 | using System.Text;
3 | using System.Collections;
4 |
5 | ///
6 | /// Static class which emulates a mesh explosion into triangles
7 | ///
8 | /// @author Alex M.A. and joshcamas
9 | ///
10 | public class MeshTriangulator
11 | {
12 |
13 | ///
14 | /// Splits the mesh of the given transform into triangles deactivating
15 | /// its gameobject and creating multiple triangles simulating and explosion
16 | ///
17 | ///
18 | /// The force applied to the generated triangles
19 | /// How long the generated triangles will live
20 | public static void Triangulate(Transform transform, float explosionForce = 1500f, float destroySeconds = 5f)
21 | {
22 | //getting info about the mesh of the given transform
23 | MeshFilter meshFilter = transform.GetComponent();
24 | MeshRenderer meshRenderer = transform.GetComponent();
25 |
26 | Triangulate(transform, meshFilter, meshRenderer, explosionForce, destroySeconds);
27 | }
28 |
29 | ///
30 | /// Splits the mesh of the given transform into triangles deactivating
31 | /// its gameobject and creating multiple triangles simulating and explosion
32 | ///
33 | ///
34 | ///
35 | ///
36 | ///
37 | ///
38 | public static void Triangulate(Transform meshTransform, MeshFilter meshFilter, MeshRenderer meshRenderer,float explosionForce = 1500f, float destroySeconds = 5f)
39 | {
40 | Mesh mesh = meshFilter.sharedMesh;
41 | Vector3[] verts = mesh.vertices;
42 | Vector3[] normals = mesh.normals;
43 | Vector2[] uvs = mesh.uv;
44 |
45 | //deactivating the gameobject which is going to be splitted
46 | meshTransform.gameObject.SetActive(false);
47 |
48 | //looping the submeshes of the main mesh
49 | for (int intSubmeshIndex = 0, smCount = mesh.subMeshCount; intSubmeshIndex < smCount; intSubmeshIndex++)
50 | {
51 | //getting the triangles of the submesh
52 | int[] triangles = mesh.GetTriangles(intSubmeshIndex);
53 | //getting the number of triangles of the submesh
54 | int numberOfTriangles = triangles.Length;
55 | //looping the triangles and creating the corresponding triangles
56 | for (int i = 0; i < numberOfTriangles; i += 3)
57 | {
58 | //init the triangles vars
59 | Vector3[] triVertexs = new Vector3[3];
60 | Vector3[] triNormals = new Vector3[3];
61 | Vector2[] triUvs = new Vector2[3];
62 |
63 | //getting the vertexs, uvs and normals from the main mesh
64 | for (int n = 0; n < 3; n++)
65 | {
66 | int index = triangles[i + n];
67 | triVertexs[n] = verts[index];
68 |
69 | //Sometimes UV's don't exit
70 | if(uvs.Length > n)
71 | triUvs[n] = uvs[index];
72 |
73 | triNormals[n] = normals[index];
74 | }
75 |
76 | //Configuring the new triangle with the info of the main mesh
77 | Mesh triMesh = new Mesh();
78 | triMesh.vertices = triVertexs;
79 | triMesh.normals = triNormals;
80 | triMesh.uv = triUvs;
81 | triMesh.triangles = new int[] { 0, 1, 2, 2, 1, 0 };
82 |
83 | //Creating the new triangle game object
84 | GameObject triangleObj = new GameObject(new StringBuilder().Append("Tiangle").Append((i / 3) + intSubmeshIndex).ToString());
85 | //positioning the triangle at the same position of the original mesh
86 | triangleObj.transform.position = meshTransform.position;
87 | triangleObj.transform.rotation = meshTransform.rotation;
88 | triangleObj.transform.localScale = meshTransform.localScale;
89 | //adding meshrenderer and the material of the main mesh
90 | triangleObj.AddComponent().material = meshRenderer.sharedMaterials[intSubmeshIndex];
91 | //adding the triange mesh
92 | triangleObj.AddComponent().sharedMesh = triMesh;
93 | //adding collider
94 | triangleObj.AddComponent();
95 | //adding rigidbody and a explosion
96 | triangleObj.AddComponent().AddExplosionForce(Random.Range(50f, explosionForce), meshTransform.position, 30);
97 |
98 | //to make clean the scene: destroy all the triangles when passed the destroySeconds
99 | Object.Destroy(triangleObj, destroySeconds);
100 | }
101 | }
102 |
103 | }
104 |
105 |
106 | }
107 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # unity-mesh-triangulator
2 | A modified version of [alexmuab's unity-mesh-triangulator](https://github.com/alexmuab/unity-mesh-triangulator).
3 |
4 | 
5 |
6 | ### Usage
7 | ```c#
8 | //Simplest way: finds attached MeshFilter and MeshRenderer
9 | MeshTriangulator.Triangulate(transform);
10 |
11 | //More controlled way
12 | MeshTriangulator.Triangulate(transform,meshFilter,meshRenderer);
13 |
14 | //You can also specify the force and lifetime applied to the generated triangles
15 | MeshTriangulator.Triangulate(transform,100,3)
16 | MeshTriangulator.Triangulate(transform,meshFilter,meshRenderer,100,3)
17 | ```
18 |
19 | ## Todo
20 |
21 | * Add function to triangulate based on a Mesh and Materials instead of components
22 | * More options, such as controlling whether the triangles use collision
23 | * Adding a mode that doesn't use unity physics, possibly particles of some sort?
24 | * Possibly dropping this in favor of using a shader instead
25 |
26 |
--------------------------------------------------------------------------------