webgl_postprocessing_smaa.html 3.9 KB

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