summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/PostEffect/MobileDOFHelper.cs
blob: 1f1725d0b20010caf4fba6bac7a5d3106f792a06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
using UnityEngine;
using System.Collections.Generic;

using Object = UnityEngine.Object;

namespace FxProNS
{
    [Serializable]
    public class MobileDOFHelperParams
    {
        public Camera EffectCamera;
        public Transform Target;

        [Range(.01f, 1f)]
        public float FocalLengthMultiplier = .33f;



        [Range(.5f, 2f)]
        public float DOFBlurSize = 1f;

        public float NonTargetFocalDist = 1f;
    }
    public class MobileDOFHelper : Singleton<MobileDOFHelper>, IDisposable
    {
        private static Material _mat;

        public static Material Mat
        {
            get
            {
                if (null == _mat)
                {
                    Shader shader = XUtliPoolLib.ShaderManager.singleton.FindShader("MobileDOFPro", "Hidden/MobileDOFPro");
                    _mat = new Material(shader)
                    {
                        hideFlags = HideFlags.HideAndDontSave
                    };
                }

                return _mat;
            }
        }


        private MobileDOFHelperParams _p;

        public void SetParams(MobileDOFHelperParams p)
        {
            _p = p;
        }

        public void Init(bool searchForNonDepthmapAlphaObjects)
        {
            _p.FocalLengthMultiplier = Mathf.Clamp(_p.FocalLengthMultiplier, .01f, .99f);
            if (_p.EffectCamera.depthTextureMode != DepthTextureMode.DepthNormals)
                _p.EffectCamera.depthTextureMode = DepthTextureMode.Depth;

            Mat.EnableKeyword("USE_CAMERA_DEPTH_TEXTURE");
            Mat.DisableKeyword("DONT_USE_CAMERA_DEPTH_TEXTURE");
        }

        private void CalculateAndUpdateFocalDist()
        {
            if (null == _p.EffectCamera)
            {
                Debug.LogError("null == p.camera");
                return;
            }

            float focalDist;

            if (null != _p.Target)
            {
                Vector3 targetPosInViewportSpace = _p.EffectCamera.WorldToViewportPoint(_p.Target.position);
                focalDist = targetPosInViewportSpace.z;
                //		float focalDist = (target.position - transform.position).magnitude / camera.farClipPlane;
            }
            else
            {
                focalDist = _p.NonTargetFocalDist;
                //            Debug.Log("focalDist: " + focalDist);
            }

            focalDist /= _p.EffectCamera.farClipPlane;

            //focalDist *= _p.FocalDistMultiplier ;

            Mat.SetFloat("_FocalDist", focalDist);

            //Make sure that focalLength < focalDist
            Mat.SetFloat("_FocalLength", focalDist * _p.FocalLengthMultiplier);
        }


        public void RenderMobileDOFBlur(RenderTexture src, RenderTexture dest)
        {
            //Graphics.Blit( src, dest );

            //if (null == cocTexture)
            //{
            //    Debug.LogError("null == cocTexture");
            //    return;
            //}

            //Mat.SetTexture("_COCTex", cocTexture);

            //		//Apply separable DOF

            //Mat.SetFloat("", _p.FocalLengthMultiplier);
            //Mat.SetFloat("", _p.FocalDistMultiplier);

            CalculateAndUpdateFocalDist();

            RenderTexture tempRt = RenderTextureManager.Instance.RequestRenderTexture(src.width, src.height, src.depth, src.format);

                Mat.SetVector("_SeparableBlurOffsets", new Vector4(_p.DOFBlurSize, 0f, 0f, 0f));
                Graphics.Blit(src, tempRt, Mat, 0);
                Mat.SetVector("_SeparableBlurOffsets", new Vector4(0f, _p.DOFBlurSize, 0f, 0f));
                Graphics.Blit(tempRt, dest, Mat, 0);

                RenderTextureManager.Instance.ReleaseRenderTexture(tempRt);

        }


        public void SetBlurRadius(int radius)
        {
            Shader.DisableKeyword("BLUR_RADIUS_10");
            Shader.DisableKeyword("BLUR_RADIUS_5");
            Shader.DisableKeyword("BLUR_RADIUS_3");
            Shader.DisableKeyword("BLUR_RADIUS_2");
            Shader.DisableKeyword("BLUR_RADIUS_1");

            if (radius != 10 && radius != 5 && radius != 3 && radius != 2 && radius != 1) radius = 5;

            if (radius < 3) radius = 3;

            //Debug.Log( "blur radius: " + radius );

            Shader.EnableKeyword("BLUR_RADIUS_" + radius);
        }





        public void Dispose()
        {
            if (null != Mat)
                Object.DestroyImmediate(Mat);
            if (_p != null && _p.EffectCamera != null)
                _p.EffectCamera.depthTextureMode = DepthTextureMode.None;
            RenderTextureManager.Instance.Dispose();
        }
    }
}