webgpu_postprocessing_motion_blur.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - motion blur</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> webgpu - motion blur
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.webgpu.js",
  17. "three/tsl": "../build/three.webgpu.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import { pass, texture, motionBlur, uniform, output, mrt, mix, velocity, uv, screenUV } from 'three/tsl';
  25. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  26. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  27. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  28. import Stats from 'three/addons/libs/stats.module.js';
  29. let camera, scene, renderer;
  30. let boxLeft, boxRight, model, mixer, clock;
  31. let postProcessing;
  32. let controls;
  33. let stats;
  34. const params = {
  35. speed: 1.0
  36. };
  37. init();
  38. function init() {
  39. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.25, 30 );
  40. camera.position.set( 0, 1.5, 4.5 );
  41. scene = new THREE.Scene();
  42. scene.fog = new THREE.Fog( 0x0487e2, 7, 25 );
  43. const sunLight = new THREE.DirectionalLight( 0xFFE499, 5 );
  44. sunLight.castShadow = true;
  45. sunLight.shadow.camera.near = .1;
  46. sunLight.shadow.camera.far = 10;
  47. sunLight.shadow.camera.right = 2;
  48. sunLight.shadow.camera.left = - 2;
  49. sunLight.shadow.camera.top = 2;
  50. sunLight.shadow.camera.bottom = - 2;
  51. sunLight.shadow.mapSize.width = 2048;
  52. sunLight.shadow.mapSize.height = 2048;
  53. sunLight.shadow.bias = - 0.001;
  54. sunLight.position.set( 4, 4, 2 );
  55. const waterAmbientLight = new THREE.HemisphereLight( 0x333366, 0x74ccf4, 5 );
  56. const skyAmbientLight = new THREE.HemisphereLight( 0x74ccf4, 0, 1 );
  57. scene.add( sunLight );
  58. scene.add( skyAmbientLight );
  59. scene.add( waterAmbientLight );
  60. clock = new THREE.Clock();
  61. // animated model
  62. const loader = new GLTFLoader();
  63. loader.load( 'models/gltf/Xbot.glb', function ( gltf ) {
  64. model = gltf.scene;
  65. model.rotation.y = Math.PI / 2;
  66. model.traverse( function ( child ) {
  67. if ( child.isMesh ) {
  68. child.castShadow = true;
  69. child.receiveShadow = true;
  70. }
  71. } );
  72. mixer = new THREE.AnimationMixer( model );
  73. const action = mixer.clipAction( gltf.animations[ 3 ] );
  74. action.play();
  75. scene.add( model );
  76. } );
  77. // textures
  78. const textureLoader = new THREE.TextureLoader();
  79. const floorColor = textureLoader.load( 'textures/floors/FloorsCheckerboard_S_Diffuse.jpg' );
  80. floorColor.wrapS = THREE.RepeatWrapping;
  81. floorColor.wrapT = THREE.RepeatWrapping;
  82. floorColor.colorSpace = THREE.SRGBColorSpace;
  83. const floorNormal = textureLoader.load( 'textures/floors/FloorsCheckerboard_S_Normal.jpg' );
  84. floorNormal.wrapS = THREE.RepeatWrapping;
  85. floorNormal.wrapT = THREE.RepeatWrapping;
  86. // floor
  87. const floorUV = uv().mul( 5 );
  88. const floorMaterial = new THREE.MeshPhongNodeMaterial();
  89. floorMaterial.colorNode = texture( floorColor, floorUV );
  90. const floor = new THREE.Mesh( new THREE.BoxGeometry( 15, .001, 15 ), floorMaterial );
  91. floor.receiveShadow = true;
  92. floor.position.set( 0, 0, 0 );
  93. scene.add( floor );
  94. const walls = new THREE.Mesh( new THREE.BoxGeometry( 15, 15, 15 ), new THREE.MeshPhongNodeMaterial( { colorNode: floorMaterial.colorNode, side: THREE.BackSide } ) );
  95. scene.add( walls );
  96. const map = new THREE.TextureLoader().load( 'textures/uv_grid_opengl.jpg' );
  97. map.colorSpace = THREE.SRGBColorSpace;
  98. const geometry = new THREE.TorusGeometry( .8 );
  99. const material = new THREE.MeshBasicMaterial( { map } );
  100. boxRight = new THREE.Mesh( geometry, material );
  101. boxRight.position.set( 3.5, 1.5, - 4 );
  102. scene.add( boxRight );
  103. boxLeft = new THREE.Mesh( geometry, material );
  104. boxLeft.position.set( - 3.5, 1.5, - 4 );
  105. scene.add( boxLeft );
  106. // renderer
  107. renderer = new THREE.WebGPURenderer();
  108. renderer.setPixelRatio( window.devicePixelRatio );
  109. renderer.setSize( window.innerWidth, window.innerHeight );
  110. renderer.setAnimationLoop( animate );
  111. document.body.appendChild( renderer.domElement );
  112. stats = new Stats();
  113. document.body.appendChild( stats.dom );
  114. controls = new OrbitControls( camera, renderer.domElement );
  115. controls.minDistance = 1;
  116. controls.maxDistance = 10;
  117. controls.maxPolarAngle = Math.PI / 2;
  118. controls.autoRotate = true;
  119. controls.autoRotateSpeed = 1;
  120. controls.target.set( 0, 1, 0 );
  121. controls.enableDamping = true;
  122. controls.dampingFactor = 0.05;
  123. controls.update();
  124. // post-processing
  125. const blurAmount = uniform( 1 );
  126. const showVelocity = uniform( 0 );
  127. const scenePass = pass( scene, camera );
  128. scenePass.setMRT( mrt( {
  129. output,
  130. velocity
  131. } ) );
  132. const beauty = scenePass.getTextureNode();
  133. const vel = scenePass.getTextureNode( 'velocity' ).mul( blurAmount );
  134. const mBlur = motionBlur( beauty, vel );
  135. const vignet = screenUV.distance( .5 ).remap( .6, 1 ).mul( 2 ).clamp().oneMinus();
  136. postProcessing = new THREE.PostProcessing( renderer );
  137. postProcessing.outputNode = mix( mBlur, vel, showVelocity ).mul( vignet );
  138. //
  139. const gui = new GUI();
  140. gui.title( 'Motion Blur Settings' );
  141. gui.add( controls, 'autoRotate' );
  142. gui.add( blurAmount, 'value', 0, 3 ).name( 'blur amount' );
  143. gui.add( params, 'speed', 0, 2 );
  144. gui.add( showVelocity, 'value', 0, 1 ).name( 'show velocity' );
  145. //
  146. window.addEventListener( 'resize', onWindowResize );
  147. }
  148. function onWindowResize() {
  149. camera.aspect = window.innerWidth / window.innerHeight;
  150. camera.updateProjectionMatrix();
  151. renderer.setSize( window.innerWidth, window.innerHeight );
  152. }
  153. function animate() {
  154. stats.update();
  155. controls.update();
  156. const delta = clock.getDelta();
  157. const speed = params.speed;
  158. boxRight.rotation.y += delta * 4 * speed;
  159. boxLeft.scale.setScalar( 1 + Math.sin( clock.elapsedTime * 10 * speed ) * .2 );
  160. if ( model ) {
  161. mixer.update( delta * speed );
  162. }
  163. postProcessing.render();
  164. }
  165. </script>
  166. </body>
  167. </html>