webgl_postprocessing_smaa.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. let camera, scene, renderer, composer, stats;
  32. init();
  33. animate();
  34. function init() {
  35. const container = document.getElementById( 'container' );
  36. renderer = new THREE.WebGLRenderer();
  37. renderer.setPixelRatio( window.devicePixelRatio );
  38. renderer.setSize( window.innerWidth, window.innerHeight );
  39. document.body.appendChild( renderer.domElement );
  40. stats = new Stats();
  41. container.appendChild( stats.dom );
  42. //
  43. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  44. camera.position.z = 300;
  45. scene = new THREE.Scene();
  46. const geometry = new THREE.BoxGeometry( 120, 120, 120 );
  47. const material1 = new THREE.MeshBasicMaterial( { color: 0xffffff, wireframe: true } );
  48. const mesh1 = new THREE.Mesh( geometry, material1 );
  49. mesh1.position.x = - 100;
  50. scene.add( mesh1 );
  51. const texture = new THREE.TextureLoader().load( 'textures/brick_diffuse.jpg' );
  52. texture.anisotropy = 4;
  53. const material2 = new THREE.MeshBasicMaterial( { map: texture } );
  54. const mesh2 = new THREE.Mesh( geometry, material2 );
  55. mesh2.position.x = 100;
  56. scene.add( mesh2 );
  57. // postprocessing
  58. composer = new EffectComposer( renderer );
  59. composer.addPass( new RenderPass( scene, camera ) );
  60. const pass = new SMAAPass( window.innerWidth * renderer.getPixelRatio(), window.innerHeight * renderer.getPixelRatio() );
  61. composer.addPass( pass );
  62. window.addEventListener( 'resize', onWindowResize );
  63. }
  64. function onWindowResize() {
  65. const width = window.innerWidth;
  66. const height = window.innerHeight;
  67. camera.aspect = width / height;
  68. camera.updateProjectionMatrix();
  69. renderer.setSize( width, height );
  70. composer.setSize( width, height );
  71. }
  72. function animate() {
  73. requestAnimationFrame( animate );
  74. stats.begin();
  75. for ( let i = 0; i < scene.children.length; i ++ ) {
  76. const child = scene.children[ i ];
  77. child.rotation.x += 0.005;
  78. child.rotation.y += 0.01;
  79. }
  80. composer.render();
  81. stats.end();
  82. }
  83. </script>
  84. </body>
  85. </html>