webgl_postprocessing_dof.html 6.1 KB

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