7     [RequireComponent (typeof(Camera))]
     8     [AddComponentMenu (
"Image Effects/Bloom and Glow/Bloom")]
    47         private bool doHdr = 
false;
    48         public float sepBlurSpread = 2.5f;
    52         public float bloomIntensity = 0.5f;
    53         public float bloomThreshold = 0.5f;
    54         public Color bloomThresholdColor = Color.white;
    55         public int bloomBlurIterations = 2;
    57         public int hollywoodFlareBlurIterations = 2;
    58         public float flareRotation = 0.0f;
    60         public float hollyStretchWidth = 2.5f;
    61         public float lensflareIntensity = 0.0f;
    62         public float lensflareThreshold = 0.3f;
    63         public float lensFlareSaturation = 0.75f;
    64         public Color flareColorA = 
new Color (0.4f, 0.4f, 0.8f, 0.75f);
    65         public Color flareColorB = 
new Color (0.4f, 0.8f, 0.8f, 0.75f);
    66         public Color flareColorC = 
new Color (0.8f, 0.4f, 0.8f, 0.75f);
    67         public Color flareColorD = 
new Color (0.8f, 0.4f, 0.0f, 0.75f);
    87             screenBlend = CheckShaderAndCreateMaterial (screenBlendShader, screenBlend);
    88             lensFlareMaterial = CheckShaderAndCreateMaterial(lensFlareShader,lensFlareMaterial);
    89             blurAndFlaresMaterial = CheckShaderAndCreateMaterial (blurAndFlaresShader, blurAndFlaresMaterial);
    90             brightPassFilterMaterial = CheckShaderAndCreateMaterial(brightPassFilterShader, brightPassFilterMaterial);
    97         public void OnRenderImage (RenderTexture source, RenderTexture destination)
    99             if (CheckResources()==
false)
   101                 Graphics.Blit (source, destination);
   109                 doHdr = source.format == RenderTextureFormat.ARGBHalf && GetComponent<Camera>().hdr;
   114             doHdr = doHdr && supportHDRTextures;
   120             var rtFormat= (doHdr) ? RenderTextureFormat.ARGBHalf : RenderTextureFormat.Default;
   121             var rtW2= source.width/2;
   122             var rtH2= source.height/2;
   123             var rtW4= source.width/4;
   124             var rtH4= source.height/4;
   126             float widthOverHeight = (1.0f * source.width) / (1.0f * source.height);
   127             float oneOverBaseSize = 1.0f / 512.0f;
   130             RenderTexture quarterRezColor = RenderTexture.GetTemporary (rtW4, rtH4, 0, rtFormat);
   131             RenderTexture halfRezColorDown = RenderTexture.GetTemporary (rtW2, rtH2, 0, rtFormat);
   133                 Graphics.Blit (source, halfRezColorDown, screenBlend, 2);
   134                 RenderTexture rtDown4 = RenderTexture.GetTemporary (rtW4, rtH4, 0, rtFormat);
   135                 Graphics.Blit (halfRezColorDown, rtDown4, screenBlend, 2);
   136                 Graphics.Blit (rtDown4, quarterRezColor, screenBlend, 6);
   137                 RenderTexture.ReleaseTemporary(rtDown4);
   140                 Graphics.Blit (source, halfRezColorDown);
   141                 Graphics.Blit (halfRezColorDown, quarterRezColor, screenBlend, 6);
   143             RenderTexture.ReleaseTemporary (halfRezColorDown);
   146             RenderTexture secondQuarterRezColor = RenderTexture.GetTemporary (rtW4, rtH4, 0, rtFormat);
   147             BrightFilter (bloomThreshold * bloomThresholdColor, quarterRezColor, secondQuarterRezColor);
   151             if (bloomBlurIterations < 1) bloomBlurIterations = 1;
   152             else if (bloomBlurIterations > 10) bloomBlurIterations = 10;
   154             for (
int iter = 0; iter < bloomBlurIterations; iter++)
   156                 float spreadForPass = (1.0f + (iter * 0.25f)) * sepBlurSpread;
   159                 RenderTexture blur4 = RenderTexture.GetTemporary (rtW4, rtH4, 0, rtFormat);
   160                 blurAndFlaresMaterial.SetVector (
"_Offsets", 
new Vector4 (0.0f, spreadForPass * oneOverBaseSize, 0.0f, 0.0f));
   161                 Graphics.Blit (secondQuarterRezColor, blur4, blurAndFlaresMaterial, 4);
   162                 RenderTexture.ReleaseTemporary(secondQuarterRezColor);
   163                 secondQuarterRezColor = blur4;
   166                 blur4 = RenderTexture.GetTemporary (rtW4, rtH4, 0, rtFormat);
   167                 blurAndFlaresMaterial.SetVector (
"_Offsets", 
new Vector4 ((spreadForPass / widthOverHeight) * oneOverBaseSize, 0.0f, 0.0f, 0.0f));
   168                 Graphics.Blit (secondQuarterRezColor, blur4, blurAndFlaresMaterial, 4);
   169                 RenderTexture.ReleaseTemporary (secondQuarterRezColor);
   170                 secondQuarterRezColor = blur4;
   176                         Graphics.SetRenderTarget(quarterRezColor);
   177                         GL.Clear(
false, 
true, Color.black); 
   178                         Graphics.Blit (secondQuarterRezColor, quarterRezColor);
   182                         quarterRezColor.MarkRestoreExpected(); 
   183                         Graphics.Blit (secondQuarterRezColor, quarterRezColor, screenBlend, 10);
   190                 Graphics.SetRenderTarget(secondQuarterRezColor);
   191                 GL.Clear(
false, 
true, Color.black); 
   192                 Graphics.Blit (quarterRezColor, secondQuarterRezColor, screenBlend, 6);
   197             if (lensflareIntensity > Mathf.Epsilon)
   200                 RenderTexture rtFlares4 = RenderTexture.GetTemporary (rtW4, rtH4, 0, rtFormat);
   202                 if (lensflareMode == 0)
   206                     BrightFilter (lensflareThreshold, secondQuarterRezColor, rtFlares4);
   211                         blurAndFlaresMaterial.SetVector (
"_Offsets", 
new Vector4 (0.0f, (1.5f) / (1.0f * quarterRezColor.height), 0.0f, 0.0f));
   212                         Graphics.SetRenderTarget(quarterRezColor);
   213                         GL.Clear(
false, 
true, Color.black); 
   214                         Graphics.Blit (rtFlares4, quarterRezColor, blurAndFlaresMaterial, 4);
   216                         blurAndFlaresMaterial.SetVector (
"_Offsets", 
new Vector4 ((1.5f) / (1.0f * quarterRezColor.width), 0.0f, 0.0f, 0.0f));
   217                         Graphics.SetRenderTarget(rtFlares4);
   218                         GL.Clear(
false, 
true, Color.black); 
   219                         Graphics.Blit (quarterRezColor, rtFlares4, blurAndFlaresMaterial, 4);
   223                     Vignette (0.975f, rtFlares4, rtFlares4);
   224                     BlendFlares (rtFlares4, secondQuarterRezColor);
   232                     float flareXRot = 1.0f * Mathf.Cos(flareRotation);
   233                     float flareyRot = 1.0f * Mathf.Sin(flareRotation);
   235                     float stretchWidth = (hollyStretchWidth * 1.0f / widthOverHeight) * oneOverBaseSize;
   237                     blurAndFlaresMaterial.SetVector (
"_Offsets", 
new Vector4 (flareXRot, flareyRot, 0.0f, 0.0f));
   238                     blurAndFlaresMaterial.SetVector (
"_Threshhold", 
new Vector4 (lensflareThreshold, 1.0f, 0.0f, 0.0f));
   239                     blurAndFlaresMaterial.SetVector (
"_TintColor", 
new Vector4 (flareColorA.r, flareColorA.g, flareColorA.b, flareColorA.a) * flareColorA.a * lensflareIntensity);
   240                     blurAndFlaresMaterial.SetFloat (
"_Saturation", lensFlareSaturation);
   243                     quarterRezColor.DiscardContents();
   244                     Graphics.Blit (rtFlares4, quarterRezColor, blurAndFlaresMaterial, 2);
   246                     rtFlares4.DiscardContents();
   247                     Graphics.Blit (quarterRezColor, rtFlares4, blurAndFlaresMaterial, 3);
   249                     blurAndFlaresMaterial.SetVector (
"_Offsets", 
new Vector4 (flareXRot * stretchWidth, flareyRot * stretchWidth, 0.0f, 0.0f));
   251                     blurAndFlaresMaterial.SetFloat (
"_StretchWidth", hollyStretchWidth);
   252                     quarterRezColor.DiscardContents();
   253                     Graphics.Blit (rtFlares4, quarterRezColor, blurAndFlaresMaterial, 1);
   255                     blurAndFlaresMaterial.SetFloat (
"_StretchWidth", hollyStretchWidth * 2.0f);
   256                     rtFlares4.DiscardContents();
   257                     Graphics.Blit (quarterRezColor, rtFlares4, blurAndFlaresMaterial, 1);
   259                     blurAndFlaresMaterial.SetFloat (
"_StretchWidth", hollyStretchWidth * 4.0f);
   260                     quarterRezColor.DiscardContents();
   261                     Graphics.Blit (rtFlares4, quarterRezColor, blurAndFlaresMaterial, 1);
   264                     for (
int iter = 0; iter < hollywoodFlareBlurIterations; iter++)
   266                         stretchWidth = (hollyStretchWidth * 2.0f / widthOverHeight) * oneOverBaseSize;
   268                         blurAndFlaresMaterial.SetVector (
"_Offsets", 
new Vector4 (stretchWidth * flareXRot, stretchWidth * flareyRot, 0.0f, 0.0f));
   269                         rtFlares4.DiscardContents();
   270                         Graphics.Blit (quarterRezColor, rtFlares4, blurAndFlaresMaterial, 4);
   272                         blurAndFlaresMaterial.SetVector (
"_Offsets", 
new Vector4 (stretchWidth * flareXRot, stretchWidth * flareyRot, 0.0f, 0.0f));
   273                         quarterRezColor.DiscardContents();
   274                         Graphics.Blit (rtFlares4, quarterRezColor, blurAndFlaresMaterial, 4);
   279                         AddTo (1.0f, quarterRezColor, secondQuarterRezColor);
   284                         Vignette (1.0f, quarterRezColor, rtFlares4);
   285                         BlendFlares (rtFlares4, quarterRezColor);
   286                         AddTo (1.0f, quarterRezColor, secondQuarterRezColor);
   289                 RenderTexture.ReleaseTemporary (rtFlares4);
   292             int blendPass = (int) realBlendMode;
   296             screenBlend.SetFloat (
"_Intensity", bloomIntensity);
   297             screenBlend.SetTexture (
"_ColorBuffer", source);
   301                 RenderTexture halfRezColorUp = RenderTexture.GetTemporary (rtW2, rtH2, 0, rtFormat);
   302                 Graphics.Blit (secondQuarterRezColor, halfRezColorUp);
   303                 Graphics.Blit (halfRezColorUp, destination, screenBlend, blendPass);
   304                 RenderTexture.ReleaseTemporary (halfRezColorUp);
   307                 Graphics.Blit (secondQuarterRezColor, destination, screenBlend, blendPass);
   309             RenderTexture.ReleaseTemporary (quarterRezColor);
   310             RenderTexture.ReleaseTemporary (secondQuarterRezColor);
   313         private void AddTo (
float intensity_, RenderTexture from, RenderTexture to)
   315             screenBlend.SetFloat (
"_Intensity", intensity_);
   316             to.MarkRestoreExpected(); 
   317             Graphics.Blit (from, to, screenBlend, 9);
   322             lensFlareMaterial.SetVector (
"colorA", 
new Vector4 (flareColorA.r, flareColorA.g, flareColorA.b, flareColorA.a) * lensflareIntensity);
   323             lensFlareMaterial.SetVector (
"colorB", 
new Vector4 (flareColorB.r, flareColorB.g, flareColorB.b, flareColorB.a) * lensflareIntensity);
   324             lensFlareMaterial.SetVector (
"colorC", 
new Vector4 (flareColorC.r, flareColorC.g, flareColorC.b, flareColorC.a) * lensflareIntensity);
   325             lensFlareMaterial.SetVector (
"colorD", 
new Vector4 (flareColorD.r, flareColorD.g, flareColorD.b, flareColorD.a) * lensflareIntensity);
   326             to.MarkRestoreExpected(); 
   327             Graphics.Blit (from, to, lensFlareMaterial);
   330         private void BrightFilter (
float thresh, RenderTexture from, RenderTexture to)
   332             brightPassFilterMaterial.SetVector (
"_Threshhold", 
new Vector4 (thresh, thresh, thresh, thresh));
   333             Graphics.Blit (from, to, brightPassFilterMaterial, 0);
   336         private void BrightFilter (Color threshColor,  RenderTexture from, RenderTexture to)
   338             brightPassFilterMaterial.SetVector (
"_Threshhold", threshColor);
   339             Graphics.Blit (from, to, brightPassFilterMaterial, 1);
   342         private void Vignette (
float amount, RenderTexture from, RenderTexture to)
   344             if (lensFlareVignetteMask)
   346                 screenBlend.SetTexture (
"_ColorBuffer", lensFlareVignetteMask);
   347                 to.MarkRestoreExpected(); 
   348                 Graphics.Blit (from == to ? null : from, to, screenBlend, from == to ? 7 : 3);
   352                 Graphics.SetRenderTarget (to);
   353                 GL.Clear(
false, 
true, Color.black); 
   354                 Graphics.Blit (from, to);
 
void BrightFilter(Color threshColor, RenderTexture from, RenderTexture to)
 
Shader brightPassFilterShader
 
void BrightFilter(float thresh, RenderTexture from, RenderTexture to)
 
void BlendFlares(RenderTexture from, RenderTexture to)
 
Texture2D lensFlareVignetteMask
 
Shader blurAndFlaresShader
 
Material brightPassFilterMaterial
 
void Vignette(float amount, RenderTexture from, RenderTexture to)
 
Material lensFlareMaterial
 
void OnRenderImage(RenderTexture source, RenderTexture destination)
 
void AddTo(float intensity_, RenderTexture from, RenderTexture to)
 
override bool CheckResources()
 
Material blurAndFlaresMaterial