Mesh Anatomy in Unity
Mesh Anatomy in Unity, Source :Thomas

Understanding Mesh Anatomy in Unity

Ahmed Schrute
Shader Coding in Unity from a to z
4 min readDec 9, 2019

--

In order to shade a model we need to understand its geometric structure

Let’s take a cube as an example of a mesh model

Cube Mesh
Cube source: Sorceror

In Unity a cube has 6 sides each is a square, each square is 2 triangle

for every triangle(Polygon) there are 3 vertices (For advanced understanding read the note at the end)

Cube Mesh Triangles
Cube Mesh source: Sorceror

with those 3 vertices, we can construct a flat surface that connects the three vertices.

the surface has a normal and each vertex have its own normal

Normal is a perpendicular vector from the surface or the origin point.

the surface has a normal and each vertex have its own normal
Mesh Normals

Normals are important for two reasons:

  1. they indicate which side of the Polygon(Triangle) to add the texture to.

2. how light interacts with the surface, for example, we used surface normals in the Lambert Lighting Model or Phong Lighting Model.

Lambert Lighting
Lambert Lighting
Phong Lighting Model
Phong Lighting Model

As we discussed in the Rendering pipeline article, after the application scripts are being processed next step in the rendering pipeline is to process the Geometry.

Rendering Pipe Line In Unity

Mesh data are being processed in the Geometry Step

Mesh data is stored in 4 arrays :

Vertex Array, Normal Array, Triangle array and UVs array.

1.Vertex Array

world position of every vertex

Vertex Array has the world positions of every triangle vertex in the mesh model
Vertex Array has the world positions of every triangle vertex in the mesh model

2. Normal Array

Corresponds to vertex array listing the normal vector for every vertex

Normal Arrays have a vector3 (x,y,z) for every vertex Normal in the mesh (cube)

3. Triangle Array

lists vertices in groups of 3 (tuple), where every tuple represents a triangle.

Triangle Array lists vertices in group of 3 (uses tuple), where every tuple represents a triangle.
Triangle Array Corresponds to the vertex array, Source: Michael Knapp

4. UV Array

UV represents a point on a texture that is mapped to a point on a polygon

A texture is a standard image that is applied over the mesh surface

UV represents a point on a texture that is mapped to a point on a polygon
Mapping a texture (square) to a mesh element Triangle using UVs, Source: OpenGL_Tutorial

Vertex (x,y,z)

UV (u,v,w) > w for internal calculation

UVs are used to put a texture on a 3d Model Source
UVs are used to dress 3d Models with 2d textures Source: Wikipedia

u,v values range from 0 to 1

UVs are used to specify how a texture should be placed on a mesh
UVs are used to specify how a texture should be placed on a mesh, Source: Textures in OpenGL

UV Array has UV values that correspond to the Vertex array.

Uvs of a mesh element are retrieved by using uv_textureName

Knowing the anatomy of the mesh model is the foundation of Shader coding and accessing those values in our CGProgram will enable us to create beautiful shaders.

Note about vertices count of a cube :

Cube Mesh Triangles
Cube Mesh source: Sorceror

a cube should have 8 vertices (shared between triangles), however, in Unity, a cube has 24 vertices (6*4) since every face needs 4 vertices for surface normals and UVs.

Hope you find this tutorial useful, I am currently looking for a Unity Dev job (Remote or in the Bay Area). If you know someone who can might be looking for a Unity Developer, my email is ah1053@stanford.edu

--

--