webgl_postprocessing_sao.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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/Detector.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 ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  50. var container, stats;
  51. var camera, scene, renderer;
  52. var depthMaterial, saoMaterial, saoModulateMaterial, normalMaterial, vBlurMaterial, hBlurMaterial, copyMaterial;
  53. var depthRenderTarget, normalRenderTarget, saoRenderTarget, beautyRenderTarget, blurIntermediateRenderTarget;
  54. var composer, renderPass, saoPass, copyPass;
  55. var group;
  56. var params = {
  57. output: 0,
  58. saoBias: 0.5,
  59. saoIntensity: 0.25,
  60. saoScale: 1,
  61. saoKernelRadius: 100,
  62. saoMinResolution: 0,
  63. saoBlur: true,
  64. saoBlurRadius: 12,
  65. saoBlurStdDev: 6,
  66. saoBlurDepthCutoff: 0.01
  67. }
  68. var supportsDepthTextureExtension = false;
  69. var isWebGL2 = false;
  70. init();
  71. animate();
  72. function init() {
  73. container = document.createElement( 'div' );
  74. document.body.appendChild( container );
  75. var width = window.innerWidth || 1;
  76. var height = window.innerHeight || 1;
  77. var devicePixelRatio = window.devicePixelRatio || 1;
  78. renderer = new THREE.WebGLRenderer( { antialias: true } );
  79. renderer.setClearColor( 0x000000 );
  80. renderer.setPixelRatio( devicePixelRatio );
  81. renderer.setSize( width, height );
  82. document.body.appendChild( renderer.domElement );
  83. camera = new THREE.PerspectiveCamera( 65, width / height, 3, 10 );
  84. camera.position.z = 7;
  85. scene = new THREE.Scene();
  86. group = new THREE.Object3D();
  87. scene.add( group );
  88. var light = new THREE.PointLight( 0xddffdd, 0.8 );
  89. light.position.z = 70;
  90. light.position.y = -70;
  91. light.position.x = -70;
  92. scene.add( light );
  93. var light2 = new THREE.PointLight( 0xffdddd, 0.8 );
  94. light2.position.z = 70;
  95. light2.position.x = -70;
  96. light2.position.y = 70;
  97. scene.add( light2 );
  98. var light3 = new THREE.PointLight( 0xddddff, 0.8 );
  99. light3.position.z = 70;
  100. light3.position.x = 70;
  101. light3.position.y = -70;
  102. scene.add( light3 );
  103. var light3 = new THREE.AmbientLight( 0xffffff, 0.05 );
  104. scene.add( light3 );
  105. var geometry = new THREE.SphereBufferGeometry( 3, 48, 24 );
  106. for ( var i = 0; i < 120; i ++ ) {
  107. var material = new THREE.MeshStandardMaterial();
  108. material.roughness = 0.5 * Math.random() + 0.25;
  109. material.metalness = 0;
  110. material.color.setHSL( Math.random(), 1.0, 0.3 );
  111. var mesh = new THREE.Mesh( geometry, material );
  112. mesh.position.x = Math.random() * 4 - 2;
  113. mesh.position.y = Math.random() * 4 - 2;
  114. mesh.position.z = Math.random() * 4 - 2;
  115. mesh.rotation.x = Math.random();
  116. mesh.rotation.y = Math.random();
  117. mesh.rotation.z = Math.random();
  118. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 0.2 + 0.05;
  119. group.add( mesh );
  120. }
  121. stats = new Stats();
  122. container.appendChild( stats.dom );
  123. composer = new THREE.EffectComposer( renderer );
  124. renderPass = new THREE.RenderPass( scene, camera );
  125. composer.addPass( renderPass );
  126. saoPass = new THREE.SAOPass( scene, camera, false, true );
  127. saoPass.renderToScreen = true;
  128. composer.addPass( saoPass );
  129. // Init gui
  130. var gui = new dat.GUI();
  131. gui.add( saoPass.params, 'output', {
  132. 'Beauty': THREE.SAOPass.OUTPUT.Beauty,
  133. 'Beauty+SAO': THREE.SAOPass.OUTPUT.Default,
  134. 'SAO': THREE.SAOPass.OUTPUT.SAO,
  135. 'Depth': THREE.SAOPass.OUTPUT.Depth,
  136. 'Normal': THREE.SAOPass.OUTPUT.Normal
  137. } ).onChange( function ( value ) { saoPass.params.output = parseInt( value ); } );
  138. gui.add( saoPass.params, 'saoBias', - 1, 1 );
  139. gui.add( saoPass.params, 'saoIntensity', 0, 1 );
  140. gui.add( saoPass.params, 'saoScale', 0, 10 );
  141. gui.add( saoPass.params, 'saoKernelRadius', 1, 100 );
  142. gui.add( saoPass.params, 'saoMinResolution', 0, 1 );
  143. gui.add( saoPass.params, 'saoBlur' );
  144. gui.add( saoPass.params, 'saoBlurRadius', 0, 200 );
  145. gui.add( saoPass.params, 'saoBlurStdDev', 0.5, 150 );
  146. gui.add( saoPass.params, 'saoBlurDepthCutoff', 0.0, 0.1 );
  147. window.addEventListener( 'resize', onWindowResize, false );
  148. }
  149. function onWindowResize() {
  150. var width = window.innerWidth || 1;
  151. var height = window.innerHeight || 1;
  152. var devicePixelRatio = window.devicePixelRatio || 1;
  153. camera.aspect = width / height;
  154. camera.updateProjectionMatrix();
  155. renderer.setSize( width, height );
  156. composer.setSize( width, height );
  157. }
  158. function animate() {
  159. requestAnimationFrame( animate );
  160. stats.begin();
  161. render();
  162. stats.end();
  163. }
  164. function render() {
  165. var timer = performance.now();
  166. group.rotation.x = timer * 0.0002;
  167. group.rotation.y = timer * 0.0001;
  168. composer.render();
  169. }
  170. </script>
  171. </body>
  172. </html>