2
0

webgl_postprocessing_dof.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. container.style.touchAction = 'none';
  89. container.addEventListener( 'pointermove', onPointerMove, false );
  90. window.addEventListener( 'resize', onWindowResize, false );
  91. var effectController = {
  92. focus: 500.0,
  93. aperture: 5,
  94. maxblur: 0.01
  95. };
  96. var matChanger = function ( ) {
  97. postprocessing.bokeh.uniforms[ "focus" ].value = effectController.focus;
  98. postprocessing.bokeh.uniforms[ "aperture" ].value = effectController.aperture * 0.00001;
  99. postprocessing.bokeh.uniforms[ "maxblur" ].value = effectController.maxblur;
  100. };
  101. var gui = new GUI();
  102. gui.add( effectController, "focus", 10.0, 3000.0, 10 ).onChange( matChanger );
  103. gui.add( effectController, "aperture", 0, 10, 0.1 ).onChange( matChanger );
  104. gui.add( effectController, "maxblur", 0.0, 0.01, 0.001 ).onChange( matChanger );
  105. gui.close();
  106. matChanger();
  107. }
  108. function onPointerMove( event ) {
  109. if ( event.isPrimary === false ) return;
  110. mouseX = event.clientX - windowHalfX;
  111. mouseY = event.clientY - windowHalfY;
  112. }
  113. function onWindowResize() {
  114. windowHalfX = window.innerWidth / 2;
  115. windowHalfY = window.innerHeight / 2;
  116. width = window.innerWidth;
  117. height = window.innerHeight;
  118. camera.aspect = width / height;
  119. camera.updateProjectionMatrix();
  120. renderer.setSize( width, height );
  121. postprocessing.composer.setSize( width, height );
  122. }
  123. function initPostprocessing() {
  124. var renderPass = new RenderPass( scene, camera );
  125. var bokehPass = new BokehPass( scene, camera, {
  126. focus: 1.0,
  127. aperture: 0.025,
  128. maxblur: 0.01,
  129. width: width,
  130. height: height
  131. } );
  132. var composer = new EffectComposer( renderer );
  133. composer.addPass( renderPass );
  134. composer.addPass( bokehPass );
  135. postprocessing.composer = composer;
  136. postprocessing.bokeh = bokehPass;
  137. }
  138. function animate() {
  139. requestAnimationFrame( animate, renderer.domElement );
  140. stats.begin();
  141. render();
  142. stats.end();
  143. }
  144. function render() {
  145. var time = Date.now() * 0.00005;
  146. camera.position.x += ( mouseX - camera.position.x ) * 0.036;
  147. camera.position.y += ( - ( mouseY ) - camera.position.y ) * 0.036;
  148. camera.lookAt( scene.position );
  149. if ( ! singleMaterial ) {
  150. for ( i = 0; i < nobjects; i ++ ) {
  151. h = ( 360 * ( i / nobjects + time ) % 360 ) / 360;
  152. materials[ i ].color.setHSL( h, 1, 0.5 );
  153. }
  154. }
  155. postprocessing.composer.render( 0.1 );
  156. }
  157. </script>
  158. </body>
  159. </html>