webgl_postprocessing_dof.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 depth-of-field with bokeh example<br/>
  12. shader by <a href="http://artmartinsh.blogspot.com/2010/02/glsl-lens-blur-filter-with-bokeh.html">Martins Upitis</a>
  13. </div>
  14. <!-- Import maps polyfill -->
  15. <!-- Remove this when import maps will be widely supported -->
  16. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js",
  21. "three/addons/": "./jsm/"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import Stats from 'three/addons/libs/stats.module.js';
  28. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  29. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  30. import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  31. import { BokehPass } from 'three/addons/postprocessing/BokehPass.js';
  32. let camera, scene, renderer, stats,
  33. singleMaterial, zmaterial,
  34. parameters, nobjects, cubeMaterial;
  35. let mouseX = 0, mouseY = 0;
  36. let windowHalfX = window.innerWidth / 2;
  37. let windowHalfY = window.innerHeight / 2;
  38. let width = window.innerWidth;
  39. let height = window.innerHeight;
  40. const materials = [], objects = [];
  41. const postprocessing = {};
  42. init();
  43. animate();
  44. function init() {
  45. const container = document.createElement( 'div' );
  46. document.body.appendChild( container );
  47. camera = new THREE.PerspectiveCamera( 70, width / height, 1, 3000 );
  48. camera.position.z = 200;
  49. scene = new THREE.Scene();
  50. renderer = new THREE.WebGLRenderer();
  51. renderer.setPixelRatio( window.devicePixelRatio );
  52. renderer.setSize( width, height );
  53. container.appendChild( renderer.domElement );
  54. const path = 'textures/cube/SwedishRoyalCastle/';
  55. const format = '.jpg';
  56. const urls = [
  57. path + 'px' + format, path + 'nx' + format,
  58. path + 'py' + format, path + 'ny' + format,
  59. path + 'pz' + format, path + 'nz' + format
  60. ];
  61. const textureCube = new THREE.CubeTextureLoader().load( urls );
  62. parameters = { color: 0xff1100, envMap: textureCube };
  63. cubeMaterial = new THREE.MeshBasicMaterial( parameters );
  64. singleMaterial = false;
  65. if ( singleMaterial ) zmaterial = [ cubeMaterial ];
  66. const geo = new THREE.SphereGeometry( 1, 20, 10 );
  67. const xgrid = 14, ygrid = 9, zgrid = 14;
  68. nobjects = xgrid * ygrid * zgrid;
  69. const s = 60;
  70. let count = 0;
  71. for ( let i = 0; i < xgrid; i ++ ) {
  72. for ( let j = 0; j < ygrid; j ++ ) {
  73. for ( let k = 0; k < zgrid; k ++ ) {
  74. let mesh;
  75. if ( singleMaterial ) {
  76. mesh = new THREE.Mesh( geo, zmaterial );
  77. } else {
  78. mesh = new THREE.Mesh( geo, new THREE.MeshBasicMaterial( parameters ) );
  79. materials[ count ] = mesh.material;
  80. }
  81. const x = 200 * ( i - xgrid / 2 );
  82. const y = 200 * ( j - ygrid / 2 );
  83. const z = 200 * ( k - zgrid / 2 );
  84. mesh.position.set( x, y, z );
  85. mesh.scale.set( s, s, s );
  86. mesh.matrixAutoUpdate = false;
  87. mesh.updateMatrix();
  88. scene.add( mesh );
  89. objects.push( mesh );
  90. count ++;
  91. }
  92. }
  93. }
  94. initPostprocessing();
  95. renderer.autoClear = false;
  96. stats = new Stats();
  97. container.appendChild( stats.dom );
  98. container.style.touchAction = 'none';
  99. container.addEventListener( 'pointermove', onPointerMove );
  100. window.addEventListener( 'resize', onWindowResize );
  101. const effectController = {
  102. focus: 500.0,
  103. aperture: 5,
  104. maxblur: 0.01
  105. };
  106. const matChanger = function ( ) {
  107. postprocessing.bokeh.uniforms[ 'focus' ].value = effectController.focus;
  108. postprocessing.bokeh.uniforms[ 'aperture' ].value = effectController.aperture * 0.00001;
  109. postprocessing.bokeh.uniforms[ 'maxblur' ].value = effectController.maxblur;
  110. };
  111. const gui = new GUI();
  112. gui.add( effectController, 'focus', 10.0, 3000.0, 10 ).onChange( matChanger );
  113. gui.add( effectController, 'aperture', 0, 10, 0.1 ).onChange( matChanger );
  114. gui.add( effectController, 'maxblur', 0.0, 0.01, 0.001 ).onChange( matChanger );
  115. gui.close();
  116. matChanger();
  117. }
  118. function onPointerMove( event ) {
  119. if ( event.isPrimary === false ) return;
  120. mouseX = event.clientX - windowHalfX;
  121. mouseY = event.clientY - windowHalfY;
  122. }
  123. function onWindowResize() {
  124. windowHalfX = window.innerWidth / 2;
  125. windowHalfY = window.innerHeight / 2;
  126. width = window.innerWidth;
  127. height = window.innerHeight;
  128. camera.aspect = width / height;
  129. camera.updateProjectionMatrix();
  130. renderer.setSize( width, height );
  131. postprocessing.composer.setSize( width, height );
  132. }
  133. function initPostprocessing() {
  134. const renderPass = new RenderPass( scene, camera );
  135. const bokehPass = new BokehPass( scene, camera, {
  136. focus: 1.0,
  137. aperture: 0.025,
  138. maxblur: 0.01
  139. } );
  140. const composer = new EffectComposer( renderer );
  141. composer.addPass( renderPass );
  142. composer.addPass( bokehPass );
  143. postprocessing.composer = composer;
  144. postprocessing.bokeh = bokehPass;
  145. }
  146. function animate() {
  147. requestAnimationFrame( animate, renderer.domElement );
  148. stats.begin();
  149. render();
  150. stats.end();
  151. }
  152. function render() {
  153. const time = Date.now() * 0.00005;
  154. camera.position.x += ( mouseX - camera.position.x ) * 0.036;
  155. camera.position.y += ( - ( mouseY ) - camera.position.y ) * 0.036;
  156. camera.lookAt( scene.position );
  157. if ( ! singleMaterial ) {
  158. for ( let i = 0; i < nobjects; i ++ ) {
  159. const h = ( 360 * ( i / nobjects + time ) % 360 ) / 360;
  160. materials[ i ].color.setHSL( h, 1, 0.5 );
  161. }
  162. }
  163. postprocessing.composer.render( 0.1 );
  164. }
  165. </script>
  166. </body>
  167. </html>