Skip to content

Instantly share code, notes, and snippets.

@Fewes
Created February 6, 2024 07:17
Show Gist options
  • Save Fewes/427ec9097d37ba955e32ca0a7553f612 to your computer and use it in GitHub Desktop.
Save Fewes/427ec9097d37ba955e32ca0a7553f612 to your computer and use it in GitHub Desktop.
Polar/spherical coordinates conversion functions
#ifndef POLAR_INCLUDED
#define POLAR_INCLUDED
#define PI 3.14159265359
float2 UVToPolar(float2 uv)
{
return float2(uv.x * (2 * PI), (uv.y - 0.5) * PI);
}
float2 PolarToUV(float2 polar)
{
return float2(polar.x / (2 * PI), polar.y / PI + 0.5);
}
float3 PolarToCartesian(float2 polar)
{
return float3(
cos(polar.x) * cos(polar.y),
sin(polar.y),
sin(polar.x) * cos(polar.y)
);
}
float2 CartesianToPolar(float3 cartesian)
{
return float2(
atan2(cartesian.z, cartesian.x),
asin(cartesian.y)
);
}
#endif // POLAR_INCLUDED
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment