webgl_postprocessing_ssao.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 id="error" style="display: none;">
  18. Your browser does not support <strong>WEBGL_depth_texture</strong>.<br/><br/>
  19. This demo will not work.
  20. </div>
  21. </div>
  22. <script type="module">
  23. import * as THREE from '../build/three.module.js';
  24. import Stats from './jsm/libs/stats.module.js';
  25. import { GUI } from './jsm/libs/dat.gui.module.js';
  26. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  27. import { SSAOPass } from './jsm/postprocessing/SSAOPass.js';
  28. var container, stats;
  29. var camera, scene, renderer;
  30. var composer;
  31. var group;
  32. init();
  33. animate();
  34. function init() {
  35. container = document.createElement( 'div' );
  36. document.body.appendChild( container );
  37. renderer = new THREE.WebGLRenderer();
  38. renderer.setSize( window.innerWidth, window.innerHeight );
  39. document.body.appendChild( renderer.domElement );
  40. if ( ! renderer.extensions.get( 'WEBGL_depth_texture' ) ) {
  41. document.querySelector( '#error' ).style.display = 'block';
  42. return;
  43. }
  44. camera = new THREE.PerspectiveCamera( 65, window.innerWidth / window.innerHeight, 100, 700 );
  45. camera.position.z = 500;
  46. scene = new THREE.Scene();
  47. scene.background = new THREE.Color( 0xaaaaaa );
  48. scene.add( new THREE.DirectionalLight() );
  49. scene.add( new THREE.HemisphereLight() );
  50. group = new THREE.Group();
  51. scene.add( group );
  52. var geometry = new THREE.BoxBufferGeometry( 10, 10, 10 );
  53. for ( var i = 0; i < 100; i ++ ) {
  54. var material = new THREE.MeshLambertMaterial( {
  55. color: Math.random() * 0xffffff
  56. } );
  57. var mesh = new THREE.Mesh( geometry, material );
  58. mesh.position.x = Math.random() * 400 - 200;
  59. mesh.position.y = Math.random() * 400 - 200;
  60. mesh.position.z = Math.random() * 400 - 200;
  61. mesh.rotation.x = Math.random();
  62. mesh.rotation.y = Math.random();
  63. mesh.rotation.z = Math.random();
  64. mesh.scale.setScalar( Math.random() * 10 + 2 );
  65. group.add( mesh );
  66. }
  67. stats = new Stats();
  68. container.appendChild( stats.dom );
  69. var width = window.innerWidth;
  70. var height = window.innerHeight;
  71. composer = new EffectComposer( renderer );
  72. var ssaoPass = new SSAOPass( scene, camera, width, height );
  73. ssaoPass.kernelRadius = 16;
  74. composer.addPass( ssaoPass );
  75. // Init gui
  76. var gui = new GUI();
  77. gui.add( ssaoPass, 'output', {
  78. 'Default': SSAOPass.OUTPUT.Default,
  79. 'SSAO Only': SSAOPass.OUTPUT.SSAO,
  80. 'SSAO Only + Blur': SSAOPass.OUTPUT.Blur,
  81. 'Beauty': SSAOPass.OUTPUT.Beauty,
  82. 'Depth': SSAOPass.OUTPUT.Depth,
  83. 'Normal': SSAOPass.OUTPUT.Normal
  84. } ).onChange( function ( value ) {
  85. ssaoPass.output = parseInt( value );
  86. } );
  87. gui.add( ssaoPass, 'kernelRadius' ).min( 0 ).max( 32 );
  88. gui.add( ssaoPass, 'minDistance' ).min( 0.001 ).max( 0.02 );
  89. gui.add( ssaoPass, 'maxDistance' ).min( 0.01 ).max( 0.3 );
  90. window.addEventListener( 'resize', onWindowResize, false );
  91. }
  92. function onWindowResize() {
  93. var width = window.innerWidth;
  94. var height = window.innerHeight;
  95. camera.aspect = width / height;
  96. camera.updateProjectionMatrix();
  97. renderer.setSize( width, height );
  98. composer.setSize( width, height );
  99. }
  100. function animate() {
  101. requestAnimationFrame( animate );
  102. stats.begin();
  103. render();
  104. stats.end();
  105. }
  106. function render() {
  107. var timer = performance.now();
  108. group.rotation.x = timer * 0.0002;
  109. group.rotation.y = timer * 0.0001;
  110. composer.render();
  111. }
  112. </script>
  113. </body>
  114. </html>