2
0

webgl_postprocessing_unreal_bloom.html 4.8 KB

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