webgl_postprocessing_unreal_bloom_selective.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - postprocessing - unreal bloom selective</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. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> Click on a sphere to toggle bloom<br>By <a href="http://github.com/Temdog007" target="_blank" rel="noopener">Temdog007</a>
  12. </div>
  13. <script type="x-shader/x-vertex" id="vertexshader">
  14. varying vec2 vUv;
  15. void main() {
  16. vUv = uv;
  17. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  18. }
  19. </script>
  20. <script type="x-shader/x-fragment" id="fragmentshader">
  21. uniform sampler2D baseTexture;
  22. uniform sampler2D bloomTexture;
  23. varying vec2 vUv;
  24. vec4 getTexture( sampler2D texelToLinearTexture ) {
  25. return mapTexelToLinear( texture2D( texelToLinearTexture , vUv ) );
  26. }
  27. void main() {
  28. gl_FragColor = ( getTexture( baseTexture ) + vec4( 1.0 ) * getTexture( bloomTexture ) );
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from '../build/three.module.js';
  33. import { GUI } from './jsm/libs/dat.gui.module.js';
  34. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  35. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  36. import { RenderPass } from './jsm/postprocessing/RenderPass.js';
  37. import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
  38. import { UnrealBloomPass } from './jsm/postprocessing/UnrealBloomPass.js';
  39. var scene, camera, controls;
  40. var renderer;
  41. var ENTIRE_SCENE = 0, BLOOM_SCENE = 1;
  42. var bloomLayer = new THREE.Layers();
  43. bloomLayer.set( BLOOM_SCENE );
  44. var params = {
  45. exposure: 1,
  46. bloomStrength: 5,
  47. bloomThreshold: 0,
  48. bloomRadius: 0,
  49. scene: "Scene with Glow"
  50. };
  51. var darkMaterial = new THREE.MeshBasicMaterial( { color: "black" } );
  52. var materials = {};
  53. renderer = new THREE.WebGLRenderer( { antialias: true } );
  54. renderer.setPixelRatio( window.devicePixelRatio );
  55. renderer.setSize( window.innerWidth, window.innerHeight );
  56. renderer.toneMapping = THREE.ReinhardToneMapping;
  57. document.body.appendChild( renderer.domElement );
  58. scene = new THREE.Scene();
  59. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 200 );
  60. camera.position.set( 0, 0, 20 );
  61. camera.lookAt( 0, 0, 0 );
  62. controls = new OrbitControls( camera, renderer.domElement );
  63. controls.maxPolarAngle = Math.PI * 0.5;
  64. controls.minDistance = 1;
  65. controls.maxDistance = 100;
  66. controls.addEventListener( 'change', render );
  67. scene.add( new THREE.AmbientLight( 0x404040 ) );
  68. var renderScene = new RenderPass( scene, camera );
  69. var bloomPass = new UnrealBloomPass( new THREE.Vector2( window.innerWidth, window.innerHeight ), 1.5, 0.4, 0.85 );
  70. bloomPass.threshold = params.bloomThreshold;
  71. bloomPass.strength = params.bloomStrength;
  72. bloomPass.radius = params.bloomRadius;
  73. var bloomComposer = new EffectComposer( renderer );
  74. bloomComposer.renderToScreen = false;
  75. bloomComposer.addPass( renderScene );
  76. bloomComposer.addPass( bloomPass );
  77. var finalPass = new ShaderPass(
  78. new THREE.ShaderMaterial( {
  79. uniforms: {
  80. baseTexture: { value: null },
  81. bloomTexture: { value: bloomComposer.renderTarget2.texture }
  82. },
  83. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  84. fragmentShader: document.getElementById( 'fragmentshader' ).textContent,
  85. defines: {}
  86. } ), "baseTexture"
  87. );
  88. finalPass.needsSwap = true;
  89. var finalComposer = new EffectComposer( renderer );
  90. finalComposer.addPass( renderScene );
  91. finalComposer.addPass( finalPass );
  92. var raycaster = new THREE.Raycaster();
  93. var mouse = new THREE.Vector2();
  94. window.addEventListener( 'click', onDocumentMouseClick, false );
  95. var gui = new GUI();
  96. gui.add( params, 'scene', [ 'Scene with Glow', 'Glow only', 'Scene only' ] ).onChange( function ( value ) {
  97. switch ( value ) {
  98. case 'Scene with Glow':
  99. bloomComposer.renderToScreen = false;
  100. break;
  101. case 'Glow only':
  102. bloomComposer.renderToScreen = true;
  103. break;
  104. case 'Scene only':
  105. // nothing to do
  106. break;
  107. }
  108. render();
  109. } );
  110. var folder = gui.addFolder( 'Bloom Parameters' );
  111. folder.add( params, 'exposure', 0.1, 2 ).onChange( function ( value ) {
  112. renderer.toneMappingExposure = Math.pow( value, 4.0 );
  113. render();
  114. } );
  115. folder.add( params, 'bloomThreshold', 0.0, 1.0 ).onChange( function ( value ) {
  116. bloomPass.threshold = Number( value );
  117. render();
  118. } );
  119. folder.add( params, 'bloomStrength', 0.0, 10.0 ).onChange( function ( value ) {
  120. bloomPass.strength = Number( value );
  121. render();
  122. } );
  123. folder.add( params, 'bloomRadius', 0.0, 1.0 ).step( 0.01 ).onChange( function ( value ) {
  124. bloomPass.radius = Number( value );
  125. render();
  126. } );
  127. setupScene();
  128. function onDocumentMouseClick( event ) {
  129. event.preventDefault();
  130. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  131. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  132. raycaster.setFromCamera( mouse, camera );
  133. var intersects = raycaster.intersectObjects( scene.children );
  134. if ( intersects.length > 0 ) {
  135. var object = intersects[ 0 ].object;
  136. object.layers.toggle( BLOOM_SCENE );
  137. render();
  138. }
  139. }
  140. window.onresize = function () {
  141. var width = window.innerWidth;
  142. var height = window.innerHeight;
  143. camera.aspect = width / height;
  144. camera.updateProjectionMatrix();
  145. renderer.setSize( width, height );
  146. bloomComposer.setSize( width, height );
  147. finalComposer.setSize( width, height );
  148. render();
  149. };
  150. function setupScene() {
  151. scene.traverse( disposeMaterial );
  152. scene.children.length = 0;
  153. var geometry = new THREE.IcosahedronBufferGeometry( 1, 4 );
  154. for ( var i = 0; i < 50; i ++ ) {
  155. var color = new THREE.Color();
  156. color.setHSL( Math.random(), 0.7, Math.random() * 0.2 + 0.05 );
  157. var material = new THREE.MeshBasicMaterial( { color: color } );
  158. var sphere = new THREE.Mesh( geometry, material );
  159. sphere.position.x = Math.random() * 10 - 5;
  160. sphere.position.y = Math.random() * 10 - 5;
  161. sphere.position.z = Math.random() * 10 - 5;
  162. sphere.position.normalize().multiplyScalar( Math.random() * 4.0 + 2.0 );
  163. sphere.scale.setScalar( Math.random() * Math.random() + 0.5 );
  164. scene.add( sphere );
  165. if ( Math.random() < 0.25 ) sphere.layers.enable( BLOOM_SCENE );
  166. }
  167. render();
  168. }
  169. function disposeMaterial( obj ) {
  170. if ( obj.material ) {
  171. obj.material.dispose();
  172. }
  173. }
  174. function render() {
  175. switch ( params.scene ) {
  176. case 'Scene only':
  177. renderer.render( scene, camera );
  178. break;
  179. case 'Glow only':
  180. renderBloom( false );
  181. break;
  182. case 'Scene with Glow':
  183. default:
  184. // render scene with bloom
  185. renderBloom( true );
  186. // render the entire scene, then render bloom scene on top
  187. finalComposer.render();
  188. break;
  189. }
  190. }
  191. function renderBloom( mask ) {
  192. if ( mask === true ) {
  193. scene.traverse( darkenNonBloomed );
  194. bloomComposer.render();
  195. scene.traverse( restoreMaterial );
  196. } else {
  197. camera.layers.set( BLOOM_SCENE );
  198. bloomComposer.render();
  199. camera.layers.set( ENTIRE_SCENE );
  200. }
  201. }
  202. function darkenNonBloomed( obj ) {
  203. if ( obj.isMesh && bloomLayer.test( obj.layers ) === false ) {
  204. materials[ obj.uuid ] = obj.material;
  205. obj.material = darkMaterial;
  206. }
  207. }
  208. function restoreMaterial( obj ) {
  209. if ( materials[ obj.uuid ] ) {
  210. obj.material = materials[ obj.uuid ];
  211. delete materials[ obj.uuid ];
  212. }
  213. }
  214. </script>
  215. </body>
  216. </html>