webgpu_postprocessing_afterimage.html 2.6 KB

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