1
0

webgpu_postprocessing_bloom_selective.html 4.5 KB

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