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
|
using System;
namespace UnityEngine.PostProcessing;
public sealed class DepthOfFieldComponent : PostProcessingComponentRenderTexture<DepthOfFieldModel>
{
private static class Uniforms
{
internal static readonly int _DepthOfFieldTex = Shader.PropertyToID("_DepthOfFieldTex");
internal static readonly int _DepthOfFieldCoCTex = Shader.PropertyToID("_DepthOfFieldCoCTex");
internal static readonly int _Distance = Shader.PropertyToID("_Distance");
internal static readonly int _LensCoeff = Shader.PropertyToID("_LensCoeff");
internal static readonly int _MaxCoC = Shader.PropertyToID("_MaxCoC");
internal static readonly int _RcpMaxCoC = Shader.PropertyToID("_RcpMaxCoC");
internal static readonly int _RcpAspect = Shader.PropertyToID("_RcpAspect");
internal static readonly int _MainTex = Shader.PropertyToID("_MainTex");
internal static readonly int _CoCTex = Shader.PropertyToID("_CoCTex");
internal static readonly int _TaaParams = Shader.PropertyToID("_TaaParams");
internal static readonly int _DepthOfFieldParams = Shader.PropertyToID("_DepthOfFieldParams");
}
private const string k_ShaderString = "Hidden/Post FX/Depth Of Field";
private RenderTexture m_CoCHistory;
private const float k_FilmHeight = 0.024f;
public override bool active
{
get
{
if (base.model.enabled)
{
return !context.interrupted;
}
return false;
}
}
public override DepthTextureMode GetCameraFlags()
{
return DepthTextureMode.Depth;
}
private float CalculateFocalLength()
{
DepthOfFieldModel.Settings settings = base.model.settings;
if (!settings.useCameraFov)
{
return settings.focalLength / 1000f;
}
float num = context.camera.fieldOfView * ((float)Math.PI / 180f);
return 0.012f / Mathf.Tan(0.5f * num);
}
private float CalculateMaxCoCRadius(int screenHeight)
{
float num = (float)base.model.settings.kernelSize * 4f + 6f;
return Mathf.Min(0.05f, num / (float)screenHeight);
}
private bool CheckHistory(int width, int height)
{
if (m_CoCHistory != null && m_CoCHistory.IsCreated() && m_CoCHistory.width == width)
{
return m_CoCHistory.height == height;
}
return false;
}
private RenderTextureFormat SelectFormat(RenderTextureFormat primary, RenderTextureFormat secondary)
{
if (SystemInfo.SupportsRenderTextureFormat(primary))
{
return primary;
}
if (SystemInfo.SupportsRenderTextureFormat(secondary))
{
return secondary;
}
return RenderTextureFormat.Default;
}
public void Prepare(RenderTexture source, Material uberMaterial, bool antialiasCoC, Vector2 taaJitter, float taaBlending)
{
DepthOfFieldModel.Settings settings = base.model.settings;
RenderTextureFormat format = RenderTextureFormat.DefaultHDR;
RenderTextureFormat format2 = SelectFormat(RenderTextureFormat.R8, RenderTextureFormat.RHalf);
float num = CalculateFocalLength();
float num2 = Mathf.Max(settings.focusDistance, num);
float num3 = (float)source.width / (float)source.height;
float num4 = num * num / (settings.aperture * (num2 - num) * 0.024f * 2f);
float num5 = CalculateMaxCoCRadius(source.height);
Material material = context.materialFactory.Get("Hidden/Post FX/Depth Of Field");
material.SetFloat(Uniforms._Distance, num2);
material.SetFloat(Uniforms._LensCoeff, num4);
material.SetFloat(Uniforms._MaxCoC, num5);
material.SetFloat(Uniforms._RcpMaxCoC, 1f / num5);
material.SetFloat(Uniforms._RcpAspect, 1f / num3);
RenderTexture renderTexture = context.renderTextureFactory.Get(context.width, context.height, 0, format2, RenderTextureReadWrite.Linear);
Graphics.Blit(null, renderTexture, material, 0);
if (antialiasCoC)
{
material.SetTexture(Uniforms._CoCTex, renderTexture);
float z = (CheckHistory(context.width, context.height) ? taaBlending : 0f);
material.SetVector(Uniforms._TaaParams, new Vector3(taaJitter.x, taaJitter.y, z));
RenderTexture temporary = RenderTexture.GetTemporary(context.width, context.height, 0, format2);
Graphics.Blit(m_CoCHistory, temporary, material, 1);
context.renderTextureFactory.Release(renderTexture);
if (m_CoCHistory != null)
{
RenderTexture.ReleaseTemporary(m_CoCHistory);
}
renderTexture = (m_CoCHistory = temporary);
}
RenderTexture renderTexture2 = context.renderTextureFactory.Get(context.width / 2, context.height / 2, 0, format);
material.SetTexture(Uniforms._CoCTex, renderTexture);
Graphics.Blit(source, renderTexture2, material, 2);
RenderTexture renderTexture3 = context.renderTextureFactory.Get(context.width / 2, context.height / 2, 0, format);
Graphics.Blit(renderTexture2, renderTexture3, material, (int)(3 + settings.kernelSize));
Graphics.Blit(renderTexture3, renderTexture2, material, 7);
uberMaterial.SetVector(Uniforms._DepthOfFieldParams, new Vector3(num2, num4, num5));
if (context.profile.debugViews.IsModeActive(BuiltinDebugViewsModel.Mode.FocusPlane))
{
uberMaterial.EnableKeyword("DEPTH_OF_FIELD_COC_VIEW");
context.Interrupt();
}
else
{
uberMaterial.SetTexture(Uniforms._DepthOfFieldTex, renderTexture2);
uberMaterial.SetTexture(Uniforms._DepthOfFieldCoCTex, renderTexture);
uberMaterial.EnableKeyword("DEPTH_OF_FIELD");
}
context.renderTextureFactory.Release(renderTexture3);
}
public override void OnDisable()
{
if (m_CoCHistory != null)
{
RenderTexture.ReleaseTemporary(m_CoCHistory);
}
m_CoCHistory = null;
}
}
|