webgl_postprocessing_taa.html 4.8 KB

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