2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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 \$\endgroup\$ – Seyed Morteza Kamali Jan 30 '18 at 16:11
  • 1
    \$\begingroup\$ @SeyedMortezaKamaly that looks like an answer to me. :) \$\endgroup\$ – DMGregory Jan 30 '18 at 17:32
6
\$\begingroup\$

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

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

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)

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.