Created
February 5, 2024 16:21
-
-
Save leventeren/f857d1dcdc058e9658f568b15f4776ee to your computer and use it in GitHub Desktop.
Unity Procedural Grid (Mesh Generate)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))] | |
public class Grid : MonoBehaviour { | |
public int xSize, ySize; | |
private Vector3[] vertices; | |
private Mesh mesh; | |
private void Awake() | |
{ | |
Generate(); | |
} | |
private void Generate() | |
{ | |
mesh = new Mesh(); | |
GetComponent<MeshFilter>().mesh = mesh; | |
mesh.name = "Procedural Grid"; | |
vertices = new Vector3[(xSize+1)*(ySize+1)]; | |
Vector4[] tangents = new Vector4[vertices.Length]; | |
Vector4 tangent = new Vector4(1f, 0f, 0f, -1f); | |
Vector2[] uv = new Vector2[vertices.Length]; | |
for (int i = 0, y=0; y <= ySize; y++) | |
{ | |
for (int x = 0; x <= xSize; x++, i++) | |
{ | |
vertices[i] = new Vector3(x, y); | |
uv[i] = new Vector2((float)x / xSize, (float)y / ySize); | |
tangents[i] = tangent; | |
} | |
} | |
int[] triangles = new int[xSize * 6 * ySize]; | |
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.vertices = vertices; | |
mesh.uv = uv; | |
mesh.tangents = tangents; | |
mesh.triangles = triangles; | |
mesh.RecalculateNormals(); | |
} | |
private void OnDrawGizmos() | |
{ | |
if (vertices == null) | |
return; | |
Gizmos.color = Color.black; | |
for (int i = 0; i < vertices.Length; i++) | |
{ | |
Gizmos.DrawSphere(vertices[i], 0.1f); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment