home/ atoms/ glsl-vector-constructors-exact

GLSL vector constructors are exact — you cannot assign a `vec2` to a `vec3`, and swizzles must exist

GLSL vector constructors are strict about component count. vec3(1.0) broadcasts to (1,1,1) and vec3(v2, 0.0) extends a vec2 to a vec3, but you cannot assign a vec2 directly to a vec3 — the counts must match. Swizzle accessors (.xyz, .rgb, .st) must reference components that actually exist on the source vector; swizzling a .z off a vec2 is a compile error.

Examples

vec3 a = vec3(1.0); // (1,1,1) vec3 b = vec3(v2, 0.0); // extend vec2 → vec3 vec3 c = v2; // error — count mismatch v2.z // error — no z on a vec2

Assessment

Correct the two errors in vec3 col = vec2(0.5, 0.3); float z = col.a; and explain each rule.

“Vector constructors are exact:** `vec3(1.0)` = `(1,1,1)`; `vec3(v2, 0.0)` extends; you can't assign a `vec2` to a `vec3`. Swizzles (`.xyz`, `.rgb`, `.st`) must exist on the source.”
context/ · L1-instruments/glsl/gotchas.md · chunk 1