- Home /
creating a mesh procedurally
hi guys i want to generate a simple mesh at runtime and then use it for my object's mesh renderer but i can not see anything on the screen. i think my problem is with setting triangles. is there anyone that he knows how should i set triangles and what are the different approaches for setting triangles vertecies?
this is my code
using UnityEngine;
using System.Collections;
public class album : MonoBehaviour
{
public Texture2D tex;
Mesh m;
IEnumerator Start()
{
yield return new WaitForSeconds(1f);
m = new Mesh();
m.vertices = new Vector3[5];
m.uv = new Vector2[5];
m.normals = new Vector3[5];
m.normals[0] = new Vector3(0,0,-1); ;
m.normals[1] = new Vector3(0, 0, -1);
m.vertices[0].x = 0;
m.vertices[0].y = 0;
m.vertices[0].z = 0;
m.uv[0] = new Vector2 (0,0);
m.vertices[1].x = 1;
m.vertices[1].y = 0;
m.vertices[1].z = 0;
m.uv[1] = new Vector2(1, 0);
m.vertices[2].x = 0;
m.vertices[2].y = -1;
m.vertices[2].z = 0;
m.uv[2] = new Vector2(0, 1);
m.vertices[3].x = 1;
m.vertices[3].y = -1;
m.vertices[3].z = 0;
m.uv[3] = new Vector2(1, 1);
m.vertices[4].x = 0;
m.vertices[4].y = 0;
m.vertices[4].z = 0.3f;
m.uv[4] = new Vector2(1, 1);
m.triangles = new int[9];
m.triangles[0] = 0;
m.triangles[1] = 1;
m.triangles[2] = 2;
m.triangles[3] = 2;
m.triangles[4] = 3;
m.triangles[5] = 1;
m.triangles[6] = 1;
m.triangles[7] = 0;
m.triangles[8] = 4;
m.RecalculateBounds();
MeshFilter mf = (MeshFilter)transform.GetComponent(typeof(MeshFilter));
mf.mesh = m;
renderer.material = new Material(Shader.Find(" Diffuse"));
mf.renderer.material.mainTexture = tex;
}
}
Hey,
I'm looking for the correct method to generate the triangles from an array of vertices. The link above is dead. $$anonymous$$ay someone make it available again?
Thanks
Here's a mirror of the PDF file from Fla$$anonymous$$gHairball's (Lincoln Green's) tutorial: http://games.deozaan.com/unity/$$anonymous$$eshTutorial.pdf
@maveryck21: Don't post an answer when you don't want to answer the queestion. I've converted your answer into a comment.
There is no correct way to form triangles out of vertices, because it depends on what you actually want to create. You can create a triangle between any vertices in the vertices array.
Just look at the example in the docs. Phil has posted the link already in his answer below...
Also there are many questions like this already on UA. Just use the internal search function or even better google
Answer by Ehren · Nov 19, 2009 at 09:38 PM
This tutorial from the UniKnowledge contest covers creating meshes via scripting, including how to correctly set the triangles (see page 9 of the pdf in Tutorial.zip).
Looks like his link is dead, is it available anywhere else?
I would also like to know if this tut on creating meshes is available anywhere.
3rd...there needs to be more info about creating meshes by hand!
The author, Fla$$anonymous$$gHairball, has posted the tutorials here: http://dl.dropbox.com/u/680550/$$anonymous$$eshTutorial.zip
EDIT: Since the above link is dead, I've mirrored the PDF file here: http://games.deozaan.com/unity/$$anonymous$$eshTutorial.pdf
Answer by flaminghairball · May 12, 2011 at 07:05 PM
Hey guys,
Sorry for the late response. I've been swamped and am in the process of transitioning hosting, so my server is down. You can temporarily access the tutorial here: http://dl.dropbox.com/u/680550/MeshTutorial.zip
It's an archive that contains the tutorial PDF and the required files. Sorry for the inconvenience!
Best wishes,
-Lincoln Green
The file only contains PDFs, no the required files for part 2.
Answer by $$anonymous$$ · Nov 20, 2009 at 08:04 AM
I believe you need to fill in your array values first and then finish by assigning mesh.uvs, mesh.uvs, mesh.vertices, mesh.triangles, all at once as in the Mesh documentation examples - http://unity3d.com/support/documentation/ScriptReference/Mesh.html
Answer by Kiyaku · Nov 19, 2009 at 11:56 PM
But can you see the new mesh gets applied in the inspector to your transform?
I never really created meshes at runtime but did you try using "m.RecalculateNormals();" instead?
.Recalculatenormals(); is required only when you need your mesh to cooperate with lighting engine. It is not required but I would advise of use it for convience.