webgl_postprocessing_sao.html 5.2 KB

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