Guest User

Untitled

a guest
Oct 17th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. // Create a mesh of triangles in a 2D grid
  2. // Attach this script to a GameObject that already has a MeshFilter - it will use that MeshFilter, replacing its existing mesh data
  3. // Creates a numCols x numRows grid of vertices in the X/Y plane. Then for each square cell in the grid it creates 2 triangles
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7.  
  8. public class grid : MonoBehaviour {
  9.  
  10. public int numRows=10, numCols=10;
  11. public float minX=-1, maxX=1, minY=-1, maxY=1;
  12.  
  13. void Start ()
  14. {
  15. int index;
  16. Vector3[] myVerts = new Vector3[numRows*numCols];
  17. index = 0;
  18. for (int j=0; j < numRows; j++)
  19. for (int i=0; i < numCols; i++)
  20. {
  21. float x = Mathf.Lerp(minX,maxX,i/(numCols-1f));
  22. float y = Mathf.Lerp(minY,maxY,j/(numRows-1f));
  23. myVerts[index] = new Vector3(x,y,0);
  24. index++;
  25. }
  26. int[] myTris = new int[(numRows-1)*(numCols-1)*2*3];
  27. index = 0;
  28. for (int j=0; j < numRows-1; j++)
  29. for (int i=0; i < numCols-1; i++)
  30. {
  31. myTris[index++] = i + j*numCols;
  32. myTris[index++] = i + (j+1)*numCols;
  33. myTris[index++] = (i+1) + j*numCols;
  34. myTris[index++] = i + (j+1)*numCols;
  35. myTris[index++] = (i+1) + (j+1)*numCols;
  36. myTris[index++] = (i+1) + j*numCols;
  37. }
  38. Mesh myMesh = gameObject.GetComponent<MeshFilter>().mesh;
  39. myMesh.Clear();
  40. myMesh.vertices = myVerts;
  41. myMesh.triangles = myTris;
  42. myMesh.RecalculateNormals();
  43. }
  44.  
  45. void Update ()
  46. {
  47. }
  48. }
Add Comment
Please, Sign In to add comment