webgl_postprocessing_taa.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js",
  21. "three/addons/": "./jsm/"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import Stats from 'three/addons/libs/stats.module.js';
  28. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  29. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  30. import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  31. import { TAARenderPass } from 'three/addons/postprocessing/TAARenderPass.js';
  32. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  33. let camera, scene, renderer, composer, taaRenderPass, renderPass;
  34. let gui, stats;
  35. let index = 0;
  36. const param = { TAAEnabled: '1', TAASampleLevel: 0 };
  37. init();
  38. clearGui();
  39. function clearGui() {
  40. if ( gui ) gui.destroy();
  41. gui = new GUI();
  42. gui.add( param, 'TAAEnabled', {
  43. 'Disabled': '0',
  44. 'Enabled': '1'
  45. } ).onFinishChange( function () {
  46. if ( taaRenderPass ) {
  47. taaRenderPass.enabled = ( param.TAAEnabled === '1' );
  48. renderPass.enabled = ( param.TAAEnabled !== '1' );
  49. }
  50. } );
  51. gui.add( param, 'TAASampleLevel', {
  52. 'Level 0: 1 Sample': 0,
  53. 'Level 1: 2 Samples': 1,
  54. 'Level 2: 4 Samples': 2,
  55. 'Level 3: 8 Samples': 3,
  56. 'Level 4: 16 Samples': 4,
  57. 'Level 5: 32 Samples': 5
  58. } ).onFinishChange( function () {
  59. if ( taaRenderPass ) {
  60. taaRenderPass.sampleLevel = param.TAASampleLevel;
  61. }
  62. } );
  63. gui.open();
  64. }
  65. function init() {
  66. const container = document.getElementById( 'container' );
  67. renderer = new THREE.WebGLRenderer();
  68. renderer.setPixelRatio( window.devicePixelRatio );
  69. renderer.setSize( window.innerWidth, window.innerHeight );
  70. renderer.setAnimationLoop( animate );
  71. document.body.appendChild( renderer.domElement );
  72. stats = new Stats();
  73. container.appendChild( stats.dom );
  74. //
  75. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  76. camera.position.z = 300;
  77. scene = new THREE.Scene();
  78. const geometry = new THREE.BoxGeometry( 120, 120, 120 );
  79. const material1 = new THREE.MeshBasicMaterial( { color: 0xffffff, wireframe: true } );
  80. const mesh1 = new THREE.Mesh( geometry, material1 );
  81. mesh1.position.x = - 100;
  82. scene.add( mesh1 );
  83. const texture = new THREE.TextureLoader().load( 'textures/brick_diffuse.jpg' );
  84. texture.minFilter = THREE.NearestFilter;
  85. texture.magFilter = THREE.NearestFilter;
  86. texture.anisotropy = 1;
  87. texture.generateMipmaps = false;
  88. texture.colorSpace = THREE.SRGBColorSpace;
  89. const material2 = new THREE.MeshBasicMaterial( { map: texture } );
  90. const mesh2 = new THREE.Mesh( geometry, material2 );
  91. mesh2.position.x = 100;
  92. scene.add( mesh2 );
  93. // postprocessing
  94. composer = new EffectComposer( renderer );
  95. taaRenderPass = new TAARenderPass( scene, camera );
  96. taaRenderPass.unbiased = false;
  97. composer.addPass( taaRenderPass );
  98. renderPass = new RenderPass( scene, camera );
  99. renderPass.enabled = false;
  100. composer.addPass( renderPass );
  101. const outputPass = new OutputPass();
  102. composer.addPass( outputPass );
  103. window.addEventListener( 'resize', onWindowResize );
  104. }
  105. function onWindowResize() {
  106. const width = window.innerWidth;
  107. const height = window.innerHeight;
  108. camera.aspect = width / height;
  109. camera.updateProjectionMatrix();
  110. renderer.setSize( width, height );
  111. composer.setSize( width, height );
  112. }
  113. function animate() {
  114. index ++;
  115. if ( Math.round( index / 200 ) % 2 === 0 ) {
  116. for ( let i = 0; i < scene.children.length; i ++ ) {
  117. const child = scene.children[ i ];
  118. child.rotation.x += 0.005;
  119. child.rotation.y += 0.01;
  120. }
  121. if ( taaRenderPass ) taaRenderPass.accumulate = false;
  122. } else {
  123. if ( taaRenderPass ) taaRenderPass.accumulate = true;
  124. }
  125. composer.render();
  126. stats.update();
  127. }
  128. </script>
  129. </body>
  130. </html>