webgl_postprocessing_smaa.html 2.9 KB

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