webgl_postprocessing_ssao.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - postprocessing - Screen Space Ambient Occlusion</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/shaders/SSAOShader.js"></script>
  33. <script src="js/postprocessing/EffectComposer.js"></script>
  34. <script src="js/postprocessing/ShaderPass.js"></script>
  35. <script src="js/postprocessing/SSAOPass.js"></script>
  36. <script src="js/shaders/CopyShader.js"></script>
  37. <script src="js/SimplexNoise.js"></script>
  38. <script src="js/WebGL.js"></script>
  39. <script src="js/libs/stats.min.js"></script>
  40. <script src='js/libs/dat.gui.min.js'></script>
  41. <div id="info">
  42. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - screen space ambient occlusion<br/>
  43. <div id="error" style="display: none;">
  44. Your browser does not support <strong>WEBGL_depth_texture</strong>.<br/><br/>
  45. This demo will not work.
  46. </div>
  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 effectComposer;
  55. var ssaoPass;
  56. var group;
  57. init();
  58. animate();
  59. function init() {
  60. container = document.createElement( 'div' );
  61. document.body.appendChild( container );
  62. renderer = new THREE.WebGLRenderer();
  63. renderer.setSize( window.innerWidth, window.innerHeight );
  64. document.body.appendChild( renderer.domElement );
  65. if ( ! renderer.extensions.get( 'WEBGL_depth_texture' ) ) {
  66. document.querySelector( '#error' ).style.display = 'block';
  67. return;
  68. }
  69. camera = new THREE.PerspectiveCamera( 65, window.innerWidth / window.innerHeight, 100, 700 );
  70. camera.position.z = 500;
  71. scene = new THREE.Scene();
  72. scene.background = new THREE.Color( 0xaaaaaa );
  73. scene.add( new THREE.DirectionalLight() );
  74. scene.add( new THREE.HemisphereLight() );
  75. group = new THREE.Group();
  76. scene.add( group );
  77. var geometry = new THREE.BoxBufferGeometry( 10, 10, 10 );
  78. for ( var i = 0; i < 100; i ++ ) {
  79. var material = new THREE.MeshLambertMaterial( {
  80. color: Math.random() * 0xffffff
  81. } );
  82. var mesh = new THREE.Mesh( geometry, material );
  83. mesh.position.x = Math.random() * 400 - 200;
  84. mesh.position.y = Math.random() * 400 - 200;
  85. mesh.position.z = Math.random() * 400 - 200;
  86. mesh.rotation.x = Math.random();
  87. mesh.rotation.y = Math.random();
  88. mesh.rotation.z = Math.random();
  89. mesh.scale.setScalar( Math.random() * 10 + 2 );
  90. group.add( mesh );
  91. }
  92. stats = new Stats();
  93. container.appendChild( stats.dom );
  94. var width = window.innerWidth;
  95. var height = window.innerHeight;
  96. ssaoPass = new THREE.SSAOPass( scene, camera, width, height );
  97. ssaoPass.kernelRadius = 16;
  98. effectComposer = new THREE.EffectComposer( renderer );
  99. effectComposer.addPass( ssaoPass );
  100. // Init gui
  101. var gui = new dat.GUI();
  102. gui.add( ssaoPass, 'output', {
  103. 'Default': THREE.SSAOPass.OUTPUT.Default,
  104. 'SSAO Only': THREE.SSAOPass.OUTPUT.SSAO,
  105. 'SSAO Only + Blur': THREE.SSAOPass.OUTPUT.Blur,
  106. 'Beauty': THREE.SSAOPass.OUTPUT.Beauty,
  107. 'Depth': THREE.SSAOPass.OUTPUT.Depth,
  108. 'Normal': THREE.SSAOPass.OUTPUT.Normal
  109. } ).onChange( function ( value ) {
  110. ssaoPass.output = parseInt( value );
  111. } );
  112. gui.add( ssaoPass, 'kernelRadius' ).min( 0 ).max( 32 );
  113. gui.add( ssaoPass, 'minDistance' ).min( 0.001 ).max( 0.02 );
  114. gui.add( ssaoPass, 'maxDistance' ).min( 0.01 ).max( 0.3 );
  115. window.addEventListener( 'resize', onWindowResize, false );
  116. onWindowResize();
  117. }
  118. function onWindowResize() {
  119. var width = window.innerWidth;
  120. var height = window.innerHeight;
  121. camera.aspect = width / height;
  122. camera.updateProjectionMatrix();
  123. renderer.setSize( width, height );
  124. ssaoPass.setSize( width, height );
  125. }
  126. function animate() {
  127. requestAnimationFrame( animate );
  128. stats.begin();
  129. render();
  130. stats.end();
  131. }
  132. function render() {
  133. var timer = performance.now();
  134. group.rotation.x = timer * 0.0002;
  135. group.rotation.y = timer * 0.0001;
  136. effectComposer.render();
  137. }
  138. </script>
  139. </body>
  140. </html>