home/ atoms/ glsl-vector-component-aliases

A GLSL vector's components have interchangeable names: .xyzw, .rgba, .stpq, and [i]

In GLSL a vec3 or vec4 stores components accessible by several equivalent notations: position (.x .y .z .w), color (.r .g .b .a), texture (.s .t .p .q), and index ([0] [1] [2] [3]). These are the same storage: vector.r, vector.x, and vector[0] all refer to the identical value. The different names are nomenclature to make code read clearly — using .rgb when the vector is a color, .xyz when it is a position. This design deliberately blurs the line between color and space, encouraging you to think of them interchangeably. A common confusion is treating .r and .x as different fields; they are aliases for the same slot.

Examples

vec4 vector;
vector[0] = vector.r = vector.x = vector.s; // all the same slot
vector[1] = vector.g = vector.y = vector.t;
vec3 red = vec3(1.0, 0.0, 0.0);
float sameThing = red.r; // == red.x == red[0]

Assessment

Given vec3 c = vec3(0.2, 0.5, 0.8), state the value of c.g, c.y, and c[1]. Explain why they are equal.

“The values of `.x`, `.y` and `.z` can also be called `.r`, `.g` and `.b`, and `.s`, `.t` and `.p`.”
corpus · the-book-of-shaders-chapter-6-color-patricio-gonzalez-vivo-a · chunk 1