webgl_postprocessing_taa.html 4.4 KB

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