webgl_postprocessing_dof.html 6.1 KB

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