webgpu_postprocessing_ao.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. let camera, scene, renderer, postProcessing, controls, clock, stats, mixer;
  28. init();
  29. async function init() {
  30. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 100 );
  31. camera.position.set( 5, 2, 8 );
  32. scene = new THREE.Scene();
  33. scene.background = new THREE.Color( 0xbfe3dd );
  34. clock = new THREE.Clock();
  35. const hdrloader = new RGBELoader();
  36. const texture = await hdrloader.loadAsync( 'textures/equirectangular/quarry_01_1k.hdr' );
  37. texture.mapping = THREE.EquirectangularReflectionMapping;
  38. scene.environment = texture;
  39. renderer = new THREE.WebGPURenderer();
  40. renderer.setSize( window.innerWidth, window.innerHeight );
  41. renderer.setAnimationLoop( animate );
  42. document.body.appendChild( renderer.domElement );
  43. controls = new OrbitControls( camera, renderer.domElement );
  44. controls.target.set( 0, 0.5, 0 );
  45. controls.update();
  46. controls.enablePan = false;
  47. controls.enableDamping = true;
  48. stats = new Stats();
  49. document.body.appendChild( stats.dom );
  50. //
  51. postProcessing = new THREE.PostProcessing( renderer );
  52. const scenePass = pass( scene, camera );
  53. scenePass.setMRT( mrt( {
  54. output: output,
  55. normal: transformedNormalView
  56. } ) );
  57. const scenePassColor = scenePass.getTextureNode( 'output' );
  58. const scenePassNormal = scenePass.getTextureNode( 'normal' );
  59. const scenePassDepth = scenePass.getTextureNode( 'depth' );
  60. const aoPass = scenePassColor.ao( scenePassDepth, scenePassNormal, camera );
  61. postProcessing.outputNode = aoPass;
  62. //
  63. const dracoLoader = new DRACOLoader();
  64. dracoLoader.setDecoderPath( 'jsm/libs/draco/' );
  65. dracoLoader.setDecoderConfig( { type: 'js' } );
  66. const loader = new GLTFLoader();
  67. loader.setDRACOLoader( dracoLoader );
  68. loader.setPath( 'models/gltf/' );
  69. const gltf = await loader.loadAsync( 'LittlestTokyo.glb' );
  70. const model = gltf.scene;
  71. model.position.set( 1, 1, 0 );
  72. model.scale.set( 0.01, 0.01, 0.01 );
  73. scene.add( model );
  74. mixer = new THREE.AnimationMixer( model );
  75. mixer.clipAction( gltf.animations[ 0 ] ).play();
  76. window.addEventListener( 'resize', onWindowResize );
  77. }
  78. function onWindowResize() {
  79. const width = window.innerWidth;
  80. const height = window.innerHeight;
  81. camera.aspect = width / height;
  82. camera.updateProjectionMatrix();
  83. renderer.setSize( width, height );
  84. }
  85. function animate() {
  86. const delta = clock.getDelta();
  87. if ( mixer ) {
  88. mixer.update( delta );
  89. }
  90. controls.update();
  91. postProcessing.render();
  92. stats.update();
  93. }
  94. </script>
  95. </body>
  96. </html>