Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Explode mesh into triangles

Discussion in 'Shaders' started by Szulc, Feb 17, 2016.

  1. Szulc

    Szulc

    Joined:
    Aug 18, 2015
    Posts:
    12
    Hi,

    I found a script which explodes a mesh into triangles. The script however creates a lot of new game objects (one for each triangle) with rigidbodies, colliders, etc. and it kills performance on mobile devices.

    I wonder if similar effect can be achieved with shaders. If so, please point me into the right direction.

    The script looks more less like this:
    Code (CSharp):
    1. MeshFilter MF = GetComponent<MeshFilter>();
    2.         MeshRenderer MR = GetComponent<MeshRenderer>();
    3.         Mesh M = MF.mesh;
    4.         for (int submesh = 0; submesh < M.subMeshCount; submesh++)
    5.         {
    6.             int[] indices = M.GetTriangles(submesh);
    7.             for (int i = 0; i < indices.Length; i += 3)
    8.             {  
    9.  
    10. Vector3[] newVerts = new Vector3[3];
    11.                 Vector3[] newNormals = new Vector3[3];
    12.                 Vector2[] newUvs = new Vector2[3];
    13.                 for (int n = 0; n < 3; n++)
    14.                 {
    15.                     int index = indices[i + n];
    16.                     newVerts[n] = verts[index];
    17.                     newUvs[n] = uvs[index];
    18.                     newNormals[n] = normals[index];
    19.                 }
    20.                 Mesh mesh = new Mesh();
    21.                 mesh.vertices = newVerts;
    22.                 mesh.normals = newNormals;
    23.                 mesh.uv = newUvs;
    24.                
    25.                 mesh.triangles = new int[] { 0, 1, 2, 2, 1, 0 };
    26.  
    27.                 triangleMeshes.Add(mesh);
    28.  
    29.                 GameObject GO = new GameObject("Triangle " + (i / 3));
    30.                 GO.transform.position = transform.position;
    31.                 GO.transform.rotation = transform.rotation;
    32.                 GO.AddComponent<MeshRenderer>().material = MR.materials[submesh];
    33.                 GO.AddComponent<MeshFilter>().mesh = triangleMeshes[(submesh + (i/3))];
    34.                 GO.AddComponent<BoxCollider>();
    35.                 Rigidbody rb = GO.AddComponent<Rigidbody>();
    36.                 rb.mass = Random.Range(0.5f, 1.0f);
    37.                 rb.AddExplosionForce(10, touch, 30, 0, ForceMode.Impulse);
    38.                 Destroy(GO, 1+ Random.Range(0.0f, 1.0f));
    39.             }
    40.         }
     
    aksiwot likes this.
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,245
    Not if you want physics and collision, no. If you just want it to break apart and move along a basic mathematical curve, sure. You still have to break up the mesh into individual polygons with a script and encode their center position into vertex colors, or use a geometry shader which isn't great for mobile perf either.
     
  3. StevenGerrard

    StevenGerrard

    Joined:
    Jun 1, 2015
    Posts:
    97
    => You can use geometry shader to do this, and there is a book talk about it Graphics Shaders: Theory and Practice, the chapter 16.
    => More practiced way is split to individual polygons. If unity's build-in physics & collision system is too heavy, you may have to remove them and code your simple solution.
    => In fact, you don't really need split mesh into many many many game objects. You just need split mesh into individual triangles. Move all triangles in script and all triangles are contained into a single mesh.