webgpu_postprocessing_afterimage.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 WebGPU from 'three/addons/capabilities/WebGPU.js';
  23. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  24. import PostProcessing from 'three/addons/renderers/common/PostProcessing.js';
  25. import { pass } from 'three/nodes';
  26. let camera, scene, renderer;
  27. let mesh, postProcessing, combinedPass;
  28. const params = {
  29. damp: 0.96
  30. };
  31. init();
  32. createGUI();
  33. animate();
  34. function init() {
  35. if ( WebGPU.isAvailable() === false ) {
  36. document.body.appendChild( WebGPU.getErrorMessage() );
  37. throw new Error( 'No WebGPU support' );
  38. }
  39. renderer = new WebGPURenderer( { antialias: true } );
  40. renderer.setPixelRatio( window.devicePixelRatio );
  41. renderer.setSize( window.innerWidth, window.innerHeight );
  42. document.body.appendChild( renderer.domElement );
  43. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  44. camera.position.z = 400;
  45. scene = new THREE.Scene();
  46. scene.fog = new THREE.Fog( 0x000000, 1, 1000 );
  47. const geometry = new THREE.TorusKnotGeometry( 100, 30, 100, 16 );
  48. const material = new THREE.MeshNormalMaterial();
  49. mesh = new THREE.Mesh( geometry, material );
  50. scene.add( mesh );
  51. // postprocessing
  52. postProcessing = new PostProcessing( renderer );
  53. const scenePass = pass( scene, camera );
  54. const scenePassColor = scenePass.getTextureNode();
  55. combinedPass = scenePassColor;
  56. combinedPass = combinedPass.afterImage( params.damp );
  57. postProcessing.outputNode = combinedPass;
  58. window.addEventListener( 'resize', onWindowResize );
  59. }
  60. function createGUI() {
  61. const gui = new GUI( { title: 'Damp setting' } );
  62. gui.add( combinedPass.damp, 'value', 0, 1 ).step( 0.001 );
  63. }
  64. function onWindowResize() {
  65. camera.aspect = window.innerWidth / window.innerHeight;
  66. camera.updateProjectionMatrix();
  67. renderer.setSize( window.innerWidth, window.innerHeight );
  68. }
  69. function render() {
  70. mesh.rotation.x += 0.0075;
  71. mesh.rotation.y += 0.015;
  72. if ( renderer.backend.isWebGPUBackend ) {
  73. postProcessing.render();
  74. } else {
  75. renderer.render( scene, camera );
  76. }
  77. }
  78. function animate() {
  79. requestAnimationFrame( animate );
  80. render();
  81. }
  82. </script>
  83. </body>
  84. </html>