Sub topic 11
Vertex Attributes: Normals, UVs and Split Vertices
Assume you know basic 3D coordinates, vectors, introductory dot and cross products, and simple Python. This chapter focuses on a detail that often surprises people: a mesh vertex can have different attributes in different faces.
Positions are only part of a vertex
A mesh position tells you where a point is in 3D space. A renderer also needs other data to display the surface:
- A normal describes the direction a surface faces. It affects lighting.
- A UV coordinate maps a surface point to a 2D image. It affects texturing.
- A color can store per-vertex color.
- Tangent data supports normal mapping.
These values are called vertex attributes. They may be stored alongside positions, but they do not always share the same indexing.
Suppose two faces meet at a sharp corner. They share the same position, but their normals should point in different directions. If both faces used one shared normal, lighting would smooth across the corner. The visible result would be wrong.
UV coordinates can also differ at one position. A texture seam may pass through a mesh vertex. The faces on one side of the seam need one UV coordinate, while the faces on the other side need another.
Attributes belong to corners when necessary
An indexed face set usually stores one position list and uses indices to describe faces. For attributes, the important unit is often a face corner.
A face with four positions has four corners. Each corner can refer to:
- one position index,
- one normal index,
- one UV index.
If all attributes match at a position, the data can be shared. If an attribute changes between adjacent faces, the renderer splits the vertex into separate render vertices.
| Situation | Position shared? | Attribute shared? | Result |
|---|---|---|---|
| Smooth surface | Yes | Usually yes | One render vertex |
| Hard edge | Yes | Normal differs | Split render vertices |
| Texture seam | Yes | UV differs | Split render vertices |
| Hard edge and seam | Yes | Both differ | Split for both reasons |
A modeling application may show one geometric vertex while the graphics API receives several render vertices. This is not contradictory. The geometric mesh describes position and connectivity. The render mesh expands corners whenever their complete attribute records differ.
Normals and smooth shading
A face normal comes from the cross product of two edge vectors. For a face with positions p0, p1, and p2, calculate:
- The first edge from p0 to p1.
- The second edge from p1 to p2.
- Their cross product.
The order matters. Reversing the order reverses the normal.
A flat-shaded face uses one normal for all its corners. Smooth shading usually gives each corner an averaged normal based on neighboring faces. The averaging must stop at marked hard edges or other smoothing boundaries.
def normalize(vector):
length = sum(value * value for value in vector) ** 0.5
return tuple(value / length for value in vector)
def cross(a, b):
return (
a[1] * b[2] - a[2] * b[1],
a[2] * b[0] - a[0] * b[2],
a[0] * b[1] - a[1] * b[0],
)
def subtract(a, b):
return tuple(x - y for x, y in zip(a, b))
positions = [(0, 0, 0), (1, 0, 0), (0, 1, 0)]
edge_a = subtract(positions[1], positions[0])
edge_b = subtract(positions[2], positions[0])
normal = normalize(cross(edge_a, edge_b))
print(normal) # (0.0, 0.0, 1.0)UV coordinates and seams
A UV coordinate is a pair, such as (0.25, 0.75), in texture space. U commonly runs horizontally and V vertically, although texture conventions vary.
A rectangular face can use four different UV coordinates. If the same position appears at two different places in the texture layout, that position must have two corner-level UV values. The mesh therefore needs a split at the texture seam.
When exporting or writing a renderer, compare the complete attribute tuple before sharing a render vertex. Positions alone are not enough.
Exercise
Two adjacent triangles share a position, but their UV coordinates differ there. Should they use one render vertex or two, and why?
Show answer
They should use two render vertices. The position is shared geometrically, but the different UV values make the complete attribute records different.