Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. private void Generate()
  2. {
  3. GetComponent<MeshFilter>().mesh = mesh = new Mesh();
  4. mesh.name = "Procedural Grid";
  5.  
  6. vertices = new Vector3[(xSize + 1) * (ySize + 1)];
  7. Vector2[] uv = new Vector2[vertices.Length];
  8. float multX = 1 / (float)xSize;
  9. float multY = 1 / (float)ySize;
  10.  
  11. for (int i = 0, y = 0; y <= ySize; y++)
  12. {
  13. for (int x = 0; x <= xSize; x++, i++)
  14. {
  15. //vertices[i] = new Vector3(x, y);
  16. var xNormalized = x * multX;
  17. var yNormalized = y * multY;
  18. vertices[i] = new Vector3(xNormalized, yNormalized);
  19. uv[i] = new Vector2(xNormalized, yNormalized);
  20. }
  21. }
  22. mesh.vertices = vertices;
  23. mesh.uv = uv;
  24.  
  25. var triangles = new int[xSize * ySize * 6];
  26. for (int ti = 0, vi = 0, y = 0; y < ySize; y++, vi++)
  27. {
  28. for (int x = 0; x < xSize; x++, ti += 6, vi++)
  29. {
  30. triangles[ti] = vi;
  31. triangles[ti + 3] = triangles[ti + 2] = vi + 1;
  32. triangles[ti + 4] = triangles[ti + 1] = vi + xSize + 1;
  33. triangles[ti + 5] = vi + xSize + 2;
  34. }
  35. }
  36. mesh.triangles = triangles;
  37. mesh.RecalculateNormals();
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement