webgl_postprocessing_afterimage.html 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - postprocessing - afterimage</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. <script type="module">
  11. import * as THREE from '../build/three.module.js';
  12. import { GUI } from './jsm/libs/dat.gui.module.js';
  13. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  14. import { RenderPass } from './jsm/postprocessing/RenderPass.js';
  15. import { AfterimagePass } from './jsm/postprocessing/AfterimagePass.js';
  16. var camera, scene, renderer, composer;
  17. var mesh;
  18. var afterimagePass;
  19. var params = {
  20. enable: true
  21. };
  22. init();
  23. createGUI();
  24. animate();
  25. function init() {
  26. renderer = new THREE.WebGLRenderer();
  27. renderer.setPixelRatio( window.devicePixelRatio );
  28. renderer.setSize( window.innerWidth, window.innerHeight );
  29. document.body.appendChild( renderer.domElement );
  30. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  31. camera.position.z = 400;
  32. scene = new THREE.Scene();
  33. scene.fog = new THREE.Fog( 0x000000, 1, 1000 );
  34. var geometry = new THREE.BoxBufferGeometry( 150, 150, 150, 2, 2, 2 );
  35. var material = new THREE.MeshNormalMaterial();
  36. mesh = new THREE.Mesh( geometry, material );
  37. scene.add( mesh );
  38. // postprocessing
  39. composer = new EffectComposer( renderer );
  40. composer.addPass( new RenderPass( scene, camera ) );
  41. afterimagePass = new AfterimagePass();
  42. composer.addPass( afterimagePass );
  43. window.addEventListener( 'resize', onWindowResize, false );
  44. }
  45. function createGUI() {
  46. var gui = new GUI( { name: 'Damp setting' } );
  47. gui.add( afterimagePass.uniforms[ "damp" ], 'value', 0, 1 ).step( 0.001 );
  48. gui.add( params, 'enable' );
  49. }
  50. function onWindowResize() {
  51. camera.aspect = window.innerWidth / window.innerHeight;
  52. camera.updateProjectionMatrix();
  53. renderer.setSize( window.innerWidth, window.innerHeight );
  54. composer.setSize( window.innerWidth, window.innerHeight );
  55. }
  56. function animate() {
  57. requestAnimationFrame( animate );
  58. mesh.rotation.x += 0.005;
  59. mesh.rotation.y += 0.01;
  60. if ( params.enable ) {
  61. composer.render();
  62. } else {
  63. renderer.render( scene, camera );
  64. }
  65. }
  66. </script>
  67. </body>
  68. </html>