octahedron-transform.chunk 657 B

123456789101112131415161718
  1. // Returns ±1
  2. vec2 signNotZero(vec2 v) {
  3. return vec2((v.x >= 0.0) ? +1.0 : -1.0, (v.y >= 0.0) ? +1.0 : -1.0);
  4. }
  5. // Assume normalized input. Output is on [-1, 1] for each component.
  6. vec2 float32x3_to_oct(in vec3 v) {
  7. // Project the sphere onto the octahedron, and then onto the xy plane
  8. vec2 p = v.xy * (1.0 / (abs(v.x) + abs(v.y) + abs(v.z)));
  9. // Reflect the folds of the lower hemisphere over the diagonals
  10. return (v.z <= 0.0) ? ((1.0 - abs(p.yx)) * signNotZero(p)) : p;
  11. }
  12. vec3 oct_to_float32x3(vec2 e) {
  13. vec3 v = vec3(e.xy, 1.0 - abs(e.x) - abs(e.y));
  14. if (v.z < 0.0) v.xy = (1.0 - abs(v.yx)) * signNotZero(v.xy);
  15. return normalize(v);
  16. }