webgl_postprocessing_gtao.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - postprocessing - GTAO</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> - Ground Truth Ambient Occlusion (GTAO) by <a href="https://github.com/Rabbid76" target="_blank" rel="noopener">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 { GTAOPass } from 'three/addons/postprocessing/GTAOPass.js';
  41. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  42. let camera, scene, renderer, composer, controls, clock, stats, mixer;
  43. init();
  44. animate();
  45. function init() {
  46. const dracoLoader = new DRACOLoader();
  47. dracoLoader.setDecoderPath( 'jsm/libs/draco/' );
  48. dracoLoader.setDecoderConfig( { type: 'js' } );
  49. const loader = new GLTFLoader();
  50. loader.setDRACOLoader( dracoLoader );
  51. loader.setPath( 'models/gltf/' );
  52. clock = new THREE.Clock();
  53. const container = document.createElement( 'div' );
  54. document.body.appendChild( container );
  55. stats = new Stats();
  56. container.appendChild( stats.dom );
  57. renderer = new THREE.WebGLRenderer( { antialias: true } );
  58. renderer.setSize( window.innerWidth, window.innerHeight );
  59. document.body.appendChild( renderer.domElement );
  60. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  61. scene = new THREE.Scene();
  62. scene.background = new THREE.Color( 0xbfe3dd );
  63. scene.environment = pmremGenerator.fromScene( new RoomEnvironment( renderer ), 0.04 ).texture;
  64. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 100 );
  65. camera.position.set( 5, 2, 8 );
  66. controls = new OrbitControls( camera, renderer.domElement );
  67. controls.target.set( 0, 0.5, 0 );
  68. controls.update();
  69. controls.enablePan = false;
  70. controls.enableDamping = true;
  71. const width = window.innerWidth;
  72. const height = window.innerHeight;
  73. const pixelRatio = renderer.getPixelRatio();
  74. const maxSamples = renderer.capabilities.maxSamples;
  75. const renderTarget = new THREE.WebGLRenderTarget( width * pixelRatio, height * pixelRatio, {
  76. type: THREE.HalfFloatType,
  77. samples: maxSamples,
  78. } );
  79. renderTarget.texture.name = 'EffectComposer.rt1';
  80. composer = new EffectComposer( renderer, renderTarget );
  81. const renderPass = new RenderPass( scene, camera );
  82. composer.addPass( renderPass );
  83. const gtaoPass = new GTAOPass( scene, camera, width, height );
  84. gtaoPass.output = GTAOPass.OUTPUT.Denoise;
  85. composer.addPass( gtaoPass );
  86. const outputPass = new OutputPass();
  87. composer.addPass( outputPass );
  88. //
  89. loader.load( 'LittlestTokyo.glb', ( gltf ) => {
  90. const model = gltf.scene;
  91. model.position.set( 1, 1, 0 );
  92. model.scale.set( 0.01, 0.01, 0.01 );
  93. scene.add( model );
  94. mixer = new THREE.AnimationMixer( model );
  95. mixer.clipAction( gltf.animations[ 0 ] ).play();
  96. const box = new THREE.Box3().setFromObject( scene );
  97. gtaoPass.setSceneClipBox( box );
  98. }, undefined, ( e ) => console.error( e ) );
  99. // Init gui
  100. const gui = new GUI();
  101. gui.add( gtaoPass, 'output', {
  102. 'Default': GTAOPass.OUTPUT.Default,
  103. 'Diffuse': GTAOPass.OUTPUT.Diffuse,
  104. 'AO Only': GTAOPass.OUTPUT.AO,
  105. 'AO Only + Denoise': GTAOPass.OUTPUT.Denoise,
  106. 'Depth': GTAOPass.OUTPUT.Depth,
  107. 'Normal': GTAOPass.OUTPUT.Normal
  108. } ).onChange( function ( value ) {
  109. gtaoPass.output = value;
  110. } );
  111. const aoParameters = {
  112. radius: 0.25,
  113. distanceExponent: 1.,
  114. thickness: 1.,
  115. bias: 0.001,
  116. scale: 1.,
  117. samples: 16,
  118. distanceFallOff: true,
  119. clipRangeCheck: true,
  120. depthRelativeBias: false,
  121. nvAlignedSamples: false,
  122. screenSpaceRadius: false,
  123. };
  124. const pdParameters = {
  125. lumaPhi: 10.,
  126. depthPhi: 2.,
  127. normalPhi: 3.,
  128. radius: 4.,
  129. radiusExponent: 1.,
  130. rings: 2.,
  131. samples: 16,
  132. };
  133. gtaoPass.updateGtaoMaterial( aoParameters );
  134. gtaoPass.updatePdMaterial( pdParameters );
  135. gui.add( gtaoPass, 'blendIntensity' ).min( 0 ).max( 1 ).step( 0.01 );
  136. gui.add( aoParameters, 'radius' ).min( 0.01 ).max( 1 ).step( 0.01 ).onChange( () => gtaoPass.updateGtaoMaterial( aoParameters ) );
  137. gui.add( aoParameters, 'distanceExponent' ).min( 1 ).max( 4 ).step( 0.01 ).onChange( () => gtaoPass.updateGtaoMaterial( aoParameters ) );
  138. gui.add( aoParameters, 'thickness' ).min( 0.01 ).max( 10 ).step( 0.01 ).onChange( () => gtaoPass.updateGtaoMaterial( aoParameters ) );
  139. gui.add( aoParameters, 'bias' ).min( 0 ).max( 0.1 ).step( 0.0001 ).onChange( () => gtaoPass.updateGtaoMaterial( aoParameters ) );
  140. gui.add( aoParameters, 'scale' ).min( 0.01 ).max( 2.0 ).step( 0.01 ).onChange( () => gtaoPass.updateGtaoMaterial( aoParameters ) );
  141. gui.add( aoParameters, 'samples' ).min( 2 ).max( 32 ).step( 1 ).onChange( () => gtaoPass.updateGtaoMaterial( aoParameters ) );
  142. gui.add( aoParameters, 'screenSpaceRadius' ).onChange( () => gtaoPass.updateGtaoMaterial( aoParameters ) );
  143. gui.add( pdParameters, 'lumaPhi' ).min( 0 ).max( 20 ).step( 0.01 ).onChange( () => gtaoPass.updatePdMaterial( pdParameters ) );
  144. gui.add( pdParameters, 'depthPhi' ).min( 0.01 ).max( 20 ).step( 0.01 ).onChange( () => gtaoPass.updatePdMaterial( pdParameters ) );
  145. gui.add( pdParameters, 'normalPhi' ).min( 0.01 ).max( 20 ).step( 0.01 ).onChange( () => gtaoPass.updatePdMaterial( pdParameters ) );
  146. gui.add( pdParameters, 'radius' ).min( 0 ).max( 32 ).step( 1 ).onChange( () => gtaoPass.updatePdMaterial( pdParameters ) );
  147. gui.add( pdParameters, 'radiusExponent' ).min( 0.1 ).max( 4. ).step( 0.1 ).onChange( () => gtaoPass.updatePdMaterial( pdParameters ) );
  148. gui.add( pdParameters, 'rings' ).min( 1 ).max( 16 ).step( 0.125 ).onChange( () => gtaoPass.updatePdMaterial( pdParameters ) );
  149. gui.add( pdParameters, 'samples' ).min( 2 ).max( 32 ).step( 1 ).onChange( () => gtaoPass.updatePdMaterial( pdParameters ) );
  150. window.addEventListener( 'resize', onWindowResize );
  151. }
  152. function onWindowResize() {
  153. const width = window.innerWidth;
  154. const height = window.innerHeight;
  155. camera.aspect = width / height;
  156. camera.updateProjectionMatrix();
  157. renderer.setSize( width, height );
  158. composer.setSize( width, height );
  159. }
  160. function animate() {
  161. requestAnimationFrame( animate );
  162. const delta = clock.getDelta();
  163. if ( mixer ) {
  164. mixer.update( delta );
  165. }
  166. controls.update();
  167. stats.begin();
  168. composer.render();
  169. stats.end();
  170. }
  171. </script>
  172. </body>
  173. </html>
粤ICP备19079148号