webgpu_postprocessing_traa.html 3.6 KB

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