BokehShader2.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. THREE.BokehShader = {
  9. uniforms: {
  10. "textureWidth": { type: "f", value: 1.0 },
  11. "textureHeight": { type: "f", value: 1.0 },
  12. "focalDepth": { type: "f", value: 1.0 },
  13. "focalLength": { type: "f", value: 24.0 },
  14. "fstop": { type: "f", value: 0.9 },
  15. "tColor": { type: "t", value: null },
  16. "tDepth": { type: "t", value: null },
  17. "maxblur": { type: "f", value: 1.0 },
  18. "showFocus": { type: "i", value: 0 },
  19. "manualdof": { type: "i", value: 0 },
  20. "vignetting": { type: "i", value: 0 },
  21. "depthblur": { type: "i", value: 0 },
  22. "threshold": { type: "f", value: 0.5 },
  23. "gain": { type: "f", value: 2.0 },
  24. "bias": { type: "f", value: 0.5 },
  25. "fringe": { type: "f", value: 0.7 },
  26. "znear": { type: "f", value: 0.1 },
  27. "zfar": { type: "f", value: 100 },
  28. "noise": { type: "i", value: 1 },
  29. "dithering": { type: "f", value: 0.0001 },
  30. "pentagon": { type: "i", value: 0 },
  31. "shaderFocus": { type: "i", value: 1 },
  32. "focusCoords": { type: "v2", 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. "varying vec2 vUv;",
  43. "uniform sampler2D tColor;",
  44. "uniform sampler2D tDepth;",
  45. "uniform float textureWidth;",
  46. "uniform float textureHeight;",
  47. "const float PI = 3.14159265;",
  48. "float width = textureWidth; //texture width",
  49. "float height = textureHeight; //texture height",
  50. "vec2 texel = vec2(1.0/width,1.0/height);",
  51. "uniform float focalDepth; //focal distance value in meters, but you may use autofocus option below",
  52. "uniform float focalLength; //focal length in mm",
  53. "uniform float fstop; //f-stop value",
  54. "uniform bool showFocus; //show debug focus point and focal range (red = focal point, green = focal range)",
  55. "/*",
  56. "make sure that these two values are the same for your camera, otherwise distances will be wrong.",
  57. "*/",
  58. "uniform float znear; // camera clipping start",
  59. "uniform float zfar; // camera clipping end",
  60. "//------------------------------------------",
  61. "//user variables",
  62. "const int samples = #SAMPLES#; //samples on the first ring",
  63. "const int rings = #RINGS#; //ring count",
  64. "const int maxringsamples = rings * samples;",
  65. "uniform bool manualdof; // manual dof calculation",
  66. "float ndofstart = 1.0; // near dof blur start",
  67. "float ndofdist = 2.0; // near dof blur falloff distance",
  68. "float fdofstart = 1.0; // far dof blur start",
  69. "float fdofdist = 3.0; // far dof blur falloff distance",
  70. "float CoC = 0.03; //circle of confusion size in mm (35mm film = 0.03mm)",
  71. "uniform bool vignetting; // use optical lens vignetting",
  72. "float vignout = 1.3; // vignetting outer border",
  73. "float vignin = 0.0; // vignetting inner border",
  74. "float vignfade = 22.0; // f-stops till vignete fades",
  75. "uniform bool shaderFocus;",
  76. "bool autofocus = shaderFocus;",
  77. "//use autofocus in shader - use with focusCoords",
  78. "// disable if you use external focalDepth value",
  79. "uniform vec2 focusCoords;",
  80. "// autofocus point on screen (0.0,0.0 - left lower corner, 1.0,1.0 - upper right)",
  81. "// if center of screen use vec2(0.5, 0.5);",
  82. "uniform float maxblur;",
  83. "//clamp value of max blur (0.0 = no blur, 1.0 default)",
  84. "uniform float threshold; // highlight threshold;",
  85. "uniform float gain; // highlight gain;",
  86. "uniform float bias; // bokeh edge bias",
  87. "uniform float fringe; // bokeh chromatic aberration / fringing",
  88. "uniform bool noise; //use noise instead of pattern for sample dithering",
  89. "uniform float dithering;",
  90. "float namount = dithering; //dither amount",
  91. "uniform bool depthblur; // blur the depth buffer",
  92. "float dbsize = 1.25; // depth blur size",
  93. "/*",
  94. "next part is experimental",
  95. "not looking good with small sample and ring count",
  96. "looks okay starting from samples = 4, rings = 4",
  97. "*/",
  98. "uniform bool pentagon; //use pentagon as bokeh shape?",
  99. "float feather = 0.4; //pentagon shape feather",
  100. "//------------------------------------------",
  101. "float penta(vec2 coords) {",
  102. "//pentagonal shape",
  103. "float scale = float(rings) - 1.3;",
  104. "vec4 HS0 = vec4( 1.0, 0.0, 0.0, 1.0);",
  105. "vec4 HS1 = vec4( 0.309016994, 0.951056516, 0.0, 1.0);",
  106. "vec4 HS2 = vec4(-0.809016994, 0.587785252, 0.0, 1.0);",
  107. "vec4 HS3 = vec4(-0.809016994,-0.587785252, 0.0, 1.0);",
  108. "vec4 HS4 = vec4( 0.309016994,-0.951056516, 0.0, 1.0);",
  109. "vec4 HS5 = vec4( 0.0 ,0.0 , 1.0, 1.0);",
  110. "vec4 one = vec4( 1.0 );",
  111. "vec4 P = vec4((coords),vec2(scale, scale));",
  112. "vec4 dist = vec4(0.0);",
  113. "float inorout = -4.0;",
  114. "dist.x = dot( P, HS0 );",
  115. "dist.y = dot( P, HS1 );",
  116. "dist.z = dot( P, HS2 );",
  117. "dist.w = dot( P, HS3 );",
  118. "dist = smoothstep( -feather, feather, dist );",
  119. "inorout += dot( dist, one );",
  120. "dist.x = dot( P, HS4 );",
  121. "dist.y = HS5.w - abs( P.z );",
  122. "dist = smoothstep( -feather, feather, dist );",
  123. "inorout += dist.x;",
  124. "return clamp( inorout, 0.0, 1.0 );",
  125. "}",
  126. "float bdepth(vec2 coords) {",
  127. "// Depth buffer blur",
  128. "float d = 0.0;",
  129. "float kernel[9];",
  130. "vec2 offset[9];",
  131. "vec2 wh = vec2(texel.x, texel.y) * dbsize;",
  132. "offset[0] = vec2(-wh.x,-wh.y);",
  133. "offset[1] = vec2( 0.0, -wh.y);",
  134. "offset[2] = vec2( wh.x -wh.y);",
  135. "offset[3] = vec2(-wh.x, 0.0);",
  136. "offset[4] = vec2( 0.0, 0.0);",
  137. "offset[5] = vec2( wh.x, 0.0);",
  138. "offset[6] = vec2(-wh.x, wh.y);",
  139. "offset[7] = vec2( 0.0, wh.y);",
  140. "offset[8] = vec2( wh.x, wh.y);",
  141. "kernel[0] = 1.0/16.0; kernel[1] = 2.0/16.0; kernel[2] = 1.0/16.0;",
  142. "kernel[3] = 2.0/16.0; kernel[4] = 4.0/16.0; kernel[5] = 2.0/16.0;",
  143. "kernel[6] = 1.0/16.0; kernel[7] = 2.0/16.0; kernel[8] = 1.0/16.0;",
  144. "for( int i=0; i<9; i++ ) {",
  145. "float tmp = texture2D(tDepth, coords + offset[i]).r;",
  146. "d += tmp * kernel[i];",
  147. "}",
  148. "return d;",
  149. "}",
  150. "vec3 color(vec2 coords,float blur) {",
  151. "//processing the sample",
  152. "vec3 col = vec3(0.0);",
  153. "col.r = texture2D(tColor,coords + vec2(0.0,1.0)*texel*fringe*blur).r;",
  154. "col.g = texture2D(tColor,coords + vec2(-0.866,-0.5)*texel*fringe*blur).g;",
  155. "col.b = texture2D(tColor,coords + vec2(0.866,-0.5)*texel*fringe*blur).b;",
  156. "vec3 lumcoeff = vec3(0.299,0.587,0.114);",
  157. "float lum = dot(col.rgb, lumcoeff);",
  158. "float thresh = max((lum-threshold)*gain, 0.0);",
  159. "return col+mix(vec3(0.0),col,thresh*blur);",
  160. "}",
  161. "vec2 rand(vec2 coord) {",
  162. "// generating noise / pattern texture for dithering",
  163. "float noiseX = ((fract(1.0-coord.s*(width/2.0))*0.25)+(fract(coord.t*(height/2.0))*0.75))*2.0-1.0;",
  164. "float noiseY = ((fract(1.0-coord.s*(width/2.0))*0.75)+(fract(coord.t*(height/2.0))*0.25))*2.0-1.0;",
  165. "if (noise) {",
  166. "noiseX = clamp(fract(sin(dot(coord ,vec2(12.9898,78.233))) * 43758.5453),0.0,1.0)*2.0-1.0;",
  167. "noiseY = clamp(fract(sin(dot(coord ,vec2(12.9898,78.233)*2.0)) * 43758.5453),0.0,1.0)*2.0-1.0;",
  168. "}",
  169. "return vec2(noiseX,noiseY);",
  170. "}",
  171. "vec3 debugFocus(vec3 col, float blur, float depth) {",
  172. "float edge = 0.002*depth; //distance based edge smoothing",
  173. "float m = clamp(smoothstep(0.0,edge,blur),0.0,1.0);",
  174. "float e = clamp(smoothstep(1.0-edge,1.0,blur),0.0,1.0);",
  175. "col = mix(col,vec3(1.0,0.5,0.0),(1.0-m)*0.6);",
  176. "col = mix(col,vec3(0.0,0.5,1.0),((1.0-e)-(1.0-m))*0.2);",
  177. "return col;",
  178. "}",
  179. "float linearize(float depth) {",
  180. "return -zfar * znear / (depth * (zfar - znear) - zfar);",
  181. "}",
  182. "float vignette() {",
  183. "float dist = distance(vUv.xy, vec2(0.5,0.5));",
  184. "dist = smoothstep(vignout+(fstop/vignfade), vignin+(fstop/vignfade), dist);",
  185. "return clamp(dist,0.0,1.0);",
  186. "}",
  187. "float gather(float i, float j, int ringsamples, inout vec3 col, float w, float h, float blur) {",
  188. "float rings2 = float(rings);",
  189. "float step = PI*2.0 / float(ringsamples);",
  190. "float pw = cos(j*step)*i;",
  191. "float ph = sin(j*step)*i;",
  192. "float p = 1.0;",
  193. "if (pentagon) {",
  194. "p = penta(vec2(pw,ph));",
  195. "}",
  196. "col += color(vUv.xy + vec2(pw*w,ph*h), blur) * mix(1.0, i/rings2, bias) * p;",
  197. "return 1.0 * mix(1.0, i /rings2, bias) * p;",
  198. "}",
  199. "void main() {",
  200. "//scene depth calculation",
  201. "float depth = linearize(texture2D(tDepth,vUv.xy).x);",
  202. "// Blur depth?",
  203. "if (depthblur) {",
  204. "depth = linearize(bdepth(vUv.xy));",
  205. "}",
  206. "//focal plane calculation",
  207. "float fDepth = focalDepth;",
  208. "if (autofocus) {",
  209. "fDepth = linearize(texture2D(tDepth,focusCoords).x);",
  210. "}",
  211. "// dof blur factor calculation",
  212. "float blur = 0.0;",
  213. "if (manualdof) {",
  214. "float a = depth-fDepth; // Focal plane",
  215. "float b = (a-fdofstart)/fdofdist; // Far DoF",
  216. "float c = (-a-ndofstart)/ndofdist; // Near Dof",
  217. "blur = (a>0.0) ? b : c;",
  218. "} else {",
  219. "float f = focalLength; // focal length in mm",
  220. "float d = fDepth*1000.0; // focal plane in mm",
  221. "float o = depth*1000.0; // depth in mm",
  222. "float a = (o*f)/(o-f);",
  223. "float b = (d*f)/(d-f);",
  224. "float c = (d-f)/(d*fstop*CoC);",
  225. "blur = abs(a-b)*c;",
  226. "}",
  227. "blur = clamp(blur,0.0,1.0);",
  228. "// calculation of pattern for dithering",
  229. "vec2 noise = rand(vUv.xy)*namount*blur;",
  230. "// getting blur x and y step factor",
  231. "float w = (1.0/width)*blur*maxblur+noise.x;",
  232. "float h = (1.0/height)*blur*maxblur+noise.y;",
  233. "// calculation of final color",
  234. "vec3 col = vec3(0.0);",
  235. "if(blur < 0.05) {",
  236. "//some optimization thingy",
  237. "col = texture2D(tColor, vUv.xy).rgb;",
  238. "} else {",
  239. "col = texture2D(tColor, vUv.xy).rgb;",
  240. "float s = 1.0;",
  241. "int ringsamples;",
  242. "for (int i = 1; i <= rings; i++) {",
  243. "/*unboxstart*/",
  244. "ringsamples = i * samples;",
  245. "for (int j = 0 ; j < maxringsamples ; j++) {",
  246. "if (j >= ringsamples) break;",
  247. "s += gather(float(i), float(j), ringsamples, col, w, h, blur);",
  248. "}",
  249. "/*unboxend*/",
  250. "}",
  251. "col /= s; //divide by sample count",
  252. "}",
  253. "if (showFocus) {",
  254. "col = debugFocus(col, blur, depth);",
  255. "}",
  256. "if (vignetting) {",
  257. "col *= vignette();",
  258. "}",
  259. "gl_FragColor.rgb = col;",
  260. "gl_FragColor.a = 1.0;",
  261. "} "
  262. ].join("\n"),
  263. generate: function(rings, samples) {
  264. var frag = THREE.BokehShader.fragmentShader;
  265. frag = frag
  266. .replace(/#RINGS#/, rings)
  267. .replace(/#SAMPLES#/, samples);
  268. /*
  269. var unboxing = false;
  270. if (unboxing) {
  271. var codegen = [];
  272. for (var i=1;i<=rings;i++) {
  273. var ringsamples = i * samples;
  274. var a = 'if (i==?) {'.replace('?', i);
  275. codegen.push( i == 1 ? a : ' ' + a); //else
  276. codegen.push('for (int j = 0 ; j < ?; j++) '.replace('?', ringsamples));
  277. codegen.push('\ts += gather(float(i), float(j), ?, col, w, h, blur);'.replace('?', ringsamples))
  278. codegen.push('}');
  279. }
  280. codegen = codegen.join('\n');
  281. frag = frag.replace(/\/[*]unboxstart[*]\/([\s\S]*)\/[*]unboxend[*]\//m, codegen);
  282. }
  283. */
  284. return frag;
  285. }
  286. };