webgl_postprocessing_unreal_bloom.html 4.9 KB

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