1
0

webgpu_postprocessing_fxaa.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. const mesh = new THREE.InstancedMesh( geometry, material, 100 );
  57. const dummy = new THREE.Object3D();
  58. for ( let i = 0; i < 100; i ++ ) {
  59. dummy.position.x = Math.random() * 50 - 25;
  60. dummy.position.y = Math.random() * 50 - 25;
  61. dummy.position.z = Math.random() * 50 - 25;
  62. dummy.scale.setScalar( Math.random() * 2 + 1 );
  63. dummy.rotation.x = Math.random() * Math.PI;
  64. dummy.rotation.y = Math.random() * Math.PI;
  65. dummy.rotation.z = Math.random() * Math.PI;
  66. dummy.updateMatrix();
  67. mesh.setMatrixAt( i, dummy.matrix );
  68. }
  69. group.add( mesh );
  70. scene.add( group );
  71. renderer = new THREE.WebGPURenderer();
  72. renderer.setPixelRatio( window.devicePixelRatio );
  73. renderer.setSize( window.innerWidth, window.innerHeight );
  74. renderer.setAnimationLoop( animate );
  75. renderer.inspector = new Inspector();
  76. document.body.appendChild( renderer.domElement );
  77. // post processing
  78. postProcessing = new THREE.PostProcessing( renderer );
  79. // ignore default output color transform ( toneMapping and outputColorSpace )
  80. // use renderOutput() for control the sequence
  81. postProcessing.outputColorTransform = false;
  82. // scene pass
  83. const scenePass = pass( scene, camera ).toInspector( 'Color' );
  84. const outputPass = renderOutput( scenePass );
  85. // FXAA must be computed in sRGB color space (so after tone mapping and color space conversion)
  86. const fxaaPass = fxaa( outputPass );
  87. postProcessing.outputNode = fxaaPass;
  88. //
  89. window.addEventListener( 'resize', onWindowResize );
  90. //
  91. const gui = renderer.inspector.createParameters( 'Settings' );
  92. gui.add( params, 'enabled' ).onChange( ( value ) => {
  93. if ( value === true ) {
  94. postProcessing.outputNode = fxaaPass;
  95. } else {
  96. postProcessing.outputNode = outputPass;
  97. }
  98. postProcessing.needsUpdate = true;
  99. } );
  100. gui.add( params, 'animated' );
  101. }
  102. function onWindowResize() {
  103. camera.aspect = window.innerWidth / window.innerHeight;
  104. camera.updateProjectionMatrix();
  105. renderer.setSize( window.innerWidth, window.innerHeight );
  106. }
  107. //
  108. function animate() {
  109. const delta = clock.getDelta();
  110. if ( params.animated === true ) {
  111. group.rotation.y += delta * 0.1;
  112. }
  113. postProcessing.render();
  114. }
  115. </script>
  116. </body>
  117. </html>
粤ICP备19079148号