webgl_postprocessing_taa.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. <style>
  8. body {
  9. margin: 0px;
  10. background-color: #000;
  11. overflow: hidden;
  12. font-family:Monospace;
  13. font-size:13px;
  14. margin: 0px;
  15. text-align:center;
  16. overflow: hidden;
  17. }
  18. a { color: #88f; }
  19. #info {
  20. color: #fff;
  21. position: absolute;
  22. top: 10px;
  23. width: 100%;
  24. text-align: center;
  25. display:block;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <div id="info">
  31. <a href="http://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/>
  32. When there is no motion in the scene, the TAA render pass accumulates jittered camera samples<br/>
  33. across frames to create a high quality anti-aliased result.<br/><br/>
  34. Texture interpolation, mipmapping and anistropic sampling is disabled to emphasize<br/> the effect SSAA levels have one the resulting render quality.
  35. </div>
  36. <div id="container"></div>
  37. <script src="../build/three.js"></script>
  38. <script src="js/libs/stats.min.js"></script>
  39. <script src="js/libs/dat.gui.min.js"></script>
  40. <script src="js/shaders/CopyShader.js"></script>
  41. <script src="js/postprocessing/EffectComposer.js"></script>
  42. <script src="js/postprocessing/SSAARenderPass.js"></script>
  43. <script src="js/postprocessing/TAARenderPass.js"></script>
  44. <script src="js/postprocessing/RenderPass.js"></script>
  45. <script src="js/postprocessing/MaskPass.js"></script>
  46. <script src="js/postprocessing/ShaderPass.js"></script>
  47. <script>
  48. var camera, scene, renderer, composer, copyPass, taaRenderPass, renderPass;
  49. var gui, stats;
  50. var index = 0;
  51. var param = { TAAEnabled: "1", TAASampleLevel: 0 };
  52. init();
  53. animate();
  54. clearGui();
  55. function clearGui() {
  56. if ( gui ) gui.destroy();
  57. gui = new dat.GUI();
  58. gui.add( param, 'TAAEnabled', {
  59. 'Disabled': '0',
  60. 'Enabled': '1'
  61. } ).onFinishChange( function () {
  62. if ( taaRenderPass ) {
  63. taaRenderPass.enabled = ( param.TAAEnabled === "1" );
  64. renderPass.enabled = ( param.TAAEnabled !== "1" );
  65. }
  66. } );
  67. gui.add( param, 'TAASampleLevel', {
  68. 'Level 0: 1 Sample': 0,
  69. 'Level 1: 2 Samples': 1,
  70. 'Level 2: 4 Samples': 2,
  71. 'Level 3: 8 Samples': 3,
  72. 'Level 4: 16 Samples': 4,
  73. 'Level 5: 32 Samples': 5
  74. } ).onFinishChange( function () {
  75. if ( taaRenderPass ) {
  76. taaRenderPass.sampleLevel = param.TAASampleLevel;
  77. }
  78. } );
  79. gui.open();
  80. }
  81. function init() {
  82. var container = document.getElementById( "container" );
  83. renderer = new THREE.WebGLRenderer();
  84. renderer.setPixelRatio( window.devicePixelRatio );
  85. renderer.setSize( window.innerWidth, window.innerHeight );
  86. document.body.appendChild( renderer.domElement );
  87. stats = new Stats();
  88. container.appendChild( stats.dom );
  89. //
  90. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  91. camera.position.z = 300;
  92. scene = new THREE.Scene();
  93. var geometry = new THREE.BoxBufferGeometry( 120, 120, 120 );
  94. var material = new THREE.MeshBasicMaterial( { color: 0xffffff, wireframe: true } );
  95. var mesh = new THREE.Mesh( geometry, material );
  96. mesh.position.x = - 100;
  97. scene.add( mesh );
  98. var texture = new THREE.TextureLoader().load( "textures/brick_diffuse.jpg" );
  99. texture.minFilter = THREE.NearestFilter;
  100. texture.magFilter = THREE.NearestFilter;
  101. texture.anisotropy = 1;
  102. texture.generateMipmaps = false;
  103. var material = new THREE.MeshBasicMaterial( { map: texture } );
  104. var mesh = new THREE.Mesh( geometry, material );
  105. mesh.position.x = 100;
  106. scene.add( mesh );
  107. // postprocessing
  108. composer = new THREE.EffectComposer( renderer );
  109. taaRenderPass = new THREE.TAARenderPass( scene, camera );
  110. taaRenderPass.unbiased = false;
  111. composer.addPass( taaRenderPass );
  112. renderPass = new THREE.RenderPass( scene, camera );
  113. renderPass.enabled = false;
  114. composer.addPass( renderPass );
  115. copyPass = new THREE.ShaderPass( THREE.CopyShader );
  116. composer.addPass( copyPass );
  117. window.addEventListener( 'resize', onWindowResize, false );
  118. }
  119. function onWindowResize() {
  120. var width = window.innerWidth;
  121. var height = window.innerHeight;
  122. camera.aspect = width / height;
  123. camera.updateProjectionMatrix();
  124. renderer.setSize( width, height );
  125. var pixelRatio = renderer.getPixelRatio();
  126. var newWidth = Math.floor( width / pixelRatio ) || 1;
  127. var newHeight = Math.floor( height / pixelRatio ) || 1;
  128. composer.setSize( newWidth, newHeight );
  129. }
  130. function animate() {
  131. requestAnimationFrame( animate );
  132. index ++;
  133. if ( Math.round( index / 200 ) % 2 === 0 ) {
  134. for ( var i = 0; i < scene.children.length; i ++ ) {
  135. var child = scene.children[ i ];
  136. child.rotation.x += 0.005;
  137. child.rotation.y += 0.01;
  138. }
  139. if ( taaRenderPass ) taaRenderPass.accumulate = false;
  140. } else {
  141. if ( taaRenderPass ) taaRenderPass.accumulate = true;
  142. }
  143. composer.render();
  144. stats.update();
  145. }
  146. </script>
  147. </body>
  148. </html>