webgl_postprocessing_taa.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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.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. '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. stats.domElement.style.position = 'absolute';
  88. stats.domElement.style.top = '0px';
  89. container.appendChild( stats.domElement );
  90. //
  91. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  92. camera.position.z = 300;
  93. scene = new THREE.Scene();
  94. var geometry = new THREE.BoxGeometry( 120, 120, 120 );
  95. var material = new THREE.MeshBasicMaterial( { color: 0xffffff, wireframe: true } );
  96. var mesh = new THREE.Mesh( geometry, material );
  97. mesh.position.x = - 100;
  98. scene.add( mesh );
  99. var texture = new THREE.TextureLoader().load( "textures/brick_diffuse.jpg" );
  100. texture.minFilter = THREE.NearestFilter;
  101. texture.magFilter = THREE.NearestFilter;
  102. texture.anisotropy = 1;
  103. texture.generateMipmaps = false;
  104. var material = new THREE.MeshBasicMaterial( { map: texture } );
  105. var mesh = new THREE.Mesh( geometry, material );
  106. mesh.position.x = 100;
  107. scene.add( mesh );
  108. // postprocessing
  109. composer = new THREE.EffectComposer( renderer );
  110. taaRenderPass = new THREE.TAARenderPass( scene, camera );
  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. copyPass.renderToScreen = true;
  117. composer.addPass( copyPass );
  118. window.addEventListener( 'resize', onWindowResize, false );
  119. }
  120. function onWindowResize() {
  121. var width = window.innerWidth;
  122. var height = window.innerHeight;
  123. camera.aspect = width / height;
  124. camera.updateProjectionMatrix();
  125. renderer.setSize( width, height );
  126. var pixelRatio = renderer.getPixelRatio();
  127. var newWidth = Math.floor( width / pixelRatio ) || 1;
  128. var newHeight = Math.floor( height / pixelRatio ) || 1;
  129. composer.setSize( newWidth, newHeight );
  130. taaRenderPass.setSize( newWidth, newHeight );
  131. }
  132. function animate() {
  133. this.index = this.index || 0;
  134. requestAnimationFrame( animate );
  135. this.index ++;
  136. if( Math.round( this.index / 200 ) % 2 === 0 ) {
  137. for ( var i = 0; i < scene.children.length; i ++ ) {
  138. var child = scene.children[ i ];
  139. child.rotation.x += 0.005;
  140. child.rotation.y += 0.01;
  141. }
  142. if( taaRenderPass ) taaRenderPass.accumulate = false;
  143. }
  144. else {
  145. if( taaRenderPass ) taaRenderPass.accumulate = true;
  146. }
  147. composer.render();
  148. stats.update();
  149. }
  150. </script>
  151. <div>
  152. </body>
  153. </html>