There's more...

Where swizzling really shows its full potential is when it's applied to packed matrices. Cg allows types such as float4x4, which represents a matrix of floats with four rows and four columns. You can access a single element of the matrix using the _mRC notation, where R is the row and C is the column:

float4x4 matrix; 
// ... 
float first = matrix._m00; 
float last = matrix._m33; 

The _mRC notation can also be chained:

float4 diagonal = matrix._m00_m11_m22_m33; 

An entire row can be selected using squared brackets:

float4 firstRow = matrix[0]; 
// Equivalent to 
float4 firstRow = matrix._m00_m01_m02_m03; 

Get Unity 2018 Shaders and Effects Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.