webgl_postprocessing_ssao.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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/WebGL.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 ( WEBGL.isWebGLAvailable() === false ) {
  55. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  56. }
  57. var container, stats;
  58. var camera, scene, renderer;
  59. var effectComposer;
  60. var ssaoPass;
  61. var group;
  62. var postprocessing = { enabled: true, onlyAO: false, radius: 32, aoClamp: 0.25, lumInfluence: 0.7 };
  63. init();
  64. animate();
  65. function init() {
  66. container = document.createElement( 'div' );
  67. document.body.appendChild( container );
  68. renderer = new THREE.WebGLRenderer();
  69. renderer.setSize( window.innerWidth, window.innerHeight );
  70. document.body.appendChild( renderer.domElement );
  71. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 100, 700 );
  72. camera.position.z = 500;
  73. scene = new THREE.Scene();
  74. scene.background = new THREE.Color( 0xa0a0a0 );
  75. group = new THREE.Object3D();
  76. scene.add( group );
  77. var geometry = new THREE.BoxBufferGeometry( 10, 10, 10 );
  78. for ( var i = 0; i < 200; i ++ ) {
  79. var material = new THREE.MeshBasicMaterial();
  80. material.color.r = Math.random();
  81. material.color.g = Math.random();
  82. material.color.b = Math.random();
  83. var mesh = new THREE.Mesh( geometry, material );
  84. mesh.position.x = Math.random() * 400 - 200;
  85. mesh.position.y = Math.random() * 400 - 200;
  86. mesh.position.z = Math.random() * 400 - 200;
  87. mesh.rotation.x = Math.random();
  88. mesh.rotation.y = Math.random();
  89. mesh.rotation.z = Math.random();
  90. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 10 + 1;
  91. group.add( mesh );
  92. }
  93. stats = new Stats();
  94. container.appendChild( stats.dom );
  95. // Init postprocessing
  96. initPostprocessing();
  97. // Init gui
  98. var gui = new dat.GUI();
  99. gui.add( postprocessing, 'enabled' );
  100. gui.add( postprocessing, 'onlyAO', false ).onChange( function( value ) { ssaoPass.onlyAO = value; } );
  101. gui.add( postprocessing, 'radius' ).min( 0 ).max( 64 ).onChange( function( value ) { ssaoPass.radius = value; } );
  102. gui.add( postprocessing, 'aoClamp' ).min( 0 ).max( 1 ).onChange( function( value ) { ssaoPass.aoClamp = value; } );
  103. gui.add( postprocessing, 'lumInfluence' ).min( 0 ).max( 1 ).onChange( function( value ) { ssaoPass.lumInfluence = value; } );
  104. window.addEventListener( 'resize', onWindowResize, false );
  105. onWindowResize();
  106. }
  107. function onWindowResize() {
  108. var width = window.innerWidth;
  109. var height = window.innerHeight;
  110. camera.aspect = width / height;
  111. camera.updateProjectionMatrix();
  112. renderer.setSize( width, height );
  113. // Resize renderTargets
  114. ssaoPass.setSize( width, height );
  115. var pixelRatio = renderer.getPixelRatio();
  116. var newWidth = Math.floor( width / pixelRatio ) || 1;
  117. var newHeight = Math.floor( height / pixelRatio ) || 1;
  118. effectComposer.setSize( newWidth, newHeight );
  119. }
  120. function initPostprocessing() {
  121. // Setup render pass
  122. var renderPass = new THREE.RenderPass( scene, camera );
  123. // Setup SSAO pass
  124. ssaoPass = new THREE.SSAOPass( scene, camera );
  125. ssaoPass.renderToScreen = true;
  126. // Add pass to effect composer
  127. effectComposer = new THREE.EffectComposer( renderer );
  128. effectComposer.addPass( renderPass );
  129. effectComposer.addPass( ssaoPass );
  130. }
  131. function animate() {
  132. requestAnimationFrame( animate );
  133. stats.begin();
  134. render();
  135. stats.end();
  136. }
  137. function render() {
  138. var timer = performance.now();
  139. group.rotation.x = timer * 0.0002;
  140. group.rotation.y = timer * 0.0001;
  141. if ( postprocessing.enabled ) {
  142. effectComposer.render();
  143. } else {
  144. renderer.render( scene, camera );
  145. }
  146. }
  147. </script>
  148. </body>
  149. </html>