BokehShader2.js 9.6 KB

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