webgpu_postprocessing_ao.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - ambient occlusion (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. </head>
  9. <body>
  10. <script type="importmap">
  11. {
  12. "imports": {
  13. "three": "../build/three.webgpu.js",
  14. "three/tsl": "../build/three.webgpu.js",
  15. "three/addons/": "./jsm/"
  16. }
  17. }
  18. </script>
  19. <script type="module">
  20. import * as THREE from 'three';
  21. import { pass, mrt, output, transformedNormalView } from 'three/tsl';
  22. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  23. import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  24. import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
  25. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  26. import Stats from 'three/addons/libs/stats.module.js';
  27. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  28. let camera, scene, renderer, postProcessing, controls, clock, stats, mixer;
  29. const params = {
  30. blendIntensity: 1,
  31. enabled: true
  32. };
  33. init();
  34. async function init() {
  35. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 100 );
  36. camera.position.set( 5, 2, 8 );
  37. scene = new THREE.Scene();
  38. scene.background = new THREE.Color( 0xbfe3dd );
  39. clock = new THREE.Clock();
  40. const hdrloader = new RGBELoader();
  41. const texture = await hdrloader.loadAsync( 'textures/equirectangular/quarry_01_1k.hdr' );
  42. texture.mapping = THREE.EquirectangularReflectionMapping;
  43. scene.environment = texture;
  44. renderer = new THREE.WebGPURenderer();
  45. renderer.setSize( window.innerWidth, window.innerHeight );
  46. renderer.setAnimationLoop( animate );
  47. document.body.appendChild( renderer.domElement );
  48. controls = new OrbitControls( camera, renderer.domElement );
  49. controls.target.set( 0, 0.5, 0 );
  50. controls.update();
  51. controls.enablePan = false;
  52. controls.enableDamping = true;
  53. stats = new Stats();
  54. document.body.appendChild( stats.dom );
  55. //
  56. postProcessing = new THREE.PostProcessing( renderer );
  57. const scenePass = pass( scene, camera );
  58. scenePass.setMRT( mrt( {
  59. output: output,
  60. normal: transformedNormalView
  61. } ) );
  62. const scenePassColor = scenePass.getTextureNode( 'output' );
  63. const scenePassNormal = scenePass.getTextureNode( 'normal' );
  64. const scenePassDepth = scenePass.getTextureNode( 'depth' );
  65. const aoPass = scenePassColor.ao( scenePassDepth, scenePassNormal, camera );
  66. postProcessing.outputNode = aoPass;
  67. //
  68. const dracoLoader = new DRACOLoader();
  69. dracoLoader.setDecoderPath( 'jsm/libs/draco/' );
  70. dracoLoader.setDecoderConfig( { type: 'js' } );
  71. const loader = new GLTFLoader();
  72. loader.setDRACOLoader( dracoLoader );
  73. loader.setPath( 'models/gltf/' );
  74. const gltf = await loader.loadAsync( 'LittlestTokyo.glb' );
  75. const model = gltf.scene;
  76. model.position.set( 1, 1, 0 );
  77. model.scale.set( 0.01, 0.01, 0.01 );
  78. scene.add( model );
  79. mixer = new THREE.AnimationMixer( model );
  80. mixer.clipAction( gltf.animations[ 0 ] ).play();
  81. window.addEventListener( 'resize', onWindowResize );
  82. //
  83. const gui = new GUI();
  84. gui.title( 'AO settings' );
  85. gui.add( params, 'blendIntensity' ).min( 0 ).max( 1 ).onChange( ( value ) => {
  86. aoPass.blendIntensity.value = value;
  87. } );
  88. gui.add( params, 'enabled' ).onChange( ( value ) => {
  89. if ( value === true ) {
  90. postProcessing.outputNode = aoPass;
  91. } else {
  92. postProcessing.outputNode = scenePassColor;
  93. }
  94. postProcessing.needsUpdate = true;
  95. } );
  96. }
  97. function onWindowResize() {
  98. const width = window.innerWidth;
  99. const height = window.innerHeight;
  100. camera.aspect = width / height;
  101. camera.updateProjectionMatrix();
  102. renderer.setSize( width, height );
  103. }
  104. function animate() {
  105. const delta = clock.getDelta();
  106. if ( mixer ) {
  107. mixer.update( delta );
  108. }
  109. controls.update();
  110. postProcessing.render();
  111. stats.update();
  112. }
  113. </script>
  114. </body>
  115. </html>