imageProcessingFunctions.fx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. // Going back to gamma space
  66. result.rgb = toGammaSpace(result.rgb);
  67. result.rgb = clamp(result.rgb, 0.0, 1.0);
  68. #ifdef CONTRAST
  69. // Contrast
  70. vec3 resultHighContrast = applyEaseInOut(result.rgb);
  71. if (contrast < 1.0) {
  72. // Decrease contrast: interpolate towards zero-contrast image (flat grey)
  73. result.rgb = mix(vec3(0.5, 0.5, 0.5), result.rgb, contrast);
  74. } else {
  75. // Increase contrast: apply simple shoulder-toe high contrast curve
  76. result.rgb = mix(result.rgb, resultHighContrast, contrast - 1.0);
  77. }
  78. #endif
  79. // Apply Color Transform
  80. #ifdef COLORGRADING
  81. vec3 colorTransformInput = result.rgb * colorTransformSettings.xxx + colorTransformSettings.yyy;
  82. #ifdef COLORGRADING3D
  83. vec3 colorTransformOutput = texture(txColorTransform, colorTransformInput).rgb;
  84. #else
  85. vec3 colorTransformOutput = sampleTexture3D(txColorTransform, colorTransformInput, colorTransformSettings.yz).rgb;
  86. #endif
  87. result.rgb = mix(result.rgb, colorTransformOutput, colorTransformSettings.www);
  88. #endif
  89. #ifdef COLORCURVES
  90. // Apply Color Curves
  91. float luma = getLuminance(result.rgb);
  92. vec2 curveMix = clamp(vec2(luma * 3.0 - 1.5, luma * -3.0 + 1.5), vec2(0.0), vec2(1.0));
  93. vec4 colorCurve = vCameraColorCurveNeutral + curveMix.x * vCameraColorCurvePositive - curveMix.y * vCameraColorCurveNegative;
  94. result.rgb *= colorCurve.rgb;
  95. result.rgb = mix(vec3(luma), result.rgb, colorCurve.a);
  96. #endif
  97. return result;
  98. }