webgl_postprocessing_unreal_bloom.html 4.8 KB

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