Understanding Mesh Anatomy in Unity
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
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)
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.
Normals are important for two reasons:
- 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.
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.
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
2. Normal Array
Corresponds to vertex array listing the normal vector for every vertex
3. Triangle Array
lists vertices in groups of 3 (tuple), where every tuple represents a triangle.
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
Vertex (x,y,z)
UV (u,v,w) > w for internal calculation
u,v values range from 0 to 1
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 :
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