2
0

webgl_postprocessing_dof.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. <style>
  8. body {
  9. background-color: #000000;
  10. margin: 0px;
  11. overflow: hidden;
  12. font-family: Monospace;
  13. font-size: 13px;
  14. text-align: center;
  15. font-weight: bold;
  16. text-align: center;
  17. }
  18. a {
  19. color: #0078ff;
  20. }
  21. #info {
  22. color: #fff;
  23. position: relative;
  24. top: 0px;
  25. width: 50em;
  26. margin: 0 auto -2.1em;
  27. padding: 5px;
  28. z-index: 100;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <script src="../build/three.js"></script>
  34. <script src="js/shaders/CopyShader.js"></script>
  35. <script src="js/shaders/BokehShader.js"></script>
  36. <script src="js/postprocessing/EffectComposer.js"></script>
  37. <script src="js/postprocessing/RenderPass.js"></script>
  38. <script src="js/postprocessing/ShaderPass.js"></script>
  39. <script src="js/postprocessing/MaskPass.js"></script>
  40. <script src="js/postprocessing/BokehPass.js"></script>
  41. <script src="js/Detector.js"></script>
  42. <script src="js/libs/stats.min.js"></script>
  43. <script src='js/libs/dat.gui.min.js'></script>
  44. <div id="info">
  45. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl depth-of-field with bokeh example -
  46. shader by <a href="http://artmartinsh.blogspot.com/2010/02/glsl-lens-blur-filter-with-bokeh.html">Martins Upitis</a>
  47. </div>
  48. <script>
  49. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  50. var container, stats;
  51. var camera, scene, renderer,
  52. materials = [], objects = [],
  53. singleMaterial, zmaterial = [],
  54. parameters, i, j, k, h, color, x, y, z, s, n, nobjects, cubeMaterial;
  55. var mouseX = 0, mouseY = 0;
  56. var windowHalfX = window.innerWidth / 2;
  57. var windowHalfY = window.innerHeight / 2;
  58. var width = window.innerWidth;
  59. var height = window.innerHeight;
  60. var postprocessing = {};
  61. init();
  62. animate();
  63. function init() {
  64. container = document.createElement( 'div' );
  65. document.body.appendChild( container );
  66. camera = new THREE.PerspectiveCamera( 70, width / height, 1, 3000 );
  67. camera.position.z = 200;
  68. scene = new THREE.Scene();
  69. renderer = new THREE.WebGLRenderer();
  70. renderer.setPixelRatio( window.devicePixelRatio );
  71. renderer.setSize( width, height );
  72. container.appendChild( renderer.domElement );
  73. var path = "textures/cube/SwedishRoyalCastle/";
  74. var format = '.jpg';
  75. var urls = [
  76. path + 'px' + format, path + 'nx' + format,
  77. path + 'py' + format, path + 'ny' + format,
  78. path + 'pz' + format, path + 'nz' + format
  79. ];
  80. var textureCube = new THREE.CubeTextureLoader().load( urls );
  81. parameters = { color: 0xff1100, envMap: textureCube };
  82. cubeMaterial = new THREE.MeshBasicMaterial( parameters );
  83. singleMaterial = false;
  84. if( singleMaterial ) zmaterial = [ cubeMaterial ];
  85. var geo = new THREE.SphereGeometry( 1, 20, 10 );
  86. var start = Date.now();
  87. var xgrid = 14,
  88. ygrid = 9,
  89. zgrid = 14;
  90. nobjects = xgrid * ygrid * zgrid;
  91. var s = 60;
  92. var count = 0;
  93. for ( i = 0; i < xgrid; i ++ )
  94. for ( j = 0; j < ygrid; j ++ )
  95. for ( k = 0; k < zgrid; k ++ ) {
  96. var mesh;
  97. if ( singleMaterial ) {
  98. mesh = new THREE.Mesh( geo, zmaterial );
  99. } else {
  100. mesh = new THREE.Mesh( geo, new THREE.MeshBasicMaterial( parameters ) );
  101. materials[ count ] = mesh.material
  102. }
  103. x = 200 * ( i - xgrid/2 );
  104. y = 200 * ( j - ygrid/2 );
  105. z = 200 * ( k - zgrid/2 );
  106. mesh.position.set( x, y, z );
  107. mesh.scale.set( s, s, s );
  108. mesh.matrixAutoUpdate = false;
  109. mesh.updateMatrix();
  110. scene.add( mesh );
  111. objects.push( mesh );
  112. count ++;
  113. }
  114. //console.log("init time: ", Date.now() - start );
  115. scene.matrixAutoUpdate = false;
  116. initPostprocessing();
  117. renderer.autoClear = false;
  118. stats = new Stats();
  119. container.appendChild( stats.dom );
  120. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  121. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  122. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  123. window.addEventListener( 'resize', onWindowResize, false );
  124. var effectController = {
  125. focus: 500.0,
  126. aperture: 5,
  127. maxblur: 1.0
  128. };
  129. var matChanger = function( ) {
  130. postprocessing.bokeh.uniforms[ "focus" ].value = effectController.focus;
  131. postprocessing.bokeh.uniforms[ "aperture" ].value = effectController.aperture * 0.00001;
  132. postprocessing.bokeh.uniforms[ "maxblur" ].value = effectController.maxblur;
  133. };
  134. var gui = new dat.GUI();
  135. gui.add( effectController, "focus", 10.0, 3000.0, 10 ).onChange( matChanger );
  136. gui.add( effectController, "aperture", 0, 10, 0.1 ).onChange( matChanger );
  137. gui.add( effectController, "maxblur", 0.0, 3.0, 0.025 ).onChange( matChanger );
  138. gui.close();
  139. matChanger();
  140. }
  141. function onDocumentMouseMove( event ) {
  142. mouseX = event.clientX - windowHalfX;
  143. mouseY = event.clientY - windowHalfY;
  144. }
  145. function onDocumentTouchStart( event ) {
  146. if ( event.touches.length == 1 ) {
  147. event.preventDefault();
  148. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  149. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  150. }
  151. }
  152. function onDocumentTouchMove( event ) {
  153. if ( event.touches.length == 1 ) {
  154. event.preventDefault();
  155. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  156. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  157. }
  158. }
  159. function onWindowResize() {
  160. windowHalfX = window.innerWidth / 2;
  161. windowHalfY = window.innerHeight / 2;
  162. width = window.innerWidth;
  163. height = window.innerHeight;
  164. camera.aspect = width / height;
  165. camera.updateProjectionMatrix();
  166. renderer.setSize( width, height );
  167. postprocessing.composer.setSize( width, height );
  168. }
  169. function initPostprocessing() {
  170. var renderPass = new THREE.RenderPass( scene, camera );
  171. var bokehPass = new THREE.BokehPass( scene, camera, {
  172. focus: 1.0,
  173. aperture: 0.025,
  174. maxblur: 1.0,
  175. width: width,
  176. height: height
  177. } );
  178. bokehPass.renderToScreen = true;
  179. var composer = new THREE.EffectComposer( renderer );
  180. composer.addPass( renderPass );
  181. composer.addPass( bokehPass );
  182. postprocessing.composer = composer;
  183. postprocessing.bokeh = bokehPass;
  184. }
  185. function animate() {
  186. requestAnimationFrame( animate, renderer.domElement );
  187. stats.begin();
  188. render();
  189. stats.end();
  190. }
  191. function render() {
  192. var time = Date.now() * 0.00005;
  193. camera.position.x += ( mouseX - camera.position.x ) * 0.036;
  194. camera.position.y += ( - (mouseY) - camera.position.y ) * 0.036;
  195. camera.lookAt( scene.position );
  196. if ( !singleMaterial ) {
  197. for( i = 0; i < nobjects; i ++ ) {
  198. h = ( 360 * ( i / nobjects + time ) % 360 ) / 360;
  199. materials[ i ].color.setHSL( h, 1, 0.5 );
  200. }
  201. }
  202. postprocessing.composer.render( 0.1 );
  203. }
  204. </script>
  205. </body>
  206. </html>