webgl_postprocessing_dof.html 6.3 KB

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