webgpu_postprocessing_motion_blur.html 6.5 KB

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