官术网_书友最值得收藏!

How to do it...

There are two types of variables in Cg: single values and packed arrays. The latter can be identified because their type ends with a number such as float3 or int4. As their names suggest, these types of variables are similar to structs, which means that they each contain several single values. Cg calls them packed arrays, though they are not exactly arrays in the traditional sense.

The elements of a packed array can be accessed as a normal struct. They are typically called x, y, z, and w. However, Cg also provides you with another alias for them, that is, r, g, b, and a. Despite there being no difference between using x or r, it can make a huge difference for the readers. Shader coding, in fact, often involves calculation with positions and colors. You might have seen this in the Standard Shaders:

o.Alpha = _Color.a; 

Here, o was a struct and _Color was a packed array. This is also why Cg prohibits the mixed usage of these two syntaxes: you cannot use _Color.xgz.

There is also another important feature of packed arrays that has no equivalent in C#: swizzling. Cg allows addressing and reordering elements within packed arrays in just a single line. Once again, this appears in the Standard Shader:

o.Albedo = _Color.rgb; 

Albedo is fixed3, which means that it contains three values of the fixed type. However, _Color is defined as a  fixed4 type.  A direct assignment would result in a compiler error as _Color is bigger than Albedo. The C# way of doing this would be as follows:

o.Albedo.r = _Color.r; 
o.Albedo.g = _Color.g; 
o.Albedo.b = _Color.b; 

However, it can be compressed in Cg:

o.Albedo = _Color.rgb; 

Cg also allows reordering elements, for instance, using _Color.bgr to swap the red and blue channels.

Lastly, when a single value is assigned to a packed array, it is copied to all of its fields:

o.Albedo = 0; // Black =(0,0,0) 
o.Albedo = 1; // White =(1,1,1) 

This is referred to as smearing.

Swizzling can also be used on the left-hand side of an expression, allowing only certain components of a packed array to be overwritten:

o.Albedo.rg = _Color.rg; 

In this case, it is called masking.

主站蜘蛛池模板: 阆中市| 凭祥市| 高陵县| 剑川县| 温宿县| 南部县| 利川市| 九江县| 延寿县| 西盟| 边坝县| 南雄市| 长治县| 葫芦岛市| 汾西县| 南溪县| 伊春市| 如皋市| 黄平县| 黑河市| 凯里市| 根河市| 兴文县| 玛纳斯县| 旬阳县| 巨鹿县| 鱼台县| 延川县| 黔西| 海盐县| 宜昌市| 阳朔县| 封开县| 额济纳旗| 伊金霍洛旗| 东山县| 弋阳县| 黎城县| 思南县| 古丈县| 邵武市|