# In a shader, what is the meaning of “<some float>.xy?”

I'm trying to research this in Unity manuals online, and I'm finding plenty of references to .xy but not sure what it is. While this is for Unity, I'm assuming this principal is more generic across all shader systems.

When we have a float3 variable called b and we say b.xy what are we getting?

Is it X * Y?

Is it a float2, with X & Y passed through? If this is the case, I'm also seeing b.xy / a.xy and I'm wondering how vector division works in shaders.

• There is 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 defned as fixed4. 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; Refrence – Seyed Morteza Kamali Jan 30 '18 at 16:11
• @SeyedMortezaKamaly that looks like an answer to me. :) – DMGregory Jan 30 '18 at 17:32

Partwise grabbing of each axis.

When we have a float3 variable called b and we say b.xy what are we getting?

You're getting a float2 that contains [x,y]

This can be expanded, twisted and combined in interesting ways, too:

float3 tripleX = a.xxx
float3 reverse = a.zyx


If this is the case, I'm also seeing b.xy / a.xy and I'm wondering how vector division works in shaders.

result.x = b.x / a.x

result.y = b.y / a.y

Unity usually uses a hlsl dialect called cg. What you see is basically a selector, so if B is a float3 C = B.xy is a float2 with C.x == B.x and C.y == B.y .

In cg specification this is referred to as "swizzle operator". (Kudos to DMGregory for this)

• Link to the documentation explaining this would probably be helpful for future visitors. – Vaillancourt Jan 30 '18 at 14:24
• I don't seem to find the correct one, but feel free to edit my answer if you do. – Alakanu Jan 30 '18 at 14:35
• I'd like to add that Swizzling is not only available in Unity/CG, but also in GLSL and HLSL – Gustavo Maciel Jan 30 '18 at 18:48