Advertisement
JontePonte

plane mesh gen

Jan 4th, 2025
6
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. Vector2[] uvs = new Vector2[vertices.Length];
  2. int[] indices = new int[(resolution - 1) * (resolution - 1) * 6];
  3.  
  4. float spacing = size / resolution;
  5.  
  6. for (int z = 0, i = 0; z < resolution; z++)
  7. {
  8. for (int x = 0; x < resolution; x++, i++)
  9. {
  10. vertices[i] = new Vector3(x * spacing - size / 2, 0, z * spacing - size / 2);
  11. uvs[i] = new Vector2((float)x / resolution, (float)z / resolution);
  12. }
  13. }
  14.  
  15. for (int z = 0, i = 0, j = 0; z < resolution - 1; z++)
  16. {
  17. for (int x = 0; x < resolution - 1; x++, i++, j += 6)
  18. {
  19. indices[j] = i;
  20. indices[j + 1] = i + (resolution - 1) + 1;
  21. indices[j + 2] = i + 1;
  22.  
  23. indices[j + 3] = i + 1;
  24. indices[j + 4] = i + (resolution - 1) + 1;
  25. indices[j + 5] = i + (resolution - 1) + 2;
  26. }
  27. i++;
  28. }
  29.  
  30. Mesh mesh = new Mesh()
  31. {
  32. indexFormat = UnityEngine.Rendering.IndexFormat.UInt32,
  33. vertices = vertices,
  34. triangles = indices,
  35. uv = uvs
  36. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement