webgl_postprocessing_taa.html 5.3 KB

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