webgl_postprocessing_ssao.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 {
  23. color:#00ff78;
  24. }
  25. #info {
  26. color: #fff;
  27. position: absolute;
  28. top: 0px;
  29. width: 100%;
  30. padding: 5px;
  31. }
  32. .dg.ac {
  33. z-index: 1 !important; /* FIX DAT.GUI */
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <script src="../build/three.js"></script>
  39. <script src="js/shaders/SSAOShader.js"></script>
  40. <script src="js/shaders/CopyShader.js"></script>
  41. <script src="js/postprocessing/EffectComposer.js"></script>
  42. <script src="js/postprocessing/RenderPass.js"></script>
  43. <script src="js/postprocessing/ShaderPass.js"></script>
  44. <script src="js/postprocessing/MaskPass.js"></script>
  45. <script src="js/postprocessing/SSAOPass.js"></script>
  46. <script src="js/Detector.js"></script>
  47. <script src="js/libs/stats.min.js"></script>
  48. <script src='js/libs/dat.gui.min.js'></script>
  49. <div id="info">
  50. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl screen space ambient occlusion example<br/>
  51. shader by <a href="http://alteredqualia.com">alteredq</a>
  52. </div>
  53. <script>
  54. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  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.BoxGeometry( 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 ) { ssaoPass.onlyAO = value; } );
  99. gui.add( postprocessing, 'radius' ).min( 0 ).max( 64 ).onChange( function( value ) { ssaoPass.radius = value; } );
  100. gui.add( postprocessing, 'aoClamp' ).min( 0 ).max( 1 ).onChange( function( value ) { ssaoPass.aoClamp = value; } );
  101. gui.add( postprocessing, 'lumInfluence' ).min( 0 ).max( 1 ).onChange( function( value ) { ssaoPass.lumInfluence = value; } );
  102. window.addEventListener( 'resize', onWindowResize, false );
  103. onWindowResize();
  104. }
  105. function onWindowResize() {
  106. var width = window.innerWidth;
  107. var height = window.innerHeight;
  108. camera.aspect = width / height;
  109. camera.updateProjectionMatrix();
  110. renderer.setSize( width, height );
  111. // Resize renderTargets
  112. ssaoPass.setSize( width, height );
  113. var pixelRatio = renderer.getPixelRatio();
  114. var newWidth = Math.floor( width / pixelRatio ) || 1;
  115. var newHeight = Math.floor( height / pixelRatio ) || 1;
  116. effectComposer.setSize( newWidth, newHeight );
  117. }
  118. function initPostprocessing() {
  119. // Setup render pass
  120. var renderPass = new THREE.RenderPass( scene, camera );
  121. // Setup SSAO pass
  122. ssaoPass = new THREE.SSAOPass( scene, camera );
  123. ssaoPass.renderToScreen = true;
  124. // Add pass to effect composer
  125. effectComposer = new THREE.EffectComposer( renderer );
  126. effectComposer.addPass( renderPass );
  127. effectComposer.addPass( ssaoPass );
  128. }
  129. function animate() {
  130. requestAnimationFrame( animate );
  131. stats.begin();
  132. render();
  133. stats.end();
  134. }
  135. function render() {
  136. var timer = performance.now();
  137. group.rotation.x = timer * 0.0002;
  138. group.rotation.y = timer * 0.0001;
  139. if ( postprocessing.enabled ) {
  140. effectComposer.render();
  141. } else {
  142. renderer.render( scene, camera );
  143. }
  144. }
  145. </script>
  146. </body>
  147. </html>