webgl_postprocessing_fxaa.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js WebGL - postprocessing - FXAA</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. <style>
  9. body {
  10. background-color: #fff;
  11. color: #222;
  12. }
  13. a {
  14. color: #08f;
  15. }
  16. #container {
  17. position: absolute;
  18. top: 80px;
  19. width: 100%;
  20. bottom: 0px;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div id="info">
  26. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - postprocessing - FXAA<br />
  27. Left scene processed with FXAA, right scene is rendered without anti-aliasing.
  28. </div>
  29. <div id="container">
  30. </div>
  31. <script src="../build/three.js"></script>
  32. <script src="js/WebGL.js"></script>
  33. <script src="js/shaders/CopyShader.js"></script>
  34. <script src="js/postprocessing/EffectComposer.js"></script>
  35. <script src="js/postprocessing/RenderPass.js"></script>
  36. <script src="js/postprocessing/ShaderPass.js"></script>
  37. <script src="js/shaders/FXAAShader.js"></script>
  38. <script>
  39. if ( WEBGL.isWebGLAvailable() === false ) {
  40. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  41. }
  42. var camera, scene, renderer, clock, group;
  43. var composer1, composer2, fxaaPass;
  44. init();
  45. animate();
  46. function init() {
  47. var container = document.getElementById( 'container' );
  48. camera = new THREE.PerspectiveCamera( 45, ( container.offsetWidth * 0.5 ) / container.offsetHeight, 1, 2000 );
  49. camera.position.z = 500;
  50. scene = new THREE.Scene();
  51. scene.background = new THREE.Color( 0xffffff );
  52. scene.fog = new THREE.Fog( 0xcccccc, 100, 1500 );
  53. clock = new THREE.Clock();
  54. //
  55. var hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444 );
  56. hemiLight.position.set( 0, 1000, 0 );
  57. scene.add( hemiLight );
  58. var dirLight = new THREE.DirectionalLight( 0xffffff, 0.8 );
  59. dirLight.position.set( - 3000, 1000, - 1000 );
  60. scene.add( dirLight );
  61. //
  62. group = new THREE.Group();
  63. var geometry = new THREE.TetrahedronBufferGeometry( 10 );
  64. var material = new THREE.MeshStandardMaterial( { color: 0xee0808, flatShading: true } );
  65. for ( var i = 0; i < 100; i ++ ) {
  66. var mesh = new THREE.Mesh( geometry, material );
  67. mesh.position.x = Math.random() * 500 - 250;
  68. mesh.position.y = Math.random() * 500 - 250;
  69. mesh.position.z = Math.random() * 500 - 250;
  70. mesh.scale.setScalar( Math.random() * 2 + 1 );
  71. mesh.rotation.x = Math.random() * Math.PI;
  72. mesh.rotation.y = Math.random() * Math.PI;
  73. mesh.rotation.z = Math.random() * Math.PI;
  74. group.add( mesh );
  75. }
  76. scene.add( group );
  77. //
  78. renderer = new THREE.WebGLRenderer();
  79. renderer.autoClear = false;
  80. renderer.setPixelRatio( window.devicePixelRatio );
  81. renderer.setSize( container.offsetWidth, container.offsetHeight );
  82. container.appendChild( renderer.domElement );
  83. //
  84. var renderPass = new THREE.RenderPass( scene, camera );
  85. //
  86. fxaaPass = new THREE.ShaderPass( THREE.FXAAShader );
  87. var pixelRatio = renderer.getPixelRatio();
  88. fxaaPass.material.uniforms[ 'resolution' ].value.x = 1 / ( container.offsetWidth * pixelRatio );
  89. fxaaPass.material.uniforms[ 'resolution' ].value.y = 1 / ( container.offsetHeight * pixelRatio );
  90. composer1 = new THREE.EffectComposer( renderer );
  91. composer1.addPass( renderPass );
  92. composer1.addPass( fxaaPass );
  93. //
  94. var copyPass = new THREE.ShaderPass( THREE.CopyShader );
  95. composer2 = new THREE.EffectComposer( renderer );
  96. composer2.addPass( renderPass );
  97. composer2.addPass( copyPass );
  98. //
  99. window.addEventListener( 'resize', onWindowResize, false );
  100. }
  101. function onWindowResize() {
  102. camera.aspect = ( container.offsetWidth * 0.5 ) / container.offsetHeight;
  103. camera.updateProjectionMatrix();
  104. renderer.setSize( container.offsetWidth, container.offsetHeight );
  105. composer1.setSize( container.offsetWidth, container.offsetHeight );
  106. composer2.setSize( container.offsetWidth, container.offsetHeight );
  107. var pixelRatio = renderer.getPixelRatio();
  108. fxaaPass.material.uniforms[ 'resolution' ].value.x = 1 / ( container.offsetWidth * pixelRatio );
  109. fxaaPass.material.uniforms[ 'resolution' ].value.y = 1 / ( container.offsetHeight * pixelRatio );
  110. }
  111. function animate() {
  112. requestAnimationFrame( animate );
  113. var halfWidth = container.offsetWidth / 2;
  114. group.rotation.y += clock.getDelta() * 0.1;
  115. renderer.setViewport( 0, 0, halfWidth, container.offsetHeight );
  116. composer1.render();
  117. renderer.setViewport( halfWidth, 0, halfWidth, container.offsetHeight );
  118. composer2.render();
  119. }
  120. </script>
  121. </body>
  122. </html>