webgpu_postprocessing_bloom_selective.html 4.4 KB

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