private TerrainChunk CreateChunk(Vector2[] vertices2D, int idx)
{
int vertexIdx = 1;
int[] indices = new int[vertices2D.Length * 3];
for (int i = 0; i < indices.Length; i += 3)
{
indices[i] = 0; // we know the first vertex is the centroid
indices[i + 1] = vertexIdx;
// wrap around
int nextVertex = (vertexIdx + 1 < vertices2D.Length) ? vertexIdx + 1 : 1;
indices[i + 2] = nextVertex;
vertexIdx = nextVertex;
}
// Create the Vector3 vertices
Vector3[] vertices = new Vector3[vertices2D.Length];
for (int i = 0; i < vertices.Length; i++)
{
vertices[i] = new Vector3(vertices2D[i].x, vertices2D[i].y, 0.0f);
}
// create the uvs
Vector2[] uvs = new Vector2[vertices.Length];
for (int i = 0; i < uvs.Length; i++)
{
uvs[i] = new Vector2(vertices[i].x / textureScale, vertices[i].y / textureScale);
}
Mesh msh = new Mesh();
msh.vertices = vertices;
msh.triangles = indices;
msh.RecalculateNormals();
msh.RecalculateBounds();
msh.uv = uvs;
// Set up game object with mesh;
TerrainChunk chunkObj = (TerrainChunk)Instantiate(chunkPrototype);
chunkObj.gameObject.AddComponent(typeof(MeshRenderer));
MeshFilter filter = chunkObj.gameObject.AddComponent(typeof(MeshFilter)) as MeshFilter;
filter.mesh = msh;
chunkObj.Initialize(idx, vertices2D, indices);
return(chunkObj);
}