webgl_postprocessing_ssao.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <!DOCTYPE html>
  2. <!--Reference:
  3. SSAO algo: http://devlog-martinsh.blogspot.tw/2011/12/ssao-shader-update-v12.html?showComment=1398158188712#c1563204765906693531
  4. log depth http://outerra.blogspot.tw/2013/07/logarithmic-depth-buffer-optimizations.html
  5. convert the exponential depth to a linear value: http://www.ozone3d.net/blogs/lab/20090206/how-to-linearize-the-depth-value/
  6. Spiral sampling http://web.archive.org/web/20120421191837/http://www.cgafaq.info/wiki/Evenly_distributed_points_on_sphere-->
  7. <html lang="en">
  8. <head>
  9. <title>three.js webgl - postprocessing - Screen Space Ambient Occlusion</title>
  10. <meta charset="utf-8">
  11. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  12. <style>
  13. body {
  14. background-color: #000000;
  15. margin: 0px;
  16. overflow: hidden;
  17. font-family:Monospace;
  18. font-size:13px;
  19. text-align:center;
  20. font-weight: bold;
  21. }
  22. a { color:#00ff78; }
  23. #info {
  24. color: #fff;
  25. position: absolute;
  26. top: 0px;
  27. width: 100%;
  28. padding: 5px;
  29. }
  30. .dg.ac {
  31. z-index: 1 !important; /* FIX DAT.GUI */
  32. }
  33. </style>
  34. </head>
  35. <body>
  36. <script src="../build/three.js"></script>
  37. <script src="js/shaders/SSAOShader.js"></script>
  38. <script src="js/shaders/CopyShader.js"></script>
  39. <script src="js/postprocessing/EffectComposer.js"></script>
  40. <script src="js/postprocessing/RenderPass.js"></script>
  41. <script src="js/postprocessing/ShaderPass.js"></script>
  42. <script src="js/postprocessing/MaskPass.js"></script>
  43. <script src="js/postprocessing/SSAOPass.js"></script>
  44. <script src="js/WebGL.js"></script>
  45. <script src="js/libs/stats.min.js"></script>
  46. <script src='js/libs/dat.gui.min.js'></script>
  47. <div id="info">
  48. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl screen space ambient occlusion example<br/>
  49. shader by <a href="http://alteredqualia.com">alteredq</a>
  50. </div>
  51. <script>
  52. if ( WEBGL.isWebGLAvailable() === false ) {
  53. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  54. }
  55. var container, stats;
  56. var camera, scene, renderer;
  57. var effectComposer;
  58. var ssaoPass;
  59. var group;
  60. var postprocessing = { enabled: true, onlyAO: false, radius: 32, aoClamp: 0.25, lumInfluence: 0.7 };
  61. init();
  62. animate();
  63. function init() {
  64. container = document.createElement( 'div' );
  65. document.body.appendChild( container );
  66. renderer = new THREE.WebGLRenderer();
  67. renderer.setSize( window.innerWidth, window.innerHeight );
  68. document.body.appendChild( renderer.domElement );
  69. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 100, 700 );
  70. camera.position.z = 500;
  71. scene = new THREE.Scene();
  72. scene.background = new THREE.Color( 0xa0a0a0 );
  73. group = new THREE.Object3D();
  74. scene.add( group );
  75. var geometry = new THREE.BoxBufferGeometry( 10, 10, 10 );
  76. for ( var i = 0; i < 200; i ++ ) {
  77. var material = new THREE.MeshBasicMaterial();
  78. material.color.r = Math.random();
  79. material.color.g = Math.random();
  80. material.color.b = Math.random();
  81. var mesh = new THREE.Mesh( geometry, material );
  82. mesh.position.x = Math.random() * 400 - 200;
  83. mesh.position.y = Math.random() * 400 - 200;
  84. mesh.position.z = Math.random() * 400 - 200;
  85. mesh.rotation.x = Math.random();
  86. mesh.rotation.y = Math.random();
  87. mesh.rotation.z = Math.random();
  88. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 10 + 1;
  89. group.add( mesh );
  90. }
  91. stats = new Stats();
  92. container.appendChild( stats.dom );
  93. // Init postprocessing
  94. initPostprocessing();
  95. // Init gui
  96. var gui = new dat.GUI();
  97. gui.add( postprocessing, 'enabled' );
  98. gui.add( postprocessing, 'onlyAO', false ).onChange( function ( value ) {
  99. ssaoPass.onlyAO = value;
  100. } );
  101. gui.add( postprocessing, 'radius' ).min( 0 ).max( 64 ).onChange( function ( value ) {
  102. ssaoPass.radius = value;
  103. } );
  104. gui.add( postprocessing, 'aoClamp' ).min( 0 ).max( 1 ).onChange( function ( value ) {
  105. ssaoPass.aoClamp = value;
  106. } );
  107. gui.add( postprocessing, 'lumInfluence' ).min( 0 ).max( 1 ).onChange( function ( value ) {
  108. ssaoPass.lumInfluence = value;
  109. } );
  110. window.addEventListener( 'resize', onWindowResize, false );
  111. onWindowResize();
  112. }
  113. function onWindowResize() {
  114. var width = window.innerWidth;
  115. var height = window.innerHeight;
  116. camera.aspect = width / height;
  117. camera.updateProjectionMatrix();
  118. renderer.setSize( width, height );
  119. // Resize renderTargets
  120. ssaoPass.setSize( width, height );
  121. var pixelRatio = renderer.getPixelRatio();
  122. var newWidth = Math.floor( width / pixelRatio ) || 1;
  123. var newHeight = Math.floor( height / pixelRatio ) || 1;
  124. effectComposer.setSize( newWidth, newHeight );
  125. }
  126. function initPostprocessing() {
  127. // Setup render pass
  128. var renderPass = new THREE.RenderPass( scene, camera );
  129. // Setup SSAO pass
  130. ssaoPass = new THREE.SSAOPass( scene, camera );
  131. ssaoPass.renderToScreen = true;
  132. // Add pass to effect composer
  133. effectComposer = new THREE.EffectComposer( renderer );
  134. effectComposer.addPass( renderPass );
  135. effectComposer.addPass( ssaoPass );
  136. }
  137. function animate() {
  138. requestAnimationFrame( animate );
  139. stats.begin();
  140. render();
  141. stats.end();
  142. }
  143. function render() {
  144. var timer = performance.now();
  145. group.rotation.x = timer * 0.0002;
  146. group.rotation.y = timer * 0.0001;
  147. if ( postprocessing.enabled ) {
  148. effectComposer.render();
  149. } else {
  150. renderer.render( scene, camera );
  151. }
  152. }
  153. </script>
  154. </body>
  155. </html>