2
0

webgl_postprocessing_unreal_bloom.html 4.9 KB

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