BokehShader2.js 9.6 KB

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