-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathCP_SSSSS_Shader.shader
159 lines (138 loc) · 3.9 KB
/
CP_SSSSS_Shader.shader
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
159
Shader "Hidden/CPSSSSSShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
CGINCLUDE
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
ENDCG
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
//Separable depth based blur
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
sampler2D_float _CameraDepthTexture;
sampler2D _MaskTex;
fixed4 _BlurVec;
float _BlurStr;
float _SoftDepthBias;
inline float SAMPLE_INVERSE_DEPTH(float2 uvs) {
float t = unity_CameraProjection._m11;
float z = LinearEyeDepth(tex2D(_CameraDepthTexture, uvs).r);
/*
Voodoo math on visible size multiplier here that i dont understand, but it works
*/
float size = 0.5/(z + 0.5);
return size*t*0.6;
}
inline float SAMPLE_INVERSE_DEPTH_LINEAR(float2 uvs) {
return saturate(1.0 - Linear01Depth(tex2D(_CameraDepthTexture, uvs).r));
}
float4 frag (v2f i) : SV_Target
{
//If theres nothing in the mask texture, then we dont need to blur/process it
float4 testMask = tex2D(_MaskTex, i.uv);
if (testMask.a < 0.003 || testMask.r + testMask.g + testMask.b < 0.005) discard;
float4 col = tex2D(_MainTex, i.uv);
fixed2 blurvec = _BlurVec.xy;
float d = SAMPLE_INVERSE_DEPTH(i.uv);
float str = _BlurStr * d * testMask.a;
float dlin = SAMPLE_INVERSE_DEPTH_LINEAR(i.uv);
if (dlin < 0.1) discard;
//Gaussian blur
/*
col *= 0.38;
col += tex2D(_MainTex, i.uv + blurvec * 1 * str)*0.18;
col += tex2D(_MainTex, i.uv - blurvec * 1 * str)*0.18;
col += tex2D(_MainTex, i.uv + blurvec * 2 * str)*0.09;
col += tex2D(_MainTex, i.uv - blurvec * 2 * str)*0.09;
col += tex2D(_MainTex, i.uv + blurvec * 3 * str)*0.04;
col += tex2D(_MainTex, i.uv - blurvec * 3 * str)*0.04;
*/
//Blur with depth check
/**/
float sum = 1;
float diff = 0;
float cont = 0;
//HACK:
//We multiply the depth sample vector by a 1.06 to get rid of the 1 pixel wide lines,
//that show up on the edges where the depth difference between samples is significant,
//i have no clue whats causing it and how to fix it properly :(
for (uint n = 1; n <= 2; n++) {
float contrib_base = 0.99 / (n + 2);
float4 colr = tex2D(_MainTex, i.uv + blurvec * n * str);
float dr = SAMPLE_INVERSE_DEPTH_LINEAR(i.uv + blurvec * n * str * 1.06);
diff = abs(dlin - dr);
cont = 1.0-saturate(diff / _SoftDepthBias);
cont *= contrib_base;
col.rgb += colr.rgb*cont;
sum += cont;
float4 coll = tex2D(_MainTex, i.uv - blurvec * n * str);
float dl = SAMPLE_INVERSE_DEPTH_LINEAR(i.uv - blurvec * n * str * 1.06);
diff = abs(dlin - dl);
cont = 1.0 - saturate(diff / _SoftDepthBias);
cont *= contrib_base;
col.rgb += coll.rgb*cont;
sum += cont;
}
col.rgb /= sum;
//return testMask;
return col;
//return pow(sum*0.5, 6);
}
ENDCG
}
//Combine pass
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
sampler2D _BlurTex;
sampler2D _CameraDepthTexture;
sampler2D _MaskTex;
half _EffectStr;
fixed _PreserveOriginal;
float4 frag(v2f i) : SV_Target
{
float4 src = tex2D(_MainTex, i.uv);
fixed4 mask = tex2D(_MaskTex, i.uv);
float fac = 1-pow(saturate(max(max(src.r, src.g), src.b) * 1), 0.5);
float4 blr = tex2D(_BlurTex, i.uv);
//return blr;
//return mask;
//return src*0.5 + mask*0.5;
//return src;
blr = clamp(blr - src*_PreserveOriginal, 0, 50);
blr.rgb *= mask.rgb;
return src+blr*fac*_EffectStr*mask.a;
}
ENDCG
}
}
}