webgl_postprocessing_unreal_bloom.html 5.1 KB

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