- Home /
Help in understanding Mesh Generation
Hey guys,
I'm kinda' sick and I don't feel like typing much, so please excuse the brief question! I've been trying to figure out Mesh Generation, and I think I understand it a tiny bit, but I'm having a really hard time grasping this. I've been reading tutorials online about it, but they all seem to be written to people who know a bit about it... I pretty much know nothing about this, so that may be the issue.
After reading Morten Nobel's blog post on procedurally creating a Tetrahedron, I copy-and-pasted the code provided into my project and played around with it for a bit. Now I've got it creating a two-dimensional plane (what I wanted), but the code is still very long. I guess I'm just having a hard time understanding that all this is necessary to make a simple plane, and that there isn't an easier way to do it (while still making it in code).
I'll post the code I've edited bellow so you guys can read it, and maybe give me some suggestions; tell me if there is a simpler way to do this (again, while still making it in code. I don't want to use CreatePrimitive or something similar), or if I'm not understanding it and left some unnecessary code in there. Basically, please just help me understand how to make a plane procedurally!
From Morten Nobel's blog post "Procedural generated mesh in Unity":
void Start ()
{
MeshFilter meshFilter = gameObject.GetComponent<MeshFilter>();
Mesh mesh = new Mesh ();
meshFilter.mesh = mesh;
mesh.vertices = new Vector3[]
{
new Vector3(0,1,0),
new Vector3(1,1,0),
new Vector3(1,1,1),
new Vector3(0,1,1),
};
int faces = 1;
List<int> triangles = new List<int>();
List<Vector2> uvs = new List<Vector2>();
for (int i = 0; i < faces; i++)
{
int triangleOffset = i*1;
triangles.Add(0+triangleOffset);
triangles.Add(2+triangleOffset);
triangles.Add(1+triangleOffset);
triangles.Add(0+triangleOffset);
triangles.Add(3+triangleOffset);
triangles.Add(2+triangleOffset);
uvs.Add(new Vector2(0,0));
uvs.Add(new Vector2(1,0));
uvs.Add(new Vector2(1,1));
uvs.Add(new Vector2(0,1));
}
mesh.triangles = triangles.ToArray();
mesh.uv = uvs.ToArray();
renderer.material = new Material(Shader.Find("Diffuse"));
mesh.RecalculateNormals();
mesh.RecalculateBounds ();
mesh.Optimize();
}
Well, I started this saying I wasn't going to type much, and here I've written three paragraphs! Anyway, thank you for taking the time to read this, indoubtedly, confusing post. I really appreciate all your help, guys, thanks so much!
-Gibson
Looks good to me, however I am curious why you are using int triangle offset ins$$anonymous$$d of just using i in the for loop. Since you are multiplying i by 1 for the value of triangleOffset you may as well not create the extra variable.
Thanks for the reassurance, apples_mmmmmmmm! I am using int triangle offset simply because that's how it was when I coppied it, and when I tried to take it out, I got an error. Please, how would you suggest I would do it? Thanks again!
Answer by Apples_mmmmmmmm · Aug 31, 2012 at 05:31 AM
I think you should be able to replace
for (int i = 0; i < faces; i++) {
int triangleOffset = i*1;
triangles.Add(0+triangleOffset);
triangles.Add(2+triangleOffset);
triangles.Add(1+triangleOffset);
triangles.Add(0+triangleOffset);
triangles.Add(3+triangleOffset);
triangles.Add(2+triangleOffset);
uvs.Add(new Vector2(0,0));
uvs.Add(new Vector2(1,0));
uvs.Add(new Vector2(1,1));
uvs.Add(new Vector2(0,1));
}
with
for (int i = 0; i < faces; i++)
{
triangles.Add(0+i);
triangles.Add(2+i);
triangles.Add(1+i);
triangles.Add(0+i);
triangles.Add(3+i);
triangles.Add(2+i);
uvs.Add(new Vector2(0,0));
uvs.Add(new Vector2(1,0));
uvs.Add(new Vector2(1,1));
uvs.Add(new Vector2(0,1));
}
I don't think you should get any errors through doing this.
Your answer
Follow this Question
Related Questions
Procedurally Generated Cube Mesh 3 Answers
Flat shading procedural generated mesh? 1 Answer
Mesh generated from bezier curve loop going outside loop 0 Answers
how to find the distance between two points? 1 Answer
Mesh creation by code not working? 0 Answers