webgl_postprocessing_dof.html 5.4 KB

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