Beispiel #1
0
 public AmplifyGlare()
 {
     this.m_currentGlareIdx        = (int)this.m_currentGlareType;
     this.m_cromaticAberrationGrad = new Gradient();
     GradientColorKey[] colorKeys = new GradientColorKey[] { new GradientColorKey(Color.white, 0f), new GradientColorKey(Color.blue, 0.25f), new GradientColorKey(Color.green, 0.5f), new GradientColorKey(Color.yellow, 0.75f), new GradientColorKey(Color.red, 1f) };
     GradientAlphaKey[] alphaKeys = new GradientAlphaKey[] { new GradientAlphaKey(1f, 0f), new GradientAlphaKey(1f, 0.25f), new GradientAlphaKey(1f, 0.5f), new GradientAlphaKey(1f, 0.75f), new GradientAlphaKey(1f, 1f) };
     this.m_cromaticAberrationGrad.SetKeys(colorKeys, alphaKeys);
     this._rtBuffer           = new RenderTexture[0x10];
     this.m_weigthsMat        = new Matrix4x4[4];
     this.m_offsetsMat        = new Matrix4x4[4];
     this.m_amplifyGlareCache = new AmplifyGlareCache();
     this.m_whiteReference    = new Color(0.63f, 0.63f, 0.63f, 0f);
     this.m_aTanFoV           = Mathf.Atan(0.3926991f);
     this.m_starDefArr        = new StarDefData[] { new StarDefData(StarLibType.Cross, "Cross", 2, 4, 1f, 0.85f, 0f, 0.5f, -1f, 90f), new StarDefData(StarLibType.Cross_Filter, "CrossFilter", 2, 4, 1f, 0.95f, 0f, 0.5f, -1f, 90f), new StarDefData(StarLibType.Snow_Cross, "snowCross", 3, 4, 1f, 0.96f, 0.349f, 0.5f, -1f, -1f), new StarDefData(StarLibType.Vertical, "Vertical", 1, 4, 1f, 0.96f, 0f, 0f, -1f, -1f), new StarDefData(StarLibType.Sunny_Cross, "SunnyCross", 4, 4, 1f, 0.88f, 0f, 0f, 0.95f, 45f) };
     GlareDefData[] dataArray2 = new GlareDefData[9];
     dataArray2[0]      = new GlareDefData(StarLibType.Cross, 0f, 0.5f);
     dataArray2[1]      = new GlareDefData(StarLibType.Cross_Filter, 0.44f, 0.5f);
     dataArray2[2]      = new GlareDefData(StarLibType.Cross_Filter, 1.22f, 1.5f);
     dataArray2[3]      = new GlareDefData(StarLibType.Snow_Cross, 0.17f, 0.5f);
     dataArray2[4]      = new GlareDefData(StarLibType.Snow_Cross, 0.7f, 1.5f);
     dataArray2[5]      = new GlareDefData(StarLibType.Sunny_Cross, 0f, 0.5f);
     dataArray2[6]      = new GlareDefData(StarLibType.Sunny_Cross, 0.79f, 1.5f);
     dataArray2[7]      = new GlareDefData(StarLibType.Vertical, 1.57f, 0.5f);
     dataArray2[8]      = new GlareDefData(StarLibType.Vertical, 0f, 0.5f);
     this.m_glareDefArr = dataArray2;
 }
        public void OnRenderImage(Material material, RenderTexture source, RenderTexture dest, float cameraRot)
        {
            //NEED TO SET DESTINATION RENDER TARGET TO COMPLETELLY BLACK SO WE CAN SUM ALL THE GLARE/STAR PASSES ON IT
            Graphics.Blit(Texture2D.blackTexture, dest);

            if (m_isDirty ||
                m_currentWidth != source.width ||
                m_currentHeight != source.height)
            {
                m_isDirty       = false;
                m_currentWidth  = source.width;
                m_currentHeight = source.height;
            }
            else
            {
                OnRenderFromCache(source, dest, material, m_intensity, cameraRot);
                return;
            }

            GlareDefData glareDef    = null;
            bool         validCustom = false;

            if (m_currentGlareType == GlareLibType.Custom)
            {
                if (m_customGlareDef != null && m_customGlareDef.Length > 0)
                {
                    glareDef    = m_customGlareDef[m_customGlareDefIdx];
                    validCustom = true;
                }
                else
                {
                    glareDef = m_glareDefArr[0];
                }
            }
            else
            {
                glareDef = m_glareDefArr[m_currentGlareIdx];
            }


            m_amplifyGlareCache.GlareDef = glareDef;

            float srcW = source.width;
            float srcH = source.height;

            StarDefData starDef = (validCustom) ? glareDef.CustomStarData : m_starDefArr[( int )glareDef.StarType];


            m_amplifyGlareCache.StarDef = starDef;
            int currPassCount = (m_glareMaxPassCount < starDef.PassCount) ? m_glareMaxPassCount : starDef.PassCount;

            m_amplifyGlareCache.CurrentPassCount = currPassCount;
            float radOffset = glareDef.StarInclination + starDef.Inclination;

            for (int p = 0; p < m_glareMaxPassCount; p++)
            {
                float ratio = ( float )(p + 1) / ( float )m_glareMaxPassCount;

                for (int s = 0; s < MaxLineSamples; s++)
                {
                    Color chromaticAberrColor = _overallTint * Color.Lerp(m_cromaticAberrationGrad.Evaluate(( float )s / ( float )(MaxLineSamples - 1)), m_whiteReference, ratio);
                    m_amplifyGlareCache.CromaticAberrationMat[p, s] = Color.Lerp(m_whiteReference, chromaticAberrColor, glareDef.ChromaticAberration);
                }
            }
            m_amplifyGlareCache.TotalRT = starDef.StarlinesCount * currPassCount;

            for (int i = 0; i < m_amplifyGlareCache.TotalRT; i++)
            {
                _rtBuffer[i] = AmplifyUtils.GetTempRenderTarget(source.width, source.height);
            }

            int rtIdx = 0;

            for (int d = 0; d < starDef.StarlinesCount; d++)
            {
                StarLineData starLine = starDef.StarLinesArr[d];
                float        angle    = radOffset + starLine.Inclination;
                float        sinAngle = Mathf.Sin(angle);
                float        cosAngle = Mathf.Cos(angle);
                Vector2      vtStepUV = new Vector2();
                vtStepUV.x = cosAngle / srcW * (starLine.SampleLength * m_overallStreakScale);
                vtStepUV.y = sinAngle / srcH * (starLine.SampleLength * m_overallStreakScale);

                float attnPowScale = (m_aTanFoV + 0.1f) * (280.0f) / (srcW + srcH) * 1.2f;

                for (int p = 0; p < currPassCount; p++)
                {
                    for (int i = 0; i < MaxLineSamples; i++)
                    {
                        float lum = Mathf.Pow(starLine.Attenuation, attnPowScale * i);

                        m_amplifyGlareCache.Starlines[d].Passes[p].Weights[i] = m_amplifyGlareCache.CromaticAberrationMat[currPassCount - 1 - p, i] * lum * (p + 1.0f) * 0.5f;

                        // OFFSET OF SAMPLING COORDINATE
                        m_amplifyGlareCache.Starlines[d].Passes[p].Offsets[i].x = vtStepUV.x * i;
                        m_amplifyGlareCache.Starlines[d].Passes[p].Offsets[i].y = vtStepUV.y * i;
                        if (Mathf.Abs(m_amplifyGlareCache.Starlines[d].Passes[p].Offsets[i].x) >= 0.9f ||
                            Mathf.Abs(m_amplifyGlareCache.Starlines[d].Passes[p].Offsets[i].y) >= 0.9f)
                        {
                            m_amplifyGlareCache.Starlines[d].Passes[p].Offsets[i].x = 0.0f;
                            m_amplifyGlareCache.Starlines[d].Passes[p].Offsets[i].y = 0.0f;
                            m_amplifyGlareCache.Starlines[d].Passes[p].Weights[i]  *= 0.0f;
                        }
                    }

                    // MIRROR STARLINE
                    for (int i = MaxLineSamples; i < MaxTotalSamples; i++)
                    {
                        m_amplifyGlareCache.Starlines[d].Passes[p].Offsets[i] = -m_amplifyGlareCache.Starlines[d].Passes[p].Offsets[i - MaxLineSamples];
                        m_amplifyGlareCache.Starlines[d].Passes[p].Weights[i] = m_amplifyGlareCache.Starlines[d].Passes[p].Weights[i - MaxLineSamples];
                    }

                    // APPLY SHADER
                    UpdateMatrixesForPass(material, m_amplifyGlareCache.Starlines[d].Passes[p].Offsets, m_amplifyGlareCache.Starlines[d].Passes[p].Weights, m_intensity, starDef.CameraRotInfluence * cameraRot);

                    //CREATED WEIGHTED TEXTURE
                    if (p == 0)
                    {
                        Graphics.Blit(source, _rtBuffer[rtIdx], material, ( int )BloomPasses.AnamorphicGlare);
                    }
                    else
                    {
                        Graphics.Blit(_rtBuffer[rtIdx - 1], _rtBuffer[rtIdx], material, ( int )BloomPasses.AnamorphicGlare);
                    }

                    rtIdx        += 1;
                    vtStepUV     *= m_perPassDisplacement;
                    attnPowScale *= m_perPassDisplacement;
                }
            }

            //ADD TO MAIN RT
            m_amplifyGlareCache.AverageWeight = Vector4.one / starDef.StarlinesCount;
            for (int i = 0; i < starDef.StarlinesCount; i++)
            {
                material.SetVector(AmplifyUtils.AnamorphicGlareWeightsStr[i], m_amplifyGlareCache.AverageWeight);
                int idx = (i + 1) * currPassCount - 1;
                material.SetTexture(AmplifyUtils.AnamorphicRTS[i], _rtBuffer[idx]);
            }

            int passId = ( int )BloomPasses.WeightedAddPS1 + starDef.StarlinesCount - 1;

            dest.DiscardContents();
            Graphics.Blit(_rtBuffer[0], dest, material, passId);

            //RELEASE RT's
            for (rtIdx = 0; rtIdx < _rtBuffer.Length; rtIdx++)
            {
                AmplifyUtils.ReleaseTempRenderTarget(_rtBuffer[rtIdx]);
                _rtBuffer[rtIdx] = null;
            }
        }
