webgl_postprocessing_fxaa.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 type="module">
  32. import * as THREE from '../build/three.module.js';
  33. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  34. import { RenderPass } from './jsm/postprocessing/RenderPass.js';
  35. import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
  36. import { CopyShader } from './jsm/shaders/CopyShader.js';
  37. import { FXAAShader } from './jsm/shaders/FXAAShader.js';
  38. var camera, scene, renderer, clock, group, container;
  39. var composer1, composer2, fxaaPass;
  40. init();
  41. animate();
  42. function init() {
  43. container = document.getElementById( 'container' );
  44. camera = new THREE.PerspectiveCamera( 45, ( container.offsetWidth * 0.5 ) / container.offsetHeight, 1, 2000 );
  45. camera.position.z = 500;
  46. scene = new THREE.Scene();
  47. scene.background = new THREE.Color( 0xffffff );
  48. scene.fog = new THREE.Fog( 0xcccccc, 100, 1500 );
  49. clock = new THREE.Clock();
  50. //
  51. var hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444 );
  52. hemiLight.position.set( 0, 1000, 0 );
  53. scene.add( hemiLight );
  54. var dirLight = new THREE.DirectionalLight( 0xffffff, 0.8 );
  55. dirLight.position.set( - 3000, 1000, - 1000 );
  56. scene.add( dirLight );
  57. //
  58. group = new THREE.Group();
  59. var geometry = new THREE.TetrahedronBufferGeometry( 10 );
  60. var material = new THREE.MeshStandardMaterial( { color: 0xee0808, flatShading: true } );
  61. for ( var i = 0; i < 100; i ++ ) {
  62. var mesh = new THREE.Mesh( geometry, material );
  63. mesh.position.x = Math.random() * 500 - 250;
  64. mesh.position.y = Math.random() * 500 - 250;
  65. mesh.position.z = Math.random() * 500 - 250;
  66. mesh.scale.setScalar( Math.random() * 2 + 1 );
  67. mesh.rotation.x = Math.random() * Math.PI;
  68. mesh.rotation.y = Math.random() * Math.PI;
  69. mesh.rotation.z = Math.random() * Math.PI;
  70. group.add( mesh );
  71. }
  72. scene.add( group );
  73. //
  74. renderer = new THREE.WebGLRenderer();
  75. renderer.autoClear = false;
  76. renderer.setPixelRatio( window.devicePixelRatio );
  77. renderer.setSize( container.offsetWidth, container.offsetHeight );
  78. container.appendChild( renderer.domElement );
  79. //
  80. var renderPass = new RenderPass( scene, camera );
  81. //
  82. fxaaPass = new ShaderPass( FXAAShader );
  83. var pixelRatio = renderer.getPixelRatio();
  84. fxaaPass.material.uniforms[ 'resolution' ].value.x = 1 / ( container.offsetWidth * pixelRatio );
  85. fxaaPass.material.uniforms[ 'resolution' ].value.y = 1 / ( container.offsetHeight * pixelRatio );
  86. composer1 = new EffectComposer( renderer );
  87. composer1.addPass( renderPass );
  88. composer1.addPass( fxaaPass );
  89. //
  90. var copyPass = new ShaderPass( CopyShader );
  91. composer2 = new EffectComposer( renderer );
  92. composer2.addPass( renderPass );
  93. composer2.addPass( copyPass );
  94. //
  95. window.addEventListener( 'resize', onWindowResize, false );
  96. }
  97. function onWindowResize() {
  98. camera.aspect = ( container.offsetWidth * 0.5 ) / container.offsetHeight;
  99. camera.updateProjectionMatrix();
  100. renderer.setSize( container.offsetWidth, container.offsetHeight );
  101. composer1.setSize( container.offsetWidth, container.offsetHeight );
  102. composer2.setSize( container.offsetWidth, container.offsetHeight );
  103. var pixelRatio = renderer.getPixelRatio();
  104. fxaaPass.material.uniforms[ 'resolution' ].value.x = 1 / ( container.offsetWidth * pixelRatio );
  105. fxaaPass.material.uniforms[ 'resolution' ].value.y = 1 / ( container.offsetHeight * pixelRatio );
  106. }
  107. function animate() {
  108. requestAnimationFrame( animate );
  109. var halfWidth = container.offsetWidth / 2;
  110. group.rotation.y += clock.getDelta() * 0.1;
  111. renderer.setViewport( 0, 0, halfWidth, container.offsetHeight );
  112. composer1.render();
  113. renderer.setViewport( halfWidth, 0, halfWidth, container.offsetHeight );
  114. composer2.render();
  115. }
  116. </script>
  117. </body>
  118. </html>