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