webgl_postprocessing_sao.html 5.6 KB

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