webgpu_postprocessing_bloom_selective.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - bloom selective</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 - bloom selective
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.webgpu.js",
  17. "three/tsl": "../build/three.webgpu.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import { pass, mrt, output, float, uniform } from 'three/tsl';
  25. import { bloom } from 'three/addons/tsl/display/BloomNode.js';
  26. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  27. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  28. // scene
  29. const scene = new THREE.Scene();
  30. const camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 200 );
  31. camera.position.set( 0, 0, 20 );
  32. camera.lookAt( 0, 0, 0 );
  33. const geometry = new THREE.IcosahedronGeometry( 1, 15 );
  34. for ( let i = 0; i < 50; i ++ ) {
  35. const color = new THREE.Color();
  36. color.setHSL( Math.random(), 0.7, Math.random() * 0.2 + 0.05 );
  37. const bloomIntensity = Math.random() > 0.5 ? 1 : 0;
  38. const material = new THREE.MeshBasicNodeMaterial( { color: color } );
  39. material.mrtNode = mrt( {
  40. bloomIntensity: uniform( bloomIntensity )
  41. } );
  42. const sphere = new THREE.Mesh( geometry, material );
  43. sphere.position.x = Math.random() * 10 - 5;
  44. sphere.position.y = Math.random() * 10 - 5;
  45. sphere.position.z = Math.random() * 10 - 5;
  46. sphere.position.normalize().multiplyScalar( Math.random() * 4.0 + 2.0 );
  47. sphere.scale.setScalar( Math.random() * Math.random() + 0.5 );
  48. scene.add( sphere );
  49. }
  50. // renderer
  51. const renderer = new THREE.WebGPURenderer();
  52. renderer.setPixelRatio( window.devicePixelRatio );
  53. renderer.setSize( window.innerWidth, window.innerHeight );
  54. renderer.setAnimationLoop( animate );
  55. renderer.toneMapping = THREE.NeutralToneMapping;
  56. document.body.appendChild( renderer.domElement );
  57. // post processing
  58. const scenePass = pass( scene, camera );
  59. scenePass.setMRT( mrt( {
  60. output,
  61. bloomIntensity: float( 0 ) // default bloom intensity
  62. } ) );
  63. const outputPass = scenePass.getTextureNode();
  64. const bloomIntensityPass = scenePass.getTextureNode( 'bloomIntensity' );
  65. const bloomPass = bloom( outputPass.mul( bloomIntensityPass ) );
  66. const postProcessing = new THREE.PostProcessing( renderer );
  67. postProcessing.outputColorTransform = false;
  68. postProcessing.outputNode = outputPass.add( bloomPass ).renderOutput();
  69. // controls
  70. const controls = new OrbitControls( camera, renderer.domElement );
  71. controls.maxPolarAngle = Math.PI * 0.5;
  72. controls.minDistance = 1;
  73. controls.maxDistance = 100;
  74. // raycaster
  75. const raycaster = new THREE.Raycaster();
  76. const mouse = new THREE.Vector2();
  77. window.addEventListener( 'pointerdown', ( event ) => {
  78. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  79. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  80. raycaster.setFromCamera( mouse, camera );
  81. const intersects = raycaster.intersectObjects( scene.children, false );
  82. if ( intersects.length > 0 ) {
  83. const material = intersects[ 0 ].object.material;
  84. const bloomIntensity = material.mrtNode.get( 'bloomIntensity' );
  85. bloomIntensity.value = bloomIntensity.value === 0 ? 1 : 0;
  86. }
  87. } );
  88. // gui
  89. const gui = new GUI();
  90. const bloomFolder = gui.addFolder( 'bloom' );
  91. bloomFolder.add( bloomPass.threshold, 'value', 0.0, 1.0 ).name( 'threshold' );
  92. bloomFolder.add( bloomPass.strength, 'value', 0.0, 3 ).name( 'strength' );
  93. bloomFolder.add( bloomPass.radius, 'value', 0.0, 1.0 ).name( 'radius' );
  94. const toneMappingFolder = gui.addFolder( 'tone mapping' );
  95. toneMappingFolder.add( renderer, 'toneMappingExposure', 0.1, 3 ).name( 'exposure' );
  96. // events
  97. window.onresize = function () {
  98. const width = window.innerWidth;
  99. const height = window.innerHeight;
  100. camera.aspect = width / height;
  101. camera.updateProjectionMatrix();
  102. renderer.setSize( width, height );
  103. };
  104. // animate
  105. function animate() {
  106. postProcessing.render();
  107. }
  108. </script>
  109. </body>
  110. </html>