forked from lettier/3d-game-shaders-for-beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
motion-blur.frag
72 lines (51 loc) · 1.49 KB
/
motion-blur.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
(C) 2020 David Lettier
lettier.com
*/
#version 150
uniform sampler2D positionTexture;
uniform sampler2D colorTexture;
uniform mat4 previousViewWorldMat;
uniform mat4 worldViewMat;
uniform mat4 lensProjection;
uniform vec2 motionBlurEnabled;
uniform vec2 parameters;
out vec4 fragColor;
void main() {
int size = int(parameters.x);
float separation = parameters.y;
vec2 texSize = textureSize(colorTexture, 0).xy;
vec2 texCoord = gl_FragCoord.xy / texSize;
fragColor = texture(colorTexture, texCoord);
vec4 position1 = texture(positionTexture, texCoord);
if (size <= 0 || separation <= 0.0 || motionBlurEnabled.x != 1 || position1.a <= 0.0) { return; }
vec4 position0 = worldViewMat * previousViewWorldMat * position1;
position0 = lensProjection * position0;
position0.xyz /= position0.w;
position0.xy = position0.xy * 0.5 + 0.5;
position1 = lensProjection * position1;
position1.xyz /= position1.w;
position1.xy = position1.xy * 0.5 + 0.5;
vec2 direction = position1.xy - position0.xy;
if (length(direction) <= 0.0) { return; }
direction.xy *= separation;
vec2 forward = texCoord;
vec2 backward = texCoord;
float count = 1.0;
for (int i = 0; i < size; ++i) {
forward += direction;
backward -= direction;
fragColor +=
texture
( colorTexture
, forward
);
fragColor +=
texture
( colorTexture
, backward
);
count += 2.0;
}
fragColor /= count;
}