Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by loihb · May 23, 2012 at 07:26 PM · meshconverttrianglesto

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;
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · May 23, 2012 at 07:34 PM 0
Share

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.

avatar image Bunny83 · May 24, 2012 at 12:10 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
12
Best Answer

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 ;))

Comment
Add comment · Show 18 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image loihb · May 24, 2012 at 04:45 PM 0
Share

Thanks 10+

avatar image loihb · May 24, 2012 at 07:16 PM 0
Share

Thanks again, what need write if mesh have more than 1 material?

GO.AddComponent($$anonymous$$eshRenderer).material = $$anonymous$$R.material;

avatar image Bunny83 · May 24, 2012 at 07:50 PM 0
Share

You mean you have multiple materials attached to the same object? Just

 GO.AddComponent($$anonymous$$eshRenderer).materials = $$anonymous$$R.materials;

;)

avatar image loihb · May 24, 2012 at 08:06 PM 0
Share

Before :

https://docs.google.com/open?id=0B3we9LaRweh0cTZm$$anonymous$$Hl1RHVLVTQ

After :

https://docs.google.com/open?id=0B3we9LaRweh0djF0NH$$anonymous$$xNURZdmc

avatar image Bunny83 · May 24, 2012 at 08:24 PM 0
Share

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...

Show more comments
avatar image
1

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])

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image loihb · May 23, 2012 at 11:11 PM 0
Share

hmm but hou? vertices can't split on 3 or value is float

avatar image Bunny83 · May 24, 2012 at 12:06 AM 2
Share

@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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges