webgl_postprocessing_unreal_bloom.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - postprocessing - unreal 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. </head>
  9. <body>
  10. <div id="container"></div>
  11. <div id="info">
  12. <a href="http://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>
  13. <p>
  14. This Bloom Pass is inspired by the bloom pass of the Unreal Engine. It creates a mip map chain of bloom textures and blur them
  15. with different radii.<br /> Because of the weigted combination of mips, and since larger blurs are done on higher mips, this bloom
  16. is better in quality and performance.
  17. </p>
  18. Model: <a href="https://blog.sketchfab.com/art-spotlight-primary-ion-drive/" target="_blank" rel="noopener">Primary Ion Drive</a> by
  19. <a href="http://mjmurdock.com/" target="_blank" rel="noopener">Mike Murdock</a>, CC Attribution.
  20. </div>
  21. <script type="module">
  22. import * as THREE from '../build/three.module.js';
  23. import Stats from './jsm/libs/stats.module.js';
  24. import { GUI } from './jsm/libs/dat.gui.module.js';
  25. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  26. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  27. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  28. import { RenderPass } from './jsm/postprocessing/RenderPass.js';
  29. import { UnrealBloomPass } from './jsm/postprocessing/UnrealBloomPass.js';
  30. var scene, camera, controls, pointLight, stats;
  31. var composer, renderer, mixer;
  32. var params = {
  33. exposure: 1,
  34. bloomStrength: 1.5,
  35. bloomThreshold: 0,
  36. bloomRadius: 0
  37. };
  38. var clock = new THREE.Clock();
  39. var container = document.getElementById( 'container' );
  40. stats = new Stats();
  41. container.appendChild( stats.dom );
  42. renderer = new THREE.WebGLRenderer( { antialias: true } );
  43. renderer.setPixelRatio( window.devicePixelRatio );
  44. renderer.setSize( window.innerWidth, window.innerHeight );
  45. renderer.toneMapping = THREE.ReinhardToneMapping;
  46. container.appendChild( renderer.domElement );
  47. scene = new THREE.Scene();
  48. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 100 );
  49. camera.position.set( - 5, 2.5, - 3.5 );
  50. scene.add( camera );
  51. controls = new OrbitControls( camera, renderer.domElement );
  52. controls.maxPolarAngle = Math.PI * 0.5;
  53. controls.minDistance = 1;
  54. controls.maxDistance = 10;
  55. scene.add( new THREE.AmbientLight( 0x404040 ) );
  56. pointLight = new THREE.PointLight( 0xffffff, 1 );
  57. camera.add( pointLight );
  58. var renderScene = new RenderPass( scene, camera );
  59. var bloomPass = new UnrealBloomPass( new THREE.Vector2( window.innerWidth, window.innerHeight ), 1.5, 0.4, 0.85 );
  60. bloomPass.threshold = params.bloomThreshold;
  61. bloomPass.strength = params.bloomStrength;
  62. bloomPass.radius = params.bloomRadius;
  63. composer = new EffectComposer( renderer );
  64. composer.addPass( renderScene );
  65. composer.addPass( bloomPass );
  66. new GLTFLoader().load( 'models/gltf/PrimaryIonDrive.glb', function ( gltf ) {
  67. var model = gltf.scene;
  68. scene.add( model );
  69. // Mesh contains self-intersecting semi-transparent faces, which display
  70. // z-fighting unless depthWrite is disabled.
  71. var core = model.getObjectByName( 'geo1_HoloFillDark_0' );
  72. core.material.depthWrite = false;
  73. mixer = new THREE.AnimationMixer( model );
  74. var clip = gltf.animations[ 0 ];
  75. mixer.clipAction( clip.optimize() ).play();
  76. animate();
  77. } );
  78. var gui = new GUI();
  79. gui.add( params, 'exposure', 0.1, 2 ).onChange( function ( value ) {
  80. renderer.toneMappingExposure = Math.pow( value, 4.0 );
  81. } );
  82. gui.add( params, 'bloomThreshold', 0.0, 1.0 ).onChange( function ( value ) {
  83. bloomPass.threshold = Number( value );
  84. } );
  85. gui.add( params, 'bloomStrength', 0.0, 3.0 ).onChange( function ( value ) {
  86. bloomPass.strength = Number( value );
  87. } );
  88. gui.add( params, 'bloomRadius', 0.0, 1.0 ).step( 0.01 ).onChange( function ( value ) {
  89. bloomPass.radius = Number( value );
  90. } );
  91. window.onresize = function () {
  92. var width = window.innerWidth;
  93. var height = window.innerHeight;
  94. camera.aspect = width / height;
  95. camera.updateProjectionMatrix();
  96. renderer.setSize( width, height );
  97. composer.setSize( width, height );
  98. };
  99. function animate() {
  100. requestAnimationFrame( animate );
  101. const delta = clock.getDelta();
  102. mixer.update( delta );
  103. stats.update();
  104. composer.render();
  105. }
  106. </script>
  107. </body>
  108. </html>