- Home /
Question by
Tageos · Jul 26, 2016 at 10:15 PM ·
c#meshesprocedural meshprocedural-generationprocedural-terrain
Mesh generation issue (C#)
At the start of my game i want to create a chunk of terrain:
void StartChunk ()
{
refPoints = new float[chunkLength+1];
blocks = new GameObject[chunkLength];
for (int i = 0; i < refPoints.Length; i++)
{
refPoints [i] = Random.Range (y1Min, y1Max);
}
for (int i = 0; i < blocks.Length; i++)
{
GenerateMeshes (i);
}
}
The method GenerateMeshes
looks like this:
void GenerateMeshes (int i)
{
blocks [i] = Instantiate (block, new Vector3 (0, 0, 0), transform.rotation) as GameObject;
MeshFilter mf = blocks [i].GetComponent<MeshFilter> ();
Mesh mesh = new Mesh ();
mf.mesh = mesh;
//Vertices
vertices = new Vector3[4] {
new Vector3 (i-9, -6,0),
new Vector3 (i-8, -6,0),
new Vector3 (i-9, refPoints[i],0),
new Vector3 (i-8, refPoints[i+1],0),
};
//Triangles
int[] tri = new int[6];
tri [0] = 0;
tri [1] = 1;
tri [2] = 2;
tri [3] = 2;
tri [4] = 3;
tri [5] = 1;
//UV's
Vector2[] uv = new Vector2[4];
uv [0] = new Vector2 (0, 0);
uv [1] = new Vector2 (1, 0);
uv [2] = new Vector2 (0, 1);
uv [3] = new Vector2 (1, 1);
mesh.vertices = vertices;
mesh.triangles = tri;
mesh.uv = uv;
}
When the method StartChunk is called i get this result:
But what i want to achieve is something like this:
I think i have to modify my GenerateMeshes
method but i really cant figure out what to do, any help is appreciated. Thanks.
Comment
Not sure without testing it, but the order of the vertices in your tri defines the direction of the normals, so try this one for the first triangle:
tri [0] = 0;
tri [1] = 2;
tri [2] = 1;
Thanks, that solved my problem! Please post it as an answer and ill accept.
Best Answer
Answer by ScaniX · Jul 27, 2016 at 08:56 AM
The order of the vertices in your tri defines the direction of the normals, so try this one for the first triangle:
tri [0] = 0;
tri [1] = 2;
tri [2] = 1;