webgl_postprocessing_ssaa.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - postprocessing 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> - Manual Supersampling Anti-Aliasing (SSAA) pass by <a href="https://clara.io" target="_blank" rel="noopener">Ben Houston</a><br/><br/>
  12. This manual approach to SSAA re-renders the scene once for each sample with camera jitter and accumulates the results.<br/><br/>
  13. Texture interpolation, mipmapping and anistropic sampling is disabled to emphasize<br/> the effect SSAA levels have one the resulting render quality.
  14. </div>
  15. <div id="container"></div>
  16. <script type="module">
  17. import * as THREE from '../build/three.module.js';
  18. import Stats from './jsm/libs/stats.module.js';
  19. import { GUI } from './jsm/libs/dat.gui.module.js';
  20. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  21. import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
  22. import { SSAARenderPass } from './jsm/postprocessing/SSAARenderPass.js';
  23. import { CopyShader } from './jsm/shaders/CopyShader.js';
  24. var camera, scene, renderer, composer, copyPass, ssaaRenderPass;
  25. var gui, stats;
  26. var param = {
  27. sampleLevel: 2
  28. };
  29. init();
  30. animate();
  31. clearGui();
  32. function clearGui() {
  33. if ( gui ) gui.destroy();
  34. gui = new GUI();
  35. gui.add( param, 'sampleLevel', {
  36. 'Level 0: 1 Sample': 0,
  37. 'Level 1: 2 Samples': 1,
  38. 'Level 2: 4 Samples': 2,
  39. 'Level 3: 8 Samples': 3,
  40. 'Level 4: 16 Samples': 4,
  41. 'Level 5: 32 Samples': 5
  42. } );
  43. gui.open();
  44. }
  45. function init() {
  46. var container = document.getElementById( "container" );
  47. renderer = new THREE.WebGLRenderer();
  48. renderer.setPixelRatio( window.devicePixelRatio );
  49. renderer.setSize( window.innerWidth, window.innerHeight );
  50. document.body.appendChild( renderer.domElement );
  51. stats = new Stats();
  52. container.appendChild( stats.dom );
  53. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  54. camera.position.z = 300;
  55. scene = new THREE.Scene();
  56. var geometry = new THREE.BoxBufferGeometry( 120, 120, 120 );
  57. var material = new THREE.MeshBasicMaterial( { color: 0xffffff, wireframe: true } );
  58. var mesh = new THREE.Mesh( geometry, material );
  59. mesh.position.x = - 100;
  60. scene.add( mesh );
  61. var texture = new THREE.TextureLoader().load( "textures/brick_diffuse.jpg" );
  62. texture.minFilter = THREE.NearestFilter;
  63. texture.magFilter = THREE.NearestFilter;
  64. texture.anisotropy = 1;
  65. texture.generateMipmaps = false;
  66. var material = new THREE.MeshBasicMaterial( { map: texture } );
  67. var mesh = new THREE.Mesh( geometry, material );
  68. mesh.position.x = 100;
  69. scene.add( mesh );
  70. // postprocessing
  71. composer = new EffectComposer( renderer );
  72. ssaaRenderPass = new SSAARenderPass( scene, camera );
  73. ssaaRenderPass.unbiased = false;
  74. composer.addPass( ssaaRenderPass );
  75. copyPass = new ShaderPass( CopyShader );
  76. composer.addPass( copyPass );
  77. window.addEventListener( 'resize', onWindowResize, false );
  78. }
  79. function onWindowResize() {
  80. var width = window.innerWidth;
  81. var height = window.innerHeight;
  82. camera.aspect = width / height;
  83. camera.updateProjectionMatrix();
  84. renderer.setSize( width, height );
  85. composer.setSize( width, height );
  86. }
  87. function animate() {
  88. requestAnimationFrame( animate );
  89. stats.begin();
  90. for ( var i = 0; i < scene.children.length; i ++ ) {
  91. var child = scene.children[ i ];
  92. child.rotation.x += 0.005;
  93. child.rotation.y += 0.01;
  94. }
  95. ssaaRenderPass.sampleLevel = param.sampleLevel;
  96. composer.render();
  97. stats.end();
  98. }
  99. </script>
  100. </body>
  101. </html>