webgl_postprocessing_unreal_bloom.html 5.1 KB

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