Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void Generate()
- {
- GetComponent<MeshFilter>().mesh = mesh = new Mesh();
- mesh.name = "Procedural Grid";
- vertices = new Vector3[(xSize + 1) * (ySize + 1)];
- Vector2[] uv = new Vector2[vertices.Length];
- float multX = 1 / (float)xSize;
- float multY = 1 / (float)ySize;
- for (int i = 0, y = 0; y <= ySize; y++)
- {
- for (int x = 0; x <= xSize; x++, i++)
- {
- //vertices[i] = new Vector3(x, y);
- var xNormalized = x * multX;
- var yNormalized = y * multY;
- vertices[i] = new Vector3(xNormalized, yNormalized);
- uv[i] = new Vector2(xNormalized, yNormalized);
- }
- }
- mesh.vertices = vertices;
- mesh.uv = uv;
- var triangles = new int[xSize * ySize * 6];
- for (int ti = 0, vi = 0, y = 0; y < ySize; y++, vi++)
- {
- for (int x = 0; x < xSize; x++, ti += 6, vi++)
- {
- triangles[ti] = vi;
- triangles[ti + 3] = triangles[ti + 2] = vi + 1;
- triangles[ti + 4] = triangles[ti + 1] = vi + xSize + 1;
- triangles[ti + 5] = vi + xSize + 2;
- }
- }
- mesh.triangles = triangles;
- mesh.RecalculateNormals();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement