webgl_postprocessing_hbao.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - postprocessing - Horizon Based Ambient Occlusion</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. <style>
  9. body {
  10. background-color: #bfe3dd;
  11. color: #000;
  12. }
  13. a {
  14. color: #2983ff;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="info">
  20. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - horizon based ambient occlusion (HBAO) by <a href="https://github.com/Rabbid76">Rabbid76</a><br/>
  21. </div>
  22. <script type="importmap">
  23. {
  24. "imports": {
  25. "three": "../build/three.module.js",
  26. "three/addons/": "./jsm/"
  27. }
  28. }
  29. </script>
  30. <script type="module">
  31. import * as THREE from 'three';
  32. import Stats from 'three/addons/libs/stats.module.js';
  33. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  34. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  35. import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
  36. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  37. import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  38. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  39. import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  40. import { HBAOPass } from 'three/addons/postprocessing/HBAOPass.js';
  41. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  42. let mixer;
  43. const clock = new THREE.Clock();
  44. const container = document.createElement( 'div' );
  45. document.body.appendChild( container );
  46. const stats = new Stats();
  47. container.appendChild( stats.dom );
  48. const renderer = new THREE.WebGLRenderer( { antialias: true } );
  49. renderer.setSize( window.innerWidth, window.innerHeight );
  50. document.body.appendChild( renderer.domElement );
  51. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  52. const scene = new THREE.Scene();
  53. scene.background = new THREE.Color( 0xbfe3dd );
  54. scene.environment = pmremGenerator.fromScene( new RoomEnvironment( renderer ), 0.04 ).texture;
  55. const camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 100 );
  56. camera.position.set( 5, 2, 8 );
  57. const controls = new OrbitControls( camera, renderer.domElement );
  58. controls.target.set( 0, 0.5, 0 );
  59. controls.update();
  60. controls.enablePan = false;
  61. controls.enableDamping = true;
  62. const dracoLoader = new DRACOLoader();
  63. dracoLoader.setDecoderPath( 'jsm/libs/draco/gltf/' );
  64. const loader = new GLTFLoader();
  65. loader.setDRACOLoader( dracoLoader );
  66. loader.load( 'models/gltf/LittlestTokyo.glb', function ( gltf ) {
  67. const model = gltf.scene;
  68. model.position.set( 1, 1, 0 );
  69. model.scale.set( 0.01, 0.01, 0.01 );
  70. scene.add( model );
  71. mixer = new THREE.AnimationMixer( model );
  72. mixer.clipAction( gltf.animations[ 0 ] ).play();
  73. animate();
  74. }, undefined, function ( e ) {
  75. console.error( e );
  76. } );
  77. const width = window.innerWidth;
  78. const height = window.innerHeight;
  79. const pixelRatio = renderer.getPixelRatio();
  80. const maxSamples = renderer.capabilities.maxSamples;
  81. const renderTarget = new THREE.WebGLRenderTarget( width * pixelRatio, height * pixelRatio, {
  82. type: THREE.HalfFloatType,
  83. samples: maxSamples,
  84. } );
  85. renderTarget.texture.name = 'EffectComposer.rt1';
  86. const composer = new EffectComposer( renderer, renderTarget );
  87. const renderPass = new RenderPass( scene, camera );
  88. composer.addPass( renderPass );
  89. const hbaoPass = new HBAOPass( scene, camera, width, height );
  90. hbaoPass.output = HBAOPass.OUTPUT.Denoise;
  91. composer.addPass( hbaoPass );
  92. const outputPass = new OutputPass();
  93. composer.addPass( outputPass );
  94. // Init gui
  95. const gui = new GUI();
  96. gui.add( hbaoPass, 'output', {
  97. 'Default': HBAOPass.OUTPUT.Default,
  98. 'Diffuse': HBAOPass.OUTPUT.Diffuse,
  99. 'HBAO Only': HBAOPass.OUTPUT.HBAO,
  100. 'HBAO Only + Denoise': HBAOPass.OUTPUT.Denoise,
  101. 'Depth': HBAOPass.OUTPUT.Depth,
  102. 'Normal': HBAOPass.OUTPUT.Normal
  103. } ).onChange( function ( value ) {
  104. hbaoPass.output = value;
  105. } );
  106. const hbaoParameters = {
  107. radius: 2.,
  108. distanceExponent: 1.,
  109. bias: 0.01,
  110. samples: 16,
  111. };
  112. const pdParameters = {
  113. lumaPhi: 10.,
  114. depthPhi: 2.,
  115. normalPhi: 3.,
  116. radius: 10.,
  117. rings: 4,
  118. samples: 16,
  119. };
  120. hbaoPass.updateHbaoMaterial( hbaoParameters );
  121. hbaoPass.updatePdMaterial( pdParameters );
  122. gui.add( hbaoParameters, 'radius' ).min( 0.01 ).max( 10 ).step( 0.01 ).onChange( () => hbaoPass.updateHbaoMaterial( hbaoParameters ) );
  123. gui.add( hbaoParameters, 'distanceExponent' ).min( 1 ).max( 4 ).step( 0.01 ).onChange( () => hbaoPass.updateHbaoMaterial( hbaoParameters ) );
  124. gui.add( hbaoParameters, 'bias' ).min( 0 ).max( 0.1 ).step( 0.001 ).onChange( () => hbaoPass.updateHbaoMaterial( hbaoParameters ) );
  125. gui.add( hbaoParameters, 'samples' ).min( 1 ).max( 32 ).step( 1 ).onChange( () => hbaoPass.updateHbaoMaterial( hbaoParameters ) );
  126. gui.add( pdParameters, 'lumaPhi' ).min( 0 ).max( 20 ).step( 0.01 ).onChange( () => hbaoPass.updatePdMaterial( pdParameters ) );
  127. gui.add( pdParameters, 'depthPhi' ).min( 0.01 ).max( 20 ).step( 0.01 ).onChange( () => hbaoPass.updatePdMaterial( pdParameters ) );
  128. gui.add( pdParameters, 'normalPhi' ).min( 0.01 ).max( 20 ).step( 0.01 ).onChange( () => hbaoPass.updatePdMaterial( pdParameters ) );
  129. gui.add( pdParameters, 'radius' ).min( 0 ).max( 32 ).step( 1 ).onChange( () => hbaoPass.updatePdMaterial( pdParameters ) );
  130. gui.add( pdParameters, 'rings' ).min( 0 ).max( 16 ).step( 0.125 ).onChange( () => hbaoPass.updatePdMaterial( pdParameters ) );
  131. gui.add( pdParameters, 'samples' ).min( 1 ).max( 32 ).step( 1 ).onChange( () => hbaoPass.updatePdMaterial( pdParameters ) );
  132. window.addEventListener( 'resize', onWindowResize );
  133. function onWindowResize() {
  134. const width = window.innerWidth;
  135. const height = window.innerHeight;
  136. camera.aspect = width / height;
  137. camera.updateProjectionMatrix();
  138. renderer.setSize( width, height );
  139. composer.setSize( width, height );
  140. }
  141. function animate() {
  142. requestAnimationFrame( animate );
  143. const delta = clock.getDelta();
  144. mixer.update( delta );
  145. controls.update();
  146. stats.begin();
  147. composer.render();
  148. stats.end();
  149. }
  150. </script>
  151. </body>
  152. </html>