webgl_postprocessing_ssao.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  35. import { SSAOPass } from 'three/addons/postprocessing/SSAOPass.js';
  36. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  37. let container, stats;
  38. let camera, scene, renderer;
  39. let composer;
  40. let group;
  41. init();
  42. animate();
  43. function init() {
  44. container = document.createElement( 'div' );
  45. document.body.appendChild( container );
  46. renderer = new THREE.WebGLRenderer();
  47. renderer.setSize( window.innerWidth, window.innerHeight );
  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 renderPass = new RenderPass( scene, camera );
  78. composer.addPass( renderPass );
  79. const ssaoPass = new SSAOPass( scene, camera, width, height );
  80. composer.addPass( ssaoPass );
  81. const outputPass = new OutputPass();
  82. composer.addPass( outputPass );
  83. // Init gui
  84. const gui = new GUI();
  85. gui.add( ssaoPass, 'output', {
  86. 'Default': SSAOPass.OUTPUT.Default,
  87. 'SSAO Only': SSAOPass.OUTPUT.SSAO,
  88. 'SSAO Only + Blur': SSAOPass.OUTPUT.Blur,
  89. 'Depth': SSAOPass.OUTPUT.Depth,
  90. 'Normal': SSAOPass.OUTPUT.Normal
  91. } ).onChange( function ( value ) {
  92. ssaoPass.output = 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. gui.add( ssaoPass, 'enabled' );
  98. window.addEventListener( 'resize', onWindowResize );
  99. }
  100. function onWindowResize() {
  101. const width = window.innerWidth;
  102. const height = window.innerHeight;
  103. camera.aspect = width / height;
  104. camera.updateProjectionMatrix();
  105. renderer.setSize( width, height );
  106. composer.setSize( width, height );
  107. }
  108. function animate() {
  109. requestAnimationFrame( animate );
  110. stats.begin();
  111. render();
  112. stats.end();
  113. }
  114. function render() {
  115. const timer = performance.now();
  116. group.rotation.x = timer * 0.0002;
  117. group.rotation.y = timer * 0.0001;
  118. composer.render();
  119. }
  120. </script>
  121. </body>
  122. </html>