home/ atoms/ glsl-swizzle

GLSL swizzling reads or writes a vector's components in any order in one expression

Swizzling is GLSL’s ability to select, reorder, or duplicate a vector’s components by chaining component letters after a dot: v.x, v.xy, v.zyx, v.rgba. It produces a new vector from any combination of the source’s components, in any order, with repetition allowed, and without temporary variables. The xyzw and rgba letter sets are interchangeable aliases into the same storage (v.r is v.x) — the choice is convention (positional vs. color context). You can also write through a swizzle: v.rg = vec2(1.0) assigns two channels at once. A swizzle must respect the source vector’s length (you cannot read a component it lacks). GLSL is strictly typed, so a length or type mismatch is a compile error, not a silent coercion.

Examples

vec3 col = vec3(0.2, 0.8, 0.4);
vec2 gr = col.gr;    // (0.8, 0.2) — reversed
vec3 rrb = col.rrb;  // (0.2, 0.2, 0.4) — repeated
col.rg = vec2(1.0);  // write through a swizzle
vec3 rev = col.zyx;  // reversed order

iResolution.xy extracts width and height from a vec3.

Assessment

Given vec4 v = vec4(1.0, 2.0, 3.0, 4.0), write v.wzyx and v.xxy. Given a vec4 color, write the swizzle extracting RGB as a vec3 and the swizzle reversing the first three components. Explain why v.rr is a vec2.

“I'd like to pause for a second here to discuss an important concept widely used in glsl called swizzling writing irresolution.xy is equivalent to creating a new vect2 and assigning to it the X and Y components of eye resolution”
corpus · kishimisu-an-introduction-to-shader-art-coding-video · chunk 1
“You can get smaller vectors out of a larger vector by using something called "swizzling," where you chain multiple property values together after a `.` in the order you want them in the new vector.”
“Another great feature of vector types in GLSL is that the properties can be combined in any order you want, which makes it easy to cast and mix values. This ability is called _swizzle_.”
corpus · the-book-of-shaders-chapter-6-color-patricio-gonzalez-vivo-a · chunk 1