Runtime Mesh Generation in Unity (Complete Guide)

Destructible Terrain System in Unity (Complete Guide)
June 29, 2026
Runtime Mesh Deformation in Unity (Footsteps, Damage, Softbody Physics)
Runtime Mesh Deformation in Unity (Footsteps, Damage, Softbody Physics)
June 29, 2026
Destructible Terrain System in Unity (Complete Guide)
June 29, 2026
Runtime Mesh Deformation in Unity (Footsteps, Damage, Softbody Physics)
Runtime Mesh Deformation in Unity (Footsteps, Damage, Softbody Physics)
June 29, 2026

Runtime Mesh Generation in Unity (Complete Guide)

Runtime Mesh Generation allows you to create, modify, and update 3D geometry during gameplay. This technique is widely used in procedural worlds, voxel engines, deformable objects, destructible environments, and dynamic terrains.

In this guide, we’ll break down how Unity’s Mesh API works, how to generate meshes at runtime, and the best patterns to keep your system clean and optimized.


1. How Unity Meshes Work

Every mesh in Unity consists of:

  • Vertices
  • Triangles
  • UVs
  • Normals

To generate a mesh at runtime, you create a Mesh object and feed it this data.

Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();

Simple, right?


2. Creating a Simple Quad at Runtime

Let’s start with the simplest mesh: a quad (a single square).

using UnityEngine;

public class RuntimeQuad : MonoBehaviour
{
    void Start()
    {
        Mesh mesh = new Mesh();

        Vector3[] vertices = {
            new Vector3(0, 0, 0),
            new Vector3(1, 0, 0),
            new Vector3(0, 1, 0),
            new Vector3(1, 1, 0)
        };

        int[] triangles = {
            0, 2, 1,
            2, 3, 1
        };

        mesh.vertices = vertices;
        mesh.triangles = triangles;
        mesh.RecalculateNormals();

        GetComponent<MeshFilter>().mesh = mesh;
    }
}

Boom! A runtime-generated quad.


3. Generating a Runtime Grid Mesh

This technique is the backbone of procedural terrains, tilemaps, and voxel surfaces.

Concept:

  • Generate N×M vertices
  • Create triangles for each quad
  • Apply UVs and normals
public Mesh GenerateGrid(int width, int height, float cellSize)
{
    Mesh mesh = new Mesh();

    Vector3[] vertices = new Vector3[(width + 1) * (height + 1)];
    int[] triangles = new int[width * height * 6];

    int v = 0, t = 0;

    for (int y = 0; y <= height; y++)
    {
        for (int x = 0; x <= width; x++)
        {
            vertices[v++] = new Vector3(x * cellSize, 0, y * cellSize);
        }
    }

    v = 0;

    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            int i = x + y * (width + 1);

            triangles[t++] = i;
            triangles[t++] = i + width + 1;
            triangles[t++] = i + 1;

            triangles[t++] = i + 1;
            triangles[t++] = i + width + 1;
            triangles[t++] = i + width + 2;
        }
    }

    mesh.vertices = vertices;
    mesh.triangles = triangles;
    mesh.RecalculateNormals();

    return mesh;
}

This is the core of runtime terrain, ocean waves, and even hex grids.


4. Deforming Meshes at Runtime (Vertex Manipulation)

One of the coolest parts of runtime mesh generation is mesh deformation.

Common uses:

  • Footprints on snow
  • Waves on water
  • Character deformation
  • Damage effects
public void ApplyDeformation(Vector3 point, float radius, float force)
{
    Vector3[] vertices = mesh.vertices;

    for (int i = 0; i < vertices.Length; i++)
    {
        float dist = Vector3.Distance(vertices[i], point);

        if (dist < radius)
        {
            float falloff = 1 - (dist / radius);
            vertices[i].y += force * falloff;
        }
    }

    mesh.vertices = vertices;
    mesh.RecalculateNormals();
}

5. Procedural Terrain Example (Perlin Noise)

for (int i = 0; i < vertices.Length; i++)
{
    float x = vertices[i].x * 0.2f;
    float z = vertices[i].z * 0.2f;

    float noise = Mathf.PerlinNoise(x, z);

    vertices[i].y = noise * heightScale;
}

Boom. Procedural hills at runtime.


6. Performance Tips (Important!)

  • Avoid per-frame mesh recreation (use caching)
  • Use Mesh.MarkDynamic() for deforming meshes
  • Use jobs and Burst for large meshes
  • Pool mesh objects to avoid allocating

7. When to Use Runtime Mesh Generation

  • Procedural terrain / worlds
  • Voxel engines (Minecraft-style)
  • Runtime modeling tools
  • Character customization
  • Deformable/damageable objects
  • Dynamic water surfaces

Final Thoughts

Runtime mesh generation is one of the most powerful techniques in Unity, giving you full control over geometry and allowing you to build procedural or dynamic systems.

Once you understand vertices, triangles, and normals, the rest becomes pure creativity.

Your imagination becomes the mesh generator


 

Leave a Reply

Your email address will not be published. Required fields are marked *


Skip to toolbar