webgl_postprocessing_smaa.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. <!-- Import maps polyfill -->
  15. <!-- Remove this when import maps will be widely supported -->
  16. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js",
  21. "three/addons/": "./jsm/"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import Stats from 'three/addons/libs/stats.module.js';
  28. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  29. import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  30. import { SMAAPass } from 'three/addons/postprocessing/SMAAPass.js';
  31. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  32. let camera, scene, renderer, composer, stats;
  33. init();
  34. animate();
  35. function init() {
  36. const container = document.getElementById( 'container' );
  37. renderer = new THREE.WebGLRenderer();
  38. renderer.setPixelRatio( window.devicePixelRatio );
  39. renderer.setSize( window.innerWidth, window.innerHeight );
  40. renderer.useLegacyLights = false;
  41. document.body.appendChild( renderer.domElement );
  42. stats = new Stats();
  43. container.appendChild( stats.dom );
  44. //
  45. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  46. camera.position.z = 300;
  47. scene = new THREE.Scene();
  48. const geometry = new THREE.BoxGeometry( 120, 120, 120 );
  49. const material1 = new THREE.MeshBasicMaterial( { color: 0xffffff, wireframe: true } );
  50. const mesh1 = new THREE.Mesh( geometry, material1 );
  51. mesh1.position.x = - 100;
  52. scene.add( mesh1 );
  53. const texture = new THREE.TextureLoader().load( 'textures/brick_diffuse.jpg' );
  54. texture.anisotropy = 4;
  55. texture.colorSpace = THREE.SRGBColorSpace;
  56. const material2 = new THREE.MeshBasicMaterial( { map: texture } );
  57. const mesh2 = new THREE.Mesh( geometry, material2 );
  58. mesh2.position.x = 100;
  59. scene.add( mesh2 );
  60. // postprocessing
  61. composer = new EffectComposer( renderer );
  62. composer.addPass( new RenderPass( scene, camera ) );
  63. const pass = new SMAAPass( window.innerWidth * renderer.getPixelRatio(), window.innerHeight * renderer.getPixelRatio() );
  64. composer.addPass( pass );
  65. const outputPass = new OutputPass();
  66. composer.addPass( outputPass );
  67. window.addEventListener( 'resize', onWindowResize );
  68. }
  69. function onWindowResize() {
  70. const width = window.innerWidth;
  71. const height = window.innerHeight;
  72. camera.aspect = width / height;
  73. camera.updateProjectionMatrix();
  74. renderer.setSize( width, height );
  75. composer.setSize( width, height );
  76. }
  77. function animate() {
  78. requestAnimationFrame( animate );
  79. stats.begin();
  80. for ( let i = 0; i < scene.children.length; i ++ ) {
  81. const child = scene.children[ i ];
  82. child.rotation.x += 0.005;
  83. child.rotation.y += 0.01;
  84. }
  85. composer.render();
  86. stats.end();
  87. }
  88. </script>
  89. </body>
  90. </html>