1
0

webgpu_postprocessing_fxaa.html 3.8 KB

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