helperFunctions.fx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. const float PI = 3.1415926535897932384626433832795;
  2. const float LinearEncodePowerApprox = 2.2;
  3. const float GammaEncodePowerApprox = 1.0 / LinearEncodePowerApprox;
  4. const vec3 LuminanceEncodeApprox = vec3(0.2126, 0.7152, 0.0722);
  5. mat3 transposeMat3(mat3 inMatrix) {
  6. vec3 i0 = inMatrix[0];
  7. vec3 i1 = inMatrix[1];
  8. vec3 i2 = inMatrix[2];
  9. mat3 outMatrix = mat3(
  10. vec3(i0.x, i1.x, i2.x),
  11. vec3(i0.y, i1.y, i2.y),
  12. vec3(i0.z, i1.z, i2.z)
  13. );
  14. return outMatrix;
  15. }
  16. // https://github.com/glslify/glsl-inverse/blob/master/index.glsl
  17. mat3 inverseMat3(mat3 inMatrix) {
  18. float a00 = inMatrix[0][0], a01 = inMatrix[0][1], a02 = inMatrix[0][2];
  19. float a10 = inMatrix[1][0], a11 = inMatrix[1][1], a12 = inMatrix[1][2];
  20. float a20 = inMatrix[2][0], a21 = inMatrix[2][1], a22 = inMatrix[2][2];
  21. float b01 = a22 * a11 - a12 * a21;
  22. float b11 = -a22 * a10 + a12 * a20;
  23. float b21 = a21 * a10 - a11 * a20;
  24. float det = a00 * b01 + a01 * b11 + a02 * b21;
  25. return mat3(b01, (-a22 * a01 + a02 * a21), (a12 * a01 - a02 * a11),
  26. b11, (a22 * a00 - a02 * a20), (-a12 * a00 + a02 * a10),
  27. b21, (-a21 * a00 + a01 * a20), (a11 * a00 - a01 * a10)) / det;
  28. }
  29. float computeFallOff(float value, vec2 clipSpace, float frustumEdgeFalloff)
  30. {
  31. float mask = smoothstep(1.0 - frustumEdgeFalloff, 1.0, clamp(dot(clipSpace, clipSpace), 0., 1.));
  32. return mix(value, 1.0, mask);
  33. }
  34. vec3 applyEaseInOut(vec3 x){
  35. return x * x * (3.0 - 2.0 * x);
  36. }
  37. vec3 toLinearSpace(vec3 color)
  38. {
  39. return pow(color, vec3(LinearEncodePowerApprox));
  40. }
  41. vec3 toGammaSpace(vec3 color)
  42. {
  43. return pow(color, vec3(GammaEncodePowerApprox));
  44. }
  45. float square(float value)
  46. {
  47. return value * value;
  48. }
  49. float getLuminance(vec3 color)
  50. {
  51. return clamp(dot(color, LuminanceEncodeApprox), 0., 1.);
  52. }
  53. // https://stackoverflow.com/questions/4200224/random-noise-functions-for-glsl
  54. float getRand(vec2 seed) {
  55. return fract(sin(dot(seed.xy ,vec2(12.9898,78.233))) * 43758.5453);
  56. }
  57. float dither(vec2 seed, float varianceAmount) {
  58. float rand = getRand(seed);
  59. float dither = mix(-varianceAmount/255.0, varianceAmount/255.0, rand);
  60. return dither;
  61. }
  62. // Check if configurable value is needed.
  63. const float rgbdMaxRange = 255.0;
  64. vec4 toRGBD(vec3 color) {
  65. float maxRGB = max(0.0000001, max(color.r, max(color.g, color.b)));
  66. float D = max(rgbdMaxRange / maxRGB, 1.);
  67. D = clamp(floor(D) / 255.0, 0., 1.);
  68. // vec3 rgb = color.rgb * (D * (255.0 / rgbdMaxRange));
  69. vec3 rgb = color.rgb * D;
  70. // Helps with png quantization.
  71. rgb = toGammaSpace(rgb);
  72. return vec4(rgb, D);
  73. }
  74. vec3 fromRGBD(vec4 rgbd) {
  75. // Helps with png quantization.
  76. rgbd.rgb = toLinearSpace(rgbd.rgb);
  77. // return rgbd.rgb * ((rgbdMaxRange / 255.0) / rgbd.a);
  78. return rgbd.rgb / rgbd.a;
  79. }