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