webgl_postprocessing_gtao.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. <meta property="og:title" content="three.js webgl - postprocessing - GTAO">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_postprocessing_gtao.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_postprocessing_gtao.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. <style>
  13. body {
  14. background-color: #ffffff;
  15. color: #000;
  16. }
  17. a {
  18. color: #2983ff;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div id="info">
  24. <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/>
  25. </div>
  26. <script type="importmap">
  27. {
  28. "imports": {
  29. "three": "../build/three.module.js",
  30. "three/addons/": "./jsm/"
  31. }
  32. }
  33. </script>
  34. <script type="module">
  35. import * as THREE from 'three';
  36. import Stats from 'three/addons/libs/stats.module.js';
  37. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  38. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  39. import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
  40. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  41. import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  42. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  43. import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  44. import { GTAOPass } from 'three/addons/postprocessing/GTAOPass.js';
  45. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  46. let camera, scene, renderer, composer, controls, timer, stats, mixer;
  47. init();
  48. function init() {
  49. const dracoLoader = new DRACOLoader();
  50. dracoLoader.setDecoderPath( 'jsm/libs/draco/' );
  51. dracoLoader.setDecoderConfig( { type: 'js' } );
  52. const loader = new GLTFLoader();
  53. loader.setDRACOLoader( dracoLoader );
  54. loader.setPath( 'models/gltf/' );
  55. timer = new THREE.Timer();
  56. timer.connect( document );
  57. const container = document.createElement( 'div' );
  58. document.body.appendChild( container );
  59. stats = new Stats();
  60. container.appendChild( stats.dom );
  61. renderer = new THREE.WebGLRenderer();
  62. renderer.setSize( window.innerWidth, window.innerHeight );
  63. renderer.setAnimationLoop( animate );
  64. document.body.appendChild( renderer.domElement );
  65. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  66. scene = new THREE.Scene();
  67. scene.background = new THREE.Color( 0xbfe3dd );
  68. scene.environment = pmremGenerator.fromScene( new RoomEnvironment(), 0.04 ).texture;
  69. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 100 );
  70. camera.position.set( 5, 2, 8 );
  71. controls = new OrbitControls( camera, renderer.domElement );
  72. controls.target.set( 0, 0.5, 0 );
  73. controls.update();
  74. controls.enablePan = false;
  75. controls.enableDamping = true;
  76. const width = window.innerWidth;
  77. const height = window.innerHeight;
  78. composer = new EffectComposer( renderer );
  79. const renderPass = new RenderPass( scene, camera );
  80. composer.addPass( renderPass );
  81. const gtaoPass = new GTAOPass( scene, camera, width, height );
  82. gtaoPass.output = GTAOPass.OUTPUT.Denoise;
  83. composer.addPass( gtaoPass );
  84. const outputPass = new OutputPass();
  85. composer.addPass( outputPass );
  86. //
  87. loader.load( 'LittlestTokyo.glb', ( gltf ) => {
  88. const model = gltf.scene;
  89. model.position.set( 1, 1, 0 );
  90. model.scale.set( 0.01, 0.01, 0.01 );
  91. scene.add( model );
  92. mixer = new THREE.AnimationMixer( model );
  93. mixer.clipAction( gltf.animations[ 0 ] ).play();
  94. const box = new THREE.Box3().setFromObject( scene );
  95. gtaoPass.setSceneClipBox( box );
  96. }, undefined, ( e ) => console.error( e ) );
  97. // Init gui
  98. const gui = new GUI();
  99. gui.add( gtaoPass, 'output', {
  100. 'Default': GTAOPass.OUTPUT.Default,
  101. 'Diffuse': GTAOPass.OUTPUT.Diffuse,
  102. 'AO Only': GTAOPass.OUTPUT.AO,
  103. 'AO Only + Denoise': GTAOPass.OUTPUT.Denoise,
  104. 'Depth': GTAOPass.OUTPUT.Depth,
  105. 'Normal': GTAOPass.OUTPUT.Normal
  106. } ).onChange( function ( value ) {
  107. gtaoPass.output = value;
  108. } );
  109. const aoParameters = {
  110. radius: 0.25,
  111. distanceExponent: 1.,
  112. thickness: 1.,
  113. scale: 1.,
  114. samples: 16,
  115. distanceFallOff: 1.,
  116. screenSpaceRadius: false,
  117. };
  118. const pdParameters = {
  119. lumaPhi: 10.,
  120. depthPhi: 2.,
  121. normalPhi: 3.,
  122. radius: 4.,
  123. radiusExponent: 1.,
  124. rings: 2.,
  125. samples: 16,
  126. };
  127. gtaoPass.updateGtaoMaterial( aoParameters );
  128. gtaoPass.updatePdMaterial( pdParameters );
  129. gui.add( gtaoPass, 'blendIntensity' ).min( 0 ).max( 1 ).step( 0.01 );
  130. gui.add( aoParameters, 'radius' ).min( 0.01 ).max( 1 ).step( 0.01 ).onChange( () => gtaoPass.updateGtaoMaterial( aoParameters ) );
  131. gui.add( aoParameters, 'distanceExponent' ).min( 1 ).max( 4 ).step( 0.01 ).onChange( () => gtaoPass.updateGtaoMaterial( aoParameters ) );
  132. gui.add( aoParameters, 'thickness' ).min( 0.01 ).max( 10 ).step( 0.01 ).onChange( () => gtaoPass.updateGtaoMaterial( aoParameters ) );
  133. gui.add( aoParameters, 'distanceFallOff' ).min( 0 ).max( 1 ).step( 0.01 ).onChange( () => gtaoPass.updateGtaoMaterial( aoParameters ) );
  134. gui.add( aoParameters, 'scale' ).min( 0.01 ).max( 2.0 ).step( 0.01 ).onChange( () => gtaoPass.updateGtaoMaterial( aoParameters ) );
  135. gui.add( aoParameters, 'samples' ).min( 2 ).max( 32 ).step( 1 ).onChange( () => gtaoPass.updateGtaoMaterial( aoParameters ) );
  136. gui.add( aoParameters, 'screenSpaceRadius' ).onChange( () => gtaoPass.updateGtaoMaterial( aoParameters ) );
  137. gui.add( pdParameters, 'lumaPhi' ).min( 0 ).max( 20 ).step( 0.01 ).onChange( () => gtaoPass.updatePdMaterial( pdParameters ) );
  138. gui.add( pdParameters, 'depthPhi' ).min( 0.01 ).max( 20 ).step( 0.01 ).onChange( () => gtaoPass.updatePdMaterial( pdParameters ) );
  139. gui.add( pdParameters, 'normalPhi' ).min( 0.01 ).max( 20 ).step( 0.01 ).onChange( () => gtaoPass.updatePdMaterial( pdParameters ) );
  140. gui.add( pdParameters, 'radius' ).min( 0 ).max( 32 ).step( 1 ).onChange( () => gtaoPass.updatePdMaterial( pdParameters ) );
  141. gui.add( pdParameters, 'radiusExponent' ).min( 0.1 ).max( 4. ).step( 0.1 ).onChange( () => gtaoPass.updatePdMaterial( pdParameters ) );
  142. gui.add( pdParameters, 'rings' ).min( 1 ).max( 16 ).step( 0.125 ).onChange( () => gtaoPass.updatePdMaterial( pdParameters ) );
  143. gui.add( pdParameters, 'samples' ).min( 2 ).max( 32 ).step( 1 ).onChange( () => gtaoPass.updatePdMaterial( pdParameters ) );
  144. window.addEventListener( 'resize', onWindowResize );
  145. }
  146. function onWindowResize() {
  147. const width = window.innerWidth;
  148. const height = window.innerHeight;
  149. camera.aspect = width / height;
  150. camera.updateProjectionMatrix();
  151. renderer.setSize( width, height );
  152. composer.setSize( width, height );
  153. }
  154. function animate() {
  155. timer.update();
  156. const delta = timer.getDelta();
  157. if ( mixer ) {
  158. mixer.update( delta );
  159. }
  160. controls.update();
  161. stats.begin();
  162. composer.render();
  163. stats.end();
  164. }
  165. </script>
  166. </body>
  167. </html>
粤ICP备19079148号