webgl_postprocessing_sao.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - post processing - Scalable Ambient Occlusion (SAO)</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. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> - Scalable Ambient Occlusion (SAO)<br/>
  12. shader by <a href="http://clara.io">Ben Houston</a> / Post-processing pass by <a href="http://ludobaka.github.io">Ludobaka</a>
  13. </div>
  14. <!-- Import maps polyfill -->
  15. <!-- Remove this when import maps will be widely supported -->
  16. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js",
  21. "three/addons/": "./jsm/"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import Stats from 'three/addons/libs/stats.module.js';
  28. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  29. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  30. import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  31. import { SAOPass } from 'three/addons/postprocessing/SAOPass.js';
  32. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  33. let container, stats;
  34. let camera, scene, renderer;
  35. let composer, renderPass, saoPass;
  36. let group;
  37. init();
  38. animate();
  39. function init() {
  40. container = document.createElement( 'div' );
  41. document.body.appendChild( container );
  42. const width = window.innerWidth || 1;
  43. const height = window.innerHeight || 1;
  44. const devicePixelRatio = window.devicePixelRatio || 1;
  45. renderer = new THREE.WebGLRenderer( { antialias: true } );
  46. renderer.setClearColor( 0x000000 );
  47. renderer.setPixelRatio( devicePixelRatio );
  48. renderer.setSize( width, height );
  49. renderer.useLegacyLights = false;
  50. document.body.appendChild( renderer.domElement );
  51. camera = new THREE.PerspectiveCamera( 65, width / height, 3, 10 );
  52. camera.position.z = 7;
  53. scene = new THREE.Scene();
  54. group = new THREE.Object3D();
  55. scene.add( group );
  56. const light = new THREE.PointLight( 0xefffef, 500 );
  57. light.position.z = 10;
  58. light.position.y = - 10;
  59. light.position.x = - 10;
  60. scene.add( light );
  61. const light2 = new THREE.PointLight( 0xffefef, 500 );
  62. light2.position.z = 10;
  63. light2.position.x = - 10;
  64. light2.position.y = 10;
  65. scene.add( light2 );
  66. const light3 = new THREE.PointLight( 0xefefff, 500 );
  67. light3.position.z = 10;
  68. light3.position.x = 10;
  69. light3.position.y = - 10;
  70. scene.add( light3 );
  71. const light4 = new THREE.AmbientLight( 0xffffff, 0.2 );
  72. scene.add( light4 );
  73. const geometry = new THREE.SphereGeometry( 3, 48, 24 );
  74. for ( let i = 0; i < 120; i ++ ) {
  75. const material = new THREE.MeshStandardMaterial();
  76. material.roughness = 0.5 * Math.random() + 0.25;
  77. material.metalness = 0;
  78. material.color.setHSL( Math.random(), 1.0, 0.3 );
  79. const mesh = new THREE.Mesh( geometry, material );
  80. mesh.position.x = Math.random() * 4 - 2;
  81. mesh.position.y = Math.random() * 4 - 2;
  82. mesh.position.z = Math.random() * 4 - 2;
  83. mesh.rotation.x = Math.random();
  84. mesh.rotation.y = Math.random();
  85. mesh.rotation.z = Math.random();
  86. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 0.2 + 0.05;
  87. group.add( mesh );
  88. }
  89. stats = new Stats();
  90. container.appendChild( stats.dom );
  91. composer = new EffectComposer( renderer );
  92. renderPass = new RenderPass( scene, camera );
  93. composer.addPass( renderPass );
  94. saoPass = new SAOPass( scene, camera, false, true );
  95. composer.addPass( saoPass );
  96. const outputPass = new OutputPass();
  97. composer.addPass( outputPass );
  98. // Init gui
  99. const gui = new GUI();
  100. gui.add( saoPass.params, 'output', {
  101. 'Beauty': SAOPass.OUTPUT.Beauty,
  102. 'Beauty+SAO': SAOPass.OUTPUT.Default,
  103. 'SAO': SAOPass.OUTPUT.SAO,
  104. 'Depth': SAOPass.OUTPUT.Depth,
  105. 'Normal': SAOPass.OUTPUT.Normal
  106. } ).onChange( function ( value ) {
  107. saoPass.params.output = parseInt( value );
  108. } );
  109. gui.add( saoPass.params, 'saoBias', - 1, 1 );
  110. gui.add( saoPass.params, 'saoIntensity', 0, 1 );
  111. gui.add( saoPass.params, 'saoScale', 0, 10 );
  112. gui.add( saoPass.params, 'saoKernelRadius', 1, 100 );
  113. gui.add( saoPass.params, 'saoMinResolution', 0, 1 );
  114. gui.add( saoPass.params, 'saoBlur' );
  115. gui.add( saoPass.params, 'saoBlurRadius', 0, 200 );
  116. gui.add( saoPass.params, 'saoBlurStdDev', 0.5, 150 );
  117. gui.add( saoPass.params, 'saoBlurDepthCutoff', 0.0, 0.1 );
  118. window.addEventListener( 'resize', onWindowResize );
  119. }
  120. function onWindowResize() {
  121. const width = window.innerWidth || 1;
  122. const height = window.innerHeight || 1;
  123. camera.aspect = width / height;
  124. camera.updateProjectionMatrix();
  125. renderer.setSize( width, height );
  126. composer.setSize( width, height );
  127. }
  128. function animate() {
  129. requestAnimationFrame( animate );
  130. stats.begin();
  131. render();
  132. stats.end();
  133. }
  134. function render() {
  135. const timer = performance.now();
  136. group.rotation.x = timer * 0.0002;
  137. group.rotation.y = timer * 0.0001;
  138. composer.render();
  139. }
  140. </script>
  141. </body>
  142. </html>