forked from lettier/3d-game-shaders-for-beginners
-
Notifications
You must be signed in to change notification settings - Fork 1
/
depth-of-field.frag
51 lines (36 loc) · 1.05 KB
/
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
47
48
49
50
51
/*
(C) 2019 David Lettier
lettier.com
*/
#version 150
uniform sampler2D positionTexture;
uniform sampler2D noiseTexture;
uniform sampler2D focusTexture;
uniform sampler2D outOfFocusTexture;
uniform vec2 mouseFocusPoint;
uniform vec2 nearFar;
uniform vec2 enabled;
out vec4 fragColor;
out vec4 fragColor1;
void main() {
float minDistance = 8.0;
float maxDistance = 12.0;
float far = nearFar.y;
vec2 texSize = textureSize(focusTexture, 0).xy;
vec2 texCoord = gl_FragCoord.xy / texSize;
vec4 focusColor = texture(focusTexture, texCoord);
fragColor = focusColor;
if (enabled.x != 1) { return; }
vec4 position = texture(positionTexture, texCoord);
if (position.a <= 0) { fragColor1 = vec4(1.0); return; }
vec4 outOfFocusColor = texture(outOfFocusTexture, texCoord);
vec4 focusPoint = texture(positionTexture, mouseFocusPoint);
float blur =
smoothstep
( minDistance
, maxDistance
, length(position - focusPoint)
);
fragColor = mix(focusColor, outOfFocusColor, blur);
fragColor1 = vec4(blur);
}