imageProcessingFunctions.fx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 = saturate(sliceUV);
  27. vec4 slice0Color = texture2D(colorTransform, sliceUV);
  28. sliceUV.x += sliceSize;
  29. sliceUV = saturate(sliceUV);
  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. #ifdef TONEMAPPING_ACES
  41. // https://github.com/TheRealMJP/BakingLab/blob/master/BakingLab/ACES.hlsl
  42. // Thanks to MJP for all the invaluable tricks found in his repo.
  43. // As stated there, the code in this section was originally written by Stephen Hill (@self_shadow), who deserves all
  44. // credit for coming up with this fit and implementing it. Buy him a beer next time you see him. :)
  45. // sRGB => XYZ => D65_2_D60 => AP1 => RRT_SAT
  46. const mat3 ACESInputMat = mat3(
  47. vec3(0.59719, 0.07600, 0.02840),
  48. vec3(0.35458, 0.90834, 0.13383),
  49. vec3(0.04823, 0.01566, 0.83777)
  50. );
  51. // ODT_SAT => XYZ => D60_2_D65 => sRGB
  52. const mat3 ACESOutputMat = mat3(
  53. vec3( 1.60475, -0.10208, -0.00327),
  54. vec3(-0.53108, 1.10813, -0.07276),
  55. vec3(-0.07367, -0.00605, 1.07602)
  56. );
  57. vec3 RRTAndODTFit(vec3 v)
  58. {
  59. vec3 a = v * (v + 0.0245786) - 0.000090537;
  60. vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081;
  61. return a / b;
  62. }
  63. vec3 ACESFitted(vec3 color)
  64. {
  65. color = ACESInputMat * color;
  66. // Apply RRT and ODT
  67. color = RRTAndODTFit(color);
  68. color = ACESOutputMat * color;
  69. // Clamp to [0, 1]
  70. color = saturate(color);
  71. return color;
  72. }
  73. #endif
  74. vec4 applyImageProcessing(vec4 result) {
  75. #ifdef EXPOSURE
  76. result.rgb *= exposureLinear;
  77. #endif
  78. #ifdef VIGNETTE
  79. //vignette
  80. vec2 viewportXY = gl_FragCoord.xy * vInverseScreenSize;
  81. viewportXY = viewportXY * 2.0 - 1.0;
  82. vec3 vignetteXY1 = vec3(viewportXY * vignetteSettings1.xy + vignetteSettings1.zw, 1.0);
  83. float vignetteTerm = dot(vignetteXY1, vignetteXY1);
  84. float vignette = pow(vignetteTerm, vignetteSettings2.w);
  85. // Interpolate between the artist 'color' and white based on the physical transmission value 'vignette'.
  86. vec3 vignetteColor = vignetteSettings2.rgb;
  87. #ifdef VIGNETTEBLENDMODEMULTIPLY
  88. vec3 vignetteColorMultiplier = mix(vignetteColor, vec3(1, 1, 1), vignette);
  89. result.rgb *= vignetteColorMultiplier;
  90. #endif
  91. #ifdef VIGNETTEBLENDMODEOPAQUE
  92. result.rgb = mix(vignetteColor, result.rgb, vignette);
  93. #endif
  94. #endif
  95. #ifdef TONEMAPPING
  96. #ifdef TONEMAPPING_ACES
  97. result.rgb = ACESFitted(result.rgb);
  98. #else
  99. const float tonemappingCalibration = 1.590579;
  100. result.rgb = 1.0 - exp2(-tonemappingCalibration * result.rgb);
  101. #endif
  102. #endif
  103. // Going back to gamma space
  104. result.rgb = toGammaSpace(result.rgb);
  105. result.rgb = saturate(result.rgb);
  106. #ifdef CONTRAST
  107. // Contrast EaseInOut
  108. vec3 resultHighContrast = result.rgb * result.rgb * (3.0 - 2.0 * result.rgb);
  109. if (contrast < 1.0) {
  110. // Decrease contrast: interpolate towards zero-contrast image (flat grey)
  111. result.rgb = mix(vec3(0.5, 0.5, 0.5), result.rgb, contrast);
  112. } else {
  113. // Increase contrast: apply simple shoulder-toe high contrast curve
  114. result.rgb = mix(result.rgb, resultHighContrast, contrast - 1.0);
  115. }
  116. #endif
  117. // Apply Color Transform
  118. #ifdef COLORGRADING
  119. vec3 colorTransformInput = result.rgb * colorTransformSettings.xxx + colorTransformSettings.yyy;
  120. #ifdef COLORGRADING3D
  121. vec3 colorTransformOutput = texture(txColorTransform, colorTransformInput).rgb;
  122. #else
  123. vec3 colorTransformOutput = sampleTexture3D(txColorTransform, colorTransformInput, colorTransformSettings.yz).rgb;
  124. #endif
  125. result.rgb = mix(result.rgb, colorTransformOutput, colorTransformSettings.www);
  126. #endif
  127. #ifdef COLORCURVES
  128. // Apply Color Curves
  129. float luma = getLuminance(result.rgb);
  130. vec2 curveMix = clamp(vec2(luma * 3.0 - 1.5, luma * -3.0 + 1.5), vec2(0.0), vec2(1.0));
  131. vec4 colorCurve = vCameraColorCurveNeutral + curveMix.x * vCameraColorCurvePositive - curveMix.y * vCameraColorCurveNegative;
  132. result.rgb *= colorCurve.rgb;
  133. result.rgb = mix(vec3(luma), result.rgb, colorCurve.a);
  134. #endif
  135. return result;
  136. }