1
0

webgpu_postprocessing_fxaa.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <script type="importmap">
  11. {
  12. "imports": {
  13. "three": "../build/three.webgpu.js",
  14. "three/tsl": "../build/three.webgpu.js",
  15. "three/addons/": "./jsm/"
  16. }
  17. }
  18. </script>
  19. <script type="module">
  20. import * as THREE from 'three';
  21. import { pass, renderOutput } from 'three/tsl';
  22. import { fxaa } from 'three/addons/tsl/display/FXAANode.js';
  23. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  24. const params = {
  25. enabled: true,
  26. animated: false
  27. };
  28. let camera, scene, renderer, clock, group;
  29. let postProcessing;
  30. init();
  31. async function init() {
  32. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 200 );
  33. camera.position.z = 50;
  34. scene = new THREE.Scene();
  35. scene.background = new THREE.Color( 0xffffff );
  36. clock = new THREE.Clock();
  37. //
  38. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x8d8d8d );
  39. hemiLight.position.set( 0, 1000, 0 );
  40. scene.add( hemiLight );
  41. const dirLight = new THREE.DirectionalLight( 0xffffff, 3 );
  42. dirLight.position.set( - 3000, 1000, - 1000 );
  43. scene.add( dirLight );
  44. //
  45. group = new THREE.Group();
  46. const geometry = new THREE.TetrahedronGeometry();
  47. const material = new THREE.MeshStandardMaterial( { color: 0xf73232, flatShading: true } );
  48. for ( let i = 0; i < 100; i ++ ) {
  49. const mesh = new THREE.Mesh( geometry, material );
  50. mesh.position.x = Math.random() * 50 - 25;
  51. mesh.position.y = Math.random() * 50 - 25;
  52. mesh.position.z = Math.random() * 50 - 25;
  53. mesh.scale.setScalar( Math.random() * 2 + 1 );
  54. mesh.rotation.x = Math.random() * Math.PI;
  55. mesh.rotation.y = Math.random() * Math.PI;
  56. mesh.rotation.z = Math.random() * Math.PI;
  57. group.add( mesh );
  58. }
  59. scene.add( group );
  60. renderer = new THREE.WebGPURenderer();
  61. renderer.setPixelRatio( window.devicePixelRatio );
  62. renderer.setSize( window.innerWidth, window.innerHeight );
  63. renderer.setAnimationLoop( animate );
  64. document.body.appendChild( renderer.domElement );
  65. // post processing
  66. postProcessing = new THREE.PostProcessing( renderer );
  67. // ignore default output color transform ( toneMapping and outputColorSpace )
  68. // use renderOutput() for control the sequence
  69. postProcessing.outputColorTransform = false;
  70. // scene pass
  71. const scenePass = pass( scene, camera );
  72. const outputPass = renderOutput( scenePass );
  73. // FXAA must be computed in sRGB color space (so after tone mapping and color space conversion)
  74. const fxaaPass = fxaa( outputPass );
  75. postProcessing.outputNode = fxaaPass;
  76. //
  77. window.addEventListener( 'resize', onWindowResize );
  78. //
  79. const gui = new GUI();
  80. gui.title( 'FXAA settings' );
  81. gui.add( params, 'enabled' ).onChange( ( value ) => {
  82. if ( value === true ) {
  83. postProcessing.outputNode = fxaaPass;
  84. } else {
  85. postProcessing.outputNode = outputPass;
  86. }
  87. postProcessing.needsUpdate = true;
  88. } );
  89. gui.add( params, 'animated' );
  90. }
  91. function onWindowResize() {
  92. camera.aspect = window.innerWidth / window.innerHeight;
  93. camera.updateProjectionMatrix();
  94. renderer.setSize( window.innerWidth, window.innerHeight );
  95. }
  96. //
  97. function animate() {
  98. const delta = clock.getDelta();
  99. if ( params.animated === true ) {
  100. group.rotation.y += delta * 0.1;
  101. }
  102. postProcessing.render();
  103. }
  104. </script>
  105. </body>
  106. </html>