webgl_postprocessing_ssao.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. <!-- Import maps polyfill -->
  19. <!-- Remove this when import maps will be widely supported -->
  20. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  21. <script type="importmap">
  22. {
  23. "imports": {
  24. "three": "../build/three.module.js",
  25. "three/addons/": "./jsm/"
  26. }
  27. }
  28. </script>
  29. <script type="module">
  30. import * as THREE from 'three';
  31. import Stats from 'three/addons/libs/stats.module.js';
  32. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  33. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  34. import { SSAOPass } from 'three/addons/postprocessing/SSAOPass.js';
  35. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  36. let container, stats;
  37. let camera, scene, renderer;
  38. let composer;
  39. let group;
  40. init();
  41. animate();
  42. function init() {
  43. container = document.createElement( 'div' );
  44. document.body.appendChild( container );
  45. renderer = new THREE.WebGLRenderer();
  46. renderer.setSize( window.innerWidth, window.innerHeight );
  47. renderer.useLegacyLights = false;
  48. document.body.appendChild( renderer.domElement );
  49. camera = new THREE.PerspectiveCamera( 65, window.innerWidth / window.innerHeight, 100, 700 );
  50. camera.position.z = 500;
  51. scene = new THREE.Scene();
  52. scene.background = new THREE.Color( 0xaaaaaa );
  53. scene.add( new THREE.DirectionalLight( 0xffffff, 4 ) );
  54. scene.add( new THREE.AmbientLight( 0xffffff ) );
  55. group = new THREE.Group();
  56. scene.add( group );
  57. const geometry = new THREE.BoxGeometry( 10, 10, 10 );
  58. for ( let i = 0; i < 100; i ++ ) {
  59. const material = new THREE.MeshLambertMaterial( {
  60. color: Math.random() * 0xffffff
  61. } );
  62. const mesh = new THREE.Mesh( geometry, material );
  63. mesh.position.x = Math.random() * 400 - 200;
  64. mesh.position.y = Math.random() * 400 - 200;
  65. mesh.position.z = Math.random() * 400 - 200;
  66. mesh.rotation.x = Math.random();
  67. mesh.rotation.y = Math.random();
  68. mesh.rotation.z = Math.random();
  69. mesh.scale.setScalar( Math.random() * 10 + 2 );
  70. group.add( mesh );
  71. }
  72. stats = new Stats();
  73. container.appendChild( stats.dom );
  74. const width = window.innerWidth;
  75. const height = window.innerHeight;
  76. composer = new EffectComposer( renderer );
  77. const ssaoPass = new SSAOPass( scene, camera, width, height );
  78. ssaoPass.kernelRadius = 16;
  79. composer.addPass( ssaoPass );
  80. const outputPass = new OutputPass();
  81. composer.addPass( outputPass );
  82. // Init gui
  83. const gui = new GUI();
  84. gui.add( ssaoPass, 'output', {
  85. 'Default': SSAOPass.OUTPUT.Default,
  86. 'SSAO Only': SSAOPass.OUTPUT.SSAO,
  87. 'SSAO Only + Blur': SSAOPass.OUTPUT.Blur,
  88. 'Beauty': SSAOPass.OUTPUT.Beauty,
  89. 'Depth': SSAOPass.OUTPUT.Depth,
  90. 'Normal': SSAOPass.OUTPUT.Normal
  91. } ).onChange( function ( value ) {
  92. ssaoPass.output = parseInt( value );
  93. } );
  94. gui.add( ssaoPass, 'kernelRadius' ).min( 0 ).max( 32 );
  95. gui.add( ssaoPass, 'minDistance' ).min( 0.001 ).max( 0.02 );
  96. gui.add( ssaoPass, 'maxDistance' ).min( 0.01 ).max( 0.3 );
  97. window.addEventListener( 'resize', onWindowResize );
  98. }
  99. function onWindowResize() {
  100. const width = window.innerWidth;
  101. const height = window.innerHeight;
  102. camera.aspect = width / height;
  103. camera.updateProjectionMatrix();
  104. renderer.setSize( width, height );
  105. composer.setSize( width, height );
  106. }
  107. function animate() {
  108. requestAnimationFrame( animate );
  109. stats.begin();
  110. render();
  111. stats.end();
  112. }
  113. function render() {
  114. const timer = performance.now();
  115. group.rotation.x = timer * 0.0002;
  116. group.rotation.y = timer * 0.0001;
  117. composer.render();
  118. }
  119. </script>
  120. </body>
  121. </html>