webgl_postprocessing_taa.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - postprocessing manual msaa</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">three.js</a> - Temporal Anti-Aliasing (TAA) pass by <a href="https://clara.io" target="_blank">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 MSAA 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/ManualMSAARenderPass.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, texture;
  50. var param = { TAAEnabled: "1", TAASampleLevel: 0 };
  51. init();
  52. animate();
  53. clearGui();
  54. function clearGui() {
  55. if ( gui ) gui.destroy();
  56. gui = new dat.GUI();
  57. gui.add( param, 'TAAEnabled', {
  58. 'Disabled': '0',
  59. 'Enabled': '1'
  60. } ).onFinishChange( function() {
  61. if( taaRenderPass ) {
  62. taaRenderPass.enabled = ( param.TAAEnabled === "1" );
  63. renderPass.enabled = ( param.TAAEnabled !== "1" );
  64. }
  65. } );
  66. gui.add( param, 'TAASampleLevel', {
  67. 'Level 0: 1 Sample': 0,
  68. 'Level 1: 2 Samples': 1,
  69. 'Level 2: 4 Samples': 2,
  70. 'Level 3: 8 Samples': 3,
  71. 'Level 4: 16 Samples': 4,
  72. 'Level 5: 32 Samples': 5
  73. } ).onFinishChange( function() {
  74. if( taaRenderPass ) {
  75. taaRenderPass.sampleLevel = param.TAASampleLevel;
  76. }
  77. } );
  78. gui.open();
  79. }
  80. function init() {
  81. container = document.getElementById( "container" );
  82. renderer = new THREE.WebGLRenderer( { antialias: false } );
  83. renderer.setPixelRatio( window.devicePixelRatio );
  84. renderer.setSize( window.innerWidth, window.innerHeight );
  85. document.body.appendChild( renderer.domElement );
  86. stats = new Stats();
  87. container.appendChild( stats.dom );
  88. //
  89. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  90. camera.position.z = 300;
  91. scene = new THREE.Scene();
  92. var geometry = new THREE.BoxGeometry( 120, 120, 120 );
  93. var material = new THREE.MeshBasicMaterial( { color: 0xffffff, wireframe: true } );
  94. var mesh = new THREE.Mesh( geometry, material );
  95. mesh.position.x = - 100;
  96. scene.add( mesh );
  97. var texture = new THREE.TextureLoader().load( "textures/brick_diffuse.jpg" );
  98. texture.minFilter = THREE.NearestFilter;
  99. texture.magFilter = THREE.NearestFilter;
  100. texture.anisotropy = 1;
  101. texture.generateMipmaps = false;
  102. var material = new THREE.MeshBasicMaterial( { map: texture } );
  103. var mesh = new THREE.Mesh( geometry, material );
  104. mesh.position.x = 100;
  105. scene.add( mesh );
  106. // postprocessing
  107. composer = new THREE.EffectComposer( renderer );
  108. taaRenderPass = new THREE.TAARenderPass( scene, camera );
  109. taaRenderPass.unbiased = false;
  110. composer.addPass( taaRenderPass );
  111. renderPass = new THREE.RenderPass( scene, camera );
  112. renderPass.enabled = false;
  113. composer.addPass( renderPass );
  114. copyPass = new THREE.ShaderPass( THREE.CopyShader );
  115. copyPass.renderToScreen = true;
  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. this.index = this.index || 0;
  132. requestAnimationFrame( animate );
  133. this.index ++;
  134. if( Math.round( this.index / 200 ) % 2 === 0 ) {
  135. for ( var i = 0; i < scene.children.length; i ++ ) {
  136. var child = scene.children[ i ];
  137. child.rotation.x += 0.005;
  138. child.rotation.y += 0.01;
  139. }
  140. if( taaRenderPass ) taaRenderPass.accumulate = false;
  141. }
  142. else {
  143. if( taaRenderPass ) taaRenderPass.accumulate = true;
  144. }
  145. composer.render();
  146. stats.update();
  147. }
  148. </script>
  149. <div>
  150. </body>
  151. </html>