webgl_postprocessing_sao.html 4.8 KB

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