DepthOfFieldScatter.shader 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2. Shader "Hidden/Dof/DepthOfFieldHdr" {
  3. Properties {
  4. _MainTex ("-", 2D) = "black" {}
  5. _FgOverlap ("-", 2D) = "black" {}
  6. _LowRez ("-", 2D) = "black" {}
  7. }
  8. CGINCLUDE
  9. #include "UnityCG.cginc"
  10. struct v2f {
  11. float4 pos : SV_POSITION;
  12. float2 uv : TEXCOORD0;
  13. float2 uv1 : TEXCOORD1;
  14. };
  15. struct v2fRadius {
  16. float4 pos : SV_POSITION;
  17. float2 uv : TEXCOORD0;
  18. float4 uv1[4] : TEXCOORD1;
  19. };
  20. struct v2fBlur {
  21. float4 pos : SV_POSITION;
  22. float2 uv : TEXCOORD0;
  23. float4 uv01 : TEXCOORD1;
  24. float4 uv23 : TEXCOORD2;
  25. float4 uv45 : TEXCOORD3;
  26. float4 uv67 : TEXCOORD4;
  27. float4 uv89 : TEXCOORD5;
  28. };
  29. uniform sampler2D _MainTex;
  30. uniform sampler2D_float _CameraDepthTexture;
  31. uniform sampler2D _FgOverlap;
  32. uniform sampler2D _LowRez;
  33. uniform float4 _CurveParams;
  34. uniform float4 _MainTex_TexelSize;
  35. uniform float4 _Offsets;
  36. v2f vert( appdata_img v )
  37. {
  38. v2f o;
  39. o.pos = UnityObjectToClipPos (v.vertex);
  40. o.uv1.xy = v.texcoord.xy;
  41. o.uv.xy = v.texcoord.xy;
  42. #if UNITY_UV_STARTS_AT_TOP
  43. if (_MainTex_TexelSize.y < 0)
  44. o.uv.y = 1-o.uv.y;
  45. #endif
  46. return o;
  47. }
  48. v2f vertFlip( appdata_img v )
  49. {
  50. v2f o;
  51. o.pos = UnityObjectToClipPos (v.vertex);
  52. o.uv1.xy = v.texcoord.xy;
  53. o.uv.xy = v.texcoord.xy;
  54. #if UNITY_UV_STARTS_AT_TOP
  55. if (_MainTex_TexelSize.y < 0)
  56. o.uv.y = 1-o.uv.y;
  57. if (_MainTex_TexelSize.y < 0)
  58. o.uv1.y = 1-o.uv1.y;
  59. #endif
  60. return o;
  61. }
  62. v2fBlur vertBlurPlusMinus (appdata_img v)
  63. {
  64. v2fBlur o;
  65. o.pos = UnityObjectToClipPos(v.vertex);
  66. o.uv.xy = v.texcoord.xy;
  67. o.uv01 = v.texcoord.xyxy + _Offsets.xyxy * float4(1,1, -1,-1) * _MainTex_TexelSize.xyxy / 6.0;
  68. o.uv23 = v.texcoord.xyxy + _Offsets.xyxy * float4(2,2, -2,-2) * _MainTex_TexelSize.xyxy / 6.0;
  69. o.uv45 = v.texcoord.xyxy + _Offsets.xyxy * float4(3,3, -3,-3) * _MainTex_TexelSize.xyxy / 6.0;
  70. o.uv67 = v.texcoord.xyxy + _Offsets.xyxy * float4(4,4, -4,-4) * _MainTex_TexelSize.xyxy / 6.0;
  71. o.uv89 = v.texcoord.xyxy + _Offsets.xyxy * float4(5,5, -5,-5) * _MainTex_TexelSize.xyxy / 6.0;
  72. return o;
  73. }
  74. #define SCATTER_OVERLAP_SMOOTH (-0.265)
  75. inline float BokehWeightDisc(float4 theSample, float sampleDistance, float4 centerSample)
  76. {
  77. return smoothstep(SCATTER_OVERLAP_SMOOTH, 0.0, theSample.a - centerSample.a*sampleDistance);
  78. }
  79. inline float2 BokehWeightDisc2(float4 sampleA, float4 sampleB, float2 sampleDistance2, float4 centerSample)
  80. {
  81. return smoothstep(float2(SCATTER_OVERLAP_SMOOTH, SCATTER_OVERLAP_SMOOTH), float2(0.0,0.0), float2(sampleA.a, sampleB.a) - centerSample.aa*sampleDistance2); }
  82. static const int SmallDiscKernelSamples = 12;
  83. static const float2 SmallDiscKernel[SmallDiscKernelSamples] =
  84. {
  85. float2(-0.326212,-0.40581),
  86. float2(-0.840144,-0.07358),
  87. float2(-0.695914,0.457137),
  88. float2(-0.203345,0.620716),
  89. float2(0.96234,-0.194983),
  90. float2(0.473434,-0.480026),
  91. float2(0.519456,0.767022),
  92. float2(0.185461,-0.893124),
  93. float2(0.507431,0.064425),
  94. float2(0.89642,0.412458),
  95. float2(-0.32194,-0.932615),
  96. float2(-0.791559,-0.59771)
  97. };
  98. static const int NumDiscSamples = 28;
  99. static const float3 DiscKernel[NumDiscSamples] =
  100. {
  101. float3(0.62463,0.54337,0.82790),
  102. float3(-0.13414,-0.94488,0.95435),
  103. float3(0.38772,-0.43475,0.58253),
  104. float3(0.12126,-0.19282,0.22778),
  105. float3(-0.20388,0.11133,0.23230),
  106. float3(0.83114,-0.29218,0.88100),
  107. float3(0.10759,-0.57839,0.58831),
  108. float3(0.28285,0.79036,0.83945),
  109. float3(-0.36622,0.39516,0.53876),
  110. float3(0.75591,0.21916,0.78704),
  111. float3(-0.52610,0.02386,0.52664),
  112. float3(-0.88216,-0.24471,0.91547),
  113. float3(-0.48888,-0.29330,0.57011),
  114. float3(0.44014,-0.08558,0.44838),
  115. float3(0.21179,0.51373,0.55567),
  116. float3(0.05483,0.95701,0.95858),
  117. float3(-0.59001,-0.70509,0.91938),
  118. float3(-0.80065,0.24631,0.83768),
  119. float3(-0.19424,-0.18402,0.26757),
  120. float3(-0.43667,0.76751,0.88304),
  121. float3(0.21666,0.11602,0.24577),
  122. float3(0.15696,-0.85600,0.87027),
  123. float3(-0.75821,0.58363,0.95682),
  124. float3(0.99284,-0.02904,0.99327),
  125. float3(-0.22234,-0.57907,0.62029),
  126. float3(0.55052,-0.66984,0.86704),
  127. float3(0.46431,0.28115,0.54280),
  128. float3(-0.07214,0.60554,0.60982),
  129. };
  130. float4 fragBlurInsaneMQ (v2f i) : SV_Target
  131. {
  132. float4 centerTap = tex2D(_MainTex, i.uv1.xy);
  133. float4 sum = centerTap;
  134. float4 poissonScale = _MainTex_TexelSize.xyxy * centerTap.a * _Offsets.w;
  135. float sampleCount = max(centerTap.a * 0.25, _Offsets.z); // <- weighing with 0.25 looks nicer for small high freq spec
  136. sum *= sampleCount;
  137. float weights = 0;
  138. for(int l=0; l < NumDiscSamples; l++)
  139. {
  140. float2 sampleUV = i.uv1.xy + DiscKernel[l].xy * poissonScale.xy;
  141. float4 sample0 = tex2D(_MainTex, sampleUV.xy);
  142. if( sample0.a > 0.0 )
  143. {
  144. weights = BokehWeightDisc(sample0, DiscKernel[l].z, centerTap);
  145. sum += sample0 * weights;
  146. sampleCount += weights;
  147. }
  148. }
  149. float4 returnValue = sum / sampleCount;
  150. returnValue.a = centerTap.a;
  151. return returnValue;
  152. }
  153. float4 fragBlurInsaneHQ (v2f i) : SV_Target
  154. {
  155. float4 centerTap = tex2D(_MainTex, i.uv1.xy);
  156. float4 sum = centerTap;
  157. float4 poissonScale = _MainTex_TexelSize.xyxy * centerTap.a * _Offsets.w;
  158. float sampleCount = max(centerTap.a * 0.25, _Offsets.z); // <- weighing with 0.25 looks nicer for small high freq spec
  159. sum *= sampleCount;
  160. float2 weights = 0;
  161. for(int l=0; l < NumDiscSamples; l++)
  162. {
  163. float4 sampleUV = i.uv1.xyxy + DiscKernel[l].xyxy * poissonScale.xyxy / float4(1.2,1.2,DiscKernel[l].zz);
  164. float4 sample0 = tex2D(_MainTex, sampleUV.xy);
  165. float4 sample1 = tex2D(_MainTex, sampleUV.zw);
  166. if( (sample0.a + sample1.a) > 0.0 )
  167. {
  168. weights = BokehWeightDisc2(sample0, sample1, float2(DiscKernel[l].z/1.2, 1.0), centerTap);
  169. sum += sample0 * weights.x + sample1 * weights.y;
  170. sampleCount += dot(weights, 1);
  171. }
  172. }
  173. float4 returnValue = sum / sampleCount;
  174. returnValue.a = centerTap.a;
  175. return returnValue;
  176. }
  177. inline float4 BlendLowWithHighHQ(float coc, float4 low, float4 high)
  178. {
  179. float blend = smoothstep(0.65,0.85, coc);
  180. return lerp(low, high, blend);
  181. }
  182. inline float4 BlendLowWithHighMQ(float coc, float4 low, float4 high)
  183. {
  184. float blend = smoothstep(0.4,0.6, coc);
  185. return lerp(low, high, blend);
  186. }
  187. float4 fragBlurUpsampleCombineHQ (v2f i) : SV_Target
  188. {
  189. float4 bigBlur = tex2D(_LowRez, i.uv1.xy);
  190. float4 centerTap = tex2D(_MainTex, i.uv1.xy);
  191. float4 smallBlur = centerTap;
  192. float4 poissonScale = _MainTex_TexelSize.xyxy * centerTap.a * _Offsets.w ;
  193. float sampleCount = max(centerTap.a * 0.25, 0.1f); // <- weighing with 0.25 looks nicer for small high freq spec
  194. smallBlur *= sampleCount;
  195. for(int l=0; l < NumDiscSamples; l++)
  196. {
  197. float2 sampleUV = i.uv1.xy + DiscKernel[l].xy * poissonScale.xy;
  198. float4 sample0 = tex2D(_MainTex, sampleUV);
  199. float weight0 = BokehWeightDisc(sample0, DiscKernel[l].z, centerTap);
  200. smallBlur += sample0 * weight0; sampleCount += weight0;
  201. }
  202. smallBlur /= (sampleCount+1e-5f);
  203. smallBlur = BlendLowWithHighHQ(centerTap.a, smallBlur, bigBlur);
  204. return centerTap.a < 1e-2f ? centerTap : float4(smallBlur.rgb,centerTap.a);
  205. }
  206. float4 fragBlurUpsampleCombineMQ (v2f i) : SV_Target
  207. {
  208. float4 bigBlur = tex2D(_LowRez, i.uv1.xy);
  209. float4 centerTap = tex2D(_MainTex, i.uv1.xy);
  210. float4 smallBlur = centerTap;
  211. float4 poissonScale = _MainTex_TexelSize.xyxy * centerTap.a * _Offsets.w ;
  212. float sampleCount = max(centerTap.a * 0.25, 0.1f); // <- weighing with 0.25 looks nicer for small high freq spec
  213. smallBlur *= sampleCount;
  214. for(int l=0; l < SmallDiscKernelSamples; l++)
  215. {
  216. float2 sampleUV = i.uv1.xy + SmallDiscKernel[l].xy * poissonScale.xy*1.1;
  217. float4 sample0 = tex2D(_MainTex, sampleUV);
  218. float weight0 = BokehWeightDisc(sample0, length(SmallDiscKernel[l].xy*1.1), centerTap);
  219. smallBlur += sample0 * weight0; sampleCount += weight0;
  220. }
  221. smallBlur /= (sampleCount+1e-5f);
  222. smallBlur = BlendLowWithHighMQ(centerTap.a, smallBlur, bigBlur);
  223. return centerTap.a < 1e-2f ? centerTap : float4(smallBlur.rgb,centerTap.a);
  224. }
  225. float4 fragBlurUpsampleCheap (v2f i) : SV_Target
  226. {
  227. float4 centerTap = tex2D(_MainTex, i.uv1.xy);
  228. float4 bigBlur = tex2D(_LowRez, i.uv1.xy);
  229. float fgCoc = tex2D(_FgOverlap, i.uv1.xy).a;
  230. float4 smallBlur = lerp(centerTap, bigBlur, saturate( max(centerTap.a,fgCoc)*8.0 ));
  231. return float4(smallBlur.rgb, centerTap.a);
  232. }
  233. float4 fragBlurBox (v2f i) : SV_Target
  234. {
  235. const int TAPS = 12;
  236. float4 centerTap = tex2D(_MainTex, i.uv1.xy);
  237. // TODO: important ? breaks when HR blur is being used
  238. //centerTap.a = max(centerTap.a, 0.1f);
  239. float sampleCount = centerTap.a;
  240. float4 sum = centerTap * sampleCount;
  241. float2 lenStep = centerTap.aa * (1.0 / (TAPS-1.0));
  242. float4 steps = (_Offsets.xyxy * _MainTex_TexelSize.xyxy) * lenStep.xyxy * float4(1,1, -1,-1);
  243. for(int l=1; l<TAPS; l++)
  244. {
  245. float4 sampleUV = i.uv1.xyxy + steps * (float)l;
  246. float4 sample0 = tex2D(_MainTex, sampleUV.xy);
  247. float4 sample1 = tex2D(_MainTex, sampleUV.zw);
  248. float2 maxLen01 = float2(sample0.a, sample1.a);
  249. float2 r = lenStep.xx * (float)l;
  250. float2 weight01 = smoothstep(float2(-0.4,-0.4),float2(0.0,0.0), maxLen01-r);
  251. sum += sample0 * weight01.x + sample1 * weight01.y;
  252. sampleCount += dot(weight01,1);
  253. }
  254. float4 returnValue = sum / (1e-5f + sampleCount);
  255. //returnValue.a = centerTap.a;
  256. //return centerTap.a;
  257. return returnValue;
  258. }
  259. float4 fragVisualize (v2f i) : SV_Target
  260. {
  261. float4 returnValue = tex2D(_MainTex, i.uv1.xy);
  262. returnValue.rgb = lerp(float3(0.0,0.0,0.0), float3(1.0,1.0,1.0), saturate(returnValue.a/_CurveParams.x));
  263. return returnValue;
  264. }
  265. float4 fragBoxDownsample (v2f i) : SV_Target
  266. {
  267. //float4 returnValue = tex2D(_MainTex, i.uv1.xy);
  268. float4 returnValue = tex2D(_MainTex, i.uv1.xy + 0.75*_MainTex_TexelSize.xy);
  269. returnValue += tex2D(_MainTex, i.uv1.xy - 0.75*_MainTex_TexelSize.xy);
  270. returnValue += tex2D(_MainTex, i.uv1.xy + 0.75*_MainTex_TexelSize.xy * float2(1,-1));
  271. returnValue += tex2D(_MainTex, i.uv1.xy - 0.75*_MainTex_TexelSize.xy * float2(1,-1));
  272. return returnValue/4;
  273. }
  274. float4 fragBlurAlphaWeighted (v2fBlur i) : SV_Target
  275. {
  276. const float ALPHA_WEIGHT = 2.0f;
  277. float4 sum = float4 (0,0,0,0);
  278. float w = 0;
  279. float weights = 0;
  280. const float G_WEIGHTS[6] = {1.0, 0.8, 0.675, 0.5, 0.2, 0.075};
  281. float4 sampleA = tex2D(_MainTex, i.uv.xy);
  282. float4 sampleB = tex2D(_MainTex, i.uv01.xy);
  283. float4 sampleC = tex2D(_MainTex, i.uv01.zw);
  284. float4 sampleD = tex2D(_MainTex, i.uv23.xy);
  285. float4 sampleE = tex2D(_MainTex, i.uv23.zw);
  286. float4 sampleF = tex2D(_MainTex, i.uv45.xy);
  287. float4 sampleG = tex2D(_MainTex, i.uv45.zw);
  288. float4 sampleH = tex2D(_MainTex, i.uv67.xy);
  289. float4 sampleI = tex2D(_MainTex, i.uv67.zw);
  290. float4 sampleJ = tex2D(_MainTex, i.uv89.xy);
  291. float4 sampleK = tex2D(_MainTex, i.uv89.zw);
  292. w = sampleA.a * G_WEIGHTS[0]; sum += sampleA * w; weights += w;
  293. w = saturate(ALPHA_WEIGHT*sampleB.a) * G_WEIGHTS[1]; sum += sampleB * w; weights += w;
  294. w = saturate(ALPHA_WEIGHT*sampleC.a) * G_WEIGHTS[1]; sum += sampleC * w; weights += w;
  295. w = saturate(ALPHA_WEIGHT*sampleD.a) * G_WEIGHTS[2]; sum += sampleD * w; weights += w;
  296. w = saturate(ALPHA_WEIGHT*sampleE.a) * G_WEIGHTS[2]; sum += sampleE * w; weights += w;
  297. w = saturate(ALPHA_WEIGHT*sampleF.a) * G_WEIGHTS[3]; sum += sampleF * w; weights += w;
  298. w = saturate(ALPHA_WEIGHT*sampleG.a) * G_WEIGHTS[3]; sum += sampleG * w; weights += w;
  299. w = saturate(ALPHA_WEIGHT*sampleH.a) * G_WEIGHTS[4]; sum += sampleH * w; weights += w;
  300. w = saturate(ALPHA_WEIGHT*sampleI.a) * G_WEIGHTS[4]; sum += sampleI * w; weights += w;
  301. w = saturate(ALPHA_WEIGHT*sampleJ.a) * G_WEIGHTS[5]; sum += sampleJ * w; weights += w;
  302. w = saturate(ALPHA_WEIGHT*sampleK.a) * G_WEIGHTS[5]; sum += sampleK * w; weights += w;
  303. sum /= weights + 1e-4f;
  304. sum.a = sampleA.a;
  305. if(sampleA.a<1e-2f) sum.rgb = sampleA.rgb;
  306. return sum;
  307. }
  308. float4 fragBlurForFgCoc (v2fBlur i) : SV_Target
  309. {
  310. float4 sum = float4 (0,0,0,0);
  311. float w = 0;
  312. float weights = 0;
  313. const float G_WEIGHTS[6] = {1.0, 0.8, 0.675, 0.5, 0.2, 0.075};
  314. float4 sampleA = tex2D(_MainTex, i.uv.xy);
  315. float4 sampleB = tex2D(_MainTex, i.uv01.xy);
  316. float4 sampleC = tex2D(_MainTex, i.uv01.zw);
  317. float4 sampleD = tex2D(_MainTex, i.uv23.xy);
  318. float4 sampleE = tex2D(_MainTex, i.uv23.zw);
  319. float4 sampleF = tex2D(_MainTex, i.uv45.xy);
  320. float4 sampleG = tex2D(_MainTex, i.uv45.zw);
  321. float4 sampleH = tex2D(_MainTex, i.uv67.xy);
  322. float4 sampleI = tex2D(_MainTex, i.uv67.zw);
  323. float4 sampleJ = tex2D(_MainTex, i.uv89.xy);
  324. float4 sampleK = tex2D(_MainTex, i.uv89.zw);
  325. w = sampleA.a * G_WEIGHTS[0]; sum += sampleA * w; weights += w;
  326. w = smoothstep(-0.5,0.0,sampleB.a-sampleA.a) * G_WEIGHTS[1]; sum += sampleB * w; weights += w;
  327. w = smoothstep(-0.5,0.0,sampleC.a-sampleA.a) * G_WEIGHTS[1]; sum += sampleC * w; weights += w;
  328. w = smoothstep(-0.5,0.0,sampleD.a-sampleA.a) * G_WEIGHTS[2]; sum += sampleD * w; weights += w;
  329. w = smoothstep(-0.5,0.0,sampleE.a-sampleA.a) * G_WEIGHTS[2]; sum += sampleE * w; weights += w;
  330. w = smoothstep(-0.5,0.0,sampleF.a-sampleA.a) * G_WEIGHTS[3]; sum += sampleF * w; weights += w;
  331. w = smoothstep(-0.5,0.0,sampleG.a-sampleA.a) * G_WEIGHTS[3]; sum += sampleG * w; weights += w;
  332. w = smoothstep(-0.5,0.0,sampleH.a-sampleA.a) * G_WEIGHTS[4]; sum += sampleH * w; weights += w;
  333. w = smoothstep(-0.5,0.0,sampleI.a-sampleA.a) * G_WEIGHTS[4]; sum += sampleI * w; weights += w;
  334. w = smoothstep(-0.5,0.0,sampleJ.a-sampleA.a) * G_WEIGHTS[5]; sum += sampleJ * w; weights += w;
  335. w = smoothstep(-0.5,0.0,sampleK.a-sampleA.a) * G_WEIGHTS[5]; sum += sampleK * w; weights += w;
  336. sum /= weights + 1e-4f;
  337. return sum;
  338. }
  339. float4 fragGaussBlur (v2fBlur i) : SV_Target
  340. {
  341. float4 sum = float4 (0,0,0,0);
  342. float w = 0;
  343. float weights = 0;
  344. const float G_WEIGHTS[9] = {1.0, 0.8, 0.65, 0.5, 0.4, 0.2, 0.1, 0.05, 0.025};
  345. float4 sampleA = tex2D(_MainTex, i.uv.xy);
  346. float4 sampleB = tex2D(_MainTex, i.uv01.xy);
  347. float4 sampleC = tex2D(_MainTex, i.uv01.zw);
  348. float4 sampleD = tex2D(_MainTex, i.uv23.xy);
  349. float4 sampleE = tex2D(_MainTex, i.uv23.zw);
  350. float4 sampleF = tex2D(_MainTex, i.uv45.xy);
  351. float4 sampleG = tex2D(_MainTex, i.uv45.zw);
  352. float4 sampleH = tex2D(_MainTex, i.uv67.xy);
  353. float4 sampleI = tex2D(_MainTex, i.uv67.zw);
  354. float4 sampleJ = tex2D(_MainTex, i.uv89.xy);
  355. float4 sampleK = tex2D(_MainTex, i.uv89.zw);
  356. w = sampleA.a * G_WEIGHTS[0]; sum += sampleA * w; weights += w;
  357. w = sampleB.a * G_WEIGHTS[1]; sum += sampleB * w; weights += w;
  358. w = sampleC.a * G_WEIGHTS[1]; sum += sampleC * w; weights += w;
  359. w = sampleD.a * G_WEIGHTS[2]; sum += sampleD * w; weights += w;
  360. w = sampleE.a * G_WEIGHTS[2]; sum += sampleE * w; weights += w;
  361. w = sampleF.a * G_WEIGHTS[3]; sum += sampleF * w; weights += w;
  362. w = sampleG.a * G_WEIGHTS[3]; sum += sampleG * w; weights += w;
  363. w = sampleH.a * G_WEIGHTS[4]; sum += sampleH * w; weights += w;
  364. w = sampleI.a * G_WEIGHTS[4]; sum += sampleI * w; weights += w;
  365. w = sampleJ.a * G_WEIGHTS[5]; sum += sampleJ * w; weights += w;
  366. w = sampleK.a * G_WEIGHTS[5]; sum += sampleK * w; weights += w;
  367. sum /= weights + 1e-4f;
  368. return sum;
  369. }
  370. float4 frag4TapBlurForLRSpawn (v2f i) : SV_Target
  371. {
  372. float4 tap = tex2D(_MainTex, i.uv.xy);
  373. float4 tapA = tex2D(_MainTex, i.uv.xy + 0.75 * _MainTex_TexelSize.xy);
  374. float4 tapB = tex2D(_MainTex, i.uv.xy - 0.75 * _MainTex_TexelSize.xy);
  375. float4 tapC = tex2D(_MainTex, i.uv.xy + 0.75 * _MainTex_TexelSize.xy * float2(1,-1));
  376. float4 tapD = tex2D(_MainTex, i.uv.xy - 0.75 * _MainTex_TexelSize.xy * float2(1,-1));
  377. float4 weights = saturate(10.0 * float4(tapA.a, tapB.a, tapC.a, tapD.a));
  378. float sumWeights = dot(weights, 1);
  379. float4 color = (tapA*weights.x + tapB*weights.y + tapC*weights.z + tapD*weights.w);
  380. float4 outColor = tap;
  381. if(tap.a * sumWeights * 8.0 > 1e-5f) outColor.rgb = color.rgb/sumWeights;
  382. return outColor;
  383. }
  384. float4 fragCaptureColorAndSignedCoc (v2f i) : SV_Target
  385. {
  386. float4 color = tex2D (_MainTex, i.uv1.xy);
  387. float d = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv1.xy);
  388. d = Linear01Depth (d);
  389. color.a = _CurveParams.z * abs(d - _CurveParams.w) / (d + 1e-5f);
  390. color.a = clamp( max(0.0, color.a - _CurveParams.y), 0.0, _CurveParams.x) * sign(d - _CurveParams.w);
  391. return color;
  392. }
  393. float4 fragCaptureCoc (v2f i) : SV_Target
  394. {
  395. float4 color = float4(0,0,0,0); //tex2D (_MainTex, i.uv1.xy);
  396. float d = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv1.xy);
  397. d = Linear01Depth (d);
  398. color.a = _CurveParams.z * abs(d - _CurveParams.w) / (d + 1e-5f);
  399. color.a = clamp( max(0.0, color.a - _CurveParams.y), 0.0, _CurveParams.x);
  400. return color;
  401. }
  402. float4 AddFgCoc (v2f i) : SV_Target
  403. {
  404. return tex2D (_MainTex, i.uv1.xy);
  405. }
  406. float4 fragMergeCoc (v2f i) : SV_Target
  407. {
  408. float4 color = tex2D (_FgOverlap, i.uv.xy); // this is the foreground overlap value
  409. float fgCoc = color.a;
  410. float d = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv1.xy);
  411. d = Linear01Depth (d);
  412. color.a = _CurveParams.z * abs(d - _CurveParams.w) / (d + 1e-5f);
  413. color.a = clamp( max(0.0, color.a - _CurveParams.y), 0.0, _CurveParams.x);
  414. return max(color.aaaa, float4(fgCoc,fgCoc,fgCoc,fgCoc));
  415. }
  416. float4 fragCombineCocWithMaskBlur (v2f i) : SV_Target
  417. {
  418. float bgAndFgCoc = tex2D (_MainTex, i.uv1.xy).a;
  419. float fgOverlapCoc = tex2D (_FgOverlap, i.uv1.xy).a;
  420. return (bgAndFgCoc < 0.01) * saturate(fgOverlapCoc-bgAndFgCoc);
  421. }
  422. float4 fragCaptureForegroundCoc (v2f i) : SV_Target
  423. {
  424. float4 color = float4(0,0,0,0); //tex2D (_MainTex, i.uv1.xy);
  425. float d = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv1.xy);
  426. d = Linear01Depth (d);
  427. color.a = _CurveParams.z * (_CurveParams.w-d) / (d + 1e-5f);
  428. color.a = clamp(max(0.0, color.a - _CurveParams.y), 0.0, _CurveParams.x);
  429. return color;
  430. }
  431. float4 fragCaptureForegroundCocMask (v2f i) : SV_Target
  432. {
  433. float4 color = float4(0,0,0,0);
  434. float d = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv1.xy);
  435. d = Linear01Depth (d);
  436. color.a = _CurveParams.z * (_CurveParams.w-d) / (d + 1e-5f);
  437. color.a = clamp(max(0.0, color.a - _CurveParams.y), 0.0, _CurveParams.x);
  438. return color.a > 0;
  439. }
  440. float4 fragBlendInHighRez (v2f i) : SV_Target
  441. {
  442. float4 tapHighRez = tex2D(_MainTex, i.uv.xy);
  443. return float4(tapHighRez.rgb, 1.0-saturate(tapHighRez.a*5.0));
  444. }
  445. float4 fragBlendInLowRezParts (v2f i) : SV_Target
  446. {
  447. float4 from = tex2D(_MainTex, i.uv1.xy);
  448. from.a = saturate(from.a * _Offsets.w) / (_CurveParams.x + 1e-5f);
  449. float square = from.a * from.a;
  450. from.a = square * square * _CurveParams.x;
  451. return from;
  452. }
  453. float4 fragUpsampleWithAlphaMask(v2f i) : SV_Target
  454. {
  455. float4 c = tex2D(_MainTex, i.uv1.xy);
  456. return c;
  457. }
  458. float4 fragAlphaMask(v2f i) : SV_Target
  459. {
  460. float4 c = tex2D(_MainTex, i.uv1.xy);
  461. c.a = saturate(c.a*100.0);
  462. return c;
  463. }
  464. ENDCG
  465. Subshader
  466. {
  467. // pass 0
  468. Pass {
  469. ZTest Always Cull Off ZWrite Off
  470. ColorMask A
  471. CGPROGRAM
  472. #pragma target 3.0
  473. #pragma vertex vert
  474. #pragma fragment fragCaptureCoc
  475. ENDCG
  476. }
  477. // pass 1
  478. Pass
  479. {
  480. ZTest Always Cull Off ZWrite Off
  481. CGPROGRAM
  482. #pragma target 3.0
  483. #pragma vertex vertBlurPlusMinus
  484. #pragma fragment fragGaussBlur
  485. ENDCG
  486. }
  487. // pass 2
  488. Pass {
  489. ZTest Always Cull Off ZWrite Off
  490. CGPROGRAM
  491. #pragma target 3.0
  492. #pragma vertex vertBlurPlusMinus
  493. #pragma fragment fragBlurForFgCoc
  494. ENDCG
  495. }
  496. // pass 3
  497. Pass
  498. {
  499. ZTest Always Cull Off ZWrite Off
  500. ColorMask A
  501. BlendOp Max, Max
  502. Blend One One, One One
  503. CGPROGRAM
  504. #pragma target 3.0
  505. #pragma vertex vert
  506. #pragma fragment AddFgCoc
  507. ENDCG
  508. }
  509. // pass 4
  510. Pass
  511. {
  512. ZTest Always Cull Off ZWrite Off
  513. ColorMask A
  514. CGPROGRAM
  515. #pragma target 3.0
  516. #pragma vertex vert
  517. #pragma fragment fragCaptureForegroundCoc
  518. ENDCG
  519. }
  520. // pass 5
  521. Pass {
  522. ZTest Always Cull Off ZWrite Off
  523. CGPROGRAM
  524. #pragma target 3.0
  525. #pragma vertex vert
  526. #pragma fragment fragBlurBox
  527. ENDCG
  528. }
  529. // pass 6
  530. Pass {
  531. ZTest Always Cull Off ZWrite Off
  532. CGPROGRAM
  533. #pragma target 3.0
  534. #pragma vertex vert
  535. #pragma fragment frag4TapBlurForLRSpawn
  536. ENDCG
  537. }
  538. // pass 7
  539. Pass {
  540. ZTest Always Cull Off ZWrite Off
  541. ColorMask RGB
  542. Blend SrcAlpha OneMinusSrcAlpha
  543. CGPROGRAM
  544. #pragma target 3.0
  545. #pragma vertex vert
  546. #pragma fragment fragBlendInHighRez
  547. ENDCG
  548. }
  549. // pass 8
  550. Pass
  551. {
  552. ZTest Always Cull Off ZWrite Off
  553. ColorMask A
  554. CGPROGRAM
  555. #pragma target 3.0
  556. #pragma vertex vert
  557. #pragma fragment fragCaptureForegroundCocMask
  558. ENDCG
  559. }
  560. // pass 9
  561. Pass {
  562. ZTest Always Cull Off ZWrite Off
  563. CGPROGRAM
  564. #pragma target 3.0
  565. #pragma vertex vert
  566. #pragma fragment fragBlurUpsampleCheap
  567. ENDCG
  568. }
  569. // pass 10
  570. Pass {
  571. ZTest Always Cull Off ZWrite Off
  572. CGPROGRAM
  573. #pragma target 3.0
  574. #pragma vertex vert
  575. #pragma fragment fragCaptureColorAndSignedCoc
  576. ENDCG
  577. }
  578. // pass 11
  579. Pass {
  580. ZTest Always Cull Off ZWrite Off
  581. CGPROGRAM
  582. #pragma target 3.0
  583. #pragma vertex vert
  584. #pragma fragment fragBlurInsaneMQ
  585. ENDCG
  586. }
  587. // pass 12
  588. Pass {
  589. ZTest Always Cull Off ZWrite Off
  590. CGPROGRAM
  591. #pragma target 3.0
  592. #pragma vertex vert
  593. #pragma fragment fragBlurUpsampleCombineMQ
  594. ENDCG
  595. }
  596. // pass 13
  597. Pass {
  598. ZTest Always Cull Off ZWrite Off
  599. ColorMask A
  600. CGPROGRAM
  601. #pragma target 3.0
  602. #pragma vertex vert
  603. #pragma fragment fragMergeCoc
  604. ENDCG
  605. }
  606. // pass 14
  607. Pass {
  608. ZTest Always Cull Off ZWrite Off
  609. ColorMask A
  610. BlendOp Max, Max
  611. Blend One One, One One
  612. CGPROGRAM
  613. #pragma target 3.0
  614. #pragma vertex vert
  615. #pragma fragment fragCombineCocWithMaskBlur
  616. ENDCG
  617. }
  618. // pass 15
  619. Pass {
  620. ZTest Always Cull Off ZWrite Off
  621. CGPROGRAM
  622. #pragma target 3.0
  623. #pragma vertex vert
  624. #pragma fragment fragBoxDownsample
  625. ENDCG
  626. }
  627. // pass 16
  628. Pass {
  629. ZTest Always Cull Off ZWrite Off
  630. CGPROGRAM
  631. #pragma target 3.0
  632. #pragma vertex vert
  633. #pragma fragment fragVisualize
  634. ENDCG
  635. }
  636. // pass 17
  637. Pass {
  638. ZTest Always Cull Off ZWrite Off
  639. CGPROGRAM
  640. #pragma target 3.0
  641. #pragma vertex vert
  642. #pragma fragment fragBlurInsaneHQ
  643. ENDCG
  644. }
  645. // pass 18
  646. Pass {
  647. ZTest Always Cull Off ZWrite Off
  648. CGPROGRAM
  649. #pragma target 3.0
  650. #pragma vertex vert
  651. #pragma fragment fragBlurUpsampleCombineHQ
  652. ENDCG
  653. }
  654. // pass 19
  655. Pass {
  656. ZTest Always Cull Off ZWrite Off
  657. CGPROGRAM
  658. #pragma target 3.0
  659. #pragma vertex vertBlurPlusMinus
  660. #pragma fragment fragBlurAlphaWeighted
  661. ENDCG
  662. }
  663. // pass 20
  664. Pass {
  665. ZTest Always Cull Off ZWrite Off
  666. CGPROGRAM
  667. #pragma target 3.0
  668. #pragma vertex vert
  669. #pragma fragment fragAlphaMask
  670. ENDCG
  671. }
  672. // pass 21
  673. Pass {
  674. ZTest Always Cull Off ZWrite Off
  675. BlendOp Add, Add
  676. Blend DstAlpha OneMinusDstAlpha, Zero One
  677. CGPROGRAM
  678. #pragma target 3.0
  679. #pragma vertex vertFlip
  680. #pragma fragment fragBlurBox
  681. ENDCG
  682. }
  683. // pass 22
  684. Pass {
  685. ZTest Always Cull Off ZWrite Off
  686. // destination alpha needs to stay intact as we have layed alpha before
  687. BlendOp Add, Add
  688. Blend DstAlpha One, Zero One
  689. CGPROGRAM
  690. #pragma target 3.0
  691. #pragma vertex vert
  692. #pragma fragment fragUpsampleWithAlphaMask
  693. ENDCG
  694. }
  695. }
  696. Fallback off
  697. }