webgpu_upscaling_taau.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - postprocessing taau</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="example.css">
  8. </head>
  9. <body>
  10. <div id="info" class="invert">
  11. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  12. <div class="title-wrapper">
  13. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>TAAU</span>
  14. </div>
  15. <small>
  16. Temporal Reprojection Anti-Aliasing with Upsampling.<br />
  17. Model: <a href="https://artstation.com/artwork/1AGwX" target="_blank" rel="noopener">Littlest Tokyo</a> by <a href="https://artstation.com/glenatron" target="_blank" rel="noopener">Glen Fox</a>, CC Attribution.
  18. </small>
  19. </div>
  20. <script type="importmap">
  21. {
  22. "imports": {
  23. "three": "../build/three.webgpu.js",
  24. "three/webgpu": "../build/three.webgpu.js",
  25. "three/tsl": "../build/three.tsl.js",
  26. "three/addons/": "./jsm/"
  27. }
  28. }
  29. </script>
  30. <script type="module">
  31. import * as THREE from 'three/webgpu';
  32. import { mrt, output, pass, velocity } from 'three/tsl';
  33. import { taau } from 'three/addons/tsl/display/TAAUNode.js';
  34. import { sharpen } from 'three/addons/tsl/display/SharpenNode.js';
  35. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  36. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  37. import { Inspector } from 'three/addons/inspector/Inspector.js';
  38. import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
  39. import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  40. const params = {
  41. upscaleMethod: 'TAAU',
  42. resolutionScale: 0.5,
  43. sharpening: true,
  44. sharpness: 0.2,
  45. };
  46. let camera, scene, renderer, renderPipeline, controls, mixer, timer;
  47. init();
  48. async function init() {
  49. camera = new THREE.PerspectiveCamera( 25, window.innerWidth / window.innerHeight, 0.1, 100 );
  50. camera.position.set( - 0.5, 0, 12 );
  51. scene = new THREE.Scene();
  52. timer = new THREE.Timer();
  53. timer.connect( document );
  54. // model
  55. const dracoLoader = new DRACOLoader();
  56. dracoLoader.setDecoderPath( 'jsm/libs/draco/' );
  57. dracoLoader.setDecoderConfig( { type: 'js' } );
  58. const loader = new GLTFLoader();
  59. loader.setDRACOLoader( dracoLoader );
  60. loader.load( 'models/gltf/LittlestTokyo.glb', function ( gltf ) {
  61. const model = gltf.scene;
  62. model.scale.set( 0.01, 0.01, 0.01 );
  63. scene.add( model );
  64. mixer = new THREE.AnimationMixer( model );
  65. mixer.clipAction( gltf.animations[ 0 ] ).play();
  66. } );
  67. // renderer
  68. renderer = new THREE.WebGPURenderer();
  69. renderer.setPixelRatio( window.devicePixelRatio );
  70. renderer.setSize( window.innerWidth, window.innerHeight );
  71. renderer.setAnimationLoop( animate );
  72. renderer.inspector = new Inspector();
  73. document.body.appendChild( renderer.domElement );
  74. await renderer.init();
  75. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  76. scene = new THREE.Scene();
  77. scene.background = new THREE.Color( 0xbfe3dd );
  78. scene.environment = pmremGenerator.fromScene( new RoomEnvironment(), 0.04 ).texture;
  79. // controls
  80. controls = new OrbitControls( camera, renderer.domElement );
  81. controls.enableDamping = true;
  82. controls.target.set( - 0.5, 0, 0 );
  83. // render pipeline
  84. renderPipeline = new THREE.RenderPipeline( renderer );
  85. const scenePass = pass( scene, camera );
  86. scenePass.setResolutionScale( params.resolutionScale );
  87. scenePass.setMRT( mrt( {
  88. output: output,
  89. velocity: velocity
  90. } ) );
  91. const scenePassColor = scenePass.getTextureNode( 'output' ).toInspector( 'Color' );
  92. const scenePassDepth = scenePass.getTextureNode( 'depth' ).toInspector( 'Depth', () => {
  93. return scenePass.getLinearDepthNode();
  94. } );
  95. const scenePassVelocity = scenePass.getTextureNode( 'velocity' ).toInspector( 'Velocity' );
  96. const taauNode = taau( scenePassColor, scenePassDepth, scenePassVelocity, camera );
  97. const sharpenNode = sharpen( taauNode.getTextureNode(), params.sharpness );
  98. function updatePipeline() {
  99. if ( params.upscaleMethod === 'TAAU' ) {
  100. renderPipeline.outputNode = params.sharpening ? sharpenNode : taauNode;
  101. } else {
  102. renderPipeline.outputNode = scenePass;
  103. }
  104. renderPipeline.needsUpdate = true;
  105. }
  106. // gui
  107. const gui = renderer.inspector.createParameters( 'Settings' );
  108. gui.add( params, 'upscaleMethod', [ 'Bilinear', 'TAAU' ] ).onChange( updatePipeline );
  109. gui.add( params, 'resolutionScale', 0.25, 1.0, 0.25 ).onChange( ( value ) => {
  110. scenePass.setResolutionScale( value );
  111. } );
  112. gui.add( params, 'sharpening' ).onChange( updatePipeline );
  113. gui.add( params, 'sharpness', 0, 2, 0.05 ).onChange( ( value ) => {
  114. sharpenNode.sharpness.value = value;
  115. } );
  116. updatePipeline();
  117. //
  118. window.addEventListener( 'resize', onWindowResize );
  119. }
  120. function onWindowResize() {
  121. const width = window.innerWidth;
  122. const height = window.innerHeight;
  123. camera.aspect = width / height;
  124. camera.updateProjectionMatrix();
  125. renderer.setSize( width, height );
  126. }
  127. function animate() {
  128. controls.update();
  129. timer.update();
  130. const delta = timer.getDelta();
  131. if ( mixer ) {
  132. mixer.update( delta );
  133. }
  134. renderPipeline.render();
  135. }
  136. </script>
  137. </body>
  138. </html>
粤ICP备19079148号