forked from crosire/reshade-shaders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TiltShift.fx
164 lines (146 loc) · 3.94 KB
/
TiltShift.fx
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
/*
Tilt-Shift PS v1.1.3 (c) 2018 Jacob Maximilian Fober,
(based on TiltShift effect (c) 2016 kingeric1992)
This work is licensed under the Creative Commons
Attribution-ShareAlike 4.0 International License.
To view a copy of this license, visit
http://creativecommons.org/licenses/by-sa/4.0/.
*/
////////////
/// MENU ///
////////////
#include "ReShadeUI.fxh"
uniform bool Line <
ui_label = "Show Center Line";
> = false;
uniform int Axis < __UNIFORM_SLIDER_INT1
ui_label = "Angle";
#if __RESHADE__ < 40000
ui_step = 1;
#endif
ui_min = -89; ui_max = 90;
> = 0;
uniform float Offset < __UNIFORM_SLIDER_FLOAT1
ui_min = -1.41; ui_max = 1.41; ui_step = 0.01;
> = 0.05;
uniform float BlurCurve < __UNIFORM_SLIDER_FLOAT1
ui_label = "Blur Curve";
ui_min = 1.0; ui_max = 5.0; ui_step = 0.01;
ui_label = "Blur Curve";
> = 1.0;
uniform float BlurMultiplier < __UNIFORM_SLIDER_FLOAT1
ui_label = "Blur Multiplier";
ui_min = 0.0; ui_max = 100.0; ui_step = 0.2;
> = 6.0;
// First pass render target, to make sure Alpha channel exists
texture TiltShiftTarget { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RGBA8; };
sampler TiltShiftSampler { Texture = TiltShiftTarget; };
//////////////
/// SHADER ///
//////////////
#include "ReShade.fxh"
void TiltShiftPass1PS(float4 vpos : SV_Position, float2 UvCoord : TEXCOORD, out float4 Image : SV_Target)
{
const float Weight[11] =
{
0.082607,
0.080977,
0.076276,
0.069041,
0.060049,
0.050187,
0.040306,
0.031105,
0.023066,
0.016436,
0.011254
};
// Grab screen texture
Image.rgb = tex2D(ReShade::BackBuffer, UvCoord).rgb;
// Correct Aspect Ratio
float2 UvCoordAspect = UvCoord;
UvCoordAspect.y += ReShade::AspectRatio * 0.5 - 0.5;
UvCoordAspect.y /= ReShade::AspectRatio;
// Center coordinates
UvCoordAspect = UvCoordAspect * 2.0 - 1.0;
// Tilt vector
float Angle = radians(-Axis);
float2 TiltVector = float2(sin(Angle), cos(Angle));
// Blur mask
float BlurMask = abs(dot(TiltVector, UvCoordAspect) + Offset);
BlurMask = max(0.0, min(1.0, BlurMask));
// Set alpha channel
Image.a = BlurMask;
BlurMask = pow(Image.a, BlurCurve);
// Horizontal gaussian blur
if(BlurMask > 0)
{
float UvOffset = ReShade::PixelSize.x * BlurMask * BlurMultiplier;
Image.rgb *= Weight[0];
[unroll]
for (int i = 1; i < 11; i++)
{
float SampleOffset = i * UvOffset;
Image.rgb += (
tex2Dlod(ReShade::BackBuffer, float4(UvCoord.xy + float2(SampleOffset, 0.0), 0.0, 0.0)).rgb
+ tex2Dlod(ReShade::BackBuffer, float4(UvCoord.xy - float2(SampleOffset, 0.0), 0.0, 0.0)).rgb
) * Weight[i];
}
}
}
void TiltShiftPass2PS(float4 vpos : SV_Position, float2 UvCoord : TEXCOORD, out float4 Image : SV_Target)
{
const float Weight[11] =
{
0.082607,
0.080977,
0.076276,
0.069041,
0.060049,
0.050187,
0.040306,
0.031105,
0.023066,
0.016436,
0.011254
};
// Grab second pass screen texture
Image = tex2D(TiltShiftSampler, UvCoord);
// Blur mask
float BlurMask = pow(abs(Image.a), BlurCurve);
// Vertical gaussian blur
if(BlurMask > 0)
{
float UvOffset = ReShade::PixelSize.y * BlurMask * BlurMultiplier;
Image.rgb *= Weight[0];
[unroll]
for (int i = 1; i < 11; i++)
{
float SampleOffset = i * UvOffset;
Image.rgb += (
tex2Dlod(TiltShiftSampler, float4(UvCoord.xy + float2(0.0, SampleOffset), 0.0, 0.0)).rgb
+ tex2Dlod(TiltShiftSampler, float4(UvCoord.xy - float2(0.0, SampleOffset), 0.0, 0.0)).rgb
) * Weight[i];
}
}
// Draw red line
// Image IS Red IF (Line IS True AND Image.a < 0.01), ELSE Image IS Image
Image.rgb = (Line && Image.a < 0.01) ? float3(1.0, 0.0, 0.0) : Image.rgb;
}
//////////////
/// OUTPUT ///
//////////////
technique TiltShift < ui_label = "Tilt Shift"; >
{
pass AlphaAndHorizontalGaussianBlur
{
VertexShader = PostProcessVS;
PixelShader = TiltShiftPass1PS;
RenderTarget = TiltShiftTarget;
}
pass VerticalGaussianBlurAndRedLine
{
VertexShader = PostProcessVS;
PixelShader = TiltShiftPass2PS;
}
}