1
0

webgpu_postprocessing_fxaa.html 3.7 KB

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