webgpu_postprocessing_difference.html 3.5 KB

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