@@ -13,6 +13,8 @@ static class Uniforms
1313 internal static readonly int _RcpMaxCoC = Shader . PropertyToID ( "_RcpMaxCoC" ) ;
1414 internal static readonly int _RcpAspect = Shader . PropertyToID ( "_RcpAspect" ) ;
1515 internal static readonly int _DejitteredDepth = Shader . PropertyToID ( "_DejitteredDepth" ) ;
16+ internal static readonly int _MainTex = Shader . PropertyToID ( "_MainTex" ) ;
17+ internal static readonly int _HistoryCoC = Shader . PropertyToID ( "_HistoryCoC" ) ;
1618 }
1719
1820 const string k_ShaderString = "Hidden/Post FX/Depth Of Field" ;
@@ -32,6 +34,9 @@ public override DepthTextureMode GetCameraFlags()
3234 return DepthTextureMode . Depth ;
3335 }
3436
37+ RenderTexture m_CoCHistory ;
38+ RenderBuffer [ ] m_MRT = new RenderBuffer [ 2 ] ;
39+
3540 // Height of the 35mm full-frame format (36mm x 24mm)
3641 const float k_FilmHeight = 0.024f ;
3742
@@ -57,7 +62,7 @@ float CalculateMaxCoCRadius(int screenHeight)
5762 return Mathf . Min ( 0.05f , radiusInPixels / screenHeight ) ;
5863 }
5964
60- public void Prepare ( RenderTexture source , Material uberMaterial , RenderTexture dejitteredDepth )
65+ public void Prepare ( RenderTexture source , Material uberMaterial , bool antialiasCoC )
6166 {
6267 var settings = model . settings ;
6368
@@ -80,24 +85,59 @@ public void Prepare(RenderTexture source, Material uberMaterial, RenderTexture d
8085 var rcpAspect = ( float ) source . height / source . width ;
8186 material . SetFloat ( Uniforms . _RcpAspect , rcpAspect ) ;
8287
83- if ( dejitteredDepth != null )
84- {
85- material . SetTexture ( Uniforms . _DejitteredDepth , dejitteredDepth ) ;
86- material . EnableKeyword ( "DEJITTER_DEPTH" ) ;
87- }
88+ var rt1 = context . renderTextureFactory . Get ( context . width / 2 , context . height / 2 , 0 , RenderTextureFormat . ARGBHalf , filterMode : FilterMode . Point ) ;
89+ var rt2 = context . renderTextureFactory . Get ( context . width / 2 , context . height / 2 , 0 , RenderTextureFormat . ARGBHalf , filterMode : FilterMode . Bilinear ) ;
8890
8991 // Pass #1 - Downsampling, prefiltering and CoC calculation
90- var rt1 = context . renderTextureFactory . Get ( context . width / 2 , context . height / 2 , 0 , RenderTextureFormat . ARGBHalf , filterMode : FilterMode . Point ) ;
9192 Graphics . Blit ( source , rt1 , material , 0 ) ;
9293
93- // Pass #2 - Bokeh simulation
94- var rt2 = context . renderTextureFactory . Get ( context . width / 2 , context . height / 2 , 0 , RenderTextureFormat . ARGBHalf , filterMode : FilterMode . Bilinear ) ;
95- Graphics . Blit ( rt1 , rt2 , material , 1 + ( int ) settings . kernelSize ) ;
94+ // Pass #2 - CoC Antialiasing
95+ var pass = rt1 ;
96+ if ( antialiasCoC )
97+ {
98+ pass = context . renderTextureFactory . Get ( context . width / 2 , context . height / 2 , 0 , RenderTextureFormat . ARGBHalf , filterMode : FilterMode . Point ) ;
99+
100+ if ( m_CoCHistory == null || ! m_CoCHistory . IsCreated ( ) || m_CoCHistory . width != context . width / 2 || m_CoCHistory . height != context . height / 2 )
101+ {
102+ m_CoCHistory = RenderTexture . GetTemporary ( context . width / 2 , context . height / 2 , 0 , RenderTextureFormat . RHalf ) ;
103+ m_CoCHistory . filterMode = FilterMode . Point ;
104+ m_CoCHistory . name = "CoC History" ;
105+ Graphics . Blit ( rt1 , m_CoCHistory , material , 6 ) ;
106+ }
107+
108+ var tempCoCHistory = RenderTexture . GetTemporary ( context . width / 2 , context . height / 2 , 0 , RenderTextureFormat . RHalf ) ;
109+ tempCoCHistory . filterMode = FilterMode . Point ;
110+ tempCoCHistory . name = "CoC History" ;
111+
112+ m_MRT [ 0 ] = pass . colorBuffer ;
113+ m_MRT [ 1 ] = tempCoCHistory . colorBuffer ;
114+ material . SetTexture ( Uniforms . _MainTex , rt1 ) ;
115+ material . SetTexture ( Uniforms . _HistoryCoC , m_CoCHistory ) ;
116+ Graphics . SetRenderTarget ( m_MRT , rt1 . depthBuffer ) ;
117+ GraphicsUtils . Blit ( material , 5 ) ;
118+
119+ RenderTexture . ReleaseTemporary ( m_CoCHistory ) ;
120+ m_CoCHistory = tempCoCHistory ;
121+ }
122+
123+ // Pass #3 - Bokeh simulation
124+ Graphics . Blit ( pass , rt2 , material , 1 + ( int ) settings . kernelSize ) ;
96125
97126 context . renderTextureFactory . Release ( rt1 ) ;
98127
128+ if ( antialiasCoC )
129+ context . renderTextureFactory . Release ( pass ) ;
130+
99131 uberMaterial . SetTexture ( Uniforms . _DepthOfFieldTex , rt2 ) ;
100132 uberMaterial . EnableKeyword ( "DEPTH_OF_FIELD" ) ;
101133 }
134+
135+ public override void OnDisable ( )
136+ {
137+ if ( m_CoCHistory != null )
138+ RenderTexture . ReleaseTemporary ( m_CoCHistory ) ;
139+
140+ m_CoCHistory = null ;
141+ }
102142 }
103143}
0 commit comments