Skip to content

Instantly share code, notes, and snippets.

@mdomrach
Created June 25, 2016 06:33
Embed
What would you like to do?
using UnityEngine;
using System.Collections.Generic;
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshFilter))]
public class GridMesh : MonoBehaviour
{
public int GridSize;
void Awake()
{
MeshFilter filter = gameObject.GetComponent<MeshFilter>();
var mesh = new Mesh();
var verticies = new List<Vector3>();
var indicies = new List<int>();
for (int i = 0; i < GridSize; i++)
{
verticies.Add(new Vector3(i, 0, 0));
verticies.Add(new Vector3(i, 0, GridSize));
indicies.Add(4 * i + 0);
indicies.Add(4 * i + 1);
verticies.Add(new Vector3(0, 0, i));
verticies.Add(new Vector3(GridSize, 0, i));
indicies.Add(4 * i + 2);
indicies.Add(4 * i + 3);
}
mesh.vertices = verticies.ToArray();
mesh.SetIndices(indicies.ToArray(), MeshTopology.Lines, 0);
filter.mesh = mesh;
MeshRenderer meshRenderer = gameObject.GetComponent<MeshRenderer>();
meshRenderer.material = new Material(Shader.Find("Sprites/Default"));
meshRenderer.material.color = Color.white;
}
}
@LordZardeck
Copy link

line 17 should have i <= GridSize, otherwise the top and right side of the grid won't be closed

@wyliefoxxx
Copy link

wyliefoxxx commented Mar 19, 2022

Thanks for the jumping point.. Here's my modified code to create a grid of any size and add curvature... for those of you who might not have been paying as much attention back in math class :)


public MeshFilter filter;

public int gridSize = 10;
[Range(0, 1)]
public float bendX = 1;
[Range(0, 1)]
public float bendY = 0;

Mesh mesh;
List<Vector3> verticies;
List<int> indicies;

float gridSpacing;
float xMin;
float yMin;
float zMin;
float x1;
float x2;
float y1;
float y2;
float zx1;
float zx2;
float zy1;
float zy2;

void Start()
{
    //MeshFilter filter = gameObject.GetComponent<MeshFilter>();
    mesh = new Mesh();
    MeshRenderer meshRenderer = filter.GetComponent<MeshRenderer>();
    meshRenderer.material = new Material(Shader.Find("Sprites/Default"));
    meshRenderer.material.color = Color.white;
    Rebuild();
}

private void Update()
{
    Rebuild();
}

void Rebuild()
{
    verticies = new List<Vector3>();
    indicies = new List<int>();
    gridSpacing = (1 / (float)gridSize);

    xMin = gridSpacing * gridSize / 2;
    yMin = gridSpacing * gridSize / 2;
    zMin = (.25f * bendX + .25f * bendY)/2;

    for (int i = 0; i <= gridSize; i++)
    {
        for (int j = 0; j<=gridSize; j++)
        {
            x1 = i * gridSpacing - xMin;
            x2 = (i+1) * gridSpacing - xMin;
            y1 = j * gridSpacing - yMin;
            y2 = (j+1) * gridSpacing - yMin;

            zx1 = Mathf.Abs(Mathf.Pow(x1, 2)) * bendX * 2 - zMin;
            zx2 = Mathf.Abs(Mathf.Pow(x2, 2)) * bendX * 2 - zMin;
            zy1 = Mathf.Abs(Mathf.Pow(y1, 2)) * bendY * 2 - zMin;
            zy2 = Mathf.Abs(Mathf.Pow(y2, 2)) * bendY * 2 - zMin;

            if (i != gridSize)
            {
                verticies.Add(new Vector3(x1, y1, -zx1 - zy1));
                verticies.Add(new Vector3(x2, y1, -zx2 - zy1));
            }

            if (j != gridSize)
            {
                verticies.Add(new Vector3(x1, y1, -zx1 - zy1));
                verticies.Add(new Vector3(x1, y2, -zx1 - zy2));
            }
        }
    }
    int indiciesCount = 4*((int)Mathf.Pow(gridSize, 2) + gridSize);
    for (int i = 0; i < indiciesCount; i++)
    {
        indicies.Add(i);
    }

    mesh.vertices = verticies.ToArray();
    mesh.SetIndices(indicies.ToArray(), MeshTopology.Lines, 0);
    filter.mesh = mesh;
}

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