webgl_postprocessing_ssao.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. 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( 0xffffff, 4 ) );
  53. scene.add( new THREE.AmbientLight( 0xffffff ) );
  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. const outputPass = new OutputPass();
  80. composer.addPass( outputPass );
  81. // Init gui
  82. const gui = new GUI();
  83. gui.add( ssaoPass, 'output', {
  84. 'Default': SSAOPass.OUTPUT.Default,
  85. 'SSAO Only': SSAOPass.OUTPUT.SSAO,
  86. 'SSAO Only + Blur': SSAOPass.OUTPUT.Blur,
  87. 'Beauty': SSAOPass.OUTPUT.Beauty,
  88. 'Depth': SSAOPass.OUTPUT.Depth,
  89. 'Normal': SSAOPass.OUTPUT.Normal
  90. } ).onChange( function ( value ) {
  91. ssaoPass.output = parseInt( value );
  92. } );
  93. gui.add( ssaoPass, 'kernelRadius' ).min( 0 ).max( 32 );
  94. gui.add( ssaoPass, 'minDistance' ).min( 0.001 ).max( 0.02 );
  95. gui.add( ssaoPass, 'maxDistance' ).min( 0.01 ).max( 0.3 );
  96. window.addEventListener( 'resize', onWindowResize );
  97. }
  98. function onWindowResize() {
  99. const width = window.innerWidth;
  100. const height = window.innerHeight;
  101. camera.aspect = width / height;
  102. camera.updateProjectionMatrix();
  103. renderer.setSize( width, height );
  104. composer.setSize( width, height );
  105. }
  106. function animate() {
  107. requestAnimationFrame( animate );
  108. stats.begin();
  109. render();
  110. stats.end();
  111. }
  112. function render() {
  113. const timer = performance.now();
  114. group.rotation.x = timer * 0.0002;
  115. group.rotation.y = timer * 0.0001;
  116. composer.render();
  117. }
  118. </script>
  119. </body>
  120. </html>