webgpu_postprocessing_fxaa.html 4.2 KB

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