webgl_postprocessing_unreal_bloom.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. <meta property="og:title" content="three.js webgl - postprocessing - unreal bloom">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_postprocessing_unreal_bloom.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_postprocessing_unreal_bloom.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. <style>
  13. #info > * {
  14. max-width: 650px;
  15. margin-left: auto;
  16. margin-right: auto;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div id="container"></div>
  22. <div id="info">
  23. <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>
  24. <br/>
  25. Model: <a href="https://blog.sketchfab.com/art-spotlight-primary-ion-drive/" target="_blank" rel="noopener">Primary Ion Drive</a> by
  26. <a href="http://mjmurdock.com/" target="_blank" rel="noopener">Mike Murdock</a>, CC Attribution.
  27. </div>
  28. <script type="importmap">
  29. {
  30. "imports": {
  31. "three": "../build/three.module.js",
  32. "three/addons/": "./jsm/"
  33. }
  34. }
  35. </script>
  36. <script type="module">
  37. import * as THREE from 'three';
  38. import Stats from 'three/addons/libs/stats.module.js';
  39. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  40. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  41. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  42. import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
  43. import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
  44. import { UnrealBloomPass } from 'three/addons/postprocessing/UnrealBloomPass.js';
  45. import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
  46. let camera, stats;
  47. let composer, renderer, mixer, timer;
  48. const params = {
  49. threshold: 0,
  50. strength: 1,
  51. radius: 0.5,
  52. exposure: 1
  53. };
  54. init();
  55. async function init() {
  56. const container = document.getElementById( 'container' );
  57. timer = new THREE.Timer();
  58. timer.connect( document );
  59. const scene = new THREE.Scene();
  60. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 100 );
  61. camera.position.set( - 5, 2.5, - 3.5 );
  62. scene.add( camera );
  63. scene.add( new THREE.AmbientLight( 0xcccccc ) );
  64. const pointLight = new THREE.PointLight( 0xffffff, 100 );
  65. camera.add( pointLight );
  66. const loader = new GLTFLoader();
  67. const gltf = await loader.loadAsync( 'models/gltf/PrimaryIonDrive.glb' );
  68. const model = gltf.scene;
  69. scene.add( model );
  70. mixer = new THREE.AnimationMixer( model );
  71. const clip = gltf.animations[ 0 ];
  72. mixer.clipAction( clip.optimize() ).play();
  73. //
  74. renderer = new THREE.WebGLRenderer( { antialias: true } );
  75. renderer.setPixelRatio( window.devicePixelRatio );
  76. renderer.setSize( window.innerWidth, window.innerHeight );
  77. renderer.setAnimationLoop( animate );
  78. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  79. container.appendChild( renderer.domElement );
  80. //
  81. const renderScene = new RenderPass( scene, camera );
  82. const bloomPass = new UnrealBloomPass( new THREE.Vector2( window.innerWidth, window.innerHeight ), 1.5, 0.4, 0.85 );
  83. bloomPass.threshold = params.threshold;
  84. bloomPass.strength = params.strength;
  85. bloomPass.radius = params.radius;
  86. const outputPass = new OutputPass();
  87. composer = new EffectComposer( renderer );
  88. composer.addPass( renderScene );
  89. composer.addPass( bloomPass );
  90. composer.addPass( outputPass );
  91. //
  92. stats = new Stats();
  93. container.appendChild( stats.dom );
  94. //
  95. const controls = new OrbitControls( camera, renderer.domElement );
  96. controls.maxPolarAngle = Math.PI * 0.5;
  97. controls.minDistance = 3;
  98. controls.maxDistance = 8;
  99. //
  100. const gui = new GUI();
  101. const bloomFolder = gui.addFolder( 'bloom' );
  102. bloomFolder.add( params, 'threshold', 0.0, 1.0 ).onChange( function ( value ) {
  103. bloomPass.threshold = Number( value );
  104. } );
  105. bloomFolder.add( params, 'strength', 0.0, 3.0 ).onChange( function ( value ) {
  106. bloomPass.strength = Number( value );
  107. } );
  108. gui.add( params, 'radius', 0.0, 1.0 ).step( 0.01 ).onChange( function ( value ) {
  109. bloomPass.radius = Number( value );
  110. } );
  111. const toneMappingFolder = gui.addFolder( 'tone mapping' );
  112. toneMappingFolder.add( params, 'exposure', 0.1, 2 ).onChange( function ( value ) {
  113. renderer.toneMappingExposure = Math.pow( value, 4.0 );
  114. } );
  115. window.addEventListener( 'resize', onWindowResize );
  116. }
  117. function onWindowResize() {
  118. const width = window.innerWidth;
  119. const height = window.innerHeight;
  120. camera.aspect = width / height;
  121. camera.updateProjectionMatrix();
  122. renderer.setSize( width, height );
  123. composer.setSize( width, height );
  124. }
  125. function animate() {
  126. timer.update();
  127. const delta = timer.getDelta();
  128. mixer.update( delta );
  129. stats.update();
  130. composer.render();
  131. }
  132. </script>
  133. </body>
  134. </html>
粤ICP备19079148号