webgl_postprocessing_smaa.html 3.2 KB

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