webgpu_postprocessing_afterimage.html 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - 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="importmap">
  11. {
  12. "imports": {
  13. "three": "../build/three.webgpu.js",
  14. "three/tsl": "../build/three.webgpu.js",
  15. "three/addons/": "./jsm/"
  16. }
  17. }
  18. </script>
  19. <script type="module">
  20. import * as THREE from 'three';
  21. import { pass } from 'three/tsl';
  22. import { afterImage } from 'three/addons/tsl/display/AfterImageNode.js';
  23. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  24. let camera, scene, renderer;
  25. let mesh, postProcessing, combinedPass;
  26. const params = {
  27. damp: 0.96
  28. };
  29. init();
  30. createGUI();
  31. function init() {
  32. renderer = new THREE.WebGPURenderer( { antialias: true } );
  33. renderer.setPixelRatio( window.devicePixelRatio );
  34. renderer.setSize( window.innerWidth, window.innerHeight );
  35. renderer.setAnimationLoop( animate );
  36. document.body.appendChild( renderer.domElement );
  37. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  38. camera.position.z = 400;
  39. scene = new THREE.Scene();
  40. scene.fog = new THREE.Fog( 0x000000, 1, 1000 );
  41. const geometry = new THREE.TorusKnotGeometry( 100, 30, 100, 16 );
  42. const material = new THREE.MeshNormalMaterial();
  43. mesh = new THREE.Mesh( geometry, material );
  44. scene.add( mesh );
  45. // postprocessing
  46. postProcessing = new THREE.PostProcessing( renderer );
  47. const scenePass = pass( scene, camera );
  48. const scenePassColor = scenePass.getTextureNode();
  49. combinedPass = scenePassColor;
  50. combinedPass = afterImage( combinedPass, params.damp );
  51. postProcessing.outputNode = combinedPass;
  52. window.addEventListener( 'resize', onWindowResize );
  53. }
  54. function createGUI() {
  55. const gui = new GUI( { title: 'Damp setting' } );
  56. gui.add( combinedPass.damp, 'value', 0, 1 ).step( 0.001 );
  57. }
  58. function onWindowResize() {
  59. camera.aspect = window.innerWidth / window.innerHeight;
  60. camera.updateProjectionMatrix();
  61. renderer.setSize( window.innerWidth, window.innerHeight );
  62. }
  63. function render() {
  64. mesh.rotation.x += 0.0075;
  65. mesh.rotation.y += 0.015;
  66. postProcessing.render();
  67. }
  68. function animate() {
  69. render();
  70. }
  71. </script>
  72. </body>
  73. </html>