CodeBlanks Logo Icon CodeBlanks

What is a Mesh?

Learn that Mesh is the data structure that carries 3D geometry.

Mesh and the Vertex Concept

A Mesh is the core data structure that defines the geometric shape of a 3D object. Its smallest building block is vertices, which store point coordinates in 3D space. These vertices are stored as a Vector3 array in C#.

Vector3[] vertices = mesh.vertices;
Debug.Log("Vertex count: " + vertices.Length);

Triangles and Surfaces

Vertex points alone do not create a surface; they must be connected into groups of three as triangles so the graphics card can render surfaces. mesh.triangles is an integer (int) index array defining the order in which vertices connect. Every three vertex indices form a triangular face.

int[] triangles = mesh.triangles;
int triangleCount = triangles.Length / 3;

Practice Mesh Vertex and Triangle Analysis

Calculate the vertex and triangle count of the given mesh object. Print vertex count with Debug.Log("Kose: " + koseSayisi); and triangle count with Debug.Log("Ucgen: " + ucgenSayisi);.

Starter code

Player.cs
using System;

public struct Vector3
{
    public float x, y, z;
    public Vector3(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
}

public class Mesh
{
    public Vector3[] vertices = new Vector3[4];
    public int[] triangles = new int[6];
}

public static class Debug
{
    public static void Log(object message) => Console.WriteLine(message);
}

public class Program
{
    public static void Main()
    {
        Mesh mesh = new Mesh();
        // kose ve ucgen sayilarini hesaplayip konsola yazdir
    }
}

Sample solution

Player.cs
using System;

public struct Vector3
{
    public float x, y, z;
    public Vector3(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
}

public class Mesh
{
    public Vector3[] vertices = new Vector3[4];
    public int[] triangles = new int[6];
}

public static class Debug
{
    public static void Log(object message) => Console.WriteLine(message);
}

public class Program
{
    public static void Main()
    {
        Mesh mesh = new Mesh();
        int koseSayisi = mesh.vertices.Length;
        int ucgenSayisi = mesh.triangles.Length / 3;
        Debug.Log("Kose: " + koseSayisi);
        Debug.Log("Ucgen: " + ucgenSayisi);
    }
}

Expected output

Kose: 4
Ucgen: 2

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which core array holds the coordinates of vertex points in 3D space within a mesh structure?

  • vertices Correct answer
  • colors
  • normals
  • uv

Explanation: The vertices array holds all corner coordinates making up the mesh geometry in Vector3 format.

2 Read Vertex Array

Question
Vector3[] noktalar = mesh.____;
Answer
Vector3[] noktalar = mesh.vertices;

Explanation: To read all corner points on a mesh, the vertices property is used.

3 Mesh Data Structure

Term Meaning
vertices Array of vertex points in 3D space
triangles Array of vertex indices forming faces
Mesh Data structure holding 3D geometry
triangles.Length / 3 Total number of triangles on the mesh

All lessons in Intermediate