webgl_postprocessing_dof.html 6.3 KB

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