webgl_postprocessing_ssao.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. THREE.ColorManagement.enabled = false; // TODO: Consider enabling color management.
  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. document.body.appendChild( renderer.domElement );
  48. camera = new THREE.PerspectiveCamera( 65, window.innerWidth / window.innerHeight, 100, 700 );
  49. camera.position.z = 500;
  50. scene = new THREE.Scene();
  51. scene.background = new THREE.Color( 0xaaaaaa );
  52. scene.add( new THREE.DirectionalLight() );
  53. scene.add( new THREE.HemisphereLight() );
  54. group = new THREE.Group();
  55. scene.add( group );
  56. const geometry = new THREE.BoxGeometry( 10, 10, 10 );
  57. for ( let i = 0; i < 100; i ++ ) {
  58. const material = new THREE.MeshLambertMaterial( {
  59. color: Math.random() * 0xffffff
  60. } );
  61. const mesh = new THREE.Mesh( geometry, material );
  62. mesh.position.x = Math.random() * 400 - 200;
  63. mesh.position.y = Math.random() * 400 - 200;
  64. mesh.position.z = Math.random() * 400 - 200;
  65. mesh.rotation.x = Math.random();
  66. mesh.rotation.y = Math.random();
  67. mesh.rotation.z = Math.random();
  68. mesh.scale.setScalar( Math.random() * 10 + 2 );
  69. group.add( mesh );
  70. }
  71. stats = new Stats();
  72. container.appendChild( stats.dom );
  73. const width = window.innerWidth;
  74. const height = window.innerHeight;
  75. composer = new EffectComposer( renderer );
  76. const ssaoPass = new SSAOPass( scene, camera, width, height );
  77. ssaoPass.kernelRadius = 16;
  78. composer.addPass( ssaoPass );
  79. // Init gui
  80. const gui = new GUI();
  81. gui.add( ssaoPass, 'output', {
  82. 'Default': SSAOPass.OUTPUT.Default,
  83. 'SSAO Only': SSAOPass.OUTPUT.SSAO,
  84. 'SSAO Only + Blur': SSAOPass.OUTPUT.Blur,
  85. 'Beauty': SSAOPass.OUTPUT.Beauty,
  86. 'Depth': SSAOPass.OUTPUT.Depth,
  87. 'Normal': SSAOPass.OUTPUT.Normal
  88. } ).onChange( function ( value ) {
  89. ssaoPass.output = parseInt( value );
  90. } );
  91. gui.add( ssaoPass, 'kernelRadius' ).min( 0 ).max( 32 );
  92. gui.add( ssaoPass, 'minDistance' ).min( 0.001 ).max( 0.02 );
  93. gui.add( ssaoPass, 'maxDistance' ).min( 0.01 ).max( 0.3 );
  94. window.addEventListener( 'resize', onWindowResize );
  95. }
  96. function onWindowResize() {
  97. const width = window.innerWidth;
  98. const height = window.innerHeight;
  99. camera.aspect = width / height;
  100. camera.updateProjectionMatrix();
  101. renderer.setSize( width, height );
  102. composer.setSize( width, height );
  103. }
  104. function animate() {
  105. requestAnimationFrame( animate );
  106. stats.begin();
  107. render();
  108. stats.end();
  109. }
  110. function render() {
  111. const timer = performance.now();
  112. group.rotation.x = timer * 0.0002;
  113. group.rotation.y = timer * 0.0001;
  114. composer.render();
  115. }
  116. </script>
  117. </body>
  118. </html>