bloom_and_glow.adoc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. = Bloom and Glow
  2. :author:
  3. :revnumber:
  4. :revdate: 2016/03/17 20:48
  5. :keywords: documentation, effect, light
  6. :relfileprefix: ../../
  7. :imagesdir: ../..
  8. ifdef::env-github,env-browser[:outfilesuffix: .adoc]
  9. Bloom is a popular shader effect in 3D games industry. It usually consist in displaying a glowing halo around light sources or bright areas of a scene.
  10. In practice, the bright areas are extracted from the rendered scene, blurred and finally added up to the render.
  11. Those images gives an idea of what bloom does. The left image has no bloom effect, the right image does. +
  12. image:jme3/advanced/nobloomsky.png[No bloom,width="",height=""]image:jme3/advanced/blomsky.png[Bloom,width="",height=""]
  13. == Bloom Usage
  14. . Create a FilterPostProcessor
  15. . Create a BloomFilter
  16. . Add the filter to the processor
  17. . Add the processor to the viewPort
  18. [source,java]
  19. ----
  20. FilterPostProcessor fpp=new FilterPostProcessor(assetManager);
  21. BloomFilter bloom=new BloomFilter();
  22. fpp.addFilter(bloom);
  23. viewPort.addProcessor(fpp);
  24. ----
  25. Here are the parameters that you can tweak :
  26. [cols="4", options="header"]
  27. |===
  28. <a| Parameter
  29. <a| Method
  30. a| Default
  31. a| Description
  32. <a| blur scale
  33. a| `setBlurScale(float)`
  34. <a|1.5f
  35. a| the scale of the bloom effect, but be careful, high values does artifacts
  36. <a| exposure Power
  37. a| `setExposurePower(float)`
  38. <a|5.0f
  39. a| the glowing channel color is raised to the value power
  40. <a| exposure cut-off
  41. a| `setExposureCutOff(float)`
  42. <a|0.0f
  43. a| the threshold of color to bloom during extraction
  44. <a| bloom intensity
  45. a| `setBloomIntensity(float)`
  46. <a|2.0f
  47. a| the resulting bloom value is multiplied by this intensity
  48. |===
  49. You'll probably need to adjust those parameters depending on your scene.
  50. == Bloom with a glow map
  51. Sometimes, you want to have more control over what glows and does not glow.
  52. The bloom filter supports a glow map or a glow color.
  53. === Creating a glow-map
  54. Let's take the hover tank example bundled with JME3 test data. +
  55. Here you can see the diffuse map of the tank, and the associated glow map that only contains the parts of the texture that will glow and their glowing color: +
  56. image:jme3/advanced/tank_diffuse_ss.png[Tank diffuse map,width="",height=""]
  57. image:jme3/advanced/tank_glow_map_ss.png[Tank glow map,width="",height=""]
  58. Glow maps work with Lighting.j3md, Particles.j3md and Unshaded.j3md material definitions.
  59. The tank material looks like this :
  60. [source]
  61. ----
  62. Material My Material : Common/MatDefs/Light/Lighting.j3md {
  63. MaterialParameters {
  64. SpecularMap : Models/HoverTank/tank_specular.png
  65. Shininess : 8
  66. NormalMap : Models/HoverTank/tank_normals.png
  67. DiffuseMap : Models/HoverTank/tank_diffuse.png
  68. GlowMap : Models/HoverTank/tank_glow_map_highres.png
  69. UseMaterialColors : true
  70. Ambient : 0.0 0.0 0.0 1.0
  71. Diffuse : 1.0 1.0 1.0 1.0
  72. Specular : 1.0 1.0 1.0 1.0
  73. }
  74. }
  75. ----
  76. The glow map is defined here : *GlowMap : Models/HoverTank/tank_glow_map_highres.png*
  77. === Usage
  78. . Create a FilterPostProcessor
  79. . Create a BloomFilter with the GlowMode.Objects parameter
  80. . Add the filter to the processor
  81. . Add the processor to the viewPort
  82. [source]
  83. ----
  84. FilterPostProcessor fpp=new FilterPostProcessor(assetManager);
  85. BloomFilter bf=new BloomFilter(BloomFilter.GlowMode.Objects);
  86. fpp.addFilter(bf);
  87. viewPort.addProcessor(fpp);
  88. ----
  89. Here is the result : +
  90. image:jme3/advanced/tanlglow1.png[Glowing hover tank,width="",height=""]
  91. == Bloom with a glow color
  92. Sometimes you need an entire object to glow, not just parts of it.
  93. In this case you'll need to use the glow color parameter.
  94. === Usage
  95. . Create a material for your object and set the GlowColor parameter
  96. . Create a FilterPostProcessor
  97. . Create a BloomFilter with the GlowMode.Objects parameter
  98. . Add the filter to the processor
  99. . Add the processor to the viewPort
  100. [source]
  101. ----
  102. Material mat = new Material(getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
  103. mat.setColor("Color", ColorRGBA.Green);
  104. mat.setColor("GlowColor", ColorRGBA.Green);
  105. fpp=new FilterPostProcessor(assetManager);
  106. bloom= new BloomFilter(BloomFilter.GlowMode.Objects);
  107. fpp.addFilter(bloom);
  108. viewPort.addProcessor(fpp);
  109. ----
  110. Here is the result on Oto's plasma ball (before and after) : +
  111. image:jme3/advanced/otonobloom.png[Oto's plasma ball is just a big pea,width="400",height="",align="left"]image:jme3/advanced/otoglow.png[Oto's plasma ball radiates incredible power!!!,width="400",height="",align="left"]
  112. == Hints and tricks
  113. === Increasing the blur range and reducing fps cost
  114. The glow render is sampled on a texture that has the same dimensions as the viewport.
  115. You can reduce the size of the bloom sampling just by using the setDownSamplingFactor method like this : +
  116. [source,java]
  117. ----
  118. BloomFilter bloom=new BloomFilter();
  119. bloom.setDownSamplingFactor(2.0f);
  120. ----
  121. In this example the sampling size is divided by 4 (width/2,height/2), resulting in less work to blur the scene.
  122. The resulting texture is then up sampled to the screen size using hardware bilinear filtering. this results in a wider blur range.
  123. === Using classic bloom combined with a glow map
  124. let's say you want a global bloom on your scene, but you have also a glowing object on it.
  125. You can use only one bloom filter for both effects like that
  126. [source,java]
  127. ----
  128. BloomFilter bloom=new BloomFilter(BloomFilter.GlowMode.SceneAndObjects);
  129. ----
  130. However, note that both effects will share the same values of attribute, and sometimes, it won't be what you need.
  131. === Making your home brewed material definition support Glow
  132. Let's say you have made a custom material on your own, and that you want it to support glow maps and glow color.
  133. In your material definition you need to add those lines in the MaterialParameters section :
  134. [source]
  135. ----
  136. MaterialParameters {
  137. ....
  138. // Texture of the glowing parts of the material
  139. Texture2D GlowMap
  140. // The glow color of the object
  141. Color GlowColor
  142. }
  143. ----
  144. Then add the following technique :
  145. [source]
  146. ----
  147. Technique Glow {
  148. LightMode SinglePass
  149. VertexShader GLSL100: Common/MatDefs/Misc/SimpleTextured.vert
  150. FragmentShader GLSL100: Common/MatDefs/Light/Glow.frag
  151. WorldParameters {
  152. WorldViewProjectionMatrix
  153. }
  154. Defines {
  155. HAS_GLOWMAP : GlowMap
  156. HAS_GLOWCOLOR : GlowColor
  157. }
  158. }
  159. ----
  160. Then you can use this material with the BloomFilter
  161. === Make a glowing object stop to glow
  162. If you are using a glow map, remove the texture from the material.
  163. [source]
  164. ----
  165. material.clearTextureParam("GlowMap");
  166. ----
  167. If you are using a glow color, set it to black
  168. [source]
  169. ----
  170. material.setColor("GlowColor",ColorRGBA.Black);
  171. ----