- Unity 2018 Shaders and Effects Cookbook
- John P. Doran Alan Zucconi
- 101字
- 2025-04-04 16:34:20
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;