webgl_postprocessing_ssao.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.module.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import Stats from 'three/addons/libs/stats.module.js';
  29. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  30. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  31. import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  32. import { SSAOPass } from 'three/addons/postprocessing/SSAOPass.js';
  33. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  34. let container, stats;
  35. let camera, scene, renderer;
  36. let composer;
  37. let group;
  38. init();
  39. function init() {
  40. container = document.createElement( 'div' );
  41. document.body.appendChild( container );
  42. renderer = new THREE.WebGLRenderer();
  43. renderer.setSize( window.innerWidth, window.innerHeight );
  44. renderer.setAnimationLoop( animate );
  45. document.body.appendChild( renderer.domElement );
  46. camera = new THREE.PerspectiveCamera( 65, window.innerWidth / window.innerHeight, 100, 700 );
  47. camera.position.z = 500;
  48. scene = new THREE.Scene();
  49. scene.background = new THREE.Color( 0xaaaaaa );
  50. scene.add( new THREE.DirectionalLight( 0xffffff, 4 ) );
  51. scene.add( new THREE.AmbientLight( 0xffffff ) );
  52. group = new THREE.Group();
  53. scene.add( group );
  54. const geometry = new THREE.BoxGeometry( 10, 10, 10 );
  55. for ( let i = 0; i < 100; i ++ ) {
  56. const material = new THREE.MeshLambertMaterial( {
  57. color: Math.random() * 0xffffff
  58. } );
  59. const mesh = new THREE.Mesh( geometry, material );
  60. mesh.position.x = Math.random() * 400 - 200;
  61. mesh.position.y = Math.random() * 400 - 200;
  62. mesh.position.z = Math.random() * 400 - 200;
  63. mesh.rotation.x = Math.random();
  64. mesh.rotation.y = Math.random();
  65. mesh.rotation.z = Math.random();
  66. mesh.scale.setScalar( Math.random() * 10 + 2 );
  67. group.add( mesh );
  68. }
  69. stats = new Stats();
  70. container.appendChild( stats.dom );
  71. const width = window.innerWidth;
  72. const height = window.innerHeight;
  73. composer = new EffectComposer( renderer );
  74. const renderPass = new RenderPass( scene, camera );
  75. composer.addPass( renderPass );
  76. const ssaoPass = new SSAOPass( scene, camera, width, height );
  77. composer.addPass( ssaoPass );
  78. const outputPass = new OutputPass();
  79. composer.addPass( outputPass );
  80. // Init gui
  81. const gui = new GUI();
  82. gui.add( ssaoPass, 'output', {
  83. 'Default': SSAOPass.OUTPUT.Default,
  84. 'SSAO Only': SSAOPass.OUTPUT.SSAO,
  85. 'SSAO Only + Blur': SSAOPass.OUTPUT.Blur,
  86. 'Depth': SSAOPass.OUTPUT.Depth,
  87. 'Normal': SSAOPass.OUTPUT.Normal
  88. } ).onChange( function ( value ) {
  89. ssaoPass.output = 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. gui.add( ssaoPass, 'enabled' );
  95. window.addEventListener( 'resize', onWindowResize );
  96. }
  97. function onWindowResize() {
  98. const width = window.innerWidth;
  99. const height = window.innerHeight;
  100. camera.aspect = width / height;
  101. camera.updateProjectionMatrix();
  102. renderer.setSize( width, height );
  103. composer.setSize( width, height );
  104. }
  105. function 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>