webgl_postprocessing_msaa.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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> - Manual Multi-Sample Anti-Aliasing (MSAA) pass by <a href="https://clara.io" target="_blank">Ben Houston</a><br/><br/>
  31. Texture interpolation, mipmapping and anistropic sampling is disabled to emphasize<br/> the effect MSAA levels have one the resulting render quality.
  32. </div>
  33. <div id="container"></div>
  34. <script src="../build/three.min.js"></script>
  35. <script src="js/libs/stats.min.js"></script>
  36. <script src="js/libs/dat.gui.min.js"></script>
  37. <script src="js/postprocessing/MSAAPass.js"></script>
  38. <script src="js/shaders/CopyShader.js"></script>
  39. <script src="js/shaders/CompositeShader.js"></script>
  40. <script src="js/postprocessing/EffectComposer.js"></script>
  41. <script src="js/postprocessing/RenderPass.js"></script>
  42. <script src="js/postprocessing/MaskPass.js"></script>
  43. <script src="js/postprocessing/ShaderPass.js"></script>
  44. <script>
  45. var camera, scene, renderer, composer, copyPass, msaaPass;
  46. var gui, stats, texture;
  47. var param = { MSAASampleLevel: 2 };
  48. init();
  49. animate();
  50. clearGui();
  51. function clearGui() {
  52. if ( gui ) gui.destroy();
  53. gui = new dat.GUI();
  54. var example = gui.add( param, 'MSAASampleLevel', {
  55. 'Level 0: 1 Sample': 0,
  56. 'Level 1: 2 Samples': 1,
  57. 'Level 2: 4 Samples': 2,
  58. 'Level 3: 8 Samples': 3,
  59. 'Level 4: 16 Samples': 4,
  60. 'Level 5: 32 Samples': 5
  61. } ).onFinishChange( function() {
  62. if( massPass ) {
  63. massPass.sampleLevel = param.MSAASampleLevel;
  64. }
  65. } );
  66. gui.open();
  67. }
  68. function init() {
  69. container = document.getElementById( "container" );
  70. renderer = new THREE.WebGLRenderer( { antialias: false } );
  71. renderer.setPixelRatio( window.devicePixelRatio );
  72. renderer.setSize( window.innerWidth, window.innerHeight );
  73. document.body.appendChild( renderer.domElement );
  74. stats = new Stats();
  75. stats.domElement.style.position = 'absolute';
  76. stats.domElement.style.top = '0px';
  77. container.appendChild( stats.domElement );
  78. //
  79. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  80. camera.position.z = 300;
  81. scene = new THREE.Scene();
  82. var geometry = new THREE.BoxGeometry( 120, 120, 120 );
  83. var material = new THREE.MeshBasicMaterial( { color: 0xffffff, wireframe: true } );
  84. var mesh = new THREE.Mesh( geometry, material );
  85. mesh.position.x = - 100;
  86. scene.add( mesh );
  87. var texture = new THREE.TextureLoader().load( "textures/brick_diffuse.jpg" );
  88. texture.minFilter = THREE.NearestFilter;
  89. texture.magFilter = THREE.NearestFilter;
  90. texture.anisotropy = 1;
  91. texture.generateMipmaps = false;
  92. var material = new THREE.MeshBasicMaterial( { map: texture } );
  93. var mesh = new THREE.Mesh( geometry, material );
  94. mesh.position.x = 100;
  95. scene.add( mesh );
  96. // postprocessing
  97. composer = new THREE.EffectComposer( renderer );
  98. massPass = new THREE.MSAAPass( scene, camera );
  99. massPass.sampleLevel = param.MSAASampleLevel;
  100. composer.addPass( massPass );
  101. copyPass = new THREE.ShaderPass( THREE.CopyShader );
  102. copyPass.renderToScreen = true;
  103. composer.addPass( copyPass );
  104. window.addEventListener( 'resize', onWindowResize, false );
  105. }
  106. function onWindowResize() {
  107. var width = window.innerWidth;
  108. var height = window.innerHeight;
  109. camera.aspect = width / height;
  110. camera.updateProjectionMatrix();
  111. renderer.setSize( width, height );
  112. var pixelRatio = renderer.getPixelRatio();
  113. var newWidth = Math.floor( width / pixelRatio ) || 1;
  114. var newHeight = Math.floor( height / pixelRatio ) || 1;
  115. composer.setSize( newWidth, newHeight );
  116. if( msaaPass ) msaaPass.setSize( newWidth, newHeight );
  117. }
  118. function animate() {
  119. requestAnimationFrame( animate );
  120. for ( var i = 0; i < scene.children.length; i ++ ) {
  121. var child = scene.children[ i ];
  122. child.rotation.x += 0.005;
  123. child.rotation.y += 0.01;
  124. }
  125. composer.render();
  126. stats.update();
  127. }
  128. </script>
  129. <div>
  130. </body>
  131. </html>