webgl_postprocessing_taa.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - postprocessing manual taa and ssaa</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. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Temporal Anti-Aliasing (TAA) pass by <a href="https://clara.io" target="_blank" rel="noopener">Ben Houston</a><br/><br/>
  12. When there is no motion in the scene, the TAA render pass accumulates jittered camera samples<br/>
  13. across frames to create a high quality anti-aliased result.<br/><br/>
  14. Texture interpolation, mipmapping and anistropic sampling is disabled to emphasize<br/> the effect SSAA levels have one the resulting render quality.
  15. </div>
  16. <div id="container"></div>
  17. <!-- Import maps polyfill -->
  18. <!-- Remove this when import maps will be widely supported -->
  19. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  20. <script type="importmap">
  21. {
  22. "imports": {
  23. "three": "../build/three.module.js",
  24. "three/addons/": "./jsm/"
  25. }
  26. }
  27. </script>
  28. <script type="module">
  29. import * as THREE from 'three';
  30. import Stats from 'three/addons/libs/stats.module.js';
  31. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  32. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  33. import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  34. import { TAARenderPass } from 'three/addons/postprocessing/TAARenderPass.js';
  35. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  36. let camera, scene, renderer, composer, taaRenderPass, renderPass;
  37. let gui, stats;
  38. let index = 0;
  39. const param = { TAAEnabled: '1', TAASampleLevel: 0 };
  40. init();
  41. animate();
  42. clearGui();
  43. function clearGui() {
  44. if ( gui ) gui.destroy();
  45. gui = new GUI();
  46. gui.add( param, 'TAAEnabled', {
  47. 'Disabled': '0',
  48. 'Enabled': '1'
  49. } ).onFinishChange( function () {
  50. if ( taaRenderPass ) {
  51. taaRenderPass.enabled = ( param.TAAEnabled === '1' );
  52. renderPass.enabled = ( param.TAAEnabled !== '1' );
  53. }
  54. } );
  55. gui.add( param, 'TAASampleLevel', {
  56. 'Level 0: 1 Sample': 0,
  57. 'Level 1: 2 Samples': 1,
  58. 'Level 2: 4 Samples': 2,
  59. 'Level 3: 8 Samples': 3,
  60. 'Level 4: 16 Samples': 4,
  61. 'Level 5: 32 Samples': 5
  62. } ).onFinishChange( function () {
  63. if ( taaRenderPass ) {
  64. taaRenderPass.sampleLevel = param.TAASampleLevel;
  65. }
  66. } );
  67. gui.open();
  68. }
  69. function init() {
  70. const container = document.getElementById( 'container' );
  71. renderer = new THREE.WebGLRenderer();
  72. renderer.setPixelRatio( window.devicePixelRatio );
  73. renderer.setSize( window.innerWidth, window.innerHeight );
  74. renderer.useLegacyLights = false;
  75. document.body.appendChild( renderer.domElement );
  76. stats = new Stats();
  77. container.appendChild( stats.dom );
  78. //
  79. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  80. camera.position.z = 300;
  81. scene = new THREE.Scene();
  82. const geometry = new THREE.BoxGeometry( 120, 120, 120 );
  83. const material1 = new THREE.MeshBasicMaterial( { color: 0xffffff, wireframe: true } );
  84. const mesh1 = new THREE.Mesh( geometry, material1 );
  85. mesh1.position.x = - 100;
  86. scene.add( mesh1 );
  87. const texture = new THREE.TextureLoader().load( 'textures/brick_diffuse.jpg' );
  88. texture.minFilter = THREE.NearestFilter;
  89. texture.magFilter = THREE.NearestFilter;
  90. texture.anisotropy = 1;
  91. texture.generateMipmaps = false;
  92. texture.colorSpace = THREE.SRGBColorSpace;
  93. const material2 = new THREE.MeshBasicMaterial( { map: texture } );
  94. const mesh2 = new THREE.Mesh( geometry, material2 );
  95. mesh2.position.x = 100;
  96. scene.add( mesh2 );
  97. // postprocessing
  98. composer = new EffectComposer( renderer );
  99. taaRenderPass = new TAARenderPass( scene, camera );
  100. taaRenderPass.unbiased = false;
  101. composer.addPass( taaRenderPass );
  102. renderPass = new RenderPass( scene, camera );
  103. renderPass.enabled = false;
  104. composer.addPass( renderPass );
  105. const outputPass = new OutputPass();
  106. composer.addPass( outputPass );
  107. window.addEventListener( 'resize', onWindowResize );
  108. }
  109. function onWindowResize() {
  110. const width = window.innerWidth;
  111. const height = window.innerHeight;
  112. camera.aspect = width / height;
  113. camera.updateProjectionMatrix();
  114. renderer.setSize( width, height );
  115. composer.setSize( width, height );
  116. }
  117. function animate() {
  118. requestAnimationFrame( animate );
  119. index ++;
  120. if ( Math.round( index / 200 ) % 2 === 0 ) {
  121. for ( let i = 0; i < scene.children.length; i ++ ) {
  122. const child = scene.children[ i ];
  123. child.rotation.x += 0.005;
  124. child.rotation.y += 0.01;
  125. }
  126. if ( taaRenderPass ) taaRenderPass.accumulate = false;
  127. } else {
  128. if ( taaRenderPass ) taaRenderPass.accumulate = true;
  129. }
  130. composer.render();
  131. stats.update();
  132. }
  133. </script>
  134. </body>
  135. </html>