1
0

webgpu_postprocessing_bloom_selective.html 4.8 KB

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