depthOfField.fragment.fx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // BABYLON.JS Depth-of-field GLSL Shader
  2. // Author: Olivier Guyot
  3. // Does depth-of-field blur, edge blur
  4. // Inspired by Francois Tarlier & Martins Upitis
  5. // samplers
  6. uniform sampler2D textureSampler;
  7. uniform sampler2D highlightsSampler;
  8. uniform sampler2D depthSampler;
  9. uniform sampler2D grainSampler;
  10. // uniforms
  11. uniform float grain_amount;
  12. uniform bool blur_noise;
  13. uniform float screen_width;
  14. uniform float screen_height;
  15. uniform float distortion;
  16. uniform bool dof_enabled;
  17. //uniform float focus_distance; // not needed; already used to compute screen distance
  18. uniform float screen_distance; // precomputed screen distance from lens center; based on focal length & desired focus distance
  19. uniform float aperture;
  20. uniform float darken;
  21. uniform float edge_blur;
  22. uniform bool highlights;
  23. // preconputed uniforms (not effect parameters)
  24. uniform float near;
  25. uniform float far;
  26. // varyings
  27. varying vec2 vUV;
  28. // constants
  29. #define PI 3.14159265
  30. #define TWOPI 6.28318530
  31. #define inverse_focal_length 0.1 // a property of the lens used
  32. // common calculations
  33. vec2 centered_screen_pos;
  34. vec2 distorted_coords;
  35. float radius2;
  36. float radius;
  37. // on-the-fly constant noise
  38. vec2 rand(vec2 co)
  39. {
  40. float noise1 = (fract(sin(dot(co, vec2(12.9898, 78.233))) * 43758.5453));
  41. float noise2 = (fract(sin(dot(co, vec2(12.9898, 78.233)*2.0)) * 43758.5453));
  42. return clamp(vec2(noise1, noise2), 0.0, 1.0);
  43. }
  44. // applies edge distortion on texture coords
  45. vec2 getDistortedCoords(vec2 coords) {
  46. if (distortion == 0.0) { return coords; }
  47. vec2 direction = 1.0 * normalize(centered_screen_pos);
  48. vec2 dist_coords = vec2(0.5, 0.5);
  49. dist_coords.x = 0.5 + direction.x * radius2 * 1.0;
  50. dist_coords.y = 0.5 + direction.y * radius2 * 1.0;
  51. float dist_amount = clamp(distortion*0.23, 0.0, 1.0);
  52. dist_coords = mix(coords, dist_coords, dist_amount);
  53. return dist_coords;
  54. }
  55. // sample screen with an offset (randomize offset angle for better smothness), returns partial sample weight
  56. float sampleScreen(inout vec4 color, const in vec2 offset, const in float weight) {
  57. // compute coords with offset (a random angle is added)
  58. vec2 coords = distorted_coords;
  59. float angle = rand(coords * 100.0).x * TWOPI;
  60. coords += vec2(offset.x * cos(angle) - offset.y * sin(angle), offset.x * sin(angle) + offset.y * cos(angle));
  61. color += texture2D(textureSampler, coords)*weight;
  62. return weight;
  63. }
  64. // returns blur level according to blur size required
  65. float getBlurLevel(float size) {
  66. return min(3.0, ceil(size / 1.0));
  67. }
  68. // returns original screen color after blur
  69. vec4 getBlurColor(float size) {
  70. vec4 col = texture2D(textureSampler, distorted_coords);
  71. if (size == 0.0) { return col; }
  72. // there are max. 30 samples; the number of samples chosen is dependant on the blur size
  73. // there can be 10, 20 or 30 samples chosen; levels of blur are then 1, 2 or 3
  74. float blur_level = getBlurLevel(size);
  75. float w = (size / screen_width);
  76. float h = (size / screen_height);
  77. float total_weight = 1.0;
  78. vec2 sample_coords;
  79. total_weight += sampleScreen(col, vec2(-0.50*w, 0.24*h), 0.93);
  80. total_weight += sampleScreen(col, vec2(0.30*w, -0.75*h), 0.90);
  81. total_weight += sampleScreen(col, vec2(0.36*w, 0.96*h), 0.87);
  82. total_weight += sampleScreen(col, vec2(-1.08*w, -0.55*h), 0.85);
  83. total_weight += sampleScreen(col, vec2(1.33*w, -0.37*h), 0.83);
  84. total_weight += sampleScreen(col, vec2(-0.82*w, 1.31*h), 0.80);
  85. total_weight += sampleScreen(col, vec2(-0.31*w, -1.67*h), 0.78);
  86. total_weight += sampleScreen(col, vec2(1.47*w, 1.11*h), 0.76);
  87. total_weight += sampleScreen(col, vec2(-1.97*w, 0.19*h), 0.74);
  88. total_weight += sampleScreen(col, vec2(1.42*w, -1.57*h), 0.72);
  89. if (blur_level > 1.0) {
  90. total_weight += sampleScreen(col, vec2(0.01*w, 2.25*h), 0.70);
  91. total_weight += sampleScreen(col, vec2(-1.62*w, -1.74*h), 0.67);
  92. total_weight += sampleScreen(col, vec2(2.49*w, 0.20*h), 0.65);
  93. total_weight += sampleScreen(col, vec2(-2.07*w, 1.61*h), 0.63);
  94. total_weight += sampleScreen(col, vec2(0.46*w, -2.70*h), 0.61);
  95. total_weight += sampleScreen(col, vec2(1.55*w, 2.40*h), 0.59);
  96. total_weight += sampleScreen(col, vec2(-2.88*w, -0.75*h), 0.56);
  97. total_weight += sampleScreen(col, vec2(2.73*w, -1.44*h), 0.54);
  98. total_weight += sampleScreen(col, vec2(-1.08*w, 3.02*h), 0.52);
  99. total_weight += sampleScreen(col, vec2(-1.28*w, -3.05*h), 0.49);
  100. }
  101. if (blur_level > 2.0) {
  102. total_weight += sampleScreen(col, vec2(3.11*w, 1.43*h), 0.46);
  103. total_weight += sampleScreen(col, vec2(-3.36*w, 1.08*h), 0.44);
  104. total_weight += sampleScreen(col, vec2(1.80*w, -3.16*h), 0.41);
  105. total_weight += sampleScreen(col, vec2(0.83*w, 3.65*h), 0.38);
  106. total_weight += sampleScreen(col, vec2(-3.16*w, -2.19*h), 0.34);
  107. total_weight += sampleScreen(col, vec2(3.92*w, -0.53*h), 0.31);
  108. total_weight += sampleScreen(col, vec2(-2.59*w, 3.12*h), 0.26);
  109. total_weight += sampleScreen(col, vec2(-0.20*w, -4.15*h), 0.22);
  110. total_weight += sampleScreen(col, vec2(3.02*w, 3.00*h), 0.15);
  111. }
  112. col /= total_weight; // scales color according to weights
  113. // darken if out of focus
  114. if (darken > 0.0) {
  115. col.rgb *= clamp(0.3, 1.0, 1.05 - size*0.5*darken);
  116. }
  117. // blur levels debug
  118. // if(blur_level == 1.0) { col.b *= 0.5; }
  119. // if(blur_level == 2.0) { col.r *= 0.5; }
  120. // if(blur_level == 3.0) { col.g *= 0.5; }
  121. return col;
  122. }
  123. void main(void)
  124. {
  125. // Common calc: position relative to screen center, screen radius, distorted coords, position in texel space
  126. centered_screen_pos = vec2(vUV.x - 0.5, vUV.y - 0.5);
  127. radius2 = centered_screen_pos.x*centered_screen_pos.x + centered_screen_pos.y*centered_screen_pos.y;
  128. radius = sqrt(radius2);
  129. distorted_coords = getDistortedCoords(vUV); // we distort the screen coordinates (lens "magnifying" effect)
  130. vec2 texels_coords = vec2(vUV.x * screen_width, vUV.y * screen_height); // varies from 0 to SCREEN_WIDTH or _HEIGHT
  131. float depth = texture2D(depthSampler, distorted_coords).r; // depth value from DepthRenderer: 0 to 1
  132. float distance = near + (far - near)*depth; // actual distance from the lens
  133. vec4 color = texture2D(textureSampler, vUV); // original raster
  134. // compute the circle of confusion size (CoC), i.e. blur radius depending on depth
  135. // screen_distance is precomputed in code
  136. float coc = abs(aperture * (screen_distance * (inverse_focal_length - 1.0 / distance) - 1.0));
  137. // disable blur
  138. if (dof_enabled == false || coc < 0.07) { coc = 0.0; }
  139. // blur from edge blur effect
  140. float edge_blur_amount = 0.0;
  141. if (edge_blur > 0.0) {
  142. edge_blur_amount = clamp((radius*2.0 - 1.0 + 0.15*edge_blur) * 1.5, 0.0, 1.0) * 1.3;
  143. }
  144. // total blur amount
  145. float blur_amount = max(edge_blur_amount, coc);
  146. // apply blur if necessary
  147. if (blur_amount == 0.0) {
  148. gl_FragColor = texture2D(textureSampler, distorted_coords);
  149. }
  150. else {
  151. // add blurred color
  152. gl_FragColor = getBlurColor(blur_amount * 1.7);
  153. // if we have computed highlights: enhance highlights
  154. if (highlights) {
  155. gl_FragColor.rgb += clamp(coc, 0.0, 1.0)*texture2D(highlightsSampler, distorted_coords).rgb;
  156. }
  157. if (blur_noise) {
  158. // we put a slight amount of noise in the blurred color
  159. vec2 noise = rand(distorted_coords) * 0.01 * blur_amount;
  160. vec2 blurred_coord = vec2(distorted_coords.x + noise.x, distorted_coords.y + noise.y);
  161. gl_FragColor = 0.04 * texture2D(textureSampler, blurred_coord) + 0.96 * gl_FragColor;
  162. }
  163. }
  164. // apply grain
  165. if (grain_amount > 0.0) {
  166. vec4 grain_color = texture2D(grainSampler, texels_coords*0.003);
  167. gl_FragColor.rgb += (-0.5 + grain_color.rgb) * 0.30 * grain_amount;
  168. }
  169. }