webgl_postprocessing_fxaa.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. <script type="importmap">
  32. {
  33. "imports": {
  34. "three": "../build/three.module.js",
  35. "three/addons/": "./jsm/"
  36. }
  37. }
  38. </script>
  39. <script type="module">
  40. import * as THREE from 'three';
  41. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  42. import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  43. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  44. import { FXAAPass } from 'three/addons/postprocessing/FXAAPass.js';
  45. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  46. let camera, scene, renderer, controls, container;
  47. let composer1, composer2, fxaaPass;
  48. init();
  49. function init() {
  50. container = document.getElementById( 'container' );
  51. camera = new THREE.PerspectiveCamera( 45, container.offsetWidth / container.offsetHeight, 1, 2000 );
  52. camera.position.z = 500;
  53. scene = new THREE.Scene();
  54. //
  55. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x8d8d8d );
  56. hemiLight.position.set( 0, 1000, 0 );
  57. scene.add( hemiLight );
  58. const dirLight = new THREE.DirectionalLight( 0xffffff, 3 );
  59. dirLight.position.set( - 3000, 1000, - 1000 );
  60. scene.add( dirLight );
  61. //
  62. const geometry = new THREE.TetrahedronGeometry( 10 );
  63. const material = new THREE.MeshStandardMaterial( { color: 0xf73232, flatShading: true } );
  64. const mesh = new THREE.InstancedMesh( geometry, material, 100 );
  65. const dummy = new THREE.Object3D();
  66. for ( let i = 0; i < 100; i ++ ) {
  67. dummy.position.x = Math.random() * 500 - 250;
  68. dummy.position.y = Math.random() * 500 - 250;
  69. dummy.position.z = Math.random() * 500 - 250;
  70. dummy.scale.setScalar( Math.random() * 2 + 1 );
  71. dummy.rotation.x = Math.random() * Math.PI;
  72. dummy.rotation.y = Math.random() * Math.PI;
  73. dummy.rotation.z = Math.random() * Math.PI;
  74. dummy.updateMatrix();
  75. mesh.setMatrixAt( i, dummy.matrix );
  76. }
  77. scene.add( mesh );
  78. //
  79. renderer = new THREE.WebGLRenderer();
  80. renderer.setPixelRatio( window.devicePixelRatio );
  81. renderer.setSize( container.offsetWidth, container.offsetHeight );
  82. renderer.setAnimationLoop( animate );
  83. renderer.autoClear = false;
  84. controls = new OrbitControls( camera, renderer.domElement );
  85. controls.autoRotate = true;
  86. container.appendChild( renderer.domElement );
  87. //
  88. const renderPass = new RenderPass( scene, camera );
  89. renderPass.clearAlpha = 0;
  90. //
  91. fxaaPass = new FXAAPass();
  92. const outputPass = new OutputPass();
  93. composer1 = new EffectComposer( renderer );
  94. composer1.addPass( renderPass );
  95. composer1.addPass( outputPass );
  96. //
  97. composer2 = new EffectComposer( renderer );
  98. composer2.addPass( renderPass );
  99. composer2.addPass( outputPass );
  100. // 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.
  101. composer2.addPass( fxaaPass );
  102. //
  103. window.addEventListener( 'resize', onWindowResize );
  104. }
  105. function onWindowResize() {
  106. camera.aspect = container.offsetWidth / container.offsetHeight;
  107. camera.updateProjectionMatrix();
  108. renderer.setSize( container.offsetWidth, container.offsetHeight );
  109. composer1.setSize( container.offsetWidth, container.offsetHeight );
  110. composer2.setSize( container.offsetWidth, container.offsetHeight );
  111. }
  112. function animate() {
  113. const halfWidth = container.offsetWidth / 2;
  114. controls.update();
  115. renderer.setScissorTest( true );
  116. renderer.setScissor( 0, 0, halfWidth - 1, container.offsetHeight );
  117. composer1.render();
  118. renderer.setScissor( halfWidth, 0, halfWidth, container.offsetHeight );
  119. composer2.render();
  120. renderer.setScissorTest( false );
  121. }
  122. </script>
  123. </body>
  124. </html>
粤ICP备19079148号