webgpu_postprocessing_difference.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - frame difference</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. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a>
  12. <br/>saturated color of objects according to the difference from one frame to another
  13. </div>
  14. <script type="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.webgpu.js",
  18. "three/tsl": "../build/three.webgpu.js",
  19. "three/addons/": "./jsm/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import { pass, luminance, saturation } from 'three/tsl';
  26. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  27. import { Timer } from 'three/addons/misc/Timer.js';
  28. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  29. const params = {
  30. speed: 0
  31. };
  32. let camera, renderer, postProcessing;
  33. let timer, mesh, controls;
  34. init();
  35. function init() {
  36. renderer = new THREE.WebGPURenderer();
  37. renderer.setPixelRatio( window.devicePixelRatio );
  38. renderer.setSize( window.innerWidth, window.innerHeight );
  39. renderer.setAnimationLoop( animate );
  40. renderer.toneMapping = THREE.NeutralToneMapping;
  41. document.body.appendChild( renderer.domElement );
  42. //
  43. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 100 );
  44. camera.position.set( 1, 2, 3 );
  45. const scene = new THREE.Scene();
  46. scene.fog = new THREE.Fog( 0x0487e2, 7, 25 );
  47. scene.background = new THREE.Color( 0x0487e2 );
  48. timer = new Timer();
  49. const texture = new THREE.TextureLoader().load( 'textures/crate.gif' );
  50. texture.colorSpace = THREE.SRGBColorSpace;
  51. const geometry = new THREE.BoxGeometry();
  52. const material = new THREE.MeshBasicMaterial( { map: texture } );
  53. mesh = new THREE.Mesh( geometry, material );
  54. scene.add( mesh );
  55. // post processing
  56. postProcessing = new THREE.PostProcessing( renderer );
  57. const scenePass = pass( scene, camera );
  58. const currentTexture = scenePass.getTextureNode();
  59. const previousTexture = scenePass.getPreviousTextureNode();
  60. const frameDiff = previousTexture.sub( currentTexture ).abs();
  61. const saturationAmount = luminance( frameDiff ).mul( 1000 ).clamp( 0, 3 );
  62. postProcessing.outputNode = saturation( currentTexture, saturationAmount );
  63. //
  64. controls = new OrbitControls( camera, renderer.domElement );
  65. controls.minDistance = 2;
  66. controls.maxDistance = 10;
  67. controls.enableDamping = true;
  68. controls.dampingFactor = 0.01;
  69. window.addEventListener( 'resize', onWindowResize );
  70. //
  71. const gui = new GUI();
  72. gui.add( params, 'speed', 0, 2 );
  73. }
  74. function onWindowResize() {
  75. camera.aspect = window.innerWidth / window.innerHeight;
  76. camera.updateProjectionMatrix();
  77. renderer.setSize( window.innerWidth, window.innerHeight );
  78. }
  79. function animate() {
  80. timer.update();
  81. controls.update();
  82. mesh.rotation.y += timer.getDelta() * 5 * params.speed;
  83. postProcessing.render();
  84. }
  85. </script>
  86. </body>
  87. </html>