webgpu_postprocessing_radial_blur.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - radial 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 - radial blur">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_postprocessing_radial_blur.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_postprocessing_radial_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>Radial Blur</span>
  18. </div>
  19. <small>Radial Blur for faked light shafts.</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 { float, int, pass, uniform } from 'three/tsl';
  34. import { radialBlur } from 'three/addons/tsl/display/radialBlur.js';
  35. import { Inspector } from 'three/addons/inspector/Inspector.js';
  36. const params = {
  37. enabled: true,
  38. animated: true
  39. };
  40. let camera, scene, renderer, timer, group;
  41. let renderPipeline;
  42. init();
  43. async function init() {
  44. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 200 );
  45. camera.position.z = 50;
  46. scene = new THREE.Scene();
  47. scene.background = new THREE.Color( 0x000000 );
  48. timer = new THREE.Timer();
  49. //
  50. const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x8d8d8d );
  51. hemiLight.position.set( 0, 1000, 0 );
  52. scene.add( hemiLight );
  53. const pointLight = new THREE.PointLight( 0xffffff, 1000 );
  54. pointLight.position.set( 0, 0, 0 );
  55. scene.add( pointLight );
  56. //
  57. group = new THREE.Group();
  58. const geometry = new THREE.TetrahedronGeometry();
  59. const material = new THREE.MeshStandardMaterial( { flatShading: true } );
  60. const mesh = new THREE.InstancedMesh( geometry, material, 100 );
  61. const dummy = new THREE.Object3D();
  62. const col = new THREE.Color();
  63. const center = new THREE.Vector2();
  64. for ( let i = 0; i < mesh.count; i ++ ) {
  65. dummy.position.x = Math.random() * 50 - 25;
  66. dummy.position.y = Math.random() * 50 - 25;
  67. dummy.position.z = Math.random() * 50 - 25;
  68. center.set( dummy.position.x, dummy.position.y );
  69. // make sure tetrahedrons are not positioned at the origin
  70. if ( center.length() < 6 ) {
  71. center.normalize().multiplyScalar( 6 );
  72. dummy.position.x = center.x;
  73. dummy.position.y = center.y;
  74. }
  75. dummy.scale.setScalar( Math.random() * 2 + 1 );
  76. dummy.rotation.x = Math.random() * Math.PI;
  77. dummy.rotation.y = Math.random() * Math.PI;
  78. dummy.rotation.z = Math.random() * Math.PI;
  79. dummy.updateMatrix();
  80. mesh.setMatrixAt( i, dummy.matrix );
  81. col.setHSL( 0.55 + ( i / mesh.count ) * 0.15, 1, 0.2 );
  82. mesh.setColorAt( i, col );
  83. }
  84. group.add( mesh );
  85. scene.add( group );
  86. renderer = new THREE.WebGPURenderer();
  87. renderer.setPixelRatio( window.devicePixelRatio );
  88. renderer.setSize( window.innerWidth, window.innerHeight );
  89. renderer.setAnimationLoop( animate );
  90. renderer.inspector = new Inspector();
  91. renderer.toneMapping = THREE.NeutralToneMapping;
  92. document.body.appendChild( renderer.domElement );
  93. // post processing
  94. renderPipeline = new THREE.RenderPipeline( renderer );
  95. const scenePass = pass( scene, camera );
  96. const weightUniform = uniform( float( 0.9 ) );
  97. const decayUniform = uniform( float( 0.95 ) );
  98. const exposureUniform = uniform( int( 5 ) );
  99. const countUniform = uniform( int( 32 ) );
  100. const blurPass = radialBlur( scenePass, { weight: weightUniform, decay: decayUniform, count: countUniform, exposure: exposureUniform } );
  101. renderPipeline.outputNode = blurPass;
  102. //
  103. window.addEventListener( 'resize', onWindowResize );
  104. //
  105. const gui = renderer.inspector.createParameters( 'Settings' );
  106. gui.add( weightUniform, 'value', 0, 1 ).name( 'weight' );
  107. gui.add( decayUniform, 'value', 0, 1 ).name( 'decay' );
  108. gui.add( countUniform, 'value', 16, 64, 1 ).name( 'sample count' );
  109. gui.add( exposureUniform, 'value', 1, 10 ).name( 'exposure' );
  110. gui.add( params, 'enabled' ).onChange( ( value ) => {
  111. if ( value === true ) {
  112. renderPipeline.outputNode = blurPass;
  113. } else {
  114. renderPipeline.outputNode = scenePass;
  115. }
  116. renderPipeline.needsUpdate = true;
  117. } );
  118. gui.add( params, 'animated' );
  119. }
  120. function onWindowResize() {
  121. camera.aspect = window.innerWidth / window.innerHeight;
  122. camera.updateProjectionMatrix();
  123. renderer.setSize( window.innerWidth, window.innerHeight );
  124. }
  125. //
  126. function animate() {
  127. timer.update();
  128. const delta = timer.getDelta();
  129. if ( params.animated === true ) {
  130. group.rotation.y += delta * 0.1;
  131. }
  132. renderPipeline.render();
  133. }
  134. </script>
  135. </body>
  136. </html>
粤ICP备19079148号