webgl_postprocessing_sao.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. <script type="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.module.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import Stats from 'three/addons/libs/stats.module.js';
  25. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  26. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  27. import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  28. import { SAOPass } from 'three/addons/postprocessing/SAOPass.js';
  29. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  30. let container, stats;
  31. let camera, scene, renderer;
  32. let composer, renderPass, saoPass;
  33. let group;
  34. init();
  35. function init() {
  36. container = document.createElement( 'div' );
  37. document.body.appendChild( container );
  38. const width = window.innerWidth;
  39. const height = window.innerHeight;
  40. renderer = new THREE.WebGLRenderer();
  41. renderer.setPixelRatio( window.devicePixelRatio );
  42. renderer.setSize( width, height );
  43. renderer.setAnimationLoop( animate );
  44. document.body.appendChild( renderer.domElement );
  45. camera = new THREE.PerspectiveCamera( 65, width / height, 3, 10 );
  46. camera.position.z = 7;
  47. scene = new THREE.Scene();
  48. group = new THREE.Object3D();
  49. scene.add( group );
  50. const light = new THREE.PointLight( 0xefffef, 500 );
  51. light.position.z = 10;
  52. light.position.y = - 10;
  53. light.position.x = - 10;
  54. scene.add( light );
  55. const light2 = new THREE.PointLight( 0xffefef, 500 );
  56. light2.position.z = 10;
  57. light2.position.x = - 10;
  58. light2.position.y = 10;
  59. scene.add( light2 );
  60. const light3 = new THREE.PointLight( 0xefefff, 500 );
  61. light3.position.z = 10;
  62. light3.position.x = 10;
  63. light3.position.y = - 10;
  64. scene.add( light3 );
  65. const light4 = new THREE.AmbientLight( 0xffffff, 0.2 );
  66. scene.add( light4 );
  67. const geometry = new THREE.SphereGeometry( 3, 48, 24 );
  68. for ( let i = 0; i < 120; i ++ ) {
  69. const material = new THREE.MeshStandardMaterial();
  70. material.roughness = 0.5 * Math.random() + 0.25;
  71. material.metalness = 0;
  72. material.color.setHSL( Math.random(), 1.0, 0.3 );
  73. const mesh = new THREE.Mesh( geometry, material );
  74. mesh.position.x = Math.random() * 4 - 2;
  75. mesh.position.y = Math.random() * 4 - 2;
  76. mesh.position.z = Math.random() * 4 - 2;
  77. mesh.rotation.x = Math.random();
  78. mesh.rotation.y = Math.random();
  79. mesh.rotation.z = Math.random();
  80. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 0.2 + 0.05;
  81. group.add( mesh );
  82. }
  83. stats = new Stats();
  84. container.appendChild( stats.dom );
  85. composer = new EffectComposer( renderer );
  86. renderPass = new RenderPass( scene, camera );
  87. composer.addPass( renderPass );
  88. saoPass = new SAOPass( scene, camera );
  89. composer.addPass( saoPass );
  90. const outputPass = new OutputPass();
  91. composer.addPass( outputPass );
  92. // Init gui
  93. const gui = new GUI();
  94. gui.add( saoPass.params, 'output', {
  95. 'Default': SAOPass.OUTPUT.Default,
  96. 'SAO Only': SAOPass.OUTPUT.SAO,
  97. 'Normal': SAOPass.OUTPUT.Normal
  98. } ).onChange( function ( value ) {
  99. saoPass.params.output = value;
  100. } );
  101. gui.add( saoPass.params, 'saoBias', - 1, 1 );
  102. gui.add( saoPass.params, 'saoIntensity', 0, 1 );
  103. gui.add( saoPass.params, 'saoScale', 0, 10 );
  104. gui.add( saoPass.params, 'saoKernelRadius', 1, 100 );
  105. gui.add( saoPass.params, 'saoMinResolution', 0, 1 );
  106. gui.add( saoPass.params, 'saoBlur' );
  107. gui.add( saoPass.params, 'saoBlurRadius', 0, 200 );
  108. gui.add( saoPass.params, 'saoBlurStdDev', 0.5, 150 );
  109. gui.add( saoPass.params, 'saoBlurDepthCutoff', 0.0, 0.1 );
  110. gui.add( saoPass, 'enabled' );
  111. window.addEventListener( 'resize', onWindowResize );
  112. }
  113. function onWindowResize() {
  114. const width = window.innerWidth || 1;
  115. const height = window.innerHeight || 1;
  116. camera.aspect = width / height;
  117. camera.updateProjectionMatrix();
  118. renderer.setSize( width, height );
  119. composer.setSize( width, height );
  120. }
  121. function animate() {
  122. stats.begin();
  123. render();
  124. stats.end();
  125. }
  126. function render() {
  127. const timer = performance.now();
  128. group.rotation.x = timer * 0.0002;
  129. group.rotation.y = timer * 0.0001;
  130. composer.render();
  131. }
  132. </script>
  133. </body>
  134. </html>