forked from QianMo/X-PostProcessing-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TiltShiftBlur.shader
184 lines (140 loc) · 4.9 KB
/
TiltShiftBlur.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//----------------------------------------------------------------------------------------------------------
// X-PostProcessing Library
// https://github.com/QianMo/X-PostProcessing-Library
// Copyright (C) 2020 QianMo. All rights reserved.
// Licensed under the MIT License
// You may not use this file except in compliance with the License.You may obtain a copy of the License at
// http://opensource.org/licenses/MIT
//----------------------------------------------------------------------------------------------------------
// reference 1 : https://www.shadertoy.com/view/4d2Xzw
// reference 2 : https://github.com/imclab/TiltShift
Shader "Hidden/X-PostProcessing/TiltShiftBlur"
{
HLSLINCLUDE
#include "../../../Shaders/StdLib.hlsl"
#include "../../../Shaders/XPostProcessing.hlsl"
TEXTURE2D_SAMPLER2D(_BlurredTex, sampler_BlurredTex);
float4 _BlurredTex_TexelSize;
half2 _Params;
#define _BlurSize _Params.x
#define _BlurRadius _Params.y
static const int DiscKernelSampleNum_LQ = 12;
static const float2 DiscKernel_LQ[DiscKernelSampleNum_LQ] =
{
float2(-0.326212, -0.40581),
float2(-0.840144, -0.07358),
float2(-0.695914, 0.457137),
float2(-0.203345, 0.620716),
float2(0.96234, -0.194983),
float2(0.473434, -0.480026),
float2(0.519456, 0.767022),
float2(0.185461, -0.893124),
float2(0.507431, 0.064425),
float2(0.89642, 0.412458),
float2(-0.32194, -0.932615),
float2(-0.791559, -0.59771)
};
static const int DiscKernelSampleNum_HQ = 28;
static const float3 DiscKernel_HQ[DiscKernelSampleNum_HQ] =
{
float3(0.62463, 0.54337, 0.82790),
float3(-0.13414, -0.94488, 0.95435),
float3(0.38772, -0.43475, 0.58253),
float3(0.12126, -0.19282, 0.22778),
float3(-0.20388, 0.11133, 0.23230),
float3(0.83114, -0.29218, 0.88100),
float3(0.10759, -0.57839, 0.58831),
float3(0.28285, 0.79036, 0.83945),
float3(-0.36622, 0.39516, 0.53876),
float3(0.75591, 0.21916, 0.78704),
float3(-0.52610, 0.02386, 0.52664),
float3(-0.88216, -0.24471, 0.91547),
float3(-0.48888, -0.29330, 0.57011),
float3(0.44014, -0.08558, 0.44838),
float3(0.21179, 0.51373, 0.55567),
float3(0.05483, 0.95701, 0.95858),
float3(-0.59001, -0.70509, 0.91938),
float3(-0.80065, 0.24631, 0.83768),
float3(-0.19424, -0.18402, 0.26757),
float3(-0.43667, 0.76751, 0.88304),
float3(0.21666, 0.11602, 0.24577),
float3(0.15696, -0.85600, 0.87027),
float3(-0.75821, 0.58363, 0.95682),
float3(0.99284, -0.02904, 0.99327),
float3(-0.22234, -0.57907, 0.62029),
float3(0.55052, -0.66984, 0.86704),
float3(0.46431, 0.28115, 0.54280),
float3(-0.07214, 0.60554, 0.60982),
};
float TiltShiftMask(float2 uv)
{
float centerY = uv.y * 2.0 - 1.0;
return(abs(centerY * _BlurSize * 0.001));
}
float4 FragTiltShiftLQ(VaryingsDefault i): SV_Target
{
float4 screenColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord);
float3 sum = screenColor.rgb;
float mask = TiltShiftMask(i.texcoord.xy);
float4 poissonScale = (_BlurRadius * 30) * mask;
for (int l = 0; l < DiscKernelSampleNum_LQ; l ++)
{
float2 sampleUV = i.texcoord.xy + DiscKernel_LQ[l].xy * poissonScale.xy;
float3 sample0 = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, sampleUV.xy).rgb;
float3 sample1 = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, sampleUV.xy).rgb;
sum += sample0 + sample1;
}
return float4(sum.rgb / (1.0 + 2.0 * DiscKernelSampleNum_LQ), mask);
}
float4 FragTiltShiftHQ(VaryingsDefault i): SV_Target
{
float4 screenColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord);
float3 sum = screenColor.rgb;
float mask = TiltShiftMask(i.texcoord.xy);
float4 poissonScale = (_BlurRadius * 30) * mask;
for (int l = 0; l < DiscKernelSampleNum_HQ; l ++)
{
float2 sampleUV = i.texcoord.xy + DiscKernel_HQ[l].xy * poissonScale.xy;
float3 sample0 = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, sampleUV.xy).rgb;
float3 sample1 = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, sampleUV.xy).rgb;
sum += sample0 + sample1;
}
return float4(sum.rgb / (1.0 + 2.0 * DiscKernelSampleNum_HQ), mask);
}
float4 FragBlend(VaryingsDefault i): SV_Target
{
float4 screenColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord);
float4 blurredColor = SAMPLE_TEXTURE2D(_BlurredTex, sampler_BlurredTex, i.texcoord);
float3 result = lerp(blurredColor.rgb, screenColor.rgb, saturate(blurredColor.a));
return float4(result.rgb, screenColor.a);
}
ENDHLSL
SubShader
{
Cull Off ZWrite Off ZTest Always
//Pass 0 - Tilt Shift Blur High Quality
Pass
{
HLSLPROGRAM
#pragma vertex VertDefault
#pragma fragment FragTiltShiftHQ
ENDHLSL
}
//Pass 1 - Tilt Shift Blur Low Quality
Pass
{
HLSLPROGRAM
#pragma vertex VertDefault
#pragma fragment FragTiltShiftLQ
ENDHLSL
}
//Pass 2 - Blend
Pass
{
HLSLPROGRAM
#pragma vertex VertDefault
#pragma fragment FragBlend
ENDHLSL
}
}
}