Beispiel #3
0
 public unsafe void OnRenderImage(Material material, RenderTexture source, RenderTexture dest, float cameraRot)
 {
     Graphics.Blit(Texture2D.blackTexture, dest);
     if (!this.m_isDirty && ((this.m_currentWidth == source.width) && (this.m_currentHeight == source.height)))
     {
         this.OnRenderFromCache(source, dest, material, this.m_intensity, cameraRot);
     }
     else
     {
         this.m_isDirty       = false;
         this.m_currentWidth  = source.width;
         this.m_currentHeight = source.height;
         GlareDefData data = null;
         bool         flag = false;
         if (this.m_currentGlareType != GlareLibType.Custom)
         {
             data = this.m_glareDefArr[this.m_currentGlareIdx];
         }
         else if ((this.m_customGlareDef == null) || (this.m_customGlareDef.Length <= 0))
         {
             data = this.m_glareDefArr[0];
         }
         else
         {
             data = this.m_customGlareDef[this.m_customGlareDefIdx];
             flag = true;
         }
         this.m_amplifyGlareCache.GlareDef = data;
         float       width  = source.width;
         float       height = source.height;
         StarDefData data2  = !flag ? this.m_starDefArr[(int)data.StarType] : data.CustomStarData;
         this.m_amplifyGlareCache.StarDef = data2;
         int num3 = (this.m_glareMaxPassCount >= data2.PassCount) ? data2.PassCount : this.m_glareMaxPassCount;
         this.m_amplifyGlareCache.CurrentPassCount = num3;
         float num4 = data.StarInclination + data2.Inclination;
         int   num5 = 0;
         while (num5 < this.m_glareMaxPassCount)
         {
             float t    = ((float)(num5 + 1)) / ((float)this.m_glareMaxPassCount);
             int   num7 = 0;
             while (true)
             {
                 if (num7 >= 8)
                 {
                     num5++;
                     break;
                 }
                 Color b = this._overallTint * Color.Lerp(this.m_cromaticAberrationGrad.Evaluate(((float)num7) / 7f), this.m_whiteReference, t);
                 *(this.m_amplifyGlareCache.CromaticAberrationMat[num5, num7]) = Color.Lerp(this.m_whiteReference, b, data.ChromaticAberration);
                 num7++;
             }
         }
         this.m_amplifyGlareCache.TotalRT = data2.StarlinesCount * num3;
         for (int i = 0; i < this.m_amplifyGlareCache.TotalRT; i++)
         {
             this._rtBuffer[i] = AmplifyUtils.GetTempRenderTarget(source.width, source.height);
         }
         int index = 0;
         int num10 = 0;
         while (num10 < data2.StarlinesCount)
         {
             StarLineData data3  = data2.StarLinesArr[num10];
             float        f      = num4 + data3.Inclination;
             float        num12  = Mathf.Sin(f);
             float        num13  = Mathf.Cos(f);
             Vector2      vector = new Vector2 {
                 x = (num13 / width) * (data3.SampleLength * this.m_overallStreakScale),
                 y = (num12 / height) * (data3.SampleLength * this.m_overallStreakScale)
             };
             float num14 = (((this.m_aTanFoV + 0.1f) * 280f) / (width + height)) * 1.2f;
             int   num15 = 0;
             while (true)
             {
                 if (num15 >= num3)
                 {
                     num10++;
                     break;
                 }
                 int num16 = 0;
                 while (true)
                 {
                     if (num16 >= 8)
                     {
                         int num18 = 8;
                         while (true)
                         {
                             if (num18 >= 0x10)
                             {
                                 this.UpdateMatrixesForPass(material, this.m_amplifyGlareCache.Starlines[num10].Passes[num15].Offsets, this.m_amplifyGlareCache.Starlines[num10].Passes[num15].Weights, this.m_intensity, data2.CameraRotInfluence * cameraRot);
                                 if (num15 == 0)
                                 {
                                     Graphics.Blit(source, this._rtBuffer[index], material, 2);
                                 }
                                 else
                                 {
                                     Graphics.Blit(this._rtBuffer[index - 1], this._rtBuffer[index], material, 2);
                                 }
                                 index++;
                                 vector *= this.m_perPassDisplacement;
                                 num14  *= this.m_perPassDisplacement;
                                 num15++;
                                 break;
                             }
                             this.m_amplifyGlareCache.Starlines[num10].Passes[num15].Offsets[num18] = -this.m_amplifyGlareCache.Starlines[num10].Passes[num15].Offsets[num18 - 8];
                             this.m_amplifyGlareCache.Starlines[num10].Passes[num15].Weights[num18] = this.m_amplifyGlareCache.Starlines[num10].Passes[num15].Weights[num18 - 8];
                             num18++;
                         }
                         break;
                     }
                     float num17 = Mathf.Pow(data3.Attenuation, num14 * num16);
                     this.m_amplifyGlareCache.Starlines[num10].Passes[num15].Weights[num16]   = ((this.m_amplifyGlareCache.CromaticAberrationMat[(num3 - 1) - num15, num16] * num17) * (num15 + 1f)) * 0.5f;
                     this.m_amplifyGlareCache.Starlines[num10].Passes[num15].Offsets[num16].x = vector.x * num16;
                     this.m_amplifyGlareCache.Starlines[num10].Passes[num15].Offsets[num16].y = vector.y * num16;
                     if ((Mathf.Abs(this.m_amplifyGlareCache.Starlines[num10].Passes[num15].Offsets[num16].x) >= 0.9f) || (Mathf.Abs(this.m_amplifyGlareCache.Starlines[num10].Passes[num15].Offsets[num16].y) >= 0.9f))
                     {
                         this.m_amplifyGlareCache.Starlines[num10].Passes[num15].Offsets[num16].x = 0f;
                         this.m_amplifyGlareCache.Starlines[num10].Passes[num15].Offsets[num16].y = 0f;
                         Vector4 *vectorPtr1 = &(this.m_amplifyGlareCache.Starlines[num10].Passes[num15].Weights[num16]);
                         vectorPtr1[0] *= 0f;
                     }
                     num16++;
                 }
             }
         }
         this.m_amplifyGlareCache.AverageWeight = Vector4.one / ((float)data2.StarlinesCount);
         for (int j = 0; j < data2.StarlinesCount; j++)
         {
             material.SetVector(AmplifyUtils.AnamorphicGlareWeightsStr[j], this.m_amplifyGlareCache.AverageWeight);
             int num20 = ((j + 1) * num3) - 1;
             material.SetTexture(AmplifyUtils.AnamorphicRTS[j], this._rtBuffer[num20]);
         }
         int pass = (0x13 + data2.StarlinesCount) - 1;
         dest.DiscardContents();
         Graphics.Blit(this._rtBuffer[0], dest, material, pass);
         for (index = 0; index < this._rtBuffer.Length; index++)
         {
             AmplifyUtils.ReleaseTempRenderTarget(this._rtBuffer[index]);
             this._rtBuffer[index] = null;
         }
     }
 }