webgl_postprocessing_fxaa.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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: 70px;
  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: No FXAA, Right: FXAA
  28. </div>
  29. <div id="container">
  30. </div>
  31. <!-- Import maps polyfill -->
  32. <!-- Remove this when import maps will be widely supported -->
  33. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  34. <script type="importmap">
  35. {
  36. "imports": {
  37. "three": "../build/three.module.js",
  38. "three/addons/": "./jsm/"
  39. }
  40. }
  41. </script>
  42. <script type="module">
  43. import * as THREE from 'three';
  44. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  45. import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  46. import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js';
  47. import { ColorCorrectionShader } from 'three/addons/shaders/ColorCorrectionShader.js';
  48. import { FXAAShader } from 'three/addons/shaders/FXAAShader.js';
  49. let camera, scene, renderer, clock, group, container;
  50. let composer1, composer2, fxaaPass;
  51. init();
  52. animate();
  53. function init() {
  54. container = document.getElementById( 'container' );
  55. camera = new THREE.PerspectiveCamera( 45, container.offsetWidth / container.offsetHeight, 1, 2000 );
  56. camera.position.z = 500;
  57. scene = new THREE.Scene();
  58. clock = new THREE.Clock();
  59. //
  60. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x8d8d8d, 3 );
  61. hemiLight.position.set( 0, 1000, 0 );
  62. scene.add( hemiLight );
  63. const dirLight = new THREE.DirectionalLight( 0xffffff, 2.5 );
  64. dirLight.position.set( - 3000, 1000, - 1000 );
  65. scene.add( dirLight );
  66. //
  67. group = new THREE.Group();
  68. const geometry = new THREE.TetrahedronGeometry( 10 );
  69. const material = new THREE.MeshStandardMaterial( { color: 0xf73232, flatShading: true } );
  70. for ( let i = 0; i < 100; i ++ ) {
  71. const mesh = new THREE.Mesh( geometry, material );
  72. mesh.position.x = Math.random() * 500 - 250;
  73. mesh.position.y = Math.random() * 500 - 250;
  74. mesh.position.z = Math.random() * 500 - 250;
  75. mesh.scale.setScalar( Math.random() * 2 + 1 );
  76. mesh.rotation.x = Math.random() * Math.PI;
  77. mesh.rotation.y = Math.random() * Math.PI;
  78. mesh.rotation.z = Math.random() * Math.PI;
  79. group.add( mesh );
  80. }
  81. scene.add( group );
  82. //
  83. renderer = new THREE.WebGLRenderer();
  84. renderer.setPixelRatio( window.devicePixelRatio );
  85. renderer.setSize( container.offsetWidth, container.offsetHeight );
  86. renderer.autoClear = false;
  87. container.appendChild( renderer.domElement );
  88. //
  89. const renderPass = new RenderPass( scene, camera );
  90. renderPass.clearAlpha = 0;
  91. //
  92. fxaaPass = new ShaderPass( FXAAShader );
  93. const colorCorrectionPass = new ShaderPass( ColorCorrectionShader );
  94. composer1 = new EffectComposer( renderer );
  95. composer1.addPass( renderPass );
  96. composer1.addPass( colorCorrectionPass );
  97. //
  98. const pixelRatio = renderer.getPixelRatio();
  99. fxaaPass.material.uniforms[ 'resolution' ].value.x = 1 / ( container.offsetWidth * pixelRatio );
  100. fxaaPass.material.uniforms[ 'resolution' ].value.y = 1 / ( container.offsetHeight * pixelRatio );
  101. composer2 = new EffectComposer( renderer );
  102. composer2.addPass( renderPass );
  103. composer2.addPass( colorCorrectionPass );
  104. // FXAA is engineered to be applied towards the end of engine post processing after conversion to low dynamic range and conversion to the sRGB color space for display.´
  105. composer2.addPass( fxaaPass );
  106. //
  107. window.addEventListener( 'resize', onWindowResize );
  108. }
  109. function onWindowResize() {
  110. camera.aspect = container.offsetWidth / container.offsetHeight;
  111. camera.updateProjectionMatrix();
  112. renderer.setSize( container.offsetWidth, container.offsetHeight );
  113. composer1.setSize( container.offsetWidth, container.offsetHeight );
  114. composer2.setSize( container.offsetWidth, container.offsetHeight );
  115. const pixelRatio = renderer.getPixelRatio();
  116. fxaaPass.material.uniforms[ 'resolution' ].value.x = 1 / ( container.offsetWidth * pixelRatio );
  117. fxaaPass.material.uniforms[ 'resolution' ].value.y = 1 / ( container.offsetHeight * pixelRatio );
  118. }
  119. function animate() {
  120. requestAnimationFrame( animate );
  121. const halfWidth = container.offsetWidth / 2;
  122. group.rotation.y += clock.getDelta() * 0.1;
  123. renderer.setScissorTest( true );
  124. renderer.setScissor( 0, 0, halfWidth - 1, container.offsetHeight );
  125. composer1.render();
  126. renderer.setScissor( halfWidth, 0, halfWidth, container.offsetHeight );
  127. composer2.render();
  128. renderer.setScissorTest( false );
  129. }
  130. </script>
  131. </body>
  132. </html>