webgl_postprocessing_ssao.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 composer;
  55. var group;
  56. init();
  57. animate();
  58. function init() {
  59. container = document.createElement( 'div' );
  60. document.body.appendChild( container );
  61. renderer = new THREE.WebGLRenderer();
  62. renderer.setSize( window.innerWidth, window.innerHeight );
  63. document.body.appendChild( renderer.domElement );
  64. if ( ! renderer.extensions.get( 'WEBGL_depth_texture' ) ) {
  65. document.querySelector( '#error' ).style.display = 'block';
  66. return;
  67. }
  68. camera = new THREE.PerspectiveCamera( 65, window.innerWidth / window.innerHeight, 100, 700 );
  69. camera.position.z = 500;
  70. scene = new THREE.Scene();
  71. scene.background = new THREE.Color( 0xaaaaaa );
  72. scene.add( new THREE.DirectionalLight() );
  73. scene.add( new THREE.HemisphereLight() );
  74. group = new THREE.Group();
  75. scene.add( group );
  76. var geometry = new THREE.BoxBufferGeometry( 10, 10, 10 );
  77. for ( var i = 0; i < 100; i ++ ) {
  78. var material = new THREE.MeshLambertMaterial( {
  79. color: Math.random() * 0xffffff
  80. } );
  81. var mesh = new THREE.Mesh( geometry, material );
  82. mesh.position.x = Math.random() * 400 - 200;
  83. mesh.position.y = Math.random() * 400 - 200;
  84. mesh.position.z = Math.random() * 400 - 200;
  85. mesh.rotation.x = Math.random();
  86. mesh.rotation.y = Math.random();
  87. mesh.rotation.z = Math.random();
  88. mesh.scale.setScalar( Math.random() * 10 + 2 );
  89. group.add( mesh );
  90. }
  91. stats = new Stats();
  92. container.appendChild( stats.dom );
  93. var width = window.innerWidth;
  94. var height = window.innerHeight;
  95. composer = new THREE.EffectComposer( renderer );
  96. var ssaoPass = new THREE.SSAOPass( scene, camera, width, height );
  97. ssaoPass.kernelRadius = 16;
  98. composer.addPass( ssaoPass );
  99. // Init gui
  100. var gui = new dat.GUI();
  101. gui.add( ssaoPass, 'output', {
  102. 'Default': THREE.SSAOPass.OUTPUT.Default,
  103. 'SSAO Only': THREE.SSAOPass.OUTPUT.SSAO,
  104. 'SSAO Only + Blur': THREE.SSAOPass.OUTPUT.Blur,
  105. 'Beauty': THREE.SSAOPass.OUTPUT.Beauty,
  106. 'Depth': THREE.SSAOPass.OUTPUT.Depth,
  107. 'Normal': THREE.SSAOPass.OUTPUT.Normal
  108. } ).onChange( function ( value ) {
  109. ssaoPass.output = parseInt( value );
  110. } );
  111. gui.add( ssaoPass, 'kernelRadius' ).min( 0 ).max( 32 );
  112. gui.add( ssaoPass, 'minDistance' ).min( 0.001 ).max( 0.02 );
  113. gui.add( ssaoPass, 'maxDistance' ).min( 0.01 ).max( 0.3 );
  114. window.addEventListener( 'resize', onWindowResize, false );
  115. }
  116. function onWindowResize() {
  117. var width = window.innerWidth;
  118. var height = window.innerHeight;
  119. camera.aspect = width / height;
  120. camera.updateProjectionMatrix();
  121. renderer.setSize( width, height );
  122. composer.setSize( width, height );
  123. }
  124. function animate() {
  125. requestAnimationFrame( animate );
  126. stats.begin();
  127. render();
  128. stats.end();
  129. }
  130. function render() {
  131. var timer = performance.now();
  132. group.rotation.x = timer * 0.0002;
  133. group.rotation.y = timer * 0.0001;
  134. composer.render();
  135. }
  136. </script>
  137. </body>
  138. </html>