Accessing Vector Components - Built-in functions - Shader Learning

Built-in functions

Accessing Vector Components

Task

You are given a color vector with red, green, blue and alpha components. Your goal is to remove the blue channel (set it to 0.0) and use the resulting color as the pixel output.

Theory

Vectors are made up of multiple values like x, y, z, and w. In both GLSL and HLSL, you can access these components using dot notation.


GLSL Example

vec4 color = vec4(1.0, 0.5, 0.2, 1.0);

float red   = color.x; // or color.r
float green = color.y; // or color.g
float blue  = color.z; // or color.b
float alpha = color.w; // or color.a

HLSL Example

float4 color = float4(1.0, 0.5, 0.2, 1.0);

float red   = color.x; // or color.r
float green = color.y; // or color.g
float blue  = color.z; // or color.b
float alpha = color.w; // or color.a

You can use .x, .y, .z, .w or .r, .g, .b, .a depending on context - both are valid and interchangeable.