using System.Collections; using System.Collections.Generic; using UnityEngine; [RequireComponent(typeof(MeshFilter))] public class AdvancedMesh : MonoBehaviour { Mesh mesh; Vector3[] vertices; int[] triangles; [SerializeField] public int xSize = 200; [SerializeField] public int ySize = 200; void Start() { mesh = new Mesh(); GetComponent().mesh = mesh; CreateShape(); UpdateMesh(); } void CreateShape() { vertices = new Vector3[(xSize + 1) * (ySize + 1)]; for (int i = 0, y = 0; y <= ySize; y++) { for (int x = 0; x <= xSize; x++) { vertices[i] = new Vector3(x, y, 0); i++; } } triangles = new int[xSize * ySize * 6]; int vert = 0; int tris = 0; for (int y = 0; y < ySize; y++) { for (int x = 0; x < xSize; x++) { triangles[tris + 0] = vert + 0; triangles[tris + 1] = vert + xSize + 1; triangles[tris + 2] = vert + 1; triangles[tris + 3] = vert + 1; triangles[tris + 4] = vert + xSize + 1; triangles[tris + 5] = vert + xSize + 2; vert++; tris += 6; } vert++; } } private void UpdateMesh() { mesh.Clear(); mesh.vertices = vertices; mesh.triangles = triangles; mesh.RecalculateNormals(); } private void OnDrawGizmos() { if (vertices == null) return; for (int i = 0; i < vertices.Length; i++) { Gizmos.DrawSphere(vertices[i], .1f); } } }