webgl_postprocessing_unreal_bloom.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 {
  23. AmbientLight,
  24. AnimationMixer,
  25. Clock,
  26. PerspectiveCamera,
  27. PointLight,
  28. ReinhardToneMapping,
  29. Scene,
  30. Vector2,
  31. WebGLRenderer
  32. } from "../build/three.module.js";
  33. import Stats from './jsm/libs/stats.module.js';
  34. import { GUI } from './jsm/libs/dat.gui.module.js';
  35. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  36. import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
  37. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  38. import { RenderPass } from './jsm/postprocessing/RenderPass.js';
  39. import { UnrealBloomPass } from './jsm/postprocessing/UnrealBloomPass.js';
  40. var scene, camera, controls, pointLight, stats;
  41. var composer, renderer, mixer;
  42. var params = {
  43. exposure: 1,
  44. bloomStrength: 1.5,
  45. bloomThreshold: 0,
  46. bloomRadius: 0
  47. };
  48. var clock = new Clock();
  49. var container = document.getElementById( 'container' );
  50. stats = new Stats();
  51. container.appendChild( stats.dom );
  52. renderer = new WebGLRenderer( { antialias: true } );
  53. renderer.setPixelRatio( window.devicePixelRatio );
  54. renderer.setSize( window.innerWidth, window.innerHeight );
  55. renderer.toneMapping = ReinhardToneMapping;
  56. container.appendChild( renderer.domElement );
  57. scene = new Scene();
  58. camera = new PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 100 );
  59. camera.position.set( - 5, 2.5, - 3.5 );
  60. scene.add( camera );
  61. controls = new OrbitControls( camera, renderer.domElement );
  62. controls.maxPolarAngle = Math.PI * 0.5;
  63. controls.minDistance = 1;
  64. controls.maxDistance = 10;
  65. scene.add( new AmbientLight( 0x404040 ) );
  66. pointLight = new PointLight( 0xffffff, 1 );
  67. camera.add( pointLight );
  68. var renderScene = new RenderPass( scene, camera );
  69. var bloomPass = new UnrealBloomPass( new Vector2( window.innerWidth, window.innerHeight ), 1.5, 0.4, 0.85 );
  70. bloomPass.threshold = params.bloomThreshold;
  71. bloomPass.strength = params.bloomStrength;
  72. bloomPass.radius = params.bloomRadius;
  73. composer = new EffectComposer( renderer );
  74. composer.addPass( renderScene );
  75. composer.addPass( bloomPass );
  76. new GLTFLoader().load( 'models/gltf/PrimaryIonDrive.glb', function ( gltf ) {
  77. var model = gltf.scene;
  78. scene.add( model );
  79. // Mesh contains self-intersecting semi-transparent faces, which display
  80. // z-fighting unless depthWrite is disabled.
  81. var core = model.getObjectByName( 'geo1_HoloFillDark_0' );
  82. core.material.depthWrite = false;
  83. mixer = new AnimationMixer( model );
  84. var clip = gltf.animations[ 0 ];
  85. mixer.clipAction( clip.optimize() ).play();
  86. animate();
  87. } );
  88. var gui = new GUI();
  89. gui.add( params, 'exposure', 0.1, 2 ).onChange( function ( value ) {
  90. renderer.toneMappingExposure = Math.pow( value, 4.0 );
  91. } );
  92. gui.add( params, 'bloomThreshold', 0.0, 1.0 ).onChange( function ( value ) {
  93. bloomPass.threshold = Number( value );
  94. } );
  95. gui.add( params, 'bloomStrength', 0.0, 3.0 ).onChange( function ( value ) {
  96. bloomPass.strength = Number( value );
  97. } );
  98. gui.add( params, 'bloomRadius', 0.0, 1.0 ).step( 0.01 ).onChange( function ( value ) {
  99. bloomPass.radius = Number( value );
  100. } );
  101. window.onresize = function () {
  102. var width = window.innerWidth;
  103. var height = window.innerHeight;
  104. camera.aspect = width / height;
  105. camera.updateProjectionMatrix();
  106. renderer.setSize( width, height );
  107. composer.setSize( width, height );
  108. };
  109. function animate() {
  110. requestAnimationFrame( animate );
  111. const delta = clock.getDelta();
  112. mixer.update( delta );
  113. stats.update();
  114. composer.render();
  115. }
  116. </script>
  117. </body>
  118. </html>