math.glsl 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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_PHI 1.6180339887498949
  14. #define M_GOLDEN_ANGLE 2.3999632297286535
  15. /* === Functions === */
  16. vec3 M_Rotate3D(vec3 v, vec4 q)
  17. {
  18. vec3 t = 2.0 * cross(q.xyz, v);
  19. return v + q.w * t + cross(q.xyz, t);
  20. }
  21. mat3 M_OrthonormalBasis(vec3 n)
  22. {
  23. // Previously we used Frisvad's method to generate a stable orthonormal basis
  24. // SEE: https://backend.orbit.dtu.dk/ws/portalfiles/portal/126824972/onb_frisvad_jgt2012_v2.pdf
  25. // However, it can cause visible artifacts (eg. bright pixels on the -Z face of irradiance cubemaps)
  26. // So now we use the revised method by Duff et al., it's more accurate, though slightly slower
  27. // SEE: https://graphics.pixar.com/library/OrthonormalB/paper.pdf
  28. float sgn = n.z >= 0.0 ? 1.0 : -1.0;
  29. float a = -1.0 / (sgn + n.z);
  30. float b = n.x * n.y * a;
  31. vec3 t = vec3(1.0 + sgn * n.x * n.x * a, sgn * b, -sgn * n.x);
  32. vec3 bt = vec3(b, sgn + n.y * n.y * a, -n.y);
  33. return mat3(t, bt, n);
  34. }
  35. vec2 M_OctahedronWrap(vec2 val)
  36. {
  37. // Reference(s):
  38. // - Octahedron normal vector encoding
  39. // https://web.archive.org/web/20191027010600/https://knarkowicz.wordpress.com/2014/04/16/octahedron-normal-vector-encoding/comment-page-1/
  40. return (1.0 - abs(val.yx)) * mix(vec2(-1.0), vec2(1.0), vec2(greaterThanEqual(val.xy, vec2(0.0))));
  41. }
  42. vec3 M_DecodeOctahedral(vec2 encoded)
  43. {
  44. encoded = encoded * 2.0 - 1.0;
  45. vec3 normal;
  46. normal.z = 1.0 - abs(encoded.x) - abs(encoded.y);
  47. normal.xy = normal.z >= 0.0 ? encoded.xy : M_OctahedronWrap(encoded.xy);
  48. return normalize(normal);
  49. }
  50. vec2 M_EncodeOctahedral(vec3 normal)
  51. {
  52. normal /= abs(normal.x) + abs(normal.y) + abs(normal.z);
  53. normal.xy = normal.z >= 0.0 ? normal.xy : M_OctahedronWrap(normal.xy);
  54. normal.xy = normal.xy * 0.5 + 0.5;
  55. return normal.xy;
  56. }
  57. vec3 M_NormalScale(vec3 normal, float scale)
  58. {
  59. normal.xy *= scale;
  60. normal.z = sqrt(1.0 - clamp(dot(normal.xy, normal.xy), 0.0, 1.0));
  61. return normal;
  62. }
  63. float M_HashIGN(vec2 pos)
  64. {
  65. // http://www.iryoku.com/next-generation-post-processing-in-call-of-duty-advanced-warfare
  66. const vec3 magic = vec3(0.06711056, 0.00583715, 52.9829189);
  67. return fract(magic.z * fract(dot(pos, magic.xy)));
  68. }
  69. float M_HashIGN(vec2 pos, float frame)
  70. {
  71. vec3 magic = vec3(0.06711056, 0.00583715, 52.9829189);
  72. return fract(magic.z * fract(dot(vec3(pos, frame), magic)));
  73. }