webgl_postprocessing_ssao.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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="https://threejs.org" target="_blank" rel="noopener">three.js</a> - screen space ambient occlusion<br/>
  17. </div>
  18. <script type="module">
  19. import * as THREE from '../build/three.module.js';
  20. import Stats from './jsm/libs/stats.module.js';
  21. import { GUI } from './jsm/libs/dat.gui.module.js';
  22. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  23. import { SSAOPass } from './jsm/postprocessing/SSAOPass.js';
  24. let container, stats;
  25. let camera, scene, renderer;
  26. let composer;
  27. let group;
  28. init();
  29. animate();
  30. function init() {
  31. container = document.createElement( 'div' );
  32. document.body.appendChild( container );
  33. renderer = new THREE.WebGLRenderer();
  34. renderer.setSize( window.innerWidth, window.innerHeight );
  35. document.body.appendChild( renderer.domElement );
  36. camera = new THREE.PerspectiveCamera( 65, window.innerWidth / window.innerHeight, 100, 700 );
  37. camera.position.z = 500;
  38. scene = new THREE.Scene();
  39. scene.background = new THREE.Color( 0xaaaaaa );
  40. scene.add( new THREE.DirectionalLight() );
  41. scene.add( new THREE.HemisphereLight() );
  42. group = new THREE.Group();
  43. scene.add( group );
  44. const geometry = new THREE.BoxGeometry( 10, 10, 10 );
  45. for ( let i = 0; i < 100; i ++ ) {
  46. const material = new THREE.MeshLambertMaterial( {
  47. color: Math.random() * 0xffffff
  48. } );
  49. const mesh = new THREE.Mesh( geometry, material );
  50. mesh.position.x = Math.random() * 400 - 200;
  51. mesh.position.y = Math.random() * 400 - 200;
  52. mesh.position.z = Math.random() * 400 - 200;
  53. mesh.rotation.x = Math.random();
  54. mesh.rotation.y = Math.random();
  55. mesh.rotation.z = Math.random();
  56. mesh.scale.setScalar( Math.random() * 10 + 2 );
  57. group.add( mesh );
  58. }
  59. stats = new Stats();
  60. container.appendChild( stats.dom );
  61. const width = window.innerWidth;
  62. const height = window.innerHeight;
  63. composer = new EffectComposer( renderer );
  64. const ssaoPass = new SSAOPass( scene, camera, width, height );
  65. ssaoPass.kernelRadius = 16;
  66. composer.addPass( ssaoPass );
  67. // Init gui
  68. const gui = new GUI();
  69. gui.add( ssaoPass, 'output', {
  70. 'Default': SSAOPass.OUTPUT.Default,
  71. 'SSAO Only': SSAOPass.OUTPUT.SSAO,
  72. 'SSAO Only + Blur': SSAOPass.OUTPUT.Blur,
  73. 'Beauty': SSAOPass.OUTPUT.Beauty,
  74. 'Depth': SSAOPass.OUTPUT.Depth,
  75. 'Normal': SSAOPass.OUTPUT.Normal
  76. } ).onChange( function ( value ) {
  77. ssaoPass.output = parseInt( value );
  78. } );
  79. gui.add( ssaoPass, 'kernelRadius' ).min( 0 ).max( 32 );
  80. gui.add( ssaoPass, 'minDistance' ).min( 0.001 ).max( 0.02 );
  81. gui.add( ssaoPass, 'maxDistance' ).min( 0.01 ).max( 0.3 );
  82. window.addEventListener( 'resize', onWindowResize );
  83. }
  84. function onWindowResize() {
  85. const width = window.innerWidth;
  86. const height = window.innerHeight;
  87. camera.aspect = width / height;
  88. camera.updateProjectionMatrix();
  89. renderer.setSize( width, height );
  90. composer.setSize( width, height );
  91. }
  92. function animate() {
  93. requestAnimationFrame( animate );
  94. stats.begin();
  95. render();
  96. stats.end();
  97. }
  98. function render() {
  99. const timer = performance.now();
  100. group.rotation.x = timer * 0.0002;
  101. group.rotation.y = timer * 0.0001;
  102. composer.render();
  103. }
  104. </script>
  105. </body>
  106. </html>