webgl_postprocessing_taa.html 5.5 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. #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 the rotation stops the scene automatically improves its static visual quality by accumulating jittered renders.<br/>
  32. This allows for the static quality of a high quality, but slow MSAA shader at the cost of a single render per frame (if sampleLevel = 0).<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.min.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/postprocessing/ManualMSAARenderPass.js"></script>
  40. <script src="js/postprocessing/TAARenderPass.js"></script>
  41. <script src="js/shaders/CopyShader.js"></script>
  42. <script src="js/shaders/CompositeShader.js"></script>
  43. <script src="js/postprocessing/EffectComposer.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. } ).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. stats.domElement.style.position = 'absolute';
  87. stats.domElement.style.top = '0px';
  88. container.appendChild( stats.domElement );
  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.BoxGeometry( 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. 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. taaRenderPass.setSize( newWidth, newHeight );
  130. }
  131. function animate() {
  132. this.index = this.index || 0;
  133. requestAnimationFrame( animate );
  134. this.index ++;
  135. if( Math.round( this.index / 200 ) % 2 === 0 ) {
  136. for ( var i = 0; i < scene.children.length; i ++ ) {
  137. var child = scene.children[ i ];
  138. child.rotation.x += 0.005;
  139. child.rotation.y += 0.01;
  140. }
  141. if( taaRenderPass ) taaRenderPass.accumulate = false;
  142. }
  143. else {
  144. if( taaRenderPass ) taaRenderPass.accumulate = true;
  145. }
  146. composer.render();
  147. stats.update();
  148. }
  149. </script>
  150. <div>
  151. </body>
  152. </html>