webgl_postprocessing_dof.html 5.8 KB

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