- Home /
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;
}
Uhm you don't even use the triangle variable which contains the actual triangles...
Further more it seems you want quads (2 triangles with 2 shared vertices) and not seperate triangles. It would help to know how the mesh looks like and what's the desired result.
I'm still not sure what you actually want do. I guess you've copied the code above from somewhere and it isn't ecaxtly what you want to do.
I just guess you want to split the triangles of the mesh into seperate gameobjects? Well you just can put each triangle in a seperate mesh, but the pivot of each triangle would stay the same, if you want the pivot in the triangle center you have to shift the coordinates.
Answer by Bunny83 · May 24, 2012 at 01:55 AM
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 ;))
Thanks again, what need write if mesh have more than 1 material?
GO.AddComponent($$anonymous$$eshRenderer).material = $$anonymous$$R.material;
You mean you have multiple materials attached to the same object? Just
GO.AddComponent($$anonymous$$eshRenderer).materials = $$anonymous$$R.materials;
;)
Is this one $$anonymous$$esh? If so it seems you have submeshes in your $$anonymous$$esh. Well that makes it a bit more complicated. To get the triangles of a submesh you have to use GetTriangles ins$$anonymous$$d of .triangles.
Additionally in the case of submeshes each material in the materials array corresponds to a submesh. So submesh 0 uses material 0, submesh 1 material 1 ...
I don't have a mesh with submeshes, so i can't test it on my pc ;)
Btw your image links are not very straight forward. I had to signup for google drive to see them...
Answer by Tasarran · May 23, 2012 at 09:38 PM
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])
@valera3132: You have no idea how a indexed mesh works, right? :D
The triangle array contains integer values. always 3 of them define a triangle. Those integer values are indices into the vertices array. They tell you which vertices are used by this triangle. That way two or more triangles can use the same vertex (a shared vertex).
In your code you even try to build a quad (2 triangles 4 vertices)
[0,1,2,2,3,0] -->
First triangle:
0 -> Use vertex 0
1 -> Use vertex 1
2 -> Use vertex 2
Second triangle:
2 -> Use vertex 2 again
3 -> Use vertex 3
0 -> Use vertex 0 again
The two shared vertices would be the diagonal edge in a quad that connects the two triangles.
Your answer
Follow this Question
Related Questions
White artifacting bordering mesh triangles 0 Answers
Render voxels with RGB data passed to them 1 Answer
Green Wireframe Length 1 Answer
Huge GC Overhead When Accessing Tris/Verts From Mesh? 2 Answers
Convert Javascript class to a string 0 Answers