forked from lettier/3d-game-shaders-for-beginners
/
depth-of-field.frag
46 lines (35 loc) 路 903 Bytes
/
depth-of-field.frag
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
/*
(C) 2019 David Lettier
lettier.com
*/
#version 140
uniform sampler2D positionTexture;
uniform sampler2D focusTexture;
uniform sampler2D outOfFocusTexture;
uniform vec2 focalLength;
uniform vec2 enabled;
out vec4 fragColor;
void main() {
float focalLengthSharpness = 100;
float blurRate = 6;
vec2 texSize = textureSize(focusTexture, 0).xy;
vec2 texCoord = gl_FragCoord.xy / texSize;
vec4 focusColor = texture(focusTexture, texCoord);
vec4 outOfFocusColor = texture(outOfFocusTexture, texCoord);
vec4 position = texture(positionTexture, texCoord);
float blur =
clamp
( pow
( blurRate
, abs(position.y - focalLength.x)
)
/ focalLengthSharpness
, 0
, 1
);
if (enabled.x == 1) {
fragColor = mix(focusColor, outOfFocusColor, blur);
} else {
fragColor = focusColor;
}
}