webgpu_postprocessing_motion_blur.html 6.6 KB

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