Created
December 20, 2016 06:48
-
-
Save runewake2/b382ecd3abc3a32b096d08cc69c541fb to your computer and use it in GitHub Desktop.
Build a 3D Plane mesh with dynamic number of grid tiles.
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
// This code was built as part of World of Zero: https://youtu.be/iwsZAg7dReM | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
[RequireComponent(typeof(MeshFilter))] | |
public class GeneratePlaneMesh : MonoBehaviour { | |
public float size = 1; | |
public int gridSize = 16; | |
private MeshFilter filter; | |
// Use this for initialization | |
void Start () { | |
filter = GetComponent<MeshFilter>(); | |
filter.mesh = GenerateMesh(); | |
} | |
// Update is called once per frame | |
void Update () { | |
} | |
Mesh GenerateMesh() | |
{ | |
Mesh mesh = new Mesh(); | |
var vertices = new List<Vector3>(); | |
var normals = new List<Vector3>(); | |
var uvs = new List<Vector2>(); | |
for (int x = 0; x < gridSize + 1; ++x) | |
{ | |
for (int y = 0; y < gridSize + 1; ++y) | |
{ | |
vertices.Add(new Vector3(-size * 0.5f + size * (x / ((float)gridSize)), 0, -size * 0.5f + size * (y / ((float)gridSize)))); | |
normals.Add(Vector3.up); | |
uvs.Add(new Vector2(x / (float)gridSize, y / (float)gridSize)); | |
} | |
} | |
var triangles = new List<int>(); | |
var vertCount = gridSize + 1; | |
for (int i = 0; i < vertCount * vertCount - vertCount; ++i) | |
{ | |
if ((i + 1)%vertCount == 0) | |
{ | |
continue; | |
} | |
triangles.AddRange(new List<int>() | |
{ | |
i + 1 + vertCount, i + vertCount, i, | |
i, i + 1, i + vertCount + 1 | |
}); | |
} | |
mesh.SetVertices(vertices); | |
mesh.SetNormals(normals); | |
mesh.SetUVs(0, uvs); | |
mesh.SetTriangles(triangles, 0); | |
return mesh; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment