webgl_postprocessing_hbao.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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: #ffffff;
  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.setPixelRatio( window.devicePixelRatio );
  50. renderer.setSize( window.innerWidth, window.innerHeight );
  51. document.body.appendChild( renderer.domElement );
  52. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  53. const scene = new THREE.Scene();
  54. scene.background = new THREE.Color( 0xbfe3dd );
  55. scene.environment = pmremGenerator.fromScene( new RoomEnvironment( renderer ), 0.04 ).texture;
  56. const camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 100 );
  57. camera.position.set( 5, 2, 8 );
  58. const controls = new OrbitControls( camera, renderer.domElement );
  59. controls.target.set( 0, 0.5, 0 );
  60. controls.update();
  61. controls.enablePan = false;
  62. controls.enableDamping = true;
  63. const dracoLoader = new DRACOLoader();
  64. dracoLoader.setDecoderPath( 'jsm/libs/draco/gltf/' );
  65. const loader = new GLTFLoader();
  66. loader.setDRACOLoader( dracoLoader );
  67. loader.load( 'models/gltf/LittlestTokyo.glb', function ( gltf ) {
  68. const model = gltf.scene;
  69. model.position.set( 1, 1, 0 );
  70. model.scale.set( 0.01, 0.01, 0.01 );
  71. scene.add( model );
  72. mixer = new THREE.AnimationMixer( model );
  73. mixer.clipAction( gltf.animations[ 0 ] ).play();
  74. animate();
  75. }, undefined, function ( e ) {
  76. console.error( e );
  77. } );
  78. const width = window.innerWidth;
  79. const height = window.innerHeight;
  80. const pixelRatio = renderer.getPixelRatio();
  81. const maxSamples = renderer.capabilities.maxSamples;
  82. const renderTarget = new THREE.WebGLRenderTarget( width * pixelRatio, height * pixelRatio, {
  83. type: THREE.HalfFloatType,
  84. samples: maxSamples,
  85. } );
  86. renderTarget.texture.name = 'EffectComposer.rt1';
  87. const composer = new EffectComposer( renderer, renderTarget );
  88. const renderPass = new RenderPass( scene, camera );
  89. composer.addPass( renderPass );
  90. const hbaoPass = new HBAOPass( scene, camera, width, height );
  91. hbaoPass.output = HBAOPass.OUTPUT.Denoise;
  92. composer.addPass( hbaoPass );
  93. const outputPass = new OutputPass();
  94. composer.addPass( outputPass );
  95. // Init gui
  96. const gui = new GUI();
  97. gui.add( hbaoPass, 'output', {
  98. 'Default': HBAOPass.OUTPUT.Default,
  99. 'Diffuse': HBAOPass.OUTPUT.Diffuse,
  100. 'HBAO Only': HBAOPass.OUTPUT.HBAO,
  101. 'HBAO Only + Denoise': HBAOPass.OUTPUT.Denoise,
  102. 'Depth': HBAOPass.OUTPUT.Depth,
  103. 'Normal': HBAOPass.OUTPUT.Normal
  104. } ).onChange( function ( value ) {
  105. hbaoPass.output = value;
  106. } );
  107. const hbaoParameters = {
  108. radius: 2.,
  109. distanceExponent: 1.,
  110. bias: 0.01,
  111. samples: 16,
  112. };
  113. const pdParameters = {
  114. lumaPhi: 10.,
  115. depthPhi: 2.,
  116. normalPhi: 3.,
  117. radius: 10.,
  118. rings: 4,
  119. samples: 16,
  120. };
  121. hbaoPass.updateHbaoMaterial( hbaoParameters );
  122. hbaoPass.updatePdMaterial( pdParameters );
  123. gui.add( hbaoParameters, 'radius' ).min( 0.01 ).max( 10 ).step( 0.01 ).onChange( () => hbaoPass.updateHbaoMaterial( hbaoParameters ) );
  124. gui.add( hbaoParameters, 'distanceExponent' ).min( 1 ).max( 4 ).step( 0.01 ).onChange( () => hbaoPass.updateHbaoMaterial( hbaoParameters ) );
  125. gui.add( hbaoParameters, 'bias' ).min( 0 ).max( 0.1 ).step( 0.001 ).onChange( () => hbaoPass.updateHbaoMaterial( hbaoParameters ) );
  126. gui.add( hbaoParameters, 'samples' ).min( 1 ).max( 32 ).step( 1 ).onChange( () => hbaoPass.updateHbaoMaterial( hbaoParameters ) );
  127. gui.add( pdParameters, 'lumaPhi' ).min( 0 ).max( 20 ).step( 0.01 ).onChange( () => hbaoPass.updatePdMaterial( pdParameters ) );
  128. gui.add( pdParameters, 'depthPhi' ).min( 0.01 ).max( 20 ).step( 0.01 ).onChange( () => hbaoPass.updatePdMaterial( pdParameters ) );
  129. gui.add( pdParameters, 'normalPhi' ).min( 0.01 ).max( 20 ).step( 0.01 ).onChange( () => hbaoPass.updatePdMaterial( pdParameters ) );
  130. gui.add( pdParameters, 'radius' ).min( 0 ).max( 32 ).step( 1 ).onChange( () => hbaoPass.updatePdMaterial( pdParameters ) );
  131. gui.add( pdParameters, 'rings' ).min( 0 ).max( 16 ).step( 0.125 ).onChange( () => hbaoPass.updatePdMaterial( pdParameters ) );
  132. gui.add( pdParameters, 'samples' ).min( 1 ).max( 32 ).step( 1 ).onChange( () => hbaoPass.updatePdMaterial( pdParameters ) );
  133. window.addEventListener( 'resize', onWindowResize );
  134. function onWindowResize() {
  135. const width = window.innerWidth;
  136. const height = window.innerHeight;
  137. camera.aspect = width / height;
  138. camera.updateProjectionMatrix();
  139. renderer.setSize( width, height );
  140. composer.setSize( width, height );
  141. }
  142. function animate() {
  143. requestAnimationFrame( animate );
  144. const delta = clock.getDelta();
  145. mixer.update( delta );
  146. controls.update();
  147. stats.begin();
  148. composer.render();
  149. stats.end();
  150. }
  151. </script>
  152. </body>
  153. </html>