webgl_postprocessing_dof2.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - postprocessing - depth-of-field</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl realistic depth-of-field bokeh example<br/>
  12. shader ported from <a href="http://blenderartists.org/forum/showthread.php?237488-GLSL-depth-of-field-with-bokeh-v2-4-(update)">Martins Upitis</a>
  13. </div>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import Stats from './jsm/libs/stats.module.js';
  17. import { GUI } from './jsm/libs/dat.gui.module.js';
  18. import { BokehShader, BokehDepthShader } from './jsm/shaders/BokehShader2.js';
  19. var container, stats;
  20. var camera, scene, renderer, materialDepth;
  21. var windowHalfX = window.innerWidth / 2;
  22. var windowHalfY = window.innerHeight / 2;
  23. var postprocessing = { enabled: true };
  24. var shaderSettings = {
  25. rings: 3,
  26. samples: 4
  27. };
  28. var mouse = new THREE.Vector2();
  29. var raycaster = new THREE.Raycaster();
  30. var distance = 100;
  31. var target = new THREE.Vector3( 0, 20, - 50 );
  32. var effectController;
  33. var planes = [];
  34. var leaves = 100;
  35. init();
  36. animate();
  37. function init() {
  38. container = document.createElement( 'div' );
  39. document.body.appendChild( container );
  40. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 3000 );
  41. camera.position.y = 150;
  42. camera.position.z = 450;
  43. scene = new THREE.Scene();
  44. scene.add( camera );
  45. renderer = new THREE.WebGLRenderer();
  46. renderer.setPixelRatio( window.devicePixelRatio );
  47. renderer.setSize( window.innerWidth, window.innerHeight );
  48. renderer.autoClear = false;
  49. container.appendChild( renderer.domElement );
  50. var depthShader = BokehDepthShader;
  51. materialDepth = new THREE.ShaderMaterial( {
  52. uniforms: depthShader.uniforms,
  53. vertexShader: depthShader.vertexShader,
  54. fragmentShader: depthShader.fragmentShader
  55. } );
  56. materialDepth.uniforms[ 'mNear' ].value = camera.near;
  57. materialDepth.uniforms[ 'mFar' ].value = camera.far;
  58. // skybox
  59. var r = 'textures/cube/Bridge2/';
  60. var urls = [ r + 'posx.jpg', r + 'negx.jpg',
  61. r + 'posy.jpg', r + 'negy.jpg',
  62. r + 'posz.jpg', r + 'negz.jpg' ];
  63. var textureCube = new THREE.CubeTextureLoader().load( urls );
  64. textureCube.format = THREE.RGBFormat;
  65. scene.background = textureCube;
  66. // plane particles
  67. var planePiece = new THREE.PlaneBufferGeometry( 10, 10, 1, 1 );
  68. var planeMat = new THREE.MeshPhongMaterial( {
  69. color: 0xffffff * 0.4,
  70. shininess: 0.5,
  71. specular: 0xffffff,
  72. envMap: textureCube,
  73. side: THREE.DoubleSide
  74. } );
  75. var rand = Math.random;
  76. for ( var i = 0; i < leaves; i ++ ) {
  77. var plane = new THREE.Mesh( planePiece, planeMat );
  78. plane.rotation.set( rand(), rand(), rand() );
  79. plane.rotation.dx = rand() * 0.1;
  80. plane.rotation.dy = rand() * 0.1;
  81. plane.rotation.dz = rand() * 0.1;
  82. plane.position.set( rand() * 150, 0 + rand() * 300, rand() * 150 );
  83. plane.position.dx = ( rand() - 0.5 );
  84. plane.position.dz = ( rand() - 0.5 );
  85. scene.add( plane );
  86. planes.push( plane );
  87. }
  88. // adding Monkeys
  89. var loader2 = new THREE.BufferGeometryLoader();
  90. loader2.load( 'models/json/suzanne_buffergeometry.json', function ( geometry ) {
  91. geometry.computeVertexNormals();
  92. var material = new THREE.MeshPhongMaterial( {
  93. specular: 0xffffff,
  94. envMap: textureCube,
  95. shininess: 50,
  96. reflectivity: 1.0,
  97. flatShading: true
  98. } );
  99. var monkeys = 20;
  100. for ( var i = 0; i < monkeys; i ++ ) {
  101. var mesh = new THREE.Mesh( geometry, material );
  102. mesh.position.z = Math.cos( i / monkeys * Math.PI * 2 ) * 200;
  103. mesh.position.y = Math.sin( i / monkeys * Math.PI * 3 ) * 20;
  104. mesh.position.x = Math.sin( i / monkeys * Math.PI * 2 ) * 200;
  105. mesh.rotation.y = i / monkeys * Math.PI * 2;
  106. mesh.scale.setScalar( 30 );
  107. scene.add( mesh );
  108. }
  109. } );
  110. // add balls
  111. var geometry = new THREE.SphereBufferGeometry( 1, 20, 20 );
  112. for ( var i = 0; i < 20; i ++ ) {
  113. var ballmaterial = new THREE.MeshPhongMaterial( {
  114. color: 0xffffff * Math.random(),
  115. shininess: 0.5,
  116. specular: 0xffffff,
  117. envMap: textureCube } );
  118. var mesh = new THREE.Mesh( geometry, ballmaterial );
  119. mesh.position.x = ( Math.random() - 0.5 ) * 200;
  120. mesh.position.y = Math.random() * 50;
  121. mesh.position.z = ( Math.random() - 0.5 ) * 200;
  122. mesh.scale.multiplyScalar( 10 );
  123. scene.add( mesh );
  124. }
  125. // lights
  126. scene.add( new THREE.AmbientLight( 0x222222 ) );
  127. var directionalLight = new THREE.DirectionalLight( 0xffffff, 2 );
  128. directionalLight.position.set( 2, 1.2, 10 ).normalize();
  129. scene.add( directionalLight );
  130. var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
  131. directionalLight.position.set( - 2, 1.2, - 10 ).normalize();
  132. scene.add( directionalLight );
  133. initPostprocessing();
  134. stats = new Stats();
  135. container.appendChild( stats.dom );
  136. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  137. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  138. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  139. effectController = {
  140. enabled: true,
  141. jsDepthCalculation: true,
  142. shaderFocus: false,
  143. fstop: 2.2,
  144. maxblur: 1.0,
  145. showFocus: false,
  146. focalDepth: 2.8,
  147. manualdof: false,
  148. vignetting: false,
  149. depthblur: false,
  150. threshold: 0.5,
  151. gain: 2.0,
  152. bias: 0.5,
  153. fringe: 0.7,
  154. focalLength: 35,
  155. noise: true,
  156. pentagon: false,
  157. dithering: 0.0001
  158. };
  159. var matChanger = function () {
  160. for ( var e in effectController ) {
  161. if ( e in postprocessing.bokeh_uniforms ) {
  162. postprocessing.bokeh_uniforms[ e ].value = effectController[ e ];
  163. }
  164. }
  165. postprocessing.enabled = effectController.enabled;
  166. postprocessing.bokeh_uniforms[ 'znear' ].value = camera.near;
  167. postprocessing.bokeh_uniforms[ 'zfar' ].value = camera.far;
  168. camera.setFocalLength( effectController.focalLength );
  169. };
  170. var gui = new GUI();
  171. gui.add( effectController, 'enabled' ).onChange( matChanger );
  172. gui.add( effectController, 'jsDepthCalculation' ).onChange( matChanger );
  173. gui.add( effectController, 'shaderFocus' ).onChange( matChanger );
  174. gui.add( effectController, 'focalDepth', 0.0, 200.0 ).listen().onChange( matChanger );
  175. gui.add( effectController, 'fstop', 0.1, 22, 0.001 ).onChange( matChanger );
  176. gui.add( effectController, 'maxblur', 0.0, 5.0, 0.025 ).onChange( matChanger );
  177. gui.add( effectController, 'showFocus' ).onChange( matChanger );
  178. gui.add( effectController, 'manualdof' ).onChange( matChanger );
  179. gui.add( effectController, 'vignetting' ).onChange( matChanger );
  180. gui.add( effectController, 'depthblur' ).onChange( matChanger );
  181. gui.add( effectController, 'threshold', 0, 1, 0.001 ).onChange( matChanger );
  182. gui.add( effectController, 'gain', 0, 100, 0.001 ).onChange( matChanger );
  183. gui.add( effectController, 'bias', 0, 3, 0.001 ).onChange( matChanger );
  184. gui.add( effectController, 'fringe', 0, 5, 0.001 ).onChange( matChanger );
  185. gui.add( effectController, 'focalLength', 16, 80, 0.001 ).onChange( matChanger );
  186. gui.add( effectController, 'noise' ).onChange( matChanger );
  187. gui.add( effectController, 'dithering', 0, 0.001, 0.0001 ).onChange( matChanger );
  188. gui.add( effectController, 'pentagon' ).onChange( matChanger );
  189. gui.add( shaderSettings, 'rings', 1, 8 ).step( 1 ).onChange( shaderUpdate );
  190. gui.add( shaderSettings, 'samples', 1, 13 ).step( 1 ).onChange( shaderUpdate );
  191. matChanger();
  192. window.addEventListener( 'resize', onWindowResize, false );
  193. }
  194. function onWindowResize() {
  195. camera.aspect = window.innerWidth / window.innerHeight;
  196. camera.updateProjectionMatrix();
  197. windowHalfX = window.innerWidth / 2;
  198. windowHalfY = window.innerHeight / 2;
  199. postprocessing.rtTextureDepth.setSize( window.innerWidth, window.innerHeight );
  200. postprocessing.rtTextureColor.setSize( window.innerWidth, window.innerHeight );
  201. postprocessing.bokeh_uniforms[ 'textureWidth' ].value = window.innerWidth;
  202. postprocessing.bokeh_uniforms[ 'textureHeight' ].value = window.innerHeight;
  203. renderer.setSize( window.innerWidth, window.innerHeight );
  204. }
  205. function onDocumentMouseMove( event ) {
  206. mouse.x = ( event.clientX - windowHalfX ) / windowHalfX;
  207. mouse.y = - ( event.clientY - windowHalfY ) / windowHalfY;
  208. postprocessing.bokeh_uniforms[ 'focusCoords' ].value.set( event.clientX / window.innerWidth, 1 - ( event.clientY / window.innerHeight ) );
  209. }
  210. function onDocumentTouchStart( event ) {
  211. if ( event.touches.length == 1 ) {
  212. event.preventDefault();
  213. mouse.x = ( event.touches[ 0 ].pageX - windowHalfX ) / windowHalfX;
  214. mouse.y = - ( event.touches[ 0 ].pageY - windowHalfY ) / windowHalfY;
  215. }
  216. }
  217. function onDocumentTouchMove( event ) {
  218. if ( event.touches.length == 1 ) {
  219. event.preventDefault();
  220. mouse.x = ( event.touches[ 0 ].pageX - windowHalfX ) / windowHalfX;
  221. mouse.y = - ( event.touches[ 0 ].pageY - windowHalfY ) / windowHalfY;
  222. }
  223. }
  224. function initPostprocessing() {
  225. postprocessing.scene = new THREE.Scene();
  226. postprocessing.camera = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, - 10000, 10000 );
  227. postprocessing.camera.position.z = 100;
  228. postprocessing.scene.add( postprocessing.camera );
  229. var pars = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat };
  230. postprocessing.rtTextureDepth = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, pars );
  231. postprocessing.rtTextureColor = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, pars );
  232. var bokeh_shader = BokehShader;
  233. postprocessing.bokeh_uniforms = THREE.UniformsUtils.clone( bokeh_shader.uniforms );
  234. postprocessing.bokeh_uniforms[ 'tColor' ].value = postprocessing.rtTextureColor.texture;
  235. postprocessing.bokeh_uniforms[ 'tDepth' ].value = postprocessing.rtTextureDepth.texture;
  236. postprocessing.bokeh_uniforms[ 'textureWidth' ].value = window.innerWidth;
  237. postprocessing.bokeh_uniforms[ 'textureHeight' ].value = window.innerHeight;
  238. postprocessing.materialBokeh = new THREE.ShaderMaterial( {
  239. uniforms: postprocessing.bokeh_uniforms,
  240. vertexShader: bokeh_shader.vertexShader,
  241. fragmentShader: bokeh_shader.fragmentShader,
  242. defines: {
  243. RINGS: shaderSettings.rings,
  244. SAMPLES: shaderSettings.samples
  245. }
  246. } );
  247. postprocessing.quad = new THREE.Mesh( new THREE.PlaneBufferGeometry( window.innerWidth, window.innerHeight ), postprocessing.materialBokeh );
  248. postprocessing.quad.position.z = - 500;
  249. postprocessing.scene.add( postprocessing.quad );
  250. }
  251. function shaderUpdate() {
  252. postprocessing.materialBokeh.defines.RINGS = shaderSettings.rings;
  253. postprocessing.materialBokeh.defines.SAMPLES = shaderSettings.samples;
  254. postprocessing.materialBokeh.needsUpdate = true;
  255. }
  256. function animate() {
  257. requestAnimationFrame( animate, renderer.domElement );
  258. render();
  259. stats.update();
  260. }
  261. function linearize( depth ) {
  262. var zfar = camera.far;
  263. var znear = camera.near;
  264. return - zfar * znear / ( depth * ( zfar - znear ) - zfar );
  265. }
  266. function smoothstep( near, far, depth ) {
  267. var x = saturate( ( depth - near ) / ( far - near ) );
  268. return x * x * ( 3 - 2 * x );
  269. }
  270. function saturate( x ) {
  271. return Math.max( 0, Math.min( 1, x ) );
  272. }
  273. function render() {
  274. var time = Date.now() * 0.00015;
  275. camera.position.x = Math.cos( time ) * 400;
  276. camera.position.z = Math.sin( time ) * 500;
  277. camera.position.y = Math.sin( time / 1.4 ) * 100;
  278. camera.lookAt( target );
  279. camera.updateMatrixWorld();
  280. if ( effectController.jsDepthCalculation ) {
  281. raycaster.setFromCamera( mouse, camera );
  282. var intersects = raycaster.intersectObjects( scene.children, true );
  283. var targetDistance = ( intersects.length > 0 ) ? intersects[ 0 ].distance : 1000;
  284. distance += ( targetDistance - distance ) * 0.03;
  285. var sdistance = smoothstep( camera.near, camera.far, distance );
  286. var ldistance = linearize( 1 - sdistance );
  287. postprocessing.bokeh_uniforms[ 'focalDepth' ].value = ldistance;
  288. effectController[ 'focalDepth' ] = ldistance;
  289. }
  290. for ( var i = 0; i < leaves; i ++ ) {
  291. var plane = planes[ i ];
  292. plane.rotation.x += plane.rotation.dx;
  293. plane.rotation.y += plane.rotation.dy;
  294. plane.rotation.z += plane.rotation.dz;
  295. plane.position.y -= 2;
  296. plane.position.x += plane.position.dx;
  297. plane.position.z += plane.position.dz;
  298. if ( plane.position.y < 0 ) plane.position.y += 300;
  299. }
  300. if ( postprocessing.enabled ) {
  301. renderer.clear();
  302. // render scene into texture
  303. renderer.setRenderTarget( postprocessing.rtTextureColor );
  304. renderer.clear();
  305. renderer.render( scene, camera );
  306. // render depth into texture
  307. scene.overrideMaterial = materialDepth;
  308. renderer.setRenderTarget( postprocessing.rtTextureDepth );
  309. renderer.clear();
  310. renderer.render( scene, camera );
  311. scene.overrideMaterial = null;
  312. // render bokeh composite
  313. renderer.setRenderTarget( null );
  314. renderer.render( postprocessing.scene, postprocessing.camera );
  315. } else {
  316. scene.overrideMaterial = null;
  317. renderer.setRenderTarget( null );
  318. renderer.clear();
  319. renderer.render( scene, camera );
  320. }
  321. }
  322. </script>
  323. </body>
  324. </html>