2
0

webgl_postprocessing_unreal_bloom.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. <style>
  8. body {
  9. color: #fff;
  10. font-family:Monospace;
  11. font-size:13px;
  12. text-align:center;
  13. background-color: #fff;
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. #info {
  18. position: absolute;
  19. top: 0px;
  20. width: 100%;
  21. padding: 5px;
  22. }
  23. #info p {
  24. max-width: 600px;
  25. margin-left: auto;
  26. margin-right: auto;
  27. padding: 0 2em;
  28. }
  29. a {
  30. color: #2983ff;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <div id="container"></div>
  36. <div id="info">
  37. <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>
  38. <p>
  39. 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
  40. with different radii. Because of the weigted combination of mips, and since larger blurs are done on higher mips, this bloom
  41. is better in quality and performance.
  42. </p>
  43. Model: <a href="https://blog.sketchfab.com/art-spotlight-primary-ion-drive/" target="_blank" rel="noopener">Primary Ion Drive</a> by
  44. <a href="http://mjmurdock.com/" target="_blank" rel="noopener">Mike Murdock</a>, CC Attribution.
  45. </div>
  46. <script src="../build/three.js"></script>
  47. <script src="js/libs/stats.min.js"></script>
  48. <script src="js/libs/dat.gui.min.js"></script>
  49. <script src="js/controls/OrbitControls.js"></script>
  50. <script src="js/loaders/GLTFLoader.js"></script>
  51. <script src="js/postprocessing/EffectComposer.js"></script>
  52. <script src="js/postprocessing/RenderPass.js"></script>
  53. <script src="js/postprocessing/ShaderPass.js"></script>
  54. <script src="js/shaders/CopyShader.js"></script>
  55. <script src="js/shaders/LuminosityHighPassShader.js"></script>
  56. <script src="js/postprocessing/UnrealBloomPass.js"></script>
  57. <script>
  58. var scene, camera, controls, pointLight, stats;
  59. var composer, renderer, mixer;
  60. var params = {
  61. exposure: 1,
  62. bloomStrength: 1.5,
  63. bloomThreshold: 0,
  64. bloomRadius: 0
  65. };
  66. var clock = new THREE.Clock();
  67. var container = document.getElementById( 'container' );
  68. stats = new Stats();
  69. container.appendChild( stats.dom );
  70. renderer = new THREE.WebGLRenderer( { antialias: true } );
  71. renderer.setPixelRatio( window.devicePixelRatio );
  72. renderer.setSize( window.innerWidth, window.innerHeight );
  73. renderer.toneMapping = THREE.ReinhardToneMapping;
  74. container.appendChild( renderer.domElement );
  75. scene = new THREE.Scene();
  76. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 100 );
  77. camera.position.set( - 5, 2.5, - 3.5 );
  78. scene.add( camera );
  79. controls = new THREE.OrbitControls( camera, renderer.domElement );
  80. controls.maxPolarAngle = Math.PI * 0.5;
  81. controls.minDistance = 1;
  82. controls.maxDistance = 10;
  83. scene.add( new THREE.AmbientLight( 0x404040 ) );
  84. pointLight = new THREE.PointLight( 0xffffff, 1 );
  85. camera.add( pointLight );
  86. var renderScene = new THREE.RenderPass( scene, camera );
  87. var bloomPass = new THREE.UnrealBloomPass( new THREE.Vector2( window.innerWidth, window.innerHeight ), 1.5, 0.4, 0.85 );
  88. bloomPass.renderToScreen = true;
  89. bloomPass.threshold = params.bloomThreshold;
  90. bloomPass.strength = params.bloomStrength;
  91. bloomPass.radius = params.bloomRadius;
  92. composer = new THREE.EffectComposer( renderer );
  93. composer.setSize( window.innerWidth, window.innerHeight );
  94. composer.addPass( renderScene );
  95. composer.addPass( bloomPass );
  96. new THREE.GLTFLoader().load( 'models/gltf/PrimaryIonDrive.glb', function ( gltf ) {
  97. var model = gltf.scene;
  98. scene.add( model );
  99. // Mesh contains self-intersecting semi-transparent faces, which display
  100. // z-fighting unless depthWrite is disabled.
  101. var core = model.getObjectByName( 'geo1_HoloFillDark_0' );
  102. core.material.depthWrite = false;
  103. mixer = new THREE.AnimationMixer( model );
  104. var clip = gltf.animations[ 0 ];
  105. mixer.clipAction( clip.optimize() ).play();
  106. animate();
  107. } );
  108. var gui = new dat.GUI();
  109. gui.add( params, 'exposure', 0.1, 2 ).onChange( function ( value ) {
  110. renderer.toneMappingExposure = Math.pow( value, 4.0 );
  111. } );
  112. gui.add( params, 'bloomThreshold', 0.0, 1.0 ).onChange( function ( value ) {
  113. bloomPass.threshold = Number( value );
  114. } );
  115. gui.add( params, 'bloomStrength', 0.0, 3.0 ).onChange( function ( value ) {
  116. bloomPass.strength = Number( value );
  117. } );
  118. gui.add( params, 'bloomRadius', 0.0, 1.0 ).step( 0.01 ).onChange( function ( value ) {
  119. bloomPass.radius = Number( value );
  120. } );
  121. window.onresize = function () {
  122. var width = window.innerWidth;
  123. var height = window.innerHeight;
  124. camera.aspect = width / height;
  125. camera.updateProjectionMatrix();
  126. renderer.setSize( width, height );
  127. composer.setSize( width, height );
  128. };
  129. function animate() {
  130. requestAnimationFrame( animate );
  131. const delta = clock.getDelta();
  132. mixer.update( delta );
  133. stats.update();
  134. composer.render();
  135. }
  136. </script>
  137. </body>
  138. </html>