webgpu_postprocessing_bloom.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - postprocessing - bloom</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. <meta property="og:title" content="three.js webgpu - postprocessing - bloom">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_postprocessing_bloom.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_postprocessing_bloom.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  16. <div class="title-wrapper">
  17. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Bloom</span>
  18. </div>
  19. <small>
  20. Gaussian bloom by <a href="http://eduperiment.com" target="_blank" rel="noopener">Prashant Sharma</a> and <a href="https://clara.io" target="_blank" rel="noopener">Ben Houston</a>.<br/>
  21. <a href="https://blog.sketchfab.com/art-spotlight-primary-ion-drive/" target="_blank" rel="noopener">Primary Ion Drive</a>
  22. by <a href="http://mjmurdock.com/" target="_blank" rel="noopener">Mike Murdock</a> is licensed under <a href="https://creativecommons.org/licenses/by/4.0/" target="_blank" rel="noopener">Creative Commons Attribution</a>.<br />
  23. </small>
  24. </div>
  25. <script type="importmap">
  26. {
  27. "imports": {
  28. "three": "../build/three.webgpu.js",
  29. "three/webgpu": "../build/three.webgpu.js",
  30. "three/tsl": "../build/three.tsl.js",
  31. "three/addons/": "./jsm/"
  32. }
  33. }
  34. </script>
  35. <script type="module">
  36. import * as THREE from 'three/webgpu';
  37. import { pass, uniform } from 'three/tsl';
  38. import { bloom } from 'three/addons/tsl/display/BloomNode.js';
  39. import { dualKawaseBloom } from 'three/addons/tsl/display/DualKawaseBloomNode.js';
  40. import { Inspector } from 'three/addons/inspector/Inspector.js';
  41. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  42. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  43. let camera;
  44. let renderPipeline, renderer, mixer, timer;
  45. const params = {
  46. type: 'Gaussian',
  47. exposure: 1
  48. };
  49. init();
  50. async function init() {
  51. timer = new THREE.Timer();
  52. timer.connect( document );
  53. const scene = new THREE.Scene();
  54. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 100 );
  55. camera.position.set( - 5, 2.5, - 3.5 );
  56. scene.add( camera );
  57. scene.add( new THREE.AmbientLight( 0xcccccc ) );
  58. const pointLight = new THREE.PointLight( 0xffffff, 100 );
  59. camera.add( pointLight );
  60. const loader = new GLTFLoader();
  61. const gltf = await loader.loadAsync( 'models/gltf/PrimaryIonDrive.glb' );
  62. const model = gltf.scene;
  63. scene.add( model );
  64. mixer = new THREE.AnimationMixer( model );
  65. const clip = gltf.animations[ 0 ];
  66. mixer.clipAction( clip.optimize() ).play();
  67. //
  68. renderer = new THREE.WebGPURenderer( { antialias: true } );
  69. renderer.setPixelRatio( window.devicePixelRatio );
  70. renderer.setSize( window.innerWidth, window.innerHeight );
  71. renderer.setAnimationLoop( animate );
  72. renderer.toneMapping = THREE.ReinhardToneMapping;
  73. renderer.inspector = new Inspector();
  74. document.body.appendChild( renderer.domElement );
  75. //
  76. renderPipeline = new THREE.RenderPipeline( renderer );
  77. const scenePass = pass( scene, camera );
  78. const scenePassColor = scenePass.getTextureNode( 'output' ).toInspector( 'Color' );
  79. const strength = uniform( 1 );
  80. const radius = uniform( 0 );
  81. const threshold = uniform( 0 );
  82. const bloomPasses = {
  83. 'Gaussian': bloom( scenePassColor, strength, radius, threshold ),
  84. 'Dual Kawase': dualKawaseBloom( scenePassColor, strength, radius, threshold )
  85. };
  86. function updateBloom() {
  87. renderPipeline.outputNode = scenePassColor.add( bloomPasses[ params.type ] );
  88. renderPipeline.needsUpdate = true;
  89. }
  90. updateBloom();
  91. //
  92. const controls = new OrbitControls( camera, renderer.domElement );
  93. controls.maxPolarAngle = Math.PI * 0.5;
  94. controls.minDistance = 3;
  95. controls.maxDistance = 8;
  96. //
  97. const gui = renderer.inspector.createParameters( 'Settings' );
  98. const bloomFolder = gui.addFolder( 'bloom' );
  99. bloomFolder.add( params, 'type', [ 'Gaussian', 'Dual Kawase' ] ).onChange( updateBloom );
  100. bloomFolder.add( threshold, 'value', 0.0, 1.0 ).name( 'threshold' );
  101. bloomFolder.add( strength, 'value', 0.0, 3.0 ).name( 'strength' );
  102. bloomFolder.add( radius, 'value', 0.0, 1.0, 0.01 ).name( 'radius' );
  103. const toneMappingFolder = gui.addFolder( 'tone mapping' );
  104. toneMappingFolder.add( params, 'exposure', 0.1, 2 ).onChange( function ( value ) {
  105. renderer.toneMappingExposure = Math.pow( value, 4.0 );
  106. } );
  107. window.addEventListener( 'resize', onWindowResize );
  108. }
  109. function onWindowResize() {
  110. const width = window.innerWidth;
  111. const height = window.innerHeight;
  112. camera.aspect = width / height;
  113. camera.updateProjectionMatrix();
  114. renderer.setSize( width, height );
  115. }
  116. function animate() {
  117. timer.update();
  118. const delta = timer.getDelta();
  119. mixer.update( delta );
  120. renderPipeline.render();
  121. }
  122. </script>
  123. </body>
  124. </html>
粤ICP备19079148号