webgpu_postprocessing_radial_blur.html 5.0 KB

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