Hi there, Sorry I'm new to shaders. I'm having trouble to converting a mat2 Type function(GLSL). I have normally seen vec2 functions, but I can't seem to find out how to call the equivalent float2x2(HLSL). GLSL mat2 Rot(float a) { float s = sin(a); float c = cos(a); return mat2(c, -s, s, c); } HLSL float2x2 Rot(float a) { float s = sin(a); float c = cos(a); return float2x2(c, -s, s, c); } OR float2 Rot(float a) { float s = sin(a); float c = cos(a); return float2x2(c, -s, s, c); } Function call: float map(float3 p){ p.xy*=Rot(_Time*0.06); } Getting error:: "type mismatch at line xx" ie "p.xy*=Rot(_Time*0.06);" How do I rewrite the original mat2 function? This seem to run fine on GLSL. I think "rotate" is expecting float2 but that doesn't work.
How is rotate defined? Why aren't you using Rot? I'm not sure what you want to do exactly, but here is how to rotate some vertices in a shader. Code (CSharp): half4 Rotate (half4 vertex, half degrees) { half alpha = degrees * UNITY_PI / 180; half sina, cosa; sincos(alpha, sina, cosa); half2x2 m = half2x2(cosa, -sina, sina, cosa); return half4(mul(m, vertex.xy), vertex.zw).xyzw; }
When you multiply a vector and a matrix together in GLSL you can do myVector *= myMatrix to get the equivalent of myMatrix * myVector. But in HLSL you don’t use the * operator for matrices this way. You instead need to use the mul() function to multiply matrices by vectors or vice versa. That also means there’s no *= operator where the right hand variable is a matrix, unless the left hand is also a matrix. So you’ll want: Code (CSharp): p.xy = mul(rotate(_Time.y * 0.06), p.xy); Also note Unity’s _Time is a float4 where _Time.y is the unmodified game time.
Thanks, I meant to put in "p.xy*=Rot(_Time*0.06);" I don't think there's anything wrong with "Rot". I think I calling it incorrectly ""p.xy*=Rot(_Time*0.06)"