webgpu_postprocessing_bloom.html 4.5 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. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. #info > * {
  10. max-width: 650px;
  11. margin-left: auto;
  12. margin-right: auto;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div id="container"></div>
  18. <div id="info">
  19. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Bloom pass 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>
  20. <br/>
  21. Model: <a href="https://blog.sketchfab.com/art-spotlight-primary-ion-drive/" target="_blank" rel="noopener">Primary Ion Drive</a> by
  22. <a href="http://mjmurdock.com/" target="_blank" rel="noopener">Mike Murdock</a>, CC Attribution.
  23. </div>
  24. <script type="importmap">
  25. {
  26. "imports": {
  27. "three": "../build/three.webgpu.js",
  28. "three/tsl": "../build/three.webgpu.js",
  29. "three/addons/": "./jsm/"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three';
  35. import { pass } from 'three/tsl';
  36. import { bloom } from 'three/addons/tsl/display/BloomNode.js';
  37. import Stats from 'three/addons/libs/stats.module.js';
  38. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  39. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  40. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  41. let camera, stats;
  42. let postProcessing, renderer, mixer, clock;
  43. const params = {
  44. threshold: 0,
  45. strength: 1,
  46. radius: 0,
  47. exposure: 1
  48. };
  49. init();
  50. async function init() {
  51. const container = document.getElementById( 'container' );
  52. clock = new THREE.Clock();
  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. container.appendChild( renderer.domElement );
  74. //
  75. postProcessing = new THREE.PostProcessing( renderer );
  76. const scenePass = pass( scene, camera );
  77. const scenePassColor = scenePass.getTextureNode( 'output' );
  78. const bloomPass = bloom( scenePassColor );
  79. postProcessing.outputNode = scenePassColor.add( bloomPass );
  80. //
  81. stats = new Stats();
  82. container.appendChild( stats.dom );
  83. //
  84. const controls = new OrbitControls( camera, renderer.domElement );
  85. controls.maxPolarAngle = Math.PI * 0.5;
  86. controls.minDistance = 3;
  87. controls.maxDistance = 8;
  88. //
  89. const gui = new GUI();
  90. const bloomFolder = gui.addFolder( 'bloom' );
  91. bloomFolder.add( params, 'threshold', 0.0, 1.0 ).onChange( function ( value ) {
  92. bloomPass.threshold.value = value;
  93. } );
  94. bloomFolder.add( params, 'strength', 0.0, 3.0 ).onChange( function ( value ) {
  95. bloomPass.strength.value = value;
  96. } );
  97. gui.add( params, 'radius', 0.0, 1.0 ).step( 0.01 ).onChange( function ( value ) {
  98. bloomPass.radius.value = value;
  99. } );
  100. const toneMappingFolder = gui.addFolder( 'tone mapping' );
  101. toneMappingFolder.add( params, 'exposure', 0.1, 2 ).onChange( function ( value ) {
  102. renderer.toneMappingExposure = Math.pow( value, 4.0 );
  103. } );
  104. window.addEventListener( 'resize', onWindowResize );
  105. }
  106. function onWindowResize() {
  107. const width = window.innerWidth;
  108. const height = window.innerHeight;
  109. camera.aspect = width / height;
  110. camera.updateProjectionMatrix();
  111. renderer.setSize( width, height );
  112. }
  113. function animate() {
  114. const delta = clock.getDelta();
  115. mixer.update( delta );
  116. postProcessing.render();
  117. stats.update();
  118. }
  119. </script>
  120. </body>
  121. </html>