webgpu_postprocessing_motion_blur.html 6.6 KB

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