webgl_postprocessing_sao.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - post processing - Scalable Ambient Occlusion (SAO)</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. }
  17. a {
  18. color:#00ff78;
  19. }
  20. #info {
  21. color: #fff;
  22. position: absolute;
  23. top: 0px;
  24. width: 100%;
  25. padding: 5px;
  26. }
  27. .dg.ac {
  28. z-index: 1 !important; /* FIX DAT.GUI */
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <script src="../build/three.js"></script>
  34. <script src="js/postprocessing/EffectComposer.js"></script>
  35. <script src="js/postprocessing/RenderPass.js"></script>
  36. <script src="js/postprocessing/ShaderPass.js"></script>
  37. <script src="js/postprocessing/SAOPass.js"></script>
  38. <script src="js/shaders/CopyShader.js"></script>
  39. <script src="js/shaders/SAOShader.js"></script>
  40. <script src="js/shaders/DepthLimitedBlurShader.js"></script>
  41. <script src="js/shaders/UnpackDepthRGBAShader.js"></script>
  42. <script src="js/WebGL.js"></script>
  43. <script src="js/libs/stats.min.js"></script>
  44. <script src='js/libs/dat.gui.min.js'></script>
  45. <div id="info">
  46. <a href="http://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> - Scalable Ambient Occlusion (SAO) shader by <a href="http://clara.io">Ben Houston</a> / Post-processing pass by <a href="http://ludobaka.github.io">Ludobaka</a>
  47. </div>
  48. <script>
  49. if ( WEBGL.isWebGLAvailable() === false ) {
  50. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  51. }
  52. var container, stats;
  53. var camera, scene, renderer;
  54. var depthMaterial, saoMaterial, saoModulateMaterial, normalMaterial, vBlurMaterial, hBlurMaterial, copyMaterial;
  55. var depthRenderTarget, normalRenderTarget, saoRenderTarget, beautyRenderTarget, blurIntermediateRenderTarget;
  56. var composer, renderPass, saoPass, copyPass;
  57. var group;
  58. var params = {
  59. output: 0,
  60. saoBias: 0.5,
  61. saoIntensity: 0.25,
  62. saoScale: 1,
  63. saoKernelRadius: 100,
  64. saoMinResolution: 0,
  65. saoBlur: true,
  66. saoBlurRadius: 12,
  67. saoBlurStdDev: 6,
  68. saoBlurDepthCutoff: 0.01
  69. };
  70. var supportsDepthTextureExtension = false;
  71. var isWebGL2 = false;
  72. init();
  73. animate();
  74. function init() {
  75. container = document.createElement( 'div' );
  76. document.body.appendChild( container );
  77. var width = window.innerWidth || 1;
  78. var height = window.innerHeight || 1;
  79. var devicePixelRatio = window.devicePixelRatio || 1;
  80. renderer = new THREE.WebGLRenderer( { antialias: true } );
  81. renderer.setClearColor( 0x000000 );
  82. renderer.setPixelRatio( devicePixelRatio );
  83. renderer.setSize( width, height );
  84. document.body.appendChild( renderer.domElement );
  85. camera = new THREE.PerspectiveCamera( 65, width / height, 3, 10 );
  86. camera.position.z = 7;
  87. scene = new THREE.Scene();
  88. group = new THREE.Object3D();
  89. scene.add( group );
  90. var light = new THREE.PointLight( 0xddffdd, 0.8 );
  91. light.position.z = 70;
  92. light.position.y = -70;
  93. light.position.x = -70;
  94. scene.add( light );
  95. var light2 = new THREE.PointLight( 0xffdddd, 0.8 );
  96. light2.position.z = 70;
  97. light2.position.x = -70;
  98. light2.position.y = 70;
  99. scene.add( light2 );
  100. var light3 = new THREE.PointLight( 0xddddff, 0.8 );
  101. light3.position.z = 70;
  102. light3.position.x = 70;
  103. light3.position.y = -70;
  104. scene.add( light3 );
  105. var light3 = new THREE.AmbientLight( 0xffffff, 0.05 );
  106. scene.add( light3 );
  107. var geometry = new THREE.SphereBufferGeometry( 3, 48, 24 );
  108. for ( var i = 0; i < 120; i ++ ) {
  109. var material = new THREE.MeshStandardMaterial();
  110. material.roughness = 0.5 * Math.random() + 0.25;
  111. material.metalness = 0;
  112. material.color.setHSL( Math.random(), 1.0, 0.3 );
  113. var mesh = new THREE.Mesh( geometry, material );
  114. mesh.position.x = Math.random() * 4 - 2;
  115. mesh.position.y = Math.random() * 4 - 2;
  116. mesh.position.z = Math.random() * 4 - 2;
  117. mesh.rotation.x = Math.random();
  118. mesh.rotation.y = Math.random();
  119. mesh.rotation.z = Math.random();
  120. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 0.2 + 0.05;
  121. group.add( mesh );
  122. }
  123. stats = new Stats();
  124. container.appendChild( stats.dom );
  125. composer = new THREE.EffectComposer( renderer );
  126. renderPass = new THREE.RenderPass( scene, camera );
  127. composer.addPass( renderPass );
  128. saoPass = new THREE.SAOPass( scene, camera, false, true );
  129. saoPass.renderToScreen = true;
  130. composer.addPass( saoPass );
  131. // Init gui
  132. var gui = new dat.GUI();
  133. gui.add( saoPass.params, 'output', {
  134. 'Beauty': THREE.SAOPass.OUTPUT.Beauty,
  135. 'Beauty+SAO': THREE.SAOPass.OUTPUT.Default,
  136. 'SAO': THREE.SAOPass.OUTPUT.SAO,
  137. 'Depth': THREE.SAOPass.OUTPUT.Depth,
  138. 'Normal': THREE.SAOPass.OUTPUT.Normal
  139. } ).onChange( function ( value ) { saoPass.params.output = parseInt( value ); } );
  140. gui.add( saoPass.params, 'saoBias', - 1, 1 );
  141. gui.add( saoPass.params, 'saoIntensity', 0, 1 );
  142. gui.add( saoPass.params, 'saoScale', 0, 10 );
  143. gui.add( saoPass.params, 'saoKernelRadius', 1, 100 );
  144. gui.add( saoPass.params, 'saoMinResolution', 0, 1 );
  145. gui.add( saoPass.params, 'saoBlur' );
  146. gui.add( saoPass.params, 'saoBlurRadius', 0, 200 );
  147. gui.add( saoPass.params, 'saoBlurStdDev', 0.5, 150 );
  148. gui.add( saoPass.params, 'saoBlurDepthCutoff', 0.0, 0.1 );
  149. window.addEventListener( 'resize', onWindowResize, false );
  150. }
  151. function onWindowResize() {
  152. var width = window.innerWidth || 1;
  153. var height = window.innerHeight || 1;
  154. camera.aspect = width / height;
  155. camera.updateProjectionMatrix();
  156. renderer.setSize( width, height );
  157. composer.setSize( width, height );
  158. }
  159. function animate() {
  160. requestAnimationFrame( animate );
  161. stats.begin();
  162. render();
  163. stats.end();
  164. }
  165. function render() {
  166. var timer = performance.now();
  167. group.rotation.x = timer * 0.0002;
  168. group.rotation.y = timer * 0.0001;
  169. composer.render();
  170. }
  171. </script>
  172. </body>
  173. </html>