webgl_postprocessing_ssao.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. <meta property="og:title" content="three.js webgl - postprocessing - Screen Space Ambient Occlusion">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_postprocessing_ssao.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_postprocessing_ssao.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. <style>
  13. body {
  14. background-color: #aaa;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="info">
  20. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - screen space ambient occlusion<br/>
  21. </div>
  22. <script type="importmap">
  23. {
  24. "imports": {
  25. "three": "../build/three.module.js",
  26. "three/addons/": "./jsm/"
  27. }
  28. }
  29. </script>
  30. <script type="module">
  31. import * as THREE from 'three';
  32. import Stats from 'three/addons/libs/stats.module.js';
  33. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  34. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  35. import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  36. import { SSAOPass } from 'three/addons/postprocessing/SSAOPass.js';
  37. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  38. let container, stats;
  39. let camera, scene, renderer;
  40. let composer;
  41. let group;
  42. init();
  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. renderer.setAnimationLoop( animate );
  49. document.body.appendChild( renderer.domElement );
  50. camera = new THREE.PerspectiveCamera( 65, window.innerWidth / window.innerHeight, 100, 700 );
  51. camera.position.z = 500;
  52. scene = new THREE.Scene();
  53. scene.background = new THREE.Color( 0xaaaaaa );
  54. scene.add( new THREE.DirectionalLight( 0xffffff, 4 ) );
  55. scene.add( new THREE.AmbientLight( 0xffffff ) );
  56. group = new THREE.Group();
  57. scene.add( group );
  58. const geometry = new THREE.BoxGeometry( 10, 10, 10 );
  59. const material = new THREE.MeshLambertMaterial();
  60. const mesh = new THREE.InstancedMesh( geometry, material, 100 );
  61. const dummy = new THREE.Object3D();
  62. const color = new THREE.Color();
  63. for ( let i = 0; i < 100; i ++ ) {
  64. dummy.position.x = Math.random() * 400 - 200;
  65. dummy.position.y = Math.random() * 400 - 200;
  66. dummy.position.z = Math.random() * 400 - 200;
  67. dummy.rotation.x = Math.random();
  68. dummy.rotation.y = Math.random();
  69. dummy.rotation.z = Math.random();
  70. dummy.scale.setScalar( Math.random() * 10 + 2 );
  71. dummy.updateMatrix();
  72. mesh.setMatrixAt( i, dummy.matrix );
  73. color.setHex( Math.random() * 0xffffff );
  74. mesh.setColorAt( i, color );
  75. }
  76. group.add( mesh );
  77. stats = new Stats();
  78. container.appendChild( stats.dom );
  79. const width = window.innerWidth;
  80. const height = window.innerHeight;
  81. composer = new EffectComposer( renderer );
  82. const renderPass = new RenderPass( scene, camera );
  83. composer.addPass( renderPass );
  84. const ssaoPass = new SSAOPass( scene, camera, width, height );
  85. composer.addPass( ssaoPass );
  86. const outputPass = new OutputPass();
  87. composer.addPass( outputPass );
  88. // Init gui
  89. const gui = new GUI();
  90. gui.add( ssaoPass, 'output', {
  91. 'Default': SSAOPass.OUTPUT.Default,
  92. 'SSAO Only': SSAOPass.OUTPUT.SSAO,
  93. 'SSAO Only + Blur': SSAOPass.OUTPUT.Blur,
  94. 'Depth': SSAOPass.OUTPUT.Depth,
  95. 'Normal': SSAOPass.OUTPUT.Normal
  96. } ).onChange( function ( value ) {
  97. ssaoPass.output = value;
  98. } );
  99. gui.add( ssaoPass, 'kernelRadius' ).min( 0 ).max( 32 );
  100. gui.add( ssaoPass, 'minDistance' ).min( 0.001 ).max( 0.02 );
  101. gui.add( ssaoPass, 'maxDistance' ).min( 0.01 ).max( 0.3 );
  102. gui.add( ssaoPass, 'enabled' );
  103. window.addEventListener( 'resize', onWindowResize );
  104. }
  105. function onWindowResize() {
  106. const width = window.innerWidth;
  107. const height = window.innerHeight;
  108. camera.aspect = width / height;
  109. camera.updateProjectionMatrix();
  110. renderer.setSize( width, height );
  111. composer.setSize( width, height );
  112. }
  113. function animate() {
  114. stats.begin();
  115. render();
  116. stats.end();
  117. }
  118. function render() {
  119. const timer = performance.now();
  120. group.rotation.x = timer * 0.0002;
  121. group.rotation.y = timer * 0.0001;
  122. composer.render();
  123. }
  124. </script>
  125. </body>
  126. </html>
粤ICP备19079148号