1
0

webgpu_postprocessing_difference.html 3.8 KB

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