webgl_postprocessing_taa.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js';
  35. import { TAARenderPass } from 'three/addons/postprocessing/TAARenderPass.js';
  36. import { CopyShader } from 'three/addons/shaders/CopyShader.js';
  37. let camera, scene, renderer, composer, copyPass, taaRenderPass, renderPass;
  38. let gui, stats;
  39. let index = 0;
  40. const param = { TAAEnabled: '1', TAASampleLevel: 0 };
  41. init();
  42. animate();
  43. clearGui();
  44. function clearGui() {
  45. if ( gui ) gui.destroy();
  46. gui = new GUI();
  47. gui.add( param, 'TAAEnabled', {
  48. 'Disabled': '0',
  49. 'Enabled': '1'
  50. } ).onFinishChange( function () {
  51. if ( taaRenderPass ) {
  52. taaRenderPass.enabled = ( param.TAAEnabled === '1' );
  53. renderPass.enabled = ( param.TAAEnabled !== '1' );
  54. }
  55. } );
  56. gui.add( param, 'TAASampleLevel', {
  57. 'Level 0: 1 Sample': 0,
  58. 'Level 1: 2 Samples': 1,
  59. 'Level 2: 4 Samples': 2,
  60. 'Level 3: 8 Samples': 3,
  61. 'Level 4: 16 Samples': 4,
  62. 'Level 5: 32 Samples': 5
  63. } ).onFinishChange( function () {
  64. if ( taaRenderPass ) {
  65. taaRenderPass.sampleLevel = param.TAASampleLevel;
  66. }
  67. } );
  68. gui.open();
  69. }
  70. function init() {
  71. const container = document.getElementById( 'container' );
  72. renderer = new THREE.WebGLRenderer();
  73. renderer.setPixelRatio( window.devicePixelRatio );
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  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. const material2 = new THREE.MeshBasicMaterial( { map: texture } );
  93. const mesh2 = new THREE.Mesh( geometry, material2 );
  94. mesh2.position.x = 100;
  95. scene.add( mesh2 );
  96. // postprocessing
  97. composer = new EffectComposer( renderer );
  98. taaRenderPass = new TAARenderPass( scene, camera );
  99. taaRenderPass.unbiased = false;
  100. composer.addPass( taaRenderPass );
  101. renderPass = new RenderPass( scene, camera );
  102. renderPass.enabled = false;
  103. composer.addPass( renderPass );
  104. copyPass = new ShaderPass( CopyShader );
  105. composer.addPass( copyPass );
  106. window.addEventListener( 'resize', onWindowResize );
  107. }
  108. function onWindowResize() {
  109. const width = window.innerWidth;
  110. const height = window.innerHeight;
  111. camera.aspect = width / height;
  112. camera.updateProjectionMatrix();
  113. renderer.setSize( width, height );
  114. composer.setSize( width, height );
  115. }
  116. function animate() {
  117. requestAnimationFrame( animate );
  118. index ++;
  119. if ( Math.round( index / 200 ) % 2 === 0 ) {
  120. for ( let i = 0; i < scene.children.length; i ++ ) {
  121. const child = scene.children[ i ];
  122. child.rotation.x += 0.005;
  123. child.rotation.y += 0.01;
  124. }
  125. if ( taaRenderPass ) taaRenderPass.accumulate = false;
  126. } else {
  127. if ( taaRenderPass ) taaRenderPass.accumulate = true;
  128. }
  129. composer.render();
  130. stats.update();
  131. }
  132. </script>
  133. </body>
  134. </html>