BloomAndFlares.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. @script ExecuteInEditMode
  2. @script RequireComponent (Camera)
  3. @script AddComponentMenu ("Image Effects/Bloom and Flares")
  4. enum LensflareStyle {
  5. Ghosting = 0,
  6. Hollywood = 1,
  7. Combined = 2,
  8. }
  9. enum TweakMode {
  10. Simple = 0,
  11. Advanced = 1,
  12. }
  13. class BloomAndFlares extends PostEffectsBase
  14. {
  15. public var tweakMode : TweakMode = 1;
  16. public var bloomThisTag : String;
  17. public var sepBlurSpread : float = 1.5;
  18. public var useSrcAlphaAsMask : float = 0.5;
  19. public var bloomIntensity : float = 1.0;
  20. public var bloomThreshhold : float = 0.4;
  21. public var bloomBlurIterations : int = 3;
  22. public var lensflares : boolean = true;
  23. public var hollywoodFlareBlurIterations : int = 4;
  24. public var lensflareMode : LensflareStyle = 0;
  25. public var hollyStretchWidth : float = 2.5;
  26. public var lensflareIntensity : float = 0.75;
  27. public var lensflareThreshhold : float = 0.5;
  28. public var flareColorA : Color = Color(0.4,0.4,0.8,0.75);
  29. public var flareColorB : Color = Color(0.4,0.8,0.8,0.75);
  30. public var flareColorC : Color = Color(0.8,0.4,0.8,0.75);
  31. public var flareColorD : Color = Color(0.8,0.4,0.0,0.75);
  32. public var blurWidth : float = 1.0;
  33. // needed shaders & materials ...
  34. public var addAlphaHackShader : Shader;
  35. private var _alphaAddMaterial : Material;
  36. public var lensFlareShader : Shader;
  37. private var _lensFlareMaterial : Material;
  38. public var vignetteShader : Shader;
  39. private var _vignetteMaterial : Material;
  40. public var separableBlurShader : Shader;
  41. private var _separableBlurMaterial : Material;
  42. public var addBrightStuffOneOneShader: Shader;
  43. private var _addBrightStuffBlendOneOneMaterial : Material;
  44. public var hollywoodFlareBlurShader: Shader;
  45. private var _hollywoodFlareBlurMaterial : Material;
  46. public var hollywoodFlareStretchShader: Shader;
  47. private var _hollywoodFlareStretchMaterial : Material;
  48. public var brightPassFilterShader : Shader;
  49. private var _brightPassFilterMaterial : Material;
  50. function Start ()
  51. {
  52. CreateMaterials ();
  53. CheckSupport(false);
  54. }
  55. // @TODO group shaders into material passes
  56. function CreateMaterials ()
  57. {
  58. _lensFlareMaterial = CheckShaderAndCreateMaterial(lensFlareShader,_lensFlareMaterial);
  59. _vignetteMaterial = CheckShaderAndCreateMaterial(vignetteShader,_vignetteMaterial);
  60. _separableBlurMaterial = CheckShaderAndCreateMaterial(separableBlurShader,_separableBlurMaterial);
  61. _addBrightStuffBlendOneOneMaterial = CheckShaderAndCreateMaterial(addBrightStuffOneOneShader,_addBrightStuffBlendOneOneMaterial);
  62. _hollywoodFlareBlurMaterial = CheckShaderAndCreateMaterial(hollywoodFlareBlurShader,_hollywoodFlareBlurMaterial);
  63. _hollywoodFlareStretchMaterial = CheckShaderAndCreateMaterial(hollywoodFlareStretchShader,_hollywoodFlareStretchMaterial);
  64. _brightPassFilterMaterial = CheckShaderAndCreateMaterial(brightPassFilterShader,_brightPassFilterMaterial);
  65. _alphaAddMaterial = CheckShaderAndCreateMaterial(addAlphaHackShader,_alphaAddMaterial);
  66. }
  67. function OnRenderImage (source : RenderTexture, destination : RenderTexture)
  68. {
  69. CreateMaterials ();
  70. // some objects should ignore the alpha threshhold limit,
  71. // so draw .a = 1 into the color buffer for those ...
  72. //
  73. // the drawing is scheduled here
  74. if(bloomThisTag && bloomThisTag != "Untagged") {
  75. var gos : GameObject[] = GameObject.FindGameObjectsWithTag(bloomThisTag);
  76. for (var go : GameObject in gos) {
  77. if(go.GetComponent(MeshFilter)) {
  78. var mesh : Mesh = (go.GetComponent(MeshFilter) as MeshFilter).sharedMesh;
  79. _alphaAddMaterial.SetPass(0);
  80. Graphics.DrawMeshNow(mesh,go.transform.localToWorldMatrix);
  81. }
  82. }
  83. }
  84. var halfRezColor : RenderTexture = RenderTexture.GetTemporary(source.width / 2.0, source.height / 2.0, 0);
  85. var quarterRezColor : RenderTexture = RenderTexture.GetTemporary(source.width / 4.0, source.height / 4.0, 0);
  86. var secondQuarterRezColor : RenderTexture = RenderTexture.GetTemporary(source.width / 4.0, source.height / 4.0, 0);
  87. var thirdQuarterRezColor : RenderTexture = RenderTexture.GetTemporary(source.width / 4.0, source.height / 4.0, 0);
  88. // at this point, we have massaged the alpha channel enough to start downsampling process for bloom
  89. Graphics.Blit (source, halfRezColor);
  90. Graphics.Blit (halfRezColor, quarterRezColor);
  91. RenderTexture.ReleaseTemporary (halfRezColor);
  92. // cut colors (threshholding)
  93. _brightPassFilterMaterial.SetVector ("threshhold", Vector4 (bloomThreshhold, 1.0/(1.0-bloomThreshhold), 0.0, 0.0));
  94. _brightPassFilterMaterial.SetFloat ("useSrcAlphaAsMask", useSrcAlphaAsMask);
  95. Graphics.Blit (quarterRezColor, secondQuarterRezColor, _brightPassFilterMaterial);
  96. // blurring
  97. if (bloomBlurIterations < 1)
  98. bloomBlurIterations = 1;
  99. Graphics.Blit(secondQuarterRezColor, quarterRezColor);
  100. for (var iter : int = 0; iter < bloomBlurIterations; iter++ ) {
  101. _separableBlurMaterial.SetVector ("offsets", Vector4 (0.0, (sepBlurSpread * 1.0) / quarterRezColor.height, 0.0, 0.0));
  102. Graphics.Blit (quarterRezColor, thirdQuarterRezColor, _separableBlurMaterial);
  103. _separableBlurMaterial.SetVector ("offsets", Vector4 ((sepBlurSpread * 1.0) / quarterRezColor.width, 0.0, 0.0, 0.0));
  104. Graphics.Blit (thirdQuarterRezColor, quarterRezColor, _separableBlurMaterial);
  105. }
  106. Graphics.Blit (source, destination);
  107. if (lensflares)
  108. {
  109. // lens flare fun: cut some additional values
  110. // (yes, they will be cut on top of the already cut bloom values,
  111. // so just optimize away if not really needed)
  112. _brightPassFilterMaterial.SetVector ("threshhold", Vector4 (lensflareThreshhold, 1.0/(1.0-lensflareThreshhold), 0.0, 0.0));
  113. _brightPassFilterMaterial.SetFloat ("useSrcAlphaAsMask", 0.0);
  114. Graphics.Blit (secondQuarterRezColor, thirdQuarterRezColor, _brightPassFilterMaterial);
  115. if(lensflareMode == 0) // ghosting
  116. {
  117. // smooth out a little
  118. _separableBlurMaterial.SetVector ("offsets", Vector4 (0.0, (sepBlurSpread*1.0)/quarterRezColor.height, 0.0, 0.0));
  119. Graphics.Blit (thirdQuarterRezColor, secondQuarterRezColor, _separableBlurMaterial);
  120. _separableBlurMaterial.SetVector ("offsets", Vector4 ((sepBlurSpread*1.0)/quarterRezColor.width, 0.0, 0.0, 0.0));
  121. Graphics.Blit (secondQuarterRezColor, thirdQuarterRezColor, _separableBlurMaterial);
  122. // vignette for lens flares so that we don't notice any hard edges
  123. _vignetteMaterial.SetFloat ("vignetteIntensity", 0.975);
  124. Graphics.Blit (thirdQuarterRezColor, secondQuarterRezColor, _vignetteMaterial);
  125. // generating flares (_lensFlareMaterial has One One Blend)
  126. _lensFlareMaterial.SetVector ("colorA", Vector4(flareColorA.r,flareColorA.g,flareColorA.b,flareColorA.a) * lensflareIntensity);
  127. _lensFlareMaterial.SetVector ("colorB", Vector4(flareColorB.r,flareColorB.g,flareColorB.b,flareColorB.a) * lensflareIntensity);
  128. _lensFlareMaterial.SetVector ("colorC", Vector4(flareColorC.r,flareColorC.g,flareColorC.b,flareColorC.a) * lensflareIntensity);
  129. _lensFlareMaterial.SetVector ("colorD", Vector4(flareColorD.r,flareColorD.g,flareColorD.b,flareColorD.a) * lensflareIntensity);
  130. Graphics.Blit (secondQuarterRezColor, quarterRezColor, _lensFlareMaterial);
  131. }
  132. else
  133. {
  134. _hollywoodFlareBlurMaterial.SetVector ("offsets", Vector4(0.0, (sepBlurSpread * 1.0) / quarterRezColor.height, 0.0, 0.0));
  135. _hollywoodFlareBlurMaterial.SetTexture("_NonBlurredTex", quarterRezColor);
  136. _hollywoodFlareBlurMaterial.SetVector ("tintColor", Vector4(flareColorA.r,flareColorA.g,flareColorA.b,flareColorA.a) * flareColorA.a * lensflareIntensity);
  137. Graphics.Blit (thirdQuarterRezColor, secondQuarterRezColor, _hollywoodFlareBlurMaterial);
  138. _hollywoodFlareStretchMaterial.SetVector ("offsets", Vector4 ((sepBlurSpread * 1.0) / quarterRezColor.width, 0.0, 0.0, 0.0));
  139. _hollywoodFlareStretchMaterial.SetFloat("stretchWidth", hollyStretchWidth);
  140. Graphics.Blit (secondQuarterRezColor, thirdQuarterRezColor, _hollywoodFlareStretchMaterial);
  141. if(lensflareMode == 1) // hollywood flares
  142. {
  143. for (var itera : int = 0; itera < hollywoodFlareBlurIterations; itera++ ) {
  144. _separableBlurMaterial.SetVector ("offsets", Vector4 ((sepBlurSpread * 1.0) / quarterRezColor.width, 0.0, 0.0, 0.0));
  145. Graphics.Blit (thirdQuarterRezColor, secondQuarterRezColor, _separableBlurMaterial);
  146. _separableBlurMaterial.SetVector ("offsets", Vector4 ((sepBlurSpread * 1.0) / quarterRezColor.width, 0.0, 0.0, 0.0));
  147. Graphics.Blit (secondQuarterRezColor, thirdQuarterRezColor, _separableBlurMaterial);
  148. }
  149. _addBrightStuffBlendOneOneMaterial.SetFloat ("intensity", 1.0);
  150. Graphics.Blit (thirdQuarterRezColor, quarterRezColor, _addBrightStuffBlendOneOneMaterial);
  151. }
  152. else // 'both' (@NOTE: is weird, maybe just remove)
  153. {
  154. for (var ix : int = 0; ix < hollywoodFlareBlurIterations; ix++ ) {
  155. _separableBlurMaterial.SetVector ("offsets", Vector4 ((sepBlurSpread * 1.0) / quarterRezColor.width, 0.0, 0.0, 0.0));
  156. Graphics.Blit (thirdQuarterRezColor, secondQuarterRezColor, _separableBlurMaterial);
  157. _separableBlurMaterial.SetVector ("offsets", Vector4 ((sepBlurSpread * 1.0) / quarterRezColor.width, 0.0, 0.0, 0.0));
  158. Graphics.Blit (secondQuarterRezColor, thirdQuarterRezColor, _separableBlurMaterial);
  159. }
  160. // vignette for lens flares
  161. _vignetteMaterial.SetFloat ("vignetteIntensity", 1.0);
  162. Graphics.Blit (thirdQuarterRezColor, secondQuarterRezColor, _vignetteMaterial);
  163. // creating the flares
  164. // _lensFlareMaterial has One One Blend
  165. _lensFlareMaterial.SetVector ("colorA", Vector4(flareColorA.r,flareColorA.g,flareColorA.b,flareColorA.a) * flareColorA.a * lensflareIntensity);
  166. _lensFlareMaterial.SetVector ("colorB", Vector4(flareColorB.r,flareColorB.g,flareColorB.b,flareColorB.a) * flareColorB.a * lensflareIntensity);
  167. _lensFlareMaterial.SetVector ("colorC", Vector4(flareColorC.r,flareColorC.g,flareColorC.b,flareColorC.a) * flareColorC.a * lensflareIntensity);
  168. _lensFlareMaterial.SetVector ("colorD", Vector4(flareColorD.r,flareColorD.g,flareColorD.b,flareColorD.a) * flareColorD.a * lensflareIntensity);
  169. Graphics.Blit (secondQuarterRezColor, thirdQuarterRezColor, _lensFlareMaterial);
  170. _addBrightStuffBlendOneOneMaterial.SetFloat ("intensity", 1.0);
  171. Graphics.Blit (thirdQuarterRezColor, quarterRezColor, _addBrightStuffBlendOneOneMaterial);
  172. }
  173. }
  174. }
  175. _addBrightStuffBlendOneOneMaterial.SetFloat("intensity", bloomIntensity);
  176. Graphics.Blit (quarterRezColor, destination, _addBrightStuffBlendOneOneMaterial);
  177. RenderTexture.ReleaseTemporary (quarterRezColor);
  178. RenderTexture.ReleaseTemporary (secondQuarterRezColor);
  179. RenderTexture.ReleaseTemporary (thirdQuarterRezColor);
  180. }
  181. }