Exemplo n.º 1
0
        public void Init(PostProcessChain parent, string shader_name)
        {
            if (_inited)
            {
                return;
            }

            _parent = parent;

            _shader = PostProcessChain.GetShader(shader_name);

            if (_shader != null)
            {
                _material = new Material(_shader);
            }

            if (_material == null)
            {
                Common.HobaDebuger.LogErrorFormat("PostProcessItem Init Failed: {0}", shader_name);
            }

            OnInit();

            //enabled = false;

            _inited = true;
        }
Exemplo n.º 2
0
        void Render(RenderTexture src, RenderTexture dest)
        {
            if (_material == null)
            {
                Shader shader = PostProcessChain.GetShader("Hidden/PostProcessing/BloomHD");
                if (shader == null)
                {
                    Graphics.Blit(src, dest);
                    return;
                }
                _material = new Material(shader);
            }

            var threshold = Mathf.GammaToLinearSpace(Threshold);

            _material.SetFloat(_ThresholdId, threshold);

            float SoftKnee = SoftKneeA;
            var   knee     = threshold * SoftKnee + 1e-5f;
            var   curve    = new Vector3(threshold - knee, knee * 2, 0.25f / knee);

            _material.SetVector(_CurveId, curve);

            float sampleRadius = Radius;

            _material.SetFloat(_UpSampleScaleId, sampleRadius);

            _material.SetFloat(_IntensityId, Intensity);

            RenderTextureFormat rtFormat = RenderTextureFormat.DefaultHDR;

            int filterTexWidth  = Screen.width >> _Level;
            int filterTexHeight = Screen.height >> _Level;

            RenderTexture filter_texture = RenderTexture.GetTemporary(filterTexWidth, filterTexHeight, 0, rtFormat);

            filter_texture.name = "filter_texture";
            RenderTexture blend_texture = RenderTexture.GetTemporary(filterTexWidth, filterTexHeight, 0, rtFormat);

            blend_texture.name = "blend_texture";

            // 注意:WithFlicker 字面意思与实际作用正好相反
            Graphics.Blit(src, filter_texture, _material, WithFlicker ? (int)BloomHDPass.Bright : (int)BloomHDPass.BrightWithFlicker);

            int levelWidth  = filterTexWidth >> 1;
            int levelHeight = filterTexHeight >> 1;

            for (int i = 0; i < 16; ++i)
            {
                rtCache[i]  = null;
                rtCache1[i] = null;
            }

            RenderTexture last = filter_texture;

            // down sample
            int it = 0;

            for (; it < _Iteration && levelWidth >= 4 && levelHeight >= 4; ++it)
            {
                rtCache[it]      = RenderTexture.GetTemporary(levelWidth, levelHeight, 0, rtFormat);
                rtCache[it].name = "rtCache";

                // 注意:WithFlicker 字面意思与实际作用正好相反
                Graphics.Blit(last, rtCache[it], _material, WithFlicker ? (int)BloomHDPass.Downsample : (int)BloomHDPass.DownsampleWithFlicker);

                levelWidth  = levelWidth >> 1;
                levelHeight = levelHeight >> 1;

                last = rtCache[it];
            }

            // up sampler
            for (int i = it - 2; i >= 0; i--)
            {
                rtCache1[i]      = RenderTexture.GetTemporary(rtCache[i].width, rtCache[i].height, 0, rtFormat);
                rtCache1[i].name = "rtCache1";

                _material.SetTexture(_BloomTexId, rtCache[i]);

                Graphics.Blit(last, rtCache1[i], _material, (int)BloomHDPass.Upsample);

                last = rtCache1[i];
            }
            _material.SetTexture(_BloomTexId, filter_texture);

            // blend
            RenderTexture rt = rtCache1[0] == null ? last : rtCache1[0];

            Graphics.Blit(rt, blend_texture, _material, (int)BloomHDPass.Blend);

            //_material.
            _material.SetTexture(_BloomTexId, blend_texture);

            Graphics.Blit(src, dest, _material, (int)BloomHDPass.Final);

            RenderTexture.ReleaseTemporary(blend_texture);
            RenderTexture.ReleaseTemporary(filter_texture);

            for (int i = 0; i < 16; ++i)
            {
                if (rtCache[i] != null)
                {
                    RenderTexture.ReleaseTemporary(rtCache[i]);
                }
                if (rtCache1[i] != null)
                {
                    RenderTexture.ReleaseTemporary(rtCache1[i]);
                }
            }
        }