webgpu_postprocessing_motion_blur.html 7.0 KB

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