webgl_postprocessing_ssao.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. body {
  10. background-color: #aaa;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <div id="info">
  16. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - screen space ambient occlusion<br/>
  17. <div id="error" style="display: none;">
  18. Your browser does not support <strong>WEBGL_depth_texture</strong>.<br/><br/>
  19. This demo will not work.
  20. </div>
  21. </div>
  22. <script type="module">
  23. import {
  24. BoxBufferGeometry,
  25. Color,
  26. DirectionalLight,
  27. Group,
  28. HemisphereLight,
  29. Mesh,
  30. MeshLambertMaterial,
  31. PerspectiveCamera,
  32. Scene,
  33. WebGLRenderer
  34. } from "../build/three.module.js";
  35. import Stats from './jsm/libs/stats.module.js';
  36. import { GUI } from './jsm/libs/dat.gui.module.js';
  37. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  38. import { SSAOPass } from './jsm/postprocessing/SSAOPass.js';
  39. var container, stats;
  40. var camera, scene, renderer;
  41. var composer;
  42. var group;
  43. init();
  44. animate();
  45. function init() {
  46. container = document.createElement( 'div' );
  47. document.body.appendChild( container );
  48. renderer = new WebGLRenderer();
  49. renderer.setSize( window.innerWidth, window.innerHeight );
  50. document.body.appendChild( renderer.domElement );
  51. if ( ! renderer.extensions.get( 'WEBGL_depth_texture' ) ) {
  52. document.querySelector( '#error' ).style.display = 'block';
  53. return;
  54. }
  55. camera = new PerspectiveCamera( 65, window.innerWidth / window.innerHeight, 100, 700 );
  56. camera.position.z = 500;
  57. scene = new Scene();
  58. scene.background = new Color( 0xaaaaaa );
  59. scene.add( new DirectionalLight() );
  60. scene.add( new HemisphereLight() );
  61. group = new Group();
  62. scene.add( group );
  63. var geometry = new BoxBufferGeometry( 10, 10, 10 );
  64. for ( var i = 0; i < 100; i ++ ) {
  65. var material = new MeshLambertMaterial( {
  66. color: Math.random() * 0xffffff
  67. } );
  68. var mesh = new Mesh( geometry, material );
  69. mesh.position.x = Math.random() * 400 - 200;
  70. mesh.position.y = Math.random() * 400 - 200;
  71. mesh.position.z = Math.random() * 400 - 200;
  72. mesh.rotation.x = Math.random();
  73. mesh.rotation.y = Math.random();
  74. mesh.rotation.z = Math.random();
  75. mesh.scale.setScalar( Math.random() * 10 + 2 );
  76. group.add( mesh );
  77. }
  78. stats = new Stats();
  79. container.appendChild( stats.dom );
  80. var width = window.innerWidth;
  81. var height = window.innerHeight;
  82. composer = new EffectComposer( renderer );
  83. var ssaoPass = new SSAOPass( scene, camera, width, height );
  84. ssaoPass.kernelRadius = 16;
  85. composer.addPass( ssaoPass );
  86. // Init gui
  87. var gui = new GUI();
  88. gui.add( ssaoPass, 'output', {
  89. 'Default': SSAOPass.OUTPUT.Default,
  90. 'SSAO Only': SSAOPass.OUTPUT.SSAO,
  91. 'SSAO Only + Blur': SSAOPass.OUTPUT.Blur,
  92. 'Beauty': SSAOPass.OUTPUT.Beauty,
  93. 'Depth': SSAOPass.OUTPUT.Depth,
  94. 'Normal': SSAOPass.OUTPUT.Normal
  95. } ).onChange( function ( value ) {
  96. ssaoPass.output = parseInt( value );
  97. } );
  98. gui.add( ssaoPass, 'kernelRadius' ).min( 0 ).max( 32 );
  99. gui.add( ssaoPass, 'minDistance' ).min( 0.001 ).max( 0.02 );
  100. gui.add( ssaoPass, 'maxDistance' ).min( 0.01 ).max( 0.3 );
  101. window.addEventListener( 'resize', onWindowResize, false );
  102. }
  103. function onWindowResize() {
  104. var width = window.innerWidth;
  105. var height = window.innerHeight;
  106. camera.aspect = width / height;
  107. camera.updateProjectionMatrix();
  108. renderer.setSize( width, height );
  109. composer.setSize( width, height );
  110. }
  111. function animate() {
  112. requestAnimationFrame( animate );
  113. stats.begin();
  114. render();
  115. stats.end();
  116. }
  117. function render() {
  118. var timer = performance.now();
  119. group.rotation.x = timer * 0.0002;
  120. group.rotation.y = timer * 0.0001;
  121. composer.render();
  122. }
  123. </script>
  124. </body>
  125. </html>