math.glsl 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* math.glsl -- Contains everything you need for maths
  2. *
  3. * Copyright (c) 2025-2026 Le Juez Victor
  4. *
  5. * This software is provided 'as-is', without any express or implied warranty.
  6. * For conditions of distribution and use, see accompanying LICENSE file.
  7. */
  8. /* === Constants === */
  9. #define M_PI 3.1415926535897931
  10. #define M_HPI 1.5707963267948966
  11. #define M_TAU 6.2831853071795862
  12. #define M_INV_PI 0.3183098861837907
  13. #define M_GOLDEN_ANGLE 2.3999632297286535
  14. /* === Functions === */
  15. vec3 M_Rotate3D(vec3 v, vec4 q)
  16. {
  17. vec3 t = 2.0 * cross(q.xyz, v);
  18. return v + q.w * t + cross(q.xyz, t);
  19. }
  20. mat3 M_OrthonormalBasis(vec3 n)
  21. {
  22. // Previously we used Frisvad's method to generate a stable orthonormal basis
  23. // SEE: https://backend.orbit.dtu.dk/ws/portalfiles/portal/126824972/onb_frisvad_jgt2012_v2.pdf
  24. // However, it can cause visible artifacts (eg. bright pixels on the -Z face of irradiance cubemaps)
  25. // So now we use the revised method by Duff et al., it's more accurate, though slightly slower
  26. // SEE: https://graphics.pixar.com/library/OrthonormalB/paper.pdf
  27. float sgn = n.z >= 0.0 ? 1.0 : -1.0;
  28. float a = -1.0 / (sgn + n.z);
  29. float b = n.x * n.y * a;
  30. vec3 t = vec3(1.0 + sgn * n.x * n.x * a, sgn * b, -sgn * n.x);
  31. vec3 bt = vec3(b, sgn + n.y * n.y * a, -n.y);
  32. return mat3(t, bt, n);
  33. }
  34. vec2 M_OctahedronWrap(vec2 val)
  35. {
  36. // Reference(s):
  37. // - Octahedron normal vector encoding
  38. // https://web.archive.org/web/20191027010600/https://knarkowicz.wordpress.com/2014/04/16/octahedron-normal-vector-encoding/comment-page-1/
  39. return (1.0 - abs(val.yx)) * mix(vec2(-1.0), vec2(1.0), vec2(greaterThanEqual(val.xy, vec2(0.0))));
  40. }
  41. vec3 M_DecodeOctahedral(vec2 encoded)
  42. {
  43. encoded = encoded * 2.0 - 1.0;
  44. vec3 normal;
  45. normal.z = 1.0 - abs(encoded.x) - abs(encoded.y);
  46. normal.xy = normal.z >= 0.0 ? encoded.xy : M_OctahedronWrap(encoded.xy);
  47. return normalize(normal);
  48. }
  49. vec2 M_EncodeOctahedral(vec3 normal)
  50. {
  51. normal /= abs(normal.x) + abs(normal.y) + abs(normal.z);
  52. normal.xy = normal.z >= 0.0 ? normal.xy : M_OctahedronWrap(normal.xy);
  53. normal.xy = normal.xy * 0.5 + 0.5;
  54. return normal.xy;
  55. }
  56. vec3 M_NormalScale(vec3 normal, float scale)
  57. {
  58. normal.xy *= scale;
  59. normal.z = sqrt(1.0 - clamp(dot(normal.xy, normal.xy), 0.0, 1.0));
  60. return normal;
  61. }
  62. float M_HashIGN(vec2 pos)
  63. {
  64. // http://www.iryoku.com/next-generation-post-processing-in-call-of-duty-advanced-warfare
  65. const vec3 magic = vec3(0.06711056, 0.00583715, 52.9829189);
  66. return fract(magic.z * fract(dot(pos, magic.xy)));
  67. }