PostProcess.glsl 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #ifdef COMPILEPS
  2. const float PI = 3.14159265;
  3. vec2 Noise(vec2 coord)
  4. {
  5. float noiseX = clamp(fract(sin(dot(coord, vec2(12.9898, 78.233))) * 43758.5453), 0.0, 1.0);
  6. float noiseY = clamp(fract(sin(dot(coord, vec2(12.9898, 78.233) * 2.0)) * 43758.5453), 0.0, 1.0);
  7. return vec2(noiseX, noiseY);
  8. }
  9. // Adapted: http://callumhay.blogspot.com/2010/09/gaussian-blur-shader-glsl.html
  10. vec4 GaussianBlur(int blurKernelSize, vec2 blurDir, vec2 blurRadius, float sigma, sampler2D texSampler, vec2 texCoord)
  11. {
  12. #if defined(GL_ES)
  13. // hardcoded for GL_ES to avoid loop comparison issue below
  14. const int blurKernelSizeHalfSize = 3 / 2;
  15. #else
  16. int blurKernelSizeHalfSize = blurKernelSize / 2;
  17. #endif
  18. // Incremental Gaussian Coefficent Calculation (See GPU Gems 3 pp. 877 - 889)
  19. vec3 gaussCoeff;
  20. gaussCoeff.x = 1.0 / (sqrt(2.0 * PI) * sigma);
  21. gaussCoeff.y = exp(-0.5 / (sigma * sigma));
  22. gaussCoeff.z = gaussCoeff.y * gaussCoeff.y;
  23. vec2 blurVec = blurRadius * blurDir;
  24. vec4 avgValue = vec4(0.0);
  25. float gaussCoeffSum = 0.0;
  26. avgValue += texture2D(texSampler, texCoord) * gaussCoeff.x;
  27. gaussCoeffSum += gaussCoeff.x;
  28. gaussCoeff.xy *= gaussCoeff.yz;
  29. for (int i = 1; i <= blurKernelSizeHalfSize; i++)
  30. {
  31. avgValue += texture2D(texSampler, texCoord - float(i) * blurVec) * gaussCoeff.x;
  32. avgValue += texture2D(texSampler, texCoord + float(i) * blurVec) * gaussCoeff.x;
  33. gaussCoeffSum += 2.0 * gaussCoeff.x;
  34. gaussCoeff.xy *= gaussCoeff.yz;
  35. }
  36. return avgValue / gaussCoeffSum;
  37. }
  38. const vec3 LumWeights = vec3(0.2126, 0.7152, 0.0722);
  39. vec3 ReinhardEq3Tonemap(vec3 x)
  40. {
  41. return x / (1.0 + x);
  42. }
  43. vec3 ReinhardEq4Tonemap(vec3 x, float white)
  44. {
  45. return x * (1.0 + x / white) / (1.0 + x);
  46. }
  47. // Unchared2 tone mapping (See http://filmicgames.com)
  48. const float A = 0.15;
  49. const float B = 0.50;
  50. const float C = 0.10;
  51. const float D = 0.20;
  52. const float E = 0.02;
  53. const float F = 0.30;
  54. vec3 Uncharted2Tonemap(vec3 x)
  55. {
  56. return ((x*(A*x+C*B)+D*E)/(x*(A*x+B)+D*F))-E/F;
  57. }
  58. #ifndef GL_ES
  59. vec3 ColorCorrection(vec3 color, sampler3D lut)
  60. {
  61. float lutSize = 16.0;
  62. float scale = (lutSize - 1.0) / lutSize;
  63. float offset = 1.0 / (2.0 * lutSize);
  64. return texture3D(lut, clamp(color, 0.0, 1.0) * scale + offset).rgb;
  65. }
  66. #endif
  67. const float Gamma = 2.2;
  68. const float InverseGamma = 1.0 / 2.2;
  69. vec3 ToGamma(vec3 color)
  70. {
  71. return vec3(pow(color.r, Gamma), pow(color.g, Gamma), pow(color.b, Gamma));
  72. }
  73. vec3 ToInverseGamma(vec3 color)
  74. {
  75. return vec3(pow(color.r, InverseGamma), pow(color.g, InverseGamma), pow(color.b, InverseGamma));
  76. }
  77. #endif