- Home /
How do I rotate around the X axis in a vertex shader?
The Unity Skybox shader makes use of a vertex shader to rotate the skybox about the Y Axis (Heading).
The function is here :
float4 RotateAroundYInDegrees (float4 vertex, float degrees)
{
float alpha = degrees * UNITY_PI / 180.0;
float sina, cosa;
sincos(alpha, sina, cosa);
float2x2 m = float2x2(cosa, -sina, sina, cosa);
return float4(mul(m, vertex.xz), vertex.yw).xzyw;
}
How do I adjust this to rotate around the X Axis? I understand that the 2x2 rotation matrix is the same, but I'm a little confused by the use of quaternions in the final rotation.
Answer by Eliot_L · Oct 20, 2015 at 04:27 PM
I was trying to do the same thing but couldn't figure out how completely in the shader. However, the method of generating a matrix on the CPU and passing it to the shader worked for me to have a custom skybox rotation.
Answer by UDN_39161513-3fca-49d8-bd68-2fd56e7f2eff · Oct 19, 2017 at 12:57 PM
float3 RotateAroundZInDegrees (float3 vertex, float degrees)
{
float alpha = degrees * UNITY_PI / 180.0;
float sina, cosa;
sincos(alpha, sina, cosa);
float2x2 m = float2x2(cosa, -sina, sina, cosa);
//return float3(mul(m, vertex.xz), vertex.y).xzy;
return float3(mul(m, vertex.xy), vertex.z).zxy;
}
Very nice. How would I alter this to make it rotate around the axis between x and y. So on a 45 degrees angle from x. I'm trying to make my night sky rotate more realistic, so the point of rotation is a little bit above the horizon. Thanks.