imageProcessingFunctions.fx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #if defined(COLORGRADING) && !defined(COLORGRADING3D)
  2. /**
  3. * Polyfill for SAMPLE_TEXTURE_3D, which is unsupported in WebGL.
  4. * sampler3dSetting.x = textureOffset (0.5 / textureSize).
  5. * sampler3dSetting.y = textureSize.
  6. */
  7. vec3 sampleTexture3D(sampler2D colorTransform, vec3 color, vec2 sampler3dSetting)
  8. {
  9. float sliceSize = 2.0 * sampler3dSetting.x; // Size of 1 slice relative to the texture, for example 1/8
  10. #ifdef SAMPLER3DGREENDEPTH
  11. float sliceContinuous = (color.g - sampler3dSetting.x) * sampler3dSetting.y;
  12. #else
  13. float sliceContinuous = (color.b - sampler3dSetting.x) * sampler3dSetting.y;
  14. #endif
  15. float sliceInteger = floor(sliceContinuous);
  16. // Note: this is mathematically equivalent to fract(sliceContinuous); but we use explicit subtract
  17. // rather than separate fract() for correct results near slice boundaries (matching sliceInteger choice)
  18. float sliceFraction = sliceContinuous - sliceInteger;
  19. #ifdef SAMPLER3DGREENDEPTH
  20. vec2 sliceUV = color.rb;
  21. #else
  22. vec2 sliceUV = color.rg;
  23. #endif
  24. sliceUV.x *= sliceSize;
  25. sliceUV.x += sliceInteger * sliceSize;
  26. sliceUV = clamp(sliceUV, 0., 1.);
  27. vec4 slice0Color = texture2D(colorTransform, sliceUV);
  28. sliceUV.x += sliceSize;
  29. sliceUV = clamp(sliceUV, 0., 1.);
  30. vec4 slice1Color = texture2D(colorTransform, sliceUV);
  31. vec3 result = mix(slice0Color.rgb, slice1Color.rgb, sliceFraction);
  32. #ifdef SAMPLER3DBGRMAP
  33. color.rgb = result.rgb;
  34. #else
  35. color.rgb = result.bgr;
  36. #endif
  37. return color;
  38. }
  39. #endif
  40. vec4 applyImageProcessing(vec4 result) {
  41. #ifdef EXPOSURE
  42. result.rgb *= exposureLinear;
  43. #endif
  44. #ifdef VIGNETTE
  45. //vignette
  46. vec2 viewportXY = gl_FragCoord.xy * vInverseScreenSize;
  47. viewportXY = viewportXY * 2.0 - 1.0;
  48. vec3 vignetteXY1 = vec3(viewportXY * vignetteSettings1.xy + vignetteSettings1.zw, 1.0);
  49. float vignetteTerm = dot(vignetteXY1, vignetteXY1);
  50. float vignette = pow(vignetteTerm, vignetteSettings2.w);
  51. // Interpolate between the artist 'color' and white based on the physical transmission value 'vignette'.
  52. vec3 vignetteColor = vignetteSettings2.rgb;
  53. #ifdef VIGNETTEBLENDMODEMULTIPLY
  54. vec3 vignetteColorMultiplier = mix(vignetteColor, vec3(1, 1, 1), vignette);
  55. result.rgb *= vignetteColorMultiplier;
  56. #endif
  57. #ifdef VIGNETTEBLENDMODEOPAQUE
  58. result.rgb = mix(vignetteColor, result.rgb, vignette);
  59. #endif
  60. #endif
  61. #ifdef TONEMAPPING
  62. const float tonemappingCalibration = 1.590579;
  63. result.rgb = 1.0 - exp2(-tonemappingCalibration * result.rgb);
  64. #endif
  65. #ifdef GRAIN
  66. vec2 seed = vUV*(grainAnimatedSeed);
  67. float grain = dither(seed, grainVarianceAmount);
  68. // Add less grain when luminance is high or low
  69. float lum = getLuminance(result.rgb);
  70. float grainAmount = (cos(-PI + (lum*PI*2.))+1.)/2.;
  71. result.rgb += grain * grainAmount;
  72. result.rgb = max(result.rgb, 0.0);
  73. #endif
  74. // Going back to gamma space
  75. result.rgb = toGammaSpace(result.rgb);
  76. result.rgb = clamp(result.rgb, 0.0, 1.0);
  77. #ifdef CONTRAST
  78. // Contrast
  79. vec3 resultHighContrast = applyEaseInOut(result.rgb);
  80. if (contrast < 1.0) {
  81. // Decrease contrast: interpolate towards zero-contrast image (flat grey)
  82. result.rgb = mix(vec3(0.5, 0.5, 0.5), result.rgb, contrast);
  83. } else {
  84. // Increase contrast: apply simple shoulder-toe high contrast curve
  85. result.rgb = mix(result.rgb, resultHighContrast, contrast - 1.0);
  86. }
  87. #endif
  88. // Apply Color Transform
  89. #ifdef COLORGRADING
  90. vec3 colorTransformInput = result.rgb * colorTransformSettings.xxx + colorTransformSettings.yyy;
  91. #ifdef COLORGRADING3D
  92. vec3 colorTransformOutput = texture(txColorTransform, colorTransformInput).rgb;
  93. #else
  94. vec3 colorTransformOutput = sampleTexture3D(txColorTransform, colorTransformInput, colorTransformSettings.yz).rgb;
  95. #endif
  96. result.rgb = mix(result.rgb, colorTransformOutput, colorTransformSettings.www);
  97. #endif
  98. #ifdef COLORCURVES
  99. // Apply Color Curves
  100. float luma = getLuminance(result.rgb);
  101. vec2 curveMix = clamp(vec2(luma * 3.0 - 1.5, luma * -3.0 + 1.5), vec2(0.0), vec2(1.0));
  102. vec4 colorCurve = vCameraColorCurveNeutral + curveMix.x * vCameraColorCurvePositive - curveMix.y * vCameraColorCurveNegative;
  103. result.rgb *= colorCurve.rgb;
  104. result.rgb = mix(vec3(luma), result.rgb, colorCurve.a);
  105. #endif
  106. return result;
  107. }