webgpu_postprocessing_fxaa.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - FXAA</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 - FXAA">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_postprocessing_fxaa.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_postprocessing_fxaa.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="info" class="invert">
  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>FXAA</span>
  18. </div>
  19. <small>Fast Approximate 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 { pass, renderOutput } from 'three/tsl';
  34. import { fxaa } from 'three/addons/tsl/display/FXAANode.js';
  35. import { Inspector } from 'three/addons/inspector/Inspector.js';
  36. const params = {
  37. enabled: true,
  38. animated: false
  39. };
  40. let camera, scene, renderer, timer, group;
  41. let renderPipeline;
  42. init();
  43. async function init() {
  44. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 200 );
  45. camera.position.z = 50;
  46. scene = new THREE.Scene();
  47. scene.background = new THREE.Color( 0xffffff );
  48. timer = new THREE.Timer();
  49. timer.connect( document );
  50. //
  51. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x8d8d8d );
  52. hemiLight.position.set( 0, 1000, 0 );
  53. scene.add( hemiLight );
  54. const dirLight = new THREE.DirectionalLight( 0xffffff, 3 );
  55. dirLight.position.set( - 3000, 1000, - 1000 );
  56. scene.add( dirLight );
  57. //
  58. group = new THREE.Group();
  59. const geometry = new THREE.TetrahedronGeometry();
  60. const material = new THREE.MeshStandardMaterial( { color: 0xf73232, flatShading: true } );
  61. const mesh = new THREE.InstancedMesh( geometry, material, 100 );
  62. const dummy = new THREE.Object3D();
  63. for ( let i = 0; i < 100; i ++ ) {
  64. dummy.position.x = Math.random() * 50 - 25;
  65. dummy.position.y = Math.random() * 50 - 25;
  66. dummy.position.z = Math.random() * 50 - 25;
  67. dummy.scale.setScalar( Math.random() * 2 + 1 );
  68. dummy.rotation.x = Math.random() * Math.PI;
  69. dummy.rotation.y = Math.random() * Math.PI;
  70. dummy.rotation.z = Math.random() * Math.PI;
  71. dummy.updateMatrix();
  72. mesh.setMatrixAt( i, dummy.matrix );
  73. }
  74. group.add( mesh );
  75. scene.add( group );
  76. renderer = new THREE.WebGPURenderer();
  77. renderer.setPixelRatio( window.devicePixelRatio );
  78. renderer.setSize( window.innerWidth, window.innerHeight );
  79. renderer.setAnimationLoop( animate );
  80. renderer.inspector = new Inspector();
  81. document.body.appendChild( renderer.domElement );
  82. // post processing
  83. renderPipeline = new THREE.RenderPipeline( renderer );
  84. // ignore default output color transform ( toneMapping and outputColorSpace )
  85. // use renderOutput() for control the sequence
  86. renderPipeline.outputColorTransform = false;
  87. // scene pass
  88. const scenePass = pass( scene, camera ).toInspector( 'Color' );
  89. const outputPass = renderOutput( scenePass );
  90. // FXAA must be computed in sRGB color space (so after tone mapping and color space conversion)
  91. const fxaaPass = fxaa( outputPass );
  92. renderPipeline.outputNode = fxaaPass;
  93. //
  94. window.addEventListener( 'resize', onWindowResize );
  95. //
  96. const gui = renderer.inspector.createParameters( 'Settings' );
  97. gui.add( params, 'enabled' ).onChange( ( value ) => {
  98. if ( value === true ) {
  99. renderPipeline.outputNode = fxaaPass;
  100. } else {
  101. renderPipeline.outputNode = outputPass;
  102. }
  103. renderPipeline.needsUpdate = true;
  104. } );
  105. gui.add( params, 'animated' );
  106. }
  107. function onWindowResize() {
  108. camera.aspect = window.innerWidth / window.innerHeight;
  109. camera.updateProjectionMatrix();
  110. renderer.setSize( window.innerWidth, window.innerHeight );
  111. }
  112. //
  113. function animate() {
  114. timer.update();
  115. const delta = timer.getDelta();
  116. if ( params.animated === true ) {
  117. group.rotation.y += delta * 0.1;
  118. }
  119. renderPipeline.render();
  120. }
  121. </script>
  122. </body>
  123. </html>
粤ICP备19079148号