webgpu_postprocessing_difference.html 3.3 KB

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