View on GitHub

Randomized-Creativity

Collection of random unity snippets

First steps

[Top] [Next]

The first goal is to simply drawing a grid. More specifically can we vary the shape of the grid tiles in a meaningful way like in renowned explorers or Townscaper, or this science paper, and a nice demo.

Even smaller for the first step we’ll just draw a simple mesh at runtime in Unity.

To create and draw a mesh at runtime in Unity one has to:

To make sure the mesh is indeed as expected, a rotation behavior is added to make the object rotate every second.

To define a mesh, a MeshDefinition class is added which simply looks like this:

[Serializable]
public class MeshDefinition
{
    public Vector3[] vertices;
    public Vector2[] uv;
    public int[] triangles;

    public bool IsValid() => vertices.Length >= 3 && triangles.Length >= 3 && uv.Length >= 3;
}

Although subject to change, the code the core of the mesh generation is following call in MeshGenerator:

private void UpdateMeshDefinition(Mesh mesh, MeshDefinition definition)
{
    mesh.vertices = definition.vertices;
    mesh.uv = definition.uv;
    mesh.triangles = definition.triangles;

    mesh.RecalculateNormals();
    mesh.RecalculateBounds();
    mesh.RecalculateTangents();
}

The results should show up as below:

A simple runtime generated triangle

Scope Creep (next step considerations)

There are a couple of shortcomings with this approach, which could be added:


[Top] [Next]