BokehShader2.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /**
  2. * @author zz85 / https://github.com/zz85 | twitter.com/blurspline
  3. *
  4. * Depth-of-field shader with bokeh
  5. * ported from GLSL shader by Martins Upitis
  6. * http://blenderartists.org/forum/showthread.php?237488-GLSL-depth-of-field-with-bokeh-v2-4-(update)
  7. *
  8. * Requires #define RINGS and SAMPLES integers
  9. */
  10. THREE.BokehShader = {
  11. uniforms: {
  12. "textureWidth": { value: 1.0 },
  13. "textureHeight": { value: 1.0 },
  14. "focalDepth": { value: 1.0 },
  15. "focalLength": { value: 24.0 },
  16. "fstop": { value: 0.9 },
  17. "tColor": { value: null },
  18. "tDepth": { value: null },
  19. "maxblur": { value: 1.0 },
  20. "manualdof": { value: 0 },
  21. "depthblur": { value: 0 },
  22. "threshold": { value: 0.5 },
  23. "gain": { value: 2.0 },
  24. "bias": { value: 0.5 },
  25. "fringe": { value: 0.7 },
  26. "znear": { value: 0.1 },
  27. "zfar": { value: 100 },
  28. "noise": { value: 1 },
  29. "dithering": { value: 0.0001 },
  30. "pentagon": { value: 0 },
  31. "shaderFocus": { value: 1 },
  32. "focusCoords": { value: new THREE.Vector2() }
  33. },
  34. vertexShader: [
  35. "varying vec2 vUv;",
  36. "void main() {",
  37. "vUv = uv;",
  38. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  39. "}"
  40. ].join( "\n" ),
  41. fragmentShader: [
  42. "#include <common>",
  43. "#include <packing>",
  44. "varying vec2 vUv;",
  45. "uniform sampler2D tColor;",
  46. "uniform sampler2D tDepth;",
  47. "uniform float textureWidth;",
  48. "uniform float textureHeight;",
  49. "uniform float focalDepth; //focal distance value in meters, but you may use autofocus option below",
  50. "uniform float focalLength; //focal length in mm",
  51. "uniform float fstop; //f-stop value",
  52. "/*",
  53. "make sure that these two values are the same for your camera, otherwise distances will be wrong.",
  54. "*/",
  55. "uniform float znear; // camera clipping start",
  56. "uniform float zfar; // camera clipping end",
  57. "//------------------------------------------",
  58. "//user variables",
  59. "const int samples = SAMPLES; //samples on the first ring",
  60. "const int rings = RINGS; //ring count",
  61. "const int maxringsamples = rings * samples;",
  62. "uniform bool manualdof; // manual dof calculation",
  63. "float ndofstart = 1.0; // near dof blur start",
  64. "float ndofdist = 2.0; // near dof blur falloff distance",
  65. "float fdofstart = 1.0; // far dof blur start",
  66. "float fdofdist = 3.0; // far dof blur falloff distance",
  67. "float CoC = 0.03; //circle of confusion size in mm (35mm film = 0.03mm)",
  68. "uniform bool shaderFocus;",
  69. "// disable if you use external focalDepth value",
  70. "uniform vec2 focusCoords;",
  71. "// autofocus point on screen (0.0,0.0 - left lower corner, 1.0,1.0 - upper right)",
  72. "// if center of screen use vec2(0.5, 0.5);",
  73. "uniform float maxblur;",
  74. "//clamp value of max blur (0.0 = no blur, 1.0 default)",
  75. "uniform float threshold; // highlight threshold;",
  76. "uniform float gain; // highlight gain;",
  77. "uniform float bias; // bokeh edge bias",
  78. "uniform float fringe; // bokeh chromatic aberration / fringing",
  79. "uniform bool noise; //use noise instead of pattern for sample dithering",
  80. "uniform float dithering;",
  81. "uniform bool depthblur; // blur the depth buffer",
  82. "float dbsize = 1.25; // depth blur size",
  83. "/*",
  84. "next part is experimental",
  85. "not looking good with small sample and ring count",
  86. "looks okay starting from samples = 4, rings = 4",
  87. "*/",
  88. "uniform bool pentagon; //use pentagon as bokeh shape?",
  89. "float feather = 0.4; //pentagon shape feather",
  90. "//------------------------------------------",
  91. "float getDepth( const in vec2 screenPosition ) {",
  92. " #if DEPTH_PACKING == 1",
  93. " return unpackRGBAToDepth( texture2D( tDepth, screenPosition ) );",
  94. " #else",
  95. " return texture2D( tDepth, screenPosition ).x;",
  96. " #endif",
  97. "}",
  98. "float getViewZ( const in float depth ) {",
  99. " #if PERSPECTIVE_CAMERA == 1",
  100. " return perspectiveDepthToViewZ( depth, znear, zfar );",
  101. " #else",
  102. " return orthographicDepthToViewZ( depth, znear, zfar );",
  103. " #endif",
  104. "}",
  105. "float penta(vec2 coords) {",
  106. "//pentagonal shape",
  107. "float scale = float(rings) - 1.3;",
  108. "vec4 HS0 = vec4( 1.0, 0.0, 0.0, 1.0);",
  109. "vec4 HS1 = vec4( 0.309016994, 0.951056516, 0.0, 1.0);",
  110. "vec4 HS2 = vec4(-0.809016994, 0.587785252, 0.0, 1.0);",
  111. "vec4 HS3 = vec4(-0.809016994,-0.587785252, 0.0, 1.0);",
  112. "vec4 HS4 = vec4( 0.309016994,-0.951056516, 0.0, 1.0);",
  113. "vec4 HS5 = vec4( 0.0 ,0.0 , 1.0, 1.0);",
  114. "vec4 one = vec4( 1.0 );",
  115. "vec4 P = vec4((coords),vec2(scale, scale));",
  116. "vec4 dist = vec4(0.0);",
  117. "float inorout = -4.0;",
  118. "dist.x = dot( P, HS0 );",
  119. "dist.y = dot( P, HS1 );",
  120. "dist.z = dot( P, HS2 );",
  121. "dist.w = dot( P, HS3 );",
  122. "dist = smoothstep( -feather, feather, dist );",
  123. "inorout += dot( dist, one );",
  124. "dist.x = dot( P, HS4 );",
  125. "dist.y = HS5.w - abs( P.z );",
  126. "dist = smoothstep( -feather, feather, dist );",
  127. "inorout += dist.x;",
  128. "return clamp( inorout, 0.0, 1.0 );",
  129. "}",
  130. "float bdepth(vec2 coords) {",
  131. "// Depth buffer blur",
  132. "float d = 0.0;",
  133. "float kernel[9];",
  134. "vec2 offset[9];",
  135. "vec2 wh = vec2(1.0/textureWidth,1.0/textureHeight) * dbsize;",
  136. "offset[0] = vec2(-wh.x,-wh.y);",
  137. "offset[1] = vec2( 0.0, -wh.y);",
  138. "offset[2] = vec2( wh.x -wh.y);",
  139. "offset[3] = vec2(-wh.x, 0.0);",
  140. "offset[4] = vec2( 0.0, 0.0);",
  141. "offset[5] = vec2( wh.x, 0.0);",
  142. "offset[6] = vec2(-wh.x, wh.y);",
  143. "offset[7] = vec2( 0.0, wh.y);",
  144. "offset[8] = vec2( wh.x, wh.y);",
  145. "kernel[0] = 1.0/16.0; kernel[1] = 2.0/16.0; kernel[2] = 1.0/16.0;",
  146. "kernel[3] = 2.0/16.0; kernel[4] = 4.0/16.0; kernel[5] = 2.0/16.0;",
  147. "kernel[6] = 1.0/16.0; kernel[7] = 2.0/16.0; kernel[8] = 1.0/16.0;",
  148. "for( int i=0; i<9; i++ ) {",
  149. "float tmp = getDepth( coords + offset[ i ] );",
  150. "d += tmp * kernel[i];",
  151. "}",
  152. "return d;",
  153. "}",
  154. "vec3 color(vec2 coords,float blur) {",
  155. "//processing the sample",
  156. "vec3 col = vec3(0.0);",
  157. "vec2 texel = vec2(1.0/textureWidth,1.0/textureHeight);",
  158. "col.r = texture2D(tColor,coords + vec2(0.0,1.0)*texel*fringe*blur).r;",
  159. "col.g = texture2D(tColor,coords + vec2(-0.866,-0.5)*texel*fringe*blur).g;",
  160. "col.b = texture2D(tColor,coords + vec2(0.866,-0.5)*texel*fringe*blur).b;",
  161. "vec3 lumcoeff = vec3(0.299,0.587,0.114);",
  162. "float lum = dot(col.rgb, lumcoeff);",
  163. "float thresh = max((lum-threshold)*gain, 0.0);",
  164. "return col+mix(vec3(0.0),col,thresh*blur);",
  165. "}",
  166. "float gather(float i, float j, int ringsamples, inout vec3 col, float w, float h, float blur) {",
  167. "float rings2 = float(rings);",
  168. "float step = PI*2.0 / float(ringsamples);",
  169. "float pw = cos(j*step)*i;",
  170. "float ph = sin(j*step)*i;",
  171. "float p = 1.0;",
  172. "if (pentagon) {",
  173. "p = penta(vec2(pw,ph));",
  174. "}",
  175. "col += color(vUv.xy + vec2(pw*w,ph*h), blur) * mix(1.0, i/rings2, bias) * p;",
  176. "return 1.0 * mix(1.0, i /rings2, bias) * p;",
  177. "}",
  178. "void main() {",
  179. "//scene depth calculation",
  180. "float depth = getViewZ( getDepth( vUv.xy ) );",
  181. "// Blur depth?",
  182. "if (depthblur) {",
  183. "depth = getViewZ(bdepth(vUv.xy));",
  184. "}",
  185. "//focal plane calculation",
  186. "float fDepth = - focalDepth;", // viewZ is negative
  187. "if (shaderFocus) {",
  188. "fDepth = getViewZ( getDepth( focusCoords ) );",
  189. "}",
  190. "// dof blur factor calculation",
  191. "float blur = 0.0;",
  192. "if (manualdof) {",
  193. "float a = fDepth + depth; // Focal plane",
  194. "float b = (a-fdofstart)/fdofdist; // Far DoF",
  195. "float c = (-a-ndofstart)/ndofdist; // Near Dof",
  196. "blur = (a>0.0) ? b : c;",
  197. "} else {",
  198. "float f = focalLength;",
  199. "float d = fDepth;",
  200. "float o = depth;",
  201. "float a = (o*f)/(f+o);",
  202. "float b = (d*f)/(f+d);",
  203. "float c = (f+d)/(d*fstop*CoC);",
  204. "blur = abs(a-b)*c;",
  205. "}",
  206. "blur = clamp(blur,0.0,1.0);",
  207. "// calculation of pattern for dithering",
  208. "vec2 noise = vec2(rand(vUv.xy), rand( vUv.xy + vec2( 0.4, 0.6 ) ) )*dithering*blur;",
  209. "// getting blur x and y step factor",
  210. "float w = (1.0/textureWidth)*blur*maxblur+noise.x;",
  211. "float h = (1.0/textureHeight)*blur*maxblur+noise.y;",
  212. "// calculation of final color",
  213. "vec3 col = vec3(0.0);",
  214. "if(blur < 0.05) {",
  215. "//some optimization thingy",
  216. "col = texture2D(tColor, vUv.xy).rgb;",
  217. "} else {",
  218. "col = texture2D(tColor, vUv.xy).rgb;",
  219. "float s = 1.0;",
  220. "int ringsamples;",
  221. "for (int i = 1; i <= rings; i++) {",
  222. "/*unboxstart*/",
  223. "ringsamples = i * samples;",
  224. "for (int j = 0 ; j < maxringsamples ; j++) {",
  225. "if (j >= ringsamples) break;",
  226. "s += gather(float(i), float(j), ringsamples, col, w, h, blur);",
  227. "}",
  228. "/*unboxend*/",
  229. "}",
  230. "col /= s; //divide by sample count",
  231. "}",
  232. "gl_FragColor.rgb = col;",
  233. "gl_FragColor.a = 1.0;",
  234. "} "
  235. ].join( "\n" )
  236. };