webgl_postprocessing_msaa.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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: #000000;
  11. overflow: hidden;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div id="container"></div>
  17. <div id="info">
  18. <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>
  19. </div>
  20. <script src="../build/three.min.js"></script>
  21. <script src="js/postprocessing/MSAAPass.js"></script>
  22. <script src="js/shaders/CopyShader.js"></script>
  23. <script src="js/shaders/MSAAShader.js"></script>
  24. <script src="js/postprocessing/EffectComposer.js"></script>
  25. <script src="js/postprocessing/RenderPass.js"></script>
  26. <script src="js/postprocessing/MaskPass.js"></script>
  27. <script src="js/postprocessing/ShaderPass.js"></script>
  28. <script>
  29. var camera, scene, renderer, composer, pass;
  30. var geometry, material, object;
  31. var textureLoader = new THREE.TextureLoader();
  32. textureLoader.load( "textures/brick_diffuse.jpg", function( meshTexture ) {
  33. init( meshTexture );
  34. animate();
  35. });
  36. function init( meshTexture ) {
  37. renderer = new THREE.WebGLRenderer( { antialias: false } );
  38. renderer.setPixelRatio( window.devicePixelRatio );
  39. renderer.setSize( window.innerWidth, window.innerHeight );
  40. document.body.appendChild( renderer.domElement );
  41. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  42. camera.position.z = 400;
  43. scene = new THREE.Scene();
  44. geometry = new THREE.BoxGeometry( 200, 200, 200 );
  45. material = new THREE.MeshBasicMaterial( { map: meshTexture } );
  46. meshTexture.anisotropy = 4;
  47. object = new THREE.Mesh( geometry, material );
  48. scene.add( object );
  49. // postprocessing
  50. composer = new THREE.EffectComposer( renderer );
  51. var massPass = new THREE.MSAAPass( scene, camera, new THREE.Color( 1.0, 1.0, 1.0 ), 1.0 );
  52. massPass.currentSampleLevel = 4;
  53. composer.addPass( massPass );
  54. var copyPass = new THREE.ShaderPass(THREE.CopyShader);
  55. copyPass.renderToScreen = true;
  56. composer.addPass( copyPass );
  57. window.addEventListener( 'resize', onWindowResize, false );
  58. }
  59. function onWindowResize() {
  60. var width = window.innerWidth;
  61. var height = window.innerHeight;
  62. camera.aspect = width / height;
  63. camera.updateProjectionMatrix();
  64. renderer.setSize( width, height );
  65. var pixelRatio = renderer.getPixelRatio();
  66. var newWidth = Math.floor( width / pixelRatio ) || 1;
  67. var newHeight = Math.floor( height / pixelRatio ) || 1;
  68. composer.setSize( newWidth, newHeight );
  69. pass.setSize( newWidth, newHeight );
  70. }
  71. function animate() {
  72. requestAnimationFrame( animate );
  73. object.rotation.x += 0.005;
  74. object.rotation.y += 0.01;
  75. composer.render();
  76. }
  77. </script>
  78. </body>
  79. </html>