Skip to content

Instantly share code, notes, and snippets.

@menduz
Last active May 30, 2020 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save menduz/2c354969f9c28fed1bdde0d7b473abb6 to your computer and use it in GitHub Desktop.
Save menduz/2c354969f9c28fed1bdde0d7b473abb6 to your computer and use it in GitHub Desktop.
Normal encoding/decoding in float2
//Encoding:
float2 atanYX = atan2(normalIn.y,normalIn.x);
float2 normalOut = float2(atanYX/PI,normalIn.z);
normalOut = (normalOut+1.0)+0.5;
//Decoding:
float2 angles = normalOut*2.0- 1.0;
float2 theta = sincos(angles.x*PI,theta.x,theta.y);
float2 phi = float2(sqrt(1.0- angles.y*angles.y),angles.y);
float3 normal = float3(theta.y*phi.x,theta.x*phi.x,phi.y);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment