public BloomAndLensFlares()
 {
     this.screenBlendMode = BloomScreenBlendMode.Add;
     this.hdr = HDRBloomMode.Auto;
     this.sepBlurSpread = 1.5f;
     this.useSrcAlphaAsMask = 0.5f;
     this.bloomIntensity = 1f;
     this.bloomThreshhold = 0.5f;
     this.bloomBlurIterations = 2;
     this.hollywoodFlareBlurIterations = 2;
     this.lensflareMode = LensflareStyle34.Anamorphic;
     this.hollyStretchWidth = 3.5f;
     this.lensflareIntensity = 1f;
     this.lensflareThreshhold = 0.3f;
     this.flareColorA = new Color(0.4f, 0.4f, 0.8f, 0.75f);
     this.flareColorB = new Color(0.4f, 0.8f, 0.8f, 0.75f);
     this.flareColorC = new Color(0.8f, 0.4f, 0.8f, 0.75f);
     this.flareColorD = new Color(0.8f, 0.4f, 0.0f, 0.75f);
     this.blurWidth = 1f;
 }
Example #2
0
        public void OnRenderImage(RenderTexture source, RenderTexture destination)
        {
            if (CheckResources() == false)
            {
                Graphics.Blit(source, destination);
                return;
            }

            // screen blend is not supported when HDR is enabled (will cap values)

            doHdr = false;
            if (hdr == HDRBloomMode.Auto)
            {
                doHdr = source.format == RenderTextureFormat.ARGBHalf && GetComponent <Camera>().hdr;
            }
            else
            {
                doHdr = hdr == HDRBloomMode.On;
            }

            doHdr = doHdr && supportHDRTextures;

            BloomScreenBlendMode realBlendMode = screenBlendMode;

            if (doHdr)
            {
                realBlendMode = BloomScreenBlendMode.Add;
            }

            var rtFormat = (doHdr) ? RenderTextureFormat.ARGBHalf : RenderTextureFormat.Default;
            var rtW2     = source.width / 2;
            var rtH2     = source.height / 2;
            var rtW4     = source.width / 4;
            var rtH4     = source.height / 4;

            float widthOverHeight = (1.0f * source.width) / (1.0f * source.height);
            float oneOverBaseSize = 1.0f / 512.0f;

            // downsample
            RenderTexture quarterRezColor  = RenderTexture.GetTemporary(rtW4, rtH4, 0, rtFormat);
            RenderTexture halfRezColorDown = RenderTexture.GetTemporary(rtW2, rtH2, 0, rtFormat);

            if (quality > BloomQuality.Cheap)
            {
                Graphics.Blit(source, halfRezColorDown, screenBlend, 2);
                RenderTexture rtDown4 = RenderTexture.GetTemporary(rtW4, rtH4, 0, rtFormat);
                Graphics.Blit(halfRezColorDown, rtDown4, screenBlend, 2);
                Graphics.Blit(rtDown4, quarterRezColor, screenBlend, 6);
                RenderTexture.ReleaseTemporary(rtDown4);
            }
            else
            {
                Graphics.Blit(source, halfRezColorDown);
                Graphics.Blit(halfRezColorDown, quarterRezColor, screenBlend, 6);
            }
            RenderTexture.ReleaseTemporary(halfRezColorDown);

            // cut colors (thresholding)
            RenderTexture secondQuarterRezColor = RenderTexture.GetTemporary(rtW4, rtH4, 0, rtFormat);

            BrightFilter(bloomThreshold * bloomThresholdColor, quarterRezColor, secondQuarterRezColor);

            // blurring

            if (bloomBlurIterations < 1)
            {
                bloomBlurIterations = 1;
            }
            else if (bloomBlurIterations > 10)
            {
                bloomBlurIterations = 10;
            }

            for (int iter = 0; iter < bloomBlurIterations; iter++)
            {
                float spreadForPass = (1.0f + (iter * 0.25f)) * sepBlurSpread;

                // vertical blur
                RenderTexture blur4 = RenderTexture.GetTemporary(rtW4, rtH4, 0, rtFormat);
                blurAndFlaresMaterial.SetVector("_Offsets", new Vector4(0.0f, spreadForPass * oneOverBaseSize, 0.0f, 0.0f));
                Graphics.Blit(secondQuarterRezColor, blur4, blurAndFlaresMaterial, 4);
                RenderTexture.ReleaseTemporary(secondQuarterRezColor);
                secondQuarterRezColor = blur4;

                // horizontal blur
                blur4 = RenderTexture.GetTemporary(rtW4, rtH4, 0, rtFormat);
                blurAndFlaresMaterial.SetVector("_Offsets", new Vector4((spreadForPass / widthOverHeight) * oneOverBaseSize, 0.0f, 0.0f, 0.0f));
                Graphics.Blit(secondQuarterRezColor, blur4, blurAndFlaresMaterial, 4);
                RenderTexture.ReleaseTemporary(secondQuarterRezColor);
                secondQuarterRezColor = blur4;

                if (quality > BloomQuality.Cheap)
                {
                    if (iter == 0)
                    {
                        Graphics.SetRenderTarget(quarterRezColor);
                        GL.Clear(false, true, Color.black); // Clear to avoid RT restore
                        Graphics.Blit(secondQuarterRezColor, quarterRezColor);
                    }
                    else
                    {
                        quarterRezColor.MarkRestoreExpected(); // using max blending, RT restore expected
                        Graphics.Blit(secondQuarterRezColor, quarterRezColor, screenBlend, 10);
                    }
                }
            }

            if (quality > BloomQuality.Cheap)
            {
                Graphics.SetRenderTarget(secondQuarterRezColor);
                GL.Clear(false, true, Color.black); // Clear to avoid RT restore
                Graphics.Blit(quarterRezColor, secondQuarterRezColor, screenBlend, 6);
            }

            // lens flares: ghosting, anamorphic or both (ghosted anamorphic flares)

            if (lensflareIntensity > Mathf.Epsilon)
            {
                RenderTexture rtFlares4 = RenderTexture.GetTemporary(rtW4, rtH4, 0, rtFormat);

                if (lensflareMode == 0)
                {
                    // ghosting only

                    BrightFilter(lensflareThreshold, secondQuarterRezColor, rtFlares4);

                    if (quality > BloomQuality.Cheap)
                    {
                        // smooth a little
                        blurAndFlaresMaterial.SetVector("_Offsets", new Vector4(0.0f, (1.5f) / (1.0f * quarterRezColor.height), 0.0f, 0.0f));
                        Graphics.SetRenderTarget(quarterRezColor);
                        GL.Clear(false, true, Color.black); // Clear to avoid RT restore
                        Graphics.Blit(rtFlares4, quarterRezColor, blurAndFlaresMaterial, 4);

                        blurAndFlaresMaterial.SetVector("_Offsets", new Vector4((1.5f) / (1.0f * quarterRezColor.width), 0.0f, 0.0f, 0.0f));
                        Graphics.SetRenderTarget(rtFlares4);
                        GL.Clear(false, true, Color.black); // Clear to avoid RT restore
                        Graphics.Blit(quarterRezColor, rtFlares4, blurAndFlaresMaterial, 4);
                    }

                    // no ugly edges!
                    Vignette(0.975f, rtFlares4, rtFlares4);
                    BlendFlares(rtFlares4, secondQuarterRezColor);
                }
                else
                {
                    //Vignette (0.975ff, rtFlares4, rtFlares4);
                    //DrawBorder(rtFlares4, screenBlend, 8);

                    float flareXRot = 1.0f * Mathf.Cos(flareRotation);
                    float flareyRot = 1.0f * Mathf.Sin(flareRotation);

                    float stretchWidth = (hollyStretchWidth * 1.0f / widthOverHeight) * oneOverBaseSize;

                    blurAndFlaresMaterial.SetVector("_Offsets", new Vector4(flareXRot, flareyRot, 0.0f, 0.0f));
                    blurAndFlaresMaterial.SetVector("_Threshhold", new Vector4(lensflareThreshold, 1.0f, 0.0f, 0.0f));
                    blurAndFlaresMaterial.SetVector("_TintColor", new Vector4(flareColorA.r, flareColorA.g, flareColorA.b, flareColorA.a) * flareColorA.a * lensflareIntensity);
                    blurAndFlaresMaterial.SetFloat("_Saturation", lensFlareSaturation);

                    // "pre and cut"
                    quarterRezColor.DiscardContents();
                    Graphics.Blit(rtFlares4, quarterRezColor, blurAndFlaresMaterial, 2);
                    // "post"
                    rtFlares4.DiscardContents();
                    Graphics.Blit(quarterRezColor, rtFlares4, blurAndFlaresMaterial, 3);

                    blurAndFlaresMaterial.SetVector("_Offsets", new Vector4(flareXRot * stretchWidth, flareyRot * stretchWidth, 0.0f, 0.0f));
                    // stretch 1st
                    blurAndFlaresMaterial.SetFloat("_StretchWidth", hollyStretchWidth);
                    quarterRezColor.DiscardContents();
                    Graphics.Blit(rtFlares4, quarterRezColor, blurAndFlaresMaterial, 1);
                    // stretch 2nd
                    blurAndFlaresMaterial.SetFloat("_StretchWidth", hollyStretchWidth * 2.0f);
                    rtFlares4.DiscardContents();
                    Graphics.Blit(quarterRezColor, rtFlares4, blurAndFlaresMaterial, 1);
                    // stretch 3rd
                    blurAndFlaresMaterial.SetFloat("_StretchWidth", hollyStretchWidth * 4.0f);
                    quarterRezColor.DiscardContents();
                    Graphics.Blit(rtFlares4, quarterRezColor, blurAndFlaresMaterial, 1);

                    // additional blur passes
                    for (int iter = 0; iter < hollywoodFlareBlurIterations; iter++)
                    {
                        stretchWidth = (hollyStretchWidth * 2.0f / widthOverHeight) * oneOverBaseSize;

                        blurAndFlaresMaterial.SetVector("_Offsets", new Vector4(stretchWidth * flareXRot, stretchWidth * flareyRot, 0.0f, 0.0f));
                        rtFlares4.DiscardContents();
                        Graphics.Blit(quarterRezColor, rtFlares4, blurAndFlaresMaterial, 4);

                        blurAndFlaresMaterial.SetVector("_Offsets", new Vector4(stretchWidth * flareXRot, stretchWidth * flareyRot, 0.0f, 0.0f));
                        quarterRezColor.DiscardContents();
                        Graphics.Blit(rtFlares4, quarterRezColor, blurAndFlaresMaterial, 4);
                    }

                    if (lensflareMode == (LensFlareStyle)1)
                    {
                        // anamorphic lens flares
                        AddTo(1.0f, quarterRezColor, secondQuarterRezColor);
                    }
                    else
                    {
                        // "combined" lens flares

                        Vignette(1.0f, quarterRezColor, rtFlares4);
                        BlendFlares(rtFlares4, quarterRezColor);
                        AddTo(1.0f, quarterRezColor, secondQuarterRezColor);
                    }
                }
                RenderTexture.ReleaseTemporary(rtFlares4);
            }

            int blendPass = (int)realBlendMode;

            //if (Mathf.Abs(chromaticBloom) < Mathf.Epsilon)
            //	blendPass += 4;

            screenBlend.SetFloat("_Intensity", bloomIntensity);
            screenBlend.SetTexture("_ColorBuffer", source);

            if (quality > BloomQuality.Cheap)
            {
                RenderTexture halfRezColorUp = RenderTexture.GetTemporary(rtW2, rtH2, 0, rtFormat);
                Graphics.Blit(secondQuarterRezColor, halfRezColorUp);
                Graphics.Blit(halfRezColorUp, destination, screenBlend, blendPass);
                RenderTexture.ReleaseTemporary(halfRezColorUp);
            }
            else
            {
                Graphics.Blit(secondQuarterRezColor, destination, screenBlend, blendPass);
            }

            RenderTexture.ReleaseTemporary(quarterRezColor);
            RenderTexture.ReleaseTemporary(secondQuarterRezColor);
        }
Example #3
0
        void OnRenderImage(RenderTexture source, RenderTexture destination)
        {
            if (CheckResources() == false)
            {
                Graphics.Blit(source, destination);
                return;
            }

            // screen blend is not supported when HDR is enabled (will cap values)

            doHdr = false;
            if (hdr == HDRBloomMode.Auto)
#pragma warning disable CS0618 // Type or member is obsolete
            {
                doHdr = source.format == RenderTextureFormat.ARGBHalf && GetComponent <Camera>().allowHDR;
            }
#pragma warning restore CS0618 // Type or member is obsolete
            else
            {
                doHdr = hdr == HDRBloomMode.On;
            }

            doHdr = doHdr && supportHDRTextures;

            BloomScreenBlendMode realBlendMode = screenBlendMode;
            if (doHdr)
            {
                realBlendMode = BloomScreenBlendMode.Add;
            }

            var           rtFormat              = (doHdr) ? RenderTextureFormat.ARGBHalf : RenderTextureFormat.Default;
            RenderTexture halfRezColor          = RenderTexture.GetTemporary(source.width / 2, source.height / 2, 0, rtFormat);
            RenderTexture quarterRezColor       = RenderTexture.GetTemporary(source.width / 4, source.height / 4, 0, rtFormat);
            RenderTexture secondQuarterRezColor = RenderTexture.GetTemporary(source.width / 4, source.height / 4, 0, rtFormat);
            RenderTexture thirdQuarterRezColor  = RenderTexture.GetTemporary(source.width / 4, source.height / 4, 0, rtFormat);

            float widthOverHeight = (1.0f * source.width) / (1.0f * source.height);
            float oneOverBaseSize = 1.0f / 512.0f;

            // downsample

            Graphics.Blit(source, halfRezColor, screenBlend, 2);          // <- 2 is stable downsample
            Graphics.Blit(halfRezColor, quarterRezColor, screenBlend, 2); // <- 2 is stable downsample

            RenderTexture.ReleaseTemporary(halfRezColor);

            // cut colors (thresholding)

            BrightFilter(bloomThreshold, useSrcAlphaAsMask, quarterRezColor, secondQuarterRezColor);
            quarterRezColor.DiscardContents();

            // blurring

            if (bloomBlurIterations < 1)
            {
                bloomBlurIterations = 1;
            }

            for (int iter = 0; iter < bloomBlurIterations; iter++)
            {
                float spreadForPass = (1.0f + (iter * 0.5f)) * sepBlurSpread;
                separableBlurMaterial.SetVector("offsets", new Vector4(0.0f, spreadForPass * oneOverBaseSize, 0.0f, 0.0f));

                RenderTexture src = iter == 0 ? secondQuarterRezColor : quarterRezColor;
                Graphics.Blit(src, thirdQuarterRezColor, separableBlurMaterial);
                src.DiscardContents();

                separableBlurMaterial.SetVector("offsets", new Vector4((spreadForPass / widthOverHeight) * oneOverBaseSize, 0.0f, 0.0f, 0.0f));
                Graphics.Blit(thirdQuarterRezColor, quarterRezColor, separableBlurMaterial);
                thirdQuarterRezColor.DiscardContents();
            }

            // lens flares: ghosting, anamorphic or a combination

            if (lensflares)
            {
                if (lensflareMode == 0)
                {
                    BrightFilter(lensflareThreshold, 0.0f, quarterRezColor, thirdQuarterRezColor);
                    quarterRezColor.DiscardContents();

                    // smooth a little, this needs to be resolution dependent

                    /*
                     * separableBlurMaterial.SetVector ("offsets", Vector4 (0.0ff, (2.0ff) / (1.0ff * quarterRezColor.height), 0.0ff, 0.0ff));
                     * Graphics.Blit (thirdQuarterRezColor, secondQuarterRezColor, separableBlurMaterial);
                     * separableBlurMaterial.SetVector ("offsets", Vector4 ((2.0ff) / (1.0ff * quarterRezColor.width), 0.0ff, 0.0ff, 0.0ff));
                     * Graphics.Blit (secondQuarterRezColor, thirdQuarterRezColor, separableBlurMaterial);
                     */
                    // no ugly edges!

                    Vignette(0.975f, thirdQuarterRezColor, secondQuarterRezColor);
                    thirdQuarterRezColor.DiscardContents();

                    BlendFlares(secondQuarterRezColor, quarterRezColor);
                    secondQuarterRezColor.DiscardContents();
                }

                // (b) hollywood/anamorphic flares?

                else
                {
                    // thirdQuarter has the brightcut unblurred colors
                    // quarterRezColor is the blurred, brightcut buffer that will end up as bloom

                    hollywoodFlaresMaterial.SetVector("_threshold", new Vector4(lensflareThreshold, 1.0f / (1.0f - lensflareThreshold), 0.0f, 0.0f));
                    hollywoodFlaresMaterial.SetVector("tintColor", new Vector4(flareColorA.r, flareColorA.g, flareColorA.b, flareColorA.a) * flareColorA.a * lensflareIntensity);
                    Graphics.Blit(thirdQuarterRezColor, secondQuarterRezColor, hollywoodFlaresMaterial, 2);
                    thirdQuarterRezColor.DiscardContents();

                    Graphics.Blit(secondQuarterRezColor, thirdQuarterRezColor, hollywoodFlaresMaterial, 3);
                    secondQuarterRezColor.DiscardContents();

                    hollywoodFlaresMaterial.SetVector("offsets", new Vector4((sepBlurSpread * 1.0f / widthOverHeight) * oneOverBaseSize, 0.0f, 0.0f, 0.0f));
                    hollywoodFlaresMaterial.SetFloat("stretchWidth", hollyStretchWidth);
                    Graphics.Blit(thirdQuarterRezColor, secondQuarterRezColor, hollywoodFlaresMaterial, 1);
                    thirdQuarterRezColor.DiscardContents();

                    hollywoodFlaresMaterial.SetFloat("stretchWidth", hollyStretchWidth * 2.0f);
                    Graphics.Blit(secondQuarterRezColor, thirdQuarterRezColor, hollywoodFlaresMaterial, 1);
                    secondQuarterRezColor.DiscardContents();

                    hollywoodFlaresMaterial.SetFloat("stretchWidth", hollyStretchWidth * 4.0f);
                    Graphics.Blit(thirdQuarterRezColor, secondQuarterRezColor, hollywoodFlaresMaterial, 1);
                    thirdQuarterRezColor.DiscardContents();

                    if (lensflareMode == (LensflareStyle34)1)
                    {
                        for (int itera = 0; itera < hollywoodFlareBlurIterations; itera++)
                        {
                            separableBlurMaterial.SetVector("offsets", new Vector4((hollyStretchWidth * 2.0f / widthOverHeight) * oneOverBaseSize, 0.0f, 0.0f, 0.0f));
                            Graphics.Blit(secondQuarterRezColor, thirdQuarterRezColor, separableBlurMaterial);
                            secondQuarterRezColor.DiscardContents();

                            separableBlurMaterial.SetVector("offsets", new Vector4((hollyStretchWidth * 2.0f / widthOverHeight) * oneOverBaseSize, 0.0f, 0.0f, 0.0f));
                            Graphics.Blit(thirdQuarterRezColor, secondQuarterRezColor, separableBlurMaterial);
                            thirdQuarterRezColor.DiscardContents();
                        }

                        AddTo(1.0f, secondQuarterRezColor, quarterRezColor);
                        secondQuarterRezColor.DiscardContents();
                    }
                    else
                    {
                        // (c) combined

                        for (int ix = 0; ix < hollywoodFlareBlurIterations; ix++)
                        {
                            separableBlurMaterial.SetVector("offsets", new Vector4((hollyStretchWidth * 2.0f / widthOverHeight) * oneOverBaseSize, 0.0f, 0.0f, 0.0f));
                            Graphics.Blit(secondQuarterRezColor, thirdQuarterRezColor, separableBlurMaterial);
                            secondQuarterRezColor.DiscardContents();

                            separableBlurMaterial.SetVector("offsets", new Vector4((hollyStretchWidth * 2.0f / widthOverHeight) * oneOverBaseSize, 0.0f, 0.0f, 0.0f));
                            Graphics.Blit(thirdQuarterRezColor, secondQuarterRezColor, separableBlurMaterial);
                            thirdQuarterRezColor.DiscardContents();
                        }

                        Vignette(1.0f, secondQuarterRezColor, thirdQuarterRezColor);
                        secondQuarterRezColor.DiscardContents();

                        BlendFlares(thirdQuarterRezColor, secondQuarterRezColor);
                        thirdQuarterRezColor.DiscardContents();

                        AddTo(1.0f, secondQuarterRezColor, quarterRezColor);
                        secondQuarterRezColor.DiscardContents();
                    }
                }
            }

            // screen blend bloom results to color buffer

            screenBlend.SetFloat("_Intensity", bloomIntensity);
            screenBlend.SetTexture("_ColorBuffer", source);
            Graphics.Blit(quarterRezColor, destination, screenBlend, (int)realBlendMode);

            RenderTexture.ReleaseTemporary(quarterRezColor);
            RenderTexture.ReleaseTemporary(secondQuarterRezColor);
            RenderTexture.ReleaseTemporary(thirdQuarterRezColor);
        }
Example #4
0
 public override void OnRenderImage(RenderTexture source, RenderTexture destination)
 {
     if (!this.CheckResources())
     {
         Graphics.Blit(source, destination);
     }
     else
     {
         this.doHdr = false;
         if (this.hdr == HDRBloomMode.Auto)
         {
             bool arg_46_1;
             if (arg_46_1 = (source.format == RenderTextureFormat.ARGBHalf))
             {
                 arg_46_1 = this.camera.hdr;
             }
             this.doHdr = arg_46_1;
         }
         else
         {
             this.doHdr = (this.hdr == HDRBloomMode.On);
         }
         bool arg_73_1;
         if (arg_73_1 = this.doHdr)
         {
             arg_73_1 = this.supportHDRTextures;
         }
         this.doHdr = arg_73_1;
         BloomScreenBlendMode pass = this.screenBlendMode;
         if (this.doHdr)
         {
             pass = BloomScreenBlendMode.Add;
         }
         RenderTextureFormat format     = (!this.doHdr) ? RenderTextureFormat.Default : RenderTextureFormat.ARGBHalf;
         RenderTexture       temporary  = RenderTexture.GetTemporary(source.width / 2, source.height / 2, 0, format);
         RenderTexture       temporary2 = RenderTexture.GetTemporary(source.width / 4, source.height / 4, 0, format);
         RenderTexture       temporary3 = RenderTexture.GetTemporary(source.width / 4, source.height / 4, 0, format);
         RenderTexture       temporary4 = RenderTexture.GetTemporary(source.width / 4, source.height / 4, 0, format);
         float num  = 1f * (float)source.width / (1f * (float)source.height);
         float num2 = 0.001953125f;
         Graphics.Blit(source, temporary, this.screenBlend, 2);
         Graphics.Blit(temporary, temporary2, this.screenBlend, 2);
         RenderTexture.ReleaseTemporary(temporary);
         this.BrightFilter(this.bloomThreshhold, this.useSrcAlphaAsMask, temporary2, temporary3);
         temporary2.DiscardContents();
         if (this.bloomBlurIterations < 1)
         {
             this.bloomBlurIterations = 1;
         }
         for (int i = 0; i < this.bloomBlurIterations; i++)
         {
             float num3 = (1f + (float)i * 0.5f) * this.sepBlurSpread;
             this.separableBlurMaterial.SetVector("offsets", new Vector4((float)0, num3 * num2, (float)0, (float)0));
             RenderTexture renderTexture = (i != 0) ? temporary2 : temporary3;
             Graphics.Blit(renderTexture, temporary4, this.separableBlurMaterial);
             renderTexture.DiscardContents();
             this.separableBlurMaterial.SetVector("offsets", new Vector4(num3 / num * num2, (float)0, (float)0, (float)0));
             Graphics.Blit(temporary4, temporary2, this.separableBlurMaterial);
             temporary4.DiscardContents();
         }
         if (this.lensflares)
         {
             if (this.lensflareMode == LensflareStyle34.Ghosting)
             {
                 this.BrightFilter(this.lensflareThreshhold, (float)0, temporary2, temporary4);
                 temporary2.DiscardContents();
                 this.Vignette(0.975f, temporary4, temporary3);
                 temporary4.DiscardContents();
                 this.BlendFlares(temporary3, temporary2);
                 temporary3.DiscardContents();
             }
             else
             {
                 this.hollywoodFlaresMaterial.SetVector("_Threshhold", new Vector4(this.lensflareThreshhold, 1f / (1f - this.lensflareThreshhold), (float)0, (float)0));
                 this.hollywoodFlaresMaterial.SetVector("tintColor", new Vector4(this.flareColorA.r, this.flareColorA.g, this.flareColorA.b, this.flareColorA.a) * this.flareColorA.a * this.lensflareIntensity);
                 Graphics.Blit(temporary4, temporary3, this.hollywoodFlaresMaterial, 2);
                 temporary4.DiscardContents();
                 Graphics.Blit(temporary3, temporary4, this.hollywoodFlaresMaterial, 3);
                 temporary3.DiscardContents();
                 this.hollywoodFlaresMaterial.SetVector("offsets", new Vector4(this.sepBlurSpread * 1f / num * num2, (float)0, (float)0, (float)0));
                 this.hollywoodFlaresMaterial.SetFloat("stretchWidth", this.hollyStretchWidth);
                 Graphics.Blit(temporary4, temporary3, this.hollywoodFlaresMaterial, 1);
                 temporary4.DiscardContents();
                 this.hollywoodFlaresMaterial.SetFloat("stretchWidth", this.hollyStretchWidth * 2f);
                 Graphics.Blit(temporary3, temporary4, this.hollywoodFlaresMaterial, 1);
                 temporary3.DiscardContents();
                 this.hollywoodFlaresMaterial.SetFloat("stretchWidth", this.hollyStretchWidth * 4f);
                 Graphics.Blit(temporary4, temporary3, this.hollywoodFlaresMaterial, 1);
                 temporary4.DiscardContents();
                 if (this.lensflareMode == LensflareStyle34.Anamorphic)
                 {
                     for (int j = 0; j < this.hollywoodFlareBlurIterations; j++)
                     {
                         this.separableBlurMaterial.SetVector("offsets", new Vector4(this.hollyStretchWidth * 2f / num * num2, (float)0, (float)0, (float)0));
                         Graphics.Blit(temporary3, temporary4, this.separableBlurMaterial);
                         temporary3.DiscardContents();
                         this.separableBlurMaterial.SetVector("offsets", new Vector4(this.hollyStretchWidth * 2f / num * num2, (float)0, (float)0, (float)0));
                         Graphics.Blit(temporary4, temporary3, this.separableBlurMaterial);
                         temporary4.DiscardContents();
                     }
                     this.AddTo(1f, temporary3, temporary2);
                     temporary3.DiscardContents();
                 }
                 else
                 {
                     for (int k = 0; k < this.hollywoodFlareBlurIterations; k++)
                     {
                         this.separableBlurMaterial.SetVector("offsets", new Vector4(this.hollyStretchWidth * 2f / num * num2, (float)0, (float)0, (float)0));
                         Graphics.Blit(temporary3, temporary4, this.separableBlurMaterial);
                         temporary3.DiscardContents();
                         this.separableBlurMaterial.SetVector("offsets", new Vector4(this.hollyStretchWidth * 2f / num * num2, (float)0, (float)0, (float)0));
                         Graphics.Blit(temporary4, temporary3, this.separableBlurMaterial);
                         temporary4.DiscardContents();
                     }
                     this.Vignette(1f, temporary3, temporary4);
                     temporary3.DiscardContents();
                     this.BlendFlares(temporary4, temporary3);
                     temporary4.DiscardContents();
                     this.AddTo(1f, temporary3, temporary2);
                     temporary3.DiscardContents();
                 }
             }
         }
         this.screenBlend.SetFloat("_Intensity", this.bloomIntensity);
         this.screenBlend.SetTexture("_ColorBuffer", source);
         Graphics.Blit(temporary2, destination, this.screenBlend, (int)pass);
         RenderTexture.ReleaseTemporary(temporary2);
         RenderTexture.ReleaseTemporary(temporary3);
         RenderTexture.ReleaseTemporary(temporary4);
     }
 }
Example #5
0
        private void OnRenderImage(RenderTexture source, RenderTexture destination)
        {
            if (CheckResources() == false)
            {
                Graphics.Blit(source, destination);
                return;
            }

            // screen blend is not supported when HDR is enabled (will cap values)

            doHdr = false;
            if (hdr == HDRBloomMode.Auto)
            {
                doHdr = source.format == RenderTextureFormat.ARGBHalf && GetComponent <Camera>().hdr;
            }
            else
            {
                doHdr = hdr == HDRBloomMode.On;
            }

            doHdr = doHdr && supportHDRTextures;

            BloomScreenBlendMode realBlendMode = screenBlendMode;

            if (doHdr)
            {
                realBlendMode = BloomScreenBlendMode.Add;
            }

            var           rtFormat              = (doHdr) ? RenderTextureFormat.ARGBHalf : RenderTextureFormat.Default;
            RenderTexture halfRezColor          = RenderTexture.GetTemporary(source.width / 2, source.height / 2, 0, rtFormat);
            RenderTexture quarterRezColor       = RenderTexture.GetTemporary(source.width / 4, source.height / 4, 0, rtFormat);
            RenderTexture secondQuarterRezColor = RenderTexture.GetTemporary(source.width / 4, source.height / 4, 0,
                                                                             rtFormat);
            RenderTexture thirdQuarterRezColor = RenderTexture.GetTemporary(source.width / 4, source.height / 4, 0, rtFormat);

            float widthOverHeight = (1.0f * source.width) / (1.0f * source.height);
            float oneOverBaseSize = 1.0f / 512.0f;

            // downsample

            if (quality > BloomQuality.Cheap)
            {
                Graphics.Blit(source, halfRezColor, screenBlend, 2);
                Graphics.Blit(halfRezColor, secondQuarterRezColor, screenBlend, 2);
                Graphics.Blit(secondQuarterRezColor, quarterRezColor, screenBlend, 6);
            }
            else
            {
                Graphics.Blit(source, halfRezColor);
                Graphics.Blit(halfRezColor, quarterRezColor, screenBlend, 6);
            }

            // cut colors (threshholding)

            BrightFilter(bloomThreshhold * bloomThreshholdColor, quarterRezColor, secondQuarterRezColor);

            // blurring

            if (bloomBlurIterations < 1)
            {
                bloomBlurIterations = 1;
            }
            else if (bloomBlurIterations > 10)
            {
                bloomBlurIterations = 10;
            }

            for (int iter = 0; iter < bloomBlurIterations; iter++)
            {
                float spreadForPass = (1.0f + (iter * 0.25f)) * sepBlurSpread;

                blurAndFlaresMaterial.SetVector("_Offsets", new Vector4(0.0f, spreadForPass * oneOverBaseSize, 0.0f, 0.0f));
                Graphics.Blit(secondQuarterRezColor, thirdQuarterRezColor, blurAndFlaresMaterial, 4);

                if (quality > BloomQuality.Cheap)
                {
                    blurAndFlaresMaterial.SetVector("_Offsets",
                                                    new Vector4((spreadForPass / widthOverHeight) * oneOverBaseSize, 0.0f,
                                                                0.0f, 0.0f));
                    Graphics.Blit(thirdQuarterRezColor, secondQuarterRezColor, blurAndFlaresMaterial, 4);

                    if (iter == 0)
                    {
                        Graphics.Blit(secondQuarterRezColor, quarterRezColor);
                    }
                    else
                    {
                        Graphics.Blit(secondQuarterRezColor, quarterRezColor, screenBlend, 10);
                    }
                }
                else
                {
                    blurAndFlaresMaterial.SetVector("_Offsets",
                                                    new Vector4((spreadForPass / widthOverHeight) * oneOverBaseSize, 0.0f,
                                                                0.0f, 0.0f));
                    Graphics.Blit(thirdQuarterRezColor, secondQuarterRezColor, blurAndFlaresMaterial, 4);
                }
            }

            if (quality > BloomQuality.Cheap)
            {
                Graphics.Blit(quarterRezColor, secondQuarterRezColor, screenBlend, 6);
            }

            // lens flares: ghosting, anamorphic or both (ghosted anamorphic flares)

            if (lensflareIntensity > Mathf.Epsilon)
            {
                if (lensflareMode == 0)
                {
                    BrightFilter(lensflareThreshhold, secondQuarterRezColor, thirdQuarterRezColor);

                    if (quality > BloomQuality.Cheap)
                    {
                        // smooth a little
                        blurAndFlaresMaterial.SetVector("_Offsets",
                                                        new Vector4(0.0f, (1.5f) / (1.0f * quarterRezColor.height), 0.0f,
                                                                    0.0f));
                        Graphics.Blit(thirdQuarterRezColor, quarterRezColor, blurAndFlaresMaterial, 4);
                        blurAndFlaresMaterial.SetVector("_Offsets",
                                                        new Vector4((1.5f) / (1.0f * quarterRezColor.width), 0.0f, 0.0f,
                                                                    0.0f));
                        Graphics.Blit(quarterRezColor, thirdQuarterRezColor, blurAndFlaresMaterial, 4);
                    }

                    // no ugly edges!
                    Vignette(0.975f, thirdQuarterRezColor, thirdQuarterRezColor);
                    BlendFlares(thirdQuarterRezColor, secondQuarterRezColor);
                }
                else
                {
                    //Vignette (0.975f, thirdQuarterRezColor, thirdQuarterRezColor);
                    //DrawBorder(thirdQuarterRezColor, screenBlend, 8);

                    float flareXRot = 1.0f * Mathf.Cos(flareRotation);
                    float flareyRot = 1.0f * Mathf.Sin(flareRotation);

                    float stretchWidth = (hollyStretchWidth * 1.0f / widthOverHeight) * oneOverBaseSize;

                    blurAndFlaresMaterial.SetVector("_Offsets", new Vector4(flareXRot, flareyRot, 0.0f, 0.0f));
                    blurAndFlaresMaterial.SetVector("_Threshhold", new Vector4(lensflareThreshhold, 1.0f, 0.0f, 0.0f));
                    blurAndFlaresMaterial.SetVector("_TintColor",
                                                    new Vector4(flareColorA.r, flareColorA.g, flareColorA.b,
                                                                flareColorA.a) * flareColorA.a * lensflareIntensity);
                    blurAndFlaresMaterial.SetFloat("_Saturation", lensFlareSaturation);

                    Graphics.Blit(thirdQuarterRezColor, quarterRezColor, blurAndFlaresMaterial, 2);
                    Graphics.Blit(quarterRezColor, thirdQuarterRezColor, blurAndFlaresMaterial, 3);

                    blurAndFlaresMaterial.SetVector("_Offsets",
                                                    new Vector4(flareXRot * stretchWidth, flareyRot * stretchWidth, 0.0f,
                                                                0.0f));
                    blurAndFlaresMaterial.SetFloat("_StretchWidth", hollyStretchWidth);

                    Graphics.Blit(thirdQuarterRezColor, quarterRezColor, blurAndFlaresMaterial, 1);
                    blurAndFlaresMaterial.SetFloat("_StretchWidth", hollyStretchWidth * 2.0f);
                    Graphics.Blit(quarterRezColor, thirdQuarterRezColor, blurAndFlaresMaterial, 1);
                    blurAndFlaresMaterial.SetFloat("_StretchWidth", hollyStretchWidth * 4.0f);
                    Graphics.Blit(thirdQuarterRezColor, quarterRezColor, blurAndFlaresMaterial, 1);

                    for (int iter = 0; iter < hollywoodFlareBlurIterations; iter++)
                    {
                        stretchWidth = (hollyStretchWidth * 2.0f / widthOverHeight) * oneOverBaseSize;
                        blurAndFlaresMaterial.SetVector("_Offsets",
                                                        new Vector4(stretchWidth * flareXRot, stretchWidth * flareyRot, 0.0f,
                                                                    0.0f));
                        Graphics.Blit(quarterRezColor, thirdQuarterRezColor, blurAndFlaresMaterial, 4);
                        blurAndFlaresMaterial.SetVector("_Offsets",
                                                        new Vector4(stretchWidth * flareXRot, stretchWidth * flareyRot, 0.0f,
                                                                    0.0f));
                        Graphics.Blit(thirdQuarterRezColor, quarterRezColor, blurAndFlaresMaterial, 4);
                    }

                    if (lensflareMode == (LensFlareStyle)1)
                    {
                        AddTo(1.0f, quarterRezColor, secondQuarterRezColor);
                    }
                    else
                    {
                        // "combined" lens flares

                        Vignette(1.0f, quarterRezColor, thirdQuarterRezColor);
                        BlendFlares(thirdQuarterRezColor, quarterRezColor);
                        AddTo(1.0f, quarterRezColor, secondQuarterRezColor);
                    }
                }
            }

            int blendPass = (int)realBlendMode;

            //if(Mathf.Abs(chromaticBloom) < Mathf.Epsilon)
            //	blendPass += 4;

            screenBlend.SetFloat("_Intensity", bloomIntensity);
            screenBlend.SetTexture("_ColorBuffer", source);

            if (quality > BloomQuality.Cheap)
            {
                Graphics.Blit(secondQuarterRezColor, halfRezColor);
                Graphics.Blit(halfRezColor, destination, screenBlend, blendPass);
            }
            else
            {
                Graphics.Blit(secondQuarterRezColor, destination, screenBlend, blendPass);
            }

            RenderTexture.ReleaseTemporary(halfRezColor);
            RenderTexture.ReleaseTemporary(quarterRezColor);
            RenderTexture.ReleaseTemporary(secondQuarterRezColor);
            RenderTexture.ReleaseTemporary(thirdQuarterRezColor);
        }
Example #6
0
    public void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        if (!CheckResources())
        {
            Graphics.Blit(source, destination);
            return;
        }
        doHdr = false;
        if (hdr == HDRBloomMode.Auto)
        {
            int num = (source.format == RenderTextureFormat.ARGBHalf) ? 1 : 0;
            if (num != 0)
            {
                num = (GetComponent <Camera>().hdr ? 1 : 0);
            }
            doHdr = ((byte)num != 0);
        }
        else
        {
            doHdr = (hdr == HDRBloomMode.On);
        }
        int supportHDRTextures = doHdr ? 1 : 0;

        if (supportHDRTextures != 0)
        {
            supportHDRTextures = (base.supportHDRTextures ? 1 : 0);
        }
        doHdr = ((byte)supportHDRTextures != 0);
        BloomScreenBlendMode pass = screenBlendMode;

        if (doHdr)
        {
            pass = BloomScreenBlendMode.Add;
        }
        RenderTextureFormat format     = (!doHdr) ? RenderTextureFormat.Default : RenderTextureFormat.ARGBHalf;
        RenderTexture       temporary  = RenderTexture.GetTemporary(source.width / 2, source.height / 2, 0, format);
        RenderTexture       temporary2 = RenderTexture.GetTemporary(source.width / 4, source.height / 4, 0, format);
        RenderTexture       temporary3 = RenderTexture.GetTemporary(source.width / 4, source.height / 4, 0, format);
        RenderTexture       temporary4 = RenderTexture.GetTemporary(source.width / 4, source.height / 4, 0, format);
        float num2 = 1f * (float)source.width / (1f * (float)source.height);
        float num3 = 0.001953125f;

        Graphics.Blit(source, temporary, screenBlend, 2);
        Graphics.Blit(temporary, temporary2, screenBlend, 2);
        RenderTexture.ReleaseTemporary(temporary);
        BrightFilter(bloomThreshhold, useSrcAlphaAsMask, temporary2, temporary3);
        temporary2.DiscardContents();
        if (bloomBlurIterations < 1)
        {
            bloomBlurIterations = 1;
        }
        for (int i = 0; i < bloomBlurIterations; i++)
        {
            float num4 = (1f + (float)i * 0.5f) * sepBlurSpread;
            separableBlurMaterial.SetVector("offsets", new Vector4(0f, num4 * num3, 0f, 0f));
            RenderTexture renderTexture = (i != 0) ? temporary2 : temporary3;
            Graphics.Blit(renderTexture, temporary4, separableBlurMaterial);
            renderTexture.DiscardContents();
            separableBlurMaterial.SetVector("offsets", new Vector4(num4 / num2 * num3, 0f, 0f, 0f));
            Graphics.Blit(temporary4, temporary2, separableBlurMaterial);
            temporary4.DiscardContents();
        }
        if (lensflares)
        {
            if (lensflareMode == LensflareStyle34.Ghosting)
            {
                BrightFilter(lensflareThreshhold, 0f, temporary2, temporary4);
                temporary2.DiscardContents();
                Vignette(0.975f, temporary4, temporary3);
                temporary4.DiscardContents();
                BlendFlares(temporary3, temporary2);
                temporary3.DiscardContents();
            }
            else
            {
                hollywoodFlaresMaterial.SetVector("_Threshhold", new Vector4(lensflareThreshhold, 1f / (1f - lensflareThreshhold), 0f, 0f));
                hollywoodFlaresMaterial.SetVector("tintColor", new Vector4(flareColorA.r, flareColorA.g, flareColorA.b, flareColorA.a) * flareColorA.a * lensflareIntensity);
                Graphics.Blit(temporary4, temporary3, hollywoodFlaresMaterial, 2);
                temporary4.DiscardContents();
                Graphics.Blit(temporary3, temporary4, hollywoodFlaresMaterial, 3);
                temporary3.DiscardContents();
                hollywoodFlaresMaterial.SetVector("offsets", new Vector4(sepBlurSpread * 1f / num2 * num3, 0f, 0f, 0f));
                hollywoodFlaresMaterial.SetFloat("stretchWidth", hollyStretchWidth);
                Graphics.Blit(temporary4, temporary3, hollywoodFlaresMaterial, 1);
                temporary4.DiscardContents();
                hollywoodFlaresMaterial.SetFloat("stretchWidth", hollyStretchWidth * 2f);
                Graphics.Blit(temporary3, temporary4, hollywoodFlaresMaterial, 1);
                temporary3.DiscardContents();
                hollywoodFlaresMaterial.SetFloat("stretchWidth", hollyStretchWidth * 4f);
                Graphics.Blit(temporary4, temporary3, hollywoodFlaresMaterial, 1);
                temporary4.DiscardContents();
                if (lensflareMode == LensflareStyle34.Anamorphic)
                {
                    for (int j = 0; j < hollywoodFlareBlurIterations; j++)
                    {
                        separableBlurMaterial.SetVector("offsets", new Vector4(hollyStretchWidth * 2f / num2 * num3, 0f, 0f, 0f));
                        Graphics.Blit(temporary3, temporary4, separableBlurMaterial);
                        temporary3.DiscardContents();
                        separableBlurMaterial.SetVector("offsets", new Vector4(hollyStretchWidth * 2f / num2 * num3, 0f, 0f, 0f));
                        Graphics.Blit(temporary4, temporary3, separableBlurMaterial);
                        temporary4.DiscardContents();
                    }
                    AddTo(1f, temporary3, temporary2);
                    temporary3.DiscardContents();
                }
                else
                {
                    for (int k = 0; k < hollywoodFlareBlurIterations; k++)
                    {
                        separableBlurMaterial.SetVector("offsets", new Vector4(hollyStretchWidth * 2f / num2 * num3, 0f, 0f, 0f));
                        Graphics.Blit(temporary3, temporary4, separableBlurMaterial);
                        temporary3.DiscardContents();
                        separableBlurMaterial.SetVector("offsets", new Vector4(hollyStretchWidth * 2f / num2 * num3, 0f, 0f, 0f));
                        Graphics.Blit(temporary4, temporary3, separableBlurMaterial);
                        temporary4.DiscardContents();
                    }
                    Vignette(1f, temporary3, temporary4);
                    temporary3.DiscardContents();
                    BlendFlares(temporary4, temporary3);
                    temporary4.DiscardContents();
                    AddTo(1f, temporary3, temporary2);
                    temporary3.DiscardContents();
                }
            }
        }
        screenBlend.SetFloat("_Intensity", bloomIntensity);
        screenBlend.SetTexture("_ColorBuffer", source);
        Graphics.Blit(temporary2, destination, screenBlend, (int)pass);
        RenderTexture.ReleaseTemporary(temporary2);
        RenderTexture.ReleaseTemporary(temporary3);
        RenderTexture.ReleaseTemporary(temporary4);
    }