webgl_postprocessing_msaa.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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>
  31. </div>
  32. <div id="container"></div>
  33. <script src="../build/three.min.js"></script>
  34. <script src="js/libs/stats.min.js"></script>
  35. <script src="js/libs/dat.gui.min.js"></script>
  36. <script src="js/postprocessing/MSAAPass.js"></script>
  37. <script src="js/shaders/CopyShader.js"></script>
  38. <script src="js/shaders/CompositeShader.js"></script>
  39. <script src="js/postprocessing/EffectComposer.js"></script>
  40. <script src="js/postprocessing/RenderPass.js"></script>
  41. <script src="js/postprocessing/MaskPass.js"></script>
  42. <script src="js/postprocessing/ShaderPass.js"></script>
  43. <script>
  44. var camera, scene, renderer, composer, copyPass, msaaPass;
  45. var geometry, material, gui, object, stats;
  46. var param = { MSAASampleLevel: 2 };
  47. var textureLoader = new THREE.TextureLoader();
  48. container = document.getElementById( "container" );
  49. stats = new Stats();
  50. stats.domElement.style.position = 'absolute';
  51. stats.domElement.style.top = '0px';
  52. container.appendChild( stats.domElement );
  53. textureLoader.load( "./textures/brick_diffuse.jpg", function( meshTexture ) {
  54. init( meshTexture );
  55. animate();
  56. clearGui();
  57. });
  58. function clearGui() {
  59. if ( gui ) gui.destroy();
  60. gui = new dat.GUI();
  61. var example = gui.add( param, 'MSAASampleLevel', {
  62. 'Level 0: 1 Sample': 0,
  63. 'Level 1: 2 Samples': 1,
  64. 'Level 2: 4 Samples': 2,
  65. 'Level 3: 8 Samples': 3,
  66. 'Level 4: 16 Samples': 4,
  67. 'Level 5: 32 Samples': 5
  68. } ).onFinishChange( function() {
  69. if( massPass ) {
  70. massPass.sampleLevel = param.MSAASampleLevel;
  71. }
  72. } );
  73. gui.open();
  74. }
  75. function init( meshTexture ) {
  76. renderer = new THREE.WebGLRenderer( { antialias: false } );
  77. renderer.setPixelRatio( window.devicePixelRatio );
  78. renderer.setSize( window.innerWidth, window.innerHeight );
  79. document.body.appendChild( renderer.domElement );
  80. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  81. camera.position.z = 400;
  82. scene = new THREE.Scene();
  83. geometry = new THREE.BoxGeometry( 200, 200, 200 );
  84. material = new THREE.MeshBasicMaterial( { map: meshTexture } );
  85. meshTexture.anisotropy = 0;
  86. object = new THREE.Mesh( geometry, material );
  87. scene.add( object );
  88. // postprocessing
  89. composer = new THREE.EffectComposer( renderer );
  90. massPass = new THREE.MSAAPass( scene, camera );
  91. massPass.sampleLevel = param.MSAASampleLevel;
  92. composer.addPass( massPass );
  93. copyPass = new THREE.ShaderPass( THREE.CopyShader );
  94. copyPass.renderToScreen = true;
  95. composer.addPass( copyPass );
  96. window.addEventListener( 'resize', onWindowResize, false );
  97. }
  98. function onWindowResize() {
  99. var width = window.innerWidth;
  100. var height = window.innerHeight;
  101. camera.aspect = width / height;
  102. camera.updateProjectionMatrix();
  103. renderer.setSize( width, height );
  104. var pixelRatio = renderer.getPixelRatio();
  105. var newWidth = Math.floor( width / pixelRatio ) || 1;
  106. var newHeight = Math.floor( height / pixelRatio ) || 1;
  107. composer.setSize( newWidth, newHeight );
  108. if( msaaPass ) msaaPass.setSize( newWidth, newHeight );
  109. }
  110. function animate() {
  111. requestAnimationFrame( animate );
  112. object.rotation.x += 0.005;
  113. object.rotation.y += 0.01;
  114. composer.render();
  115. stats.update();
  116. }
  117. </script>
  118. <div>
  119. </body>
  120. </html>