webgpu_postprocessing_traa.html 3.9 KB

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