Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [RequireComponent(typeof(MeshFilter),typeof(MeshRenderer))]
- public class Grid : MonoBehaviour
- {
- public int xScale;
- public int yScale;
- private Mesh mesh;
- private Vector3[] vertexBuffer;
- private void Awake()
- {
- StartCoroutine(Generator());
- }
- private IEnumerator Generator()
- {
- WaitForSeconds wait = new WaitForSeconds(0.05f);
- vertexBuffer = new Vector3[(xScale + 1) * (yScale + 1)];
- Vector2[] uv = new Vector2[vertexBuffer.Length];
- GetComponent<MeshFilter>().mesh = mesh = new Mesh();
- for (int i = 0, z = 0; z <= yScale; z++)
- {
- for (int x = 0; x <= xScale; x++, i++)
- {
- vertexBuffer[i] = new Vector3(x, Random.Range(0.0f,1.0f), z);
- uv[i] = new Vector2((float)x / (float)xScale, (float)z / (float)yScale);
- yield return wait;
- }
- }
- mesh.vertices = vertexBuffer;
- mesh.uv = uv;
- int[] tris = new int[xScale * yScale * 6];
- for (int ti = 0, vi = 0, y = 0; y < yScale; y++, vi++)
- {
- for (int x = 0; x < xScale; x++, ti += 6, vi++)
- {
- tris[ti] = vi;
- tris[ti + 1] = tris[ti + 4] = vi + xScale + 1;
- tris[ti + 2] = tris[ti + 3] = vi + 1;
- tris[ti + 5] = vi + xScale + 2;
- }
- }
- mesh.triangles = tris;
- mesh.RecalculateNormals();
- }
- private void OnDrawGizmos()
- {
- if (vertexBuffer == null)
- {
- return;
- }
- Gizmos.color = Color.black;
- for (int i = 0; i< vertexBuffer.Length;i++)
- {
- Gizmos.DrawSphere(vertexBuffer[i], 0.1f);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement