webgl_postprocessing_dof.html 6.3 KB

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