webgl_postprocessing_smaa.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - postprocessing smaa</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. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> - post-processing SMAA
  12. </div>
  13. <div id="container"></div>
  14. <script type="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.module.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import Stats from 'three/addons/libs/stats.module.js';
  25. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  26. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  27. import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  28. import { SMAAPass } from 'three/addons/postprocessing/SMAAPass.js';
  29. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  30. let camera, scene, renderer, composer, stats, smaaPass;
  31. const params = {
  32. enabled: true,
  33. autoRotate: true
  34. };
  35. init();
  36. function init() {
  37. const container = document.getElementById( 'container' );
  38. renderer = new THREE.WebGLRenderer();
  39. renderer.setPixelRatio( window.devicePixelRatio );
  40. renderer.setSize( window.innerWidth, window.innerHeight );
  41. renderer.setAnimationLoop( animate );
  42. document.body.appendChild( renderer.domElement );
  43. stats = new Stats();
  44. container.appendChild( stats.dom );
  45. //
  46. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  47. camera.position.z = 300;
  48. scene = new THREE.Scene();
  49. const geometry = new THREE.BoxGeometry( 120, 120, 120 );
  50. const material1 = new THREE.MeshBasicMaterial( { color: 0xffffff, wireframe: true } );
  51. const mesh1 = new THREE.Mesh( geometry, material1 );
  52. mesh1.position.x = - 100;
  53. scene.add( mesh1 );
  54. const texture = new THREE.TextureLoader().load( 'textures/brick_diffuse.jpg' );
  55. texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
  56. texture.colorSpace = THREE.SRGBColorSpace;
  57. const material2 = new THREE.MeshBasicMaterial( { map: texture } );
  58. const mesh2 = new THREE.Mesh( geometry, material2 );
  59. mesh2.position.x = 100;
  60. scene.add( mesh2 );
  61. // postprocessing
  62. composer = new EffectComposer( renderer );
  63. composer.addPass( new RenderPass( scene, camera ) );
  64. smaaPass = new SMAAPass( window.innerWidth * renderer.getPixelRatio(), window.innerHeight * renderer.getPixelRatio() );
  65. composer.addPass( smaaPass );
  66. const outputPass = new OutputPass();
  67. composer.addPass( outputPass );
  68. window.addEventListener( 'resize', onWindowResize );
  69. const gui = new GUI();
  70. const smaaFolder = gui.addFolder( 'SMAA' );
  71. smaaFolder.add( params, 'enabled' );
  72. const sceneFolder = gui.addFolder( 'Scene' );
  73. sceneFolder.add( params, 'autoRotate' );
  74. }
  75. function onWindowResize() {
  76. const width = window.innerWidth;
  77. const height = window.innerHeight;
  78. camera.aspect = width / height;
  79. camera.updateProjectionMatrix();
  80. renderer.setSize( width, height );
  81. composer.setSize( width, height );
  82. }
  83. function animate() {
  84. stats.begin();
  85. if ( params.autoRotate === true ) {
  86. for ( let i = 0; i < scene.children.length; i ++ ) {
  87. const child = scene.children[ i ];
  88. child.rotation.x += 0.005;
  89. child.rotation.y += 0.01;
  90. }
  91. }
  92. smaaPass.enabled = params.enabled;
  93. composer.render();
  94. stats.end();
  95. }
  96. </script>
  97. </body>
  98. </html>