Convert mesh to triangles. (js)

Hi guys.
Can anyone help?
I try convert mesh to triangles and next triangle is new gameobject.
But i think write something wrong.
Here is my script.

var vertices : Vector3[];
var triangles : int[];
var messh : Mesh;

function Start() {
	vertices = messh.vertices;
	triangles = messh.triangles;
	var delatjraz = (vertices.length + 1 / 4);
	var gpa = new GameObject("m");
	for (var vi = 0; vi < delatjraz; vi++) {
		var Me_sh = new Mesh();
		var vertices1 : Vector3[] = new Vector3[4];
		for (var vi2 = 0; vi2 < vertices1.Length; vi2++) {
			vertices1[vi2] = vertices[vi*4 + vi2];
		}
		Me_sh.vertices = vertices1;
		Me_sh.triangles = [0,1,2,2,3,0];
		Me_sh.RecalculateBounds();
		Me_sh.RecalculateNormals();
		var g = new GameObject("mp"+vi);
		g.transform.parent = gpa.transform;
		var g1 = g.AddComponent(MeshFilter);
		g1.sharedMesh = Me_sh;
		var r1 = g.AddComponent(MeshRenderer);
		r1.material = new Material(Shader.Find("Diffuse"));
	}
}

function Update() {
	messh.vertices = vertices;
}

I’m still not sure what exactly you want to do, but here’s a script i’ve just written which splits a mesh into seperate triangles. The script created two triangles per triangle, one front and one back face. The back face has it’s normals flipped (since it uses the same vertices) so it appears black.

I’ve written it in “my” language C#, but also converted it to UnityScript.

// C#
// SplitMeshIntoTriangles.cs
using UnityEngine;
using System.Collections;

public class SplitMeshIntoTriangles : MonoBehaviour
{
	IEnumerator SplitMesh ()
	{
        MeshFilter MF = GetComponent<MeshFilter>();
        MeshRenderer MR = GetComponent<MeshRenderer>();
        Mesh M = MF.mesh;
        Vector3[] verts = M.vertices;
        Vector3[] normals = M.normals;
        Vector2[] uvs = M.uv;
        for (int submesh = 0; submesh < M.subMeshCount; submesh++)
        {
            int[] indices = M.GetTriangles(submesh);
            for (int i = 0; i < indices.Length; i += 3)
            {
                Vector3[] newVerts = new Vector3[3];
                Vector3[] newNormals = new Vector3[3];
                Vector2[] newUvs = new Vector2[3];
                for (int n = 0; n < 3; n++)
                {
                    int index = indices[i + n];
                    newVerts[n] = verts[index];
                    newUvs[n] = uvs[index];
                    newNormals[n] = normals[index];
                }
                Mesh mesh = new Mesh();
                mesh.vertices = newVerts;
                mesh.normals = newNormals;
                mesh.uv = newUvs;
                
                mesh.triangles = new int[] { 0, 1, 2, 2, 1, 0 };
                
                GameObject GO = new GameObject("Triangle " + (i / 3));
                GO.transform.position = transform.position;
                GO.transform.rotation = transform.rotation;
                GO.AddComponent<MeshRenderer>().material = MR.materials[submesh];
                GO.AddComponent<MeshFilter>().mesh = mesh;
                GO.AddComponent<BoxCollider>();
                GO.AddComponent<Rigidbody>().AddExplosionForce(100, transform.position, 30);
                
                Destroy(GO, 5 + Random.Range(0.0f, 5.0f));
            }
        }
        MR.enabled = false;
        
        Time.timeScale = 0.2f;
        yield return new WaitForSeconds(0.8f);
        Time.timeScale = 1.0f;
        Destroy(gameObject);
	}
    void OnMouseDown()
    {
        StartCoroutine(SplitMesh());
    }
}

Here’s the UnityScript version:

// UnityScript
// SplitMeshIntoTriangles.js
	function SplitMesh ()
	{
        var MF = GetComponent<MeshFilter>();
        var MR = GetComponent<MeshRenderer>();
        var M = MF.mesh;
        var verts = M.vertices;
        var normals = M.normals;
        var uvs = M.uv;
        for (var submesh = 0; submesh < M.subMeshCount; submesh++)
        {
            var indices = M.GetTriangles(submesh);
            for (var i = 0; i < indices.Length; i += 3)
            {
                var newVerts = new Vector3[3];
                var newNormals = new Vector3[3];
                var newUvs = new Vector2[3];
                for (var n = 0; n < 3; n++)
                {
                    var index = indices[i + n];
                    newVerts[n] = verts[index];
                    newUvs[n] = uvs[index];
                    newNormals[n] = normals[index];
                }
                Mesh mesh = new Mesh();
                mesh.vertices = newVerts;
                mesh.normals = newNormals;
                mesh.uv = newUvs;
                
                mesh.triangles = [ 0, 1, 2, 2, 1, 0 ];
                
                var GO = new GameObject("Triangle " + (i / 3));
                GO.transform.position = transform.position;
                GO.transform.rotation = transform.rotation;
                GO.AddComponent(MeshRenderer).material = MR.materials[submesh];
                GO.AddComponent(MeshFilter).mesh = mesh;
                GO.AddComponent(BoxCollider);
                GO.AddComponent(Rigidbody).AddExplosionForce(100, transform.position, 30);
                
                Destroy(GO, 5 + Random.Range(0.0f, 5.0f));
            }
        }
        MR.enabled = false;
        
        Time.timeScale = 0.2f;
        yield WaitForSeconds(0.8f);
        Time.timeScale = 1.0f;
        Destroy(gameObject);
	}
    function OnMouseDown()
    {
        SplitMesh();
    }

If someone is interestet in the result, here’s a webbuild. Just click on one of the default primitives to split them into it’s triangles. Watch out, the sphere(760 tris) and the capsule(670 tris) have a lot triangles and the physics have a heavy impact on the performance (at least on my PC which isn’t up-to-date ;))

You need to rethink your flow…

Here’s an outline…

Get the vertices of your mesh in an array.

Get the triangles for that mesh in another array.

Loop through the list of triangles, grabbing three entries at a time

For each triangle:

Get the corresponding vertex for each triangle entry

Create a new mesh

Assign the new vertex list to that mesh (will have three entries)

Assign the new triangle list to that mesh (will have three entries [0, 1, 2])