1
0

webgpu_postprocessing_traa.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - postprocessing traa</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. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Temporal Reprojection Anti-Aliasing (TRAA)
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.webgpu.js",
  17. "three/webgpu": "../build/three.webgpu.js",
  18. "three/tsl": "../build/three.tsl.js",
  19. "three/addons/": "./jsm/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three/webgpu';
  25. import { mrt, output, pass, velocity } from 'three/tsl';
  26. import { traa } from 'three/addons/tsl/display/TRAANode.js';
  27. import Stats from 'three/addons/libs/stats.module.js';
  28. let camera, scene, renderer, postProcessing;
  29. let stats;
  30. let index = 0;
  31. init();
  32. function init() {
  33. renderer = new THREE.WebGPURenderer();
  34. renderer.setPixelRatio( window.devicePixelRatio );
  35. renderer.setSize( window.innerWidth, window.innerHeight );
  36. renderer.setAnimationLoop( animate );
  37. document.body.appendChild( renderer.domElement );
  38. stats = new Stats();
  39. document.body.appendChild( stats.dom );
  40. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
  41. camera.position.z = 2.5;
  42. scene = new THREE.Scene();
  43. const geometry = new THREE.BoxGeometry();
  44. const material1 = new THREE.MeshBasicMaterial( { color: 0xffffff, wireframe: true } );
  45. const mesh1 = new THREE.Mesh( geometry, material1 );
  46. mesh1.position.x = - 1;
  47. scene.add( mesh1 );
  48. const texture = new THREE.TextureLoader().load( 'textures/brick_diffuse.jpg' );
  49. texture.minFilter = THREE.NearestFilter;
  50. texture.magFilter = THREE.NearestFilter;
  51. texture.generateMipmaps = false;
  52. texture.colorSpace = THREE.SRGBColorSpace;
  53. const material2 = new THREE.MeshBasicMaterial( { map: texture } );
  54. const mesh2 = new THREE.Mesh( geometry, material2 );
  55. mesh2.position.x = 1;
  56. scene.add( mesh2 );
  57. // postprocessing
  58. postProcessing = new THREE.PostProcessing( renderer );
  59. const scenePass = pass( scene, camera );
  60. scenePass.setMRT( mrt( {
  61. output: output,
  62. velocity: velocity
  63. } ) );
  64. const scenePassColor = scenePass.getTextureNode( 'output' );
  65. const scenePassDepth = scenePass.getTextureNode( 'depth' );
  66. const scenePassVelocity = scenePass.getTextureNode( 'velocity' );
  67. const traaNode = traa( scenePassColor, scenePassDepth, scenePassVelocity, camera );
  68. postProcessing.outputNode = traaNode;
  69. //
  70. window.addEventListener( 'resize', onWindowResize );
  71. }
  72. function onWindowResize() {
  73. const width = window.innerWidth;
  74. const height = window.innerHeight;
  75. camera.aspect = width / height;
  76. camera.updateProjectionMatrix();
  77. renderer.setSize( width, height );
  78. }
  79. function animate() {
  80. index ++;
  81. if ( Math.round( index / 200 ) % 2 === 0 ) {
  82. for ( let i = 0; i < scene.children.length; i ++ ) {
  83. const child = scene.children[ i ];
  84. child.rotation.x += 0.005;
  85. child.rotation.y += 0.01;
  86. }
  87. }
  88. postProcessing.render();
  89. stats.update();
  90. }
  91. </script>
  92. </body>
  93. </html>
粤ICP备19079148号