Skip to content

Instantly share code, notes, and snippets.

@runewake2
Created December 20, 2016 06:48
Show Gist options
  • Save runewake2/b382ecd3abc3a32b096d08cc69c541fb to your computer and use it in GitHub Desktop.
Save runewake2/b382ecd3abc3a32b096d08cc69c541fb to your computer and use it in GitHub Desktop.
Build a 3D Plane mesh with dynamic number of grid tiles.
// 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;
}
}
@MastadonGames
Copy link

yo, when paired with mesh deformer it throws an error for the this script telling "GeneratePlaneMesh.cs does not contain a var mesh" since its generated on the first func call of GenerateMesh(), but since you can't play mode while errors are there, it never goes away. I think you can just add public Mesh mesh; to the start of the script like so `
// 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;

public Mesh mesh;

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;
}

}`

and that should fix it.

@MastadonGames
Copy link

sorry for the messed up markdown btw, im new to commenting lol.

@MastadonGames
Copy link

oh and i tested it and it fixs the issue 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment