webgl_postprocessing_unreal_bloom.html 4.5 KB

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