1
0

webgpu_postprocessing_motion_blur.html 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 = 2048;
  56. sunLight.shadow.mapSize.height = 2048;
  57. sunLight.shadow.bias = - 0.001;
  58. sunLight.position.set( 4, 4, 2 );
  59. const waterAmbientLight = new THREE.HemisphereLight( 0x333366, 0x74ccf4, 5 );
  60. const skyAmbientLight = new THREE.HemisphereLight( 0x74ccf4, 0, 1 );
  61. scene.add( sunLight );
  62. scene.add( skyAmbientLight );
  63. scene.add( waterAmbientLight );
  64. clock = new THREE.Clock();
  65. // animated model
  66. const loader = new GLTFLoader();
  67. loader.load( 'models/gltf/Xbot.glb', function ( gltf ) {
  68. model = gltf.scene;
  69. model.rotation.y = Math.PI / 2;
  70. model.traverse( function ( child ) {
  71. if ( child.isMesh ) {
  72. child.castShadow = true;
  73. child.receiveShadow = true;
  74. }
  75. } );
  76. mixer = new THREE.AnimationMixer( model );
  77. const action = mixer.clipAction( gltf.animations[ 3 ] );
  78. action.play();
  79. scene.add( model );
  80. } );
  81. // textures
  82. const textureLoader = new THREE.TextureLoader();
  83. const floorColor = textureLoader.load( 'textures/floors/FloorsCheckerboard_S_Diffuse.jpg' );
  84. floorColor.wrapS = THREE.RepeatWrapping;
  85. floorColor.wrapT = THREE.RepeatWrapping;
  86. floorColor.colorSpace = THREE.SRGBColorSpace;
  87. const floorNormal = textureLoader.load( 'textures/floors/FloorsCheckerboard_S_Normal.jpg' );
  88. floorNormal.wrapS = THREE.RepeatWrapping;
  89. floorNormal.wrapT = THREE.RepeatWrapping;
  90. // floor
  91. const floorUV = uv().mul( 5 );
  92. const floorMaterial = new THREE.MeshPhongNodeMaterial();
  93. floorMaterial.colorNode = texture( floorColor, floorUV );
  94. const floor = new THREE.Mesh( new THREE.BoxGeometry( 15, .001, 15 ), floorMaterial );
  95. floor.receiveShadow = true;
  96. floor.position.set( 0, 0, 0 );
  97. scene.add( floor );
  98. const walls = new THREE.Mesh( new THREE.BoxGeometry( 15, 15, 15 ), new THREE.MeshPhongNodeMaterial( { colorNode: floorMaterial.colorNode, side: THREE.BackSide } ) );
  99. scene.add( walls );
  100. const map = new THREE.TextureLoader().load( 'textures/uv_grid_opengl.jpg' );
  101. map.colorSpace = THREE.SRGBColorSpace;
  102. const geometry = new THREE.TorusGeometry( .8 );
  103. const material = new THREE.MeshBasicMaterial( { map } );
  104. boxRight = new THREE.Mesh( geometry, material );
  105. boxRight.position.set( 3.5, 1.5, - 4 );
  106. scene.add( boxRight );
  107. boxLeft = new THREE.Mesh( geometry, material );
  108. boxLeft.position.set( - 3.5, 1.5, - 4 );
  109. scene.add( boxLeft );
  110. // renderer
  111. renderer = new THREE.WebGPURenderer();
  112. renderer.setPixelRatio( window.devicePixelRatio );
  113. renderer.setSize( window.innerWidth, window.innerHeight );
  114. renderer.setAnimationLoop( animate );
  115. renderer.shadowMap.enabled = true;
  116. renderer.inspector = new Inspector();
  117. document.body.appendChild( renderer.domElement );
  118. controls = new OrbitControls( camera, renderer.domElement );
  119. controls.minDistance = 1;
  120. controls.maxDistance = 10;
  121. controls.maxPolarAngle = Math.PI / 2;
  122. controls.autoRotate = true;
  123. controls.autoRotateSpeed = 1;
  124. controls.target.set( 0, 1, 0 );
  125. controls.enableDamping = true;
  126. controls.dampingFactor = 0.05;
  127. controls.update();
  128. // post-processing
  129. const blurAmount = uniform( 1 );
  130. const scenePass = pass( scene, camera );
  131. scenePass.setMRT( mrt( {
  132. output,
  133. velocity
  134. } ) );
  135. const beauty = scenePass.getTextureNode().toInspector( 'Color' );
  136. const vel = scenePass.getTextureNode( 'velocity' ).toInspector( '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 = mBlur.mul( vignette );
  141. //
  142. const gui = renderer.inspector.createParameters( '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. //
  147. window.addEventListener( 'resize', onWindowResize );
  148. }
  149. function onWindowResize() {
  150. camera.aspect = window.innerWidth / window.innerHeight;
  151. camera.updateProjectionMatrix();
  152. renderer.setSize( window.innerWidth, window.innerHeight );
  153. }
  154. function animate() {
  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>
粤ICP备19079148号