Vertices

A vertex, simply a point in 3D space, is the most basic element of the mesh anatomy. The more vertices, the more power it took to render the object on to the screen. In Unity, these vertices position are relative to their parent game object, e.g. in local space, and number of vertices in a single mesh cannot exceed 65000.

Beside of position data, a vertex also contains some other information like normal vectors, tangent vectors, color and uv-coordinate, they are used for specific purposes.

Normal is a vector which is perpendicular to a given object in space, in this case, the vertex. Actually, vertex is just a point, so the normal vector should be perpendicular to the surface contains the vertex at its position. The normal vectors give us an impression of where the surface faces to, which is useful to determine how it react to the light. For more information about normal vectors, please see here.

Normal vectors pointing out of the surface (Source: https://commons.wikimedia.org/wiki/File:Surface_normals.svg)

Tangent is a vector which is “just touch” a curve or a surface at a given point, in this context, at a vertex in the mesh. Tangent vectors are often used in pair with normal vectors (bi-normal vectors as well) and very useful in some case such as normal mapping. For more information about tangent vectors, please see here.

Each vertex in a mesh can optionally store a RGBA color value, this is called vertex color. This piece of information can be used for many purposes. For example, you can use vertex colors to paint a mesh without bothering with texture and uv-mapping. Essentially, when the computer renders a triangle of the mesh using vertex colors, it will try to interpolate colors of each vertex of the triangle on every pixel across the surface. That’s why this method often achieve a smoother look, with less noise than using a texture with solid colors due to texture compression and bad uv-mapping. For that reason, we will use this technique to color our terrain. Beside of painting the mesh, vertex colors are also used for custom effect like ambient occlusion, wind effect for foliage, etc.

Vertex coloring is nice, but when it comes to a more complex model with lot of details, it’s not the good choice. This is when texturing and uv-mapping join the game. Each vertex in a mesh can hold a piece of information call uv-coordinate which is used for projecting, or flattening a 3D mesh onto a 2D image. The U and V letter denote the horizontal and vertical axes of the image, to avoid ambiguity with XYZ in object position. This technique is used to bake a large amount of vertex information like color, direction and custom data into a single image, for ease of editing. For more information about uv-coordinate as well as uv-mapping, please see here.

Unity engine doesn’t have a specific data type to store vertex information. Instead of creating one, they just store them in several arrays:

  • Vertex positions are stored in a Vector3 array: vertices.

  • Normal vectors are stored in a Vector3 array: normals.

  • Tangent vectors are stored in a Vector4 array: tangents.

  • Vertex colors are stored in a Color array: colors.

  • Uv-coordinates are stored in a Vector2 array: uv.

When making changes to these arrays, we need to copy them into a temporary array then do the work on it, after that, reassign it to the mesh, because there are a lot of behind the scene stuffs happen when accessing these arrays.