forked from lettier/3d-game-shaders-for-beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfog.frag
93 lines (72 loc) · 2.09 KB
/
fog.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
(C) 2019 David Lettier
lettier.com
*/
#version 150
uniform vec2 pi;
uniform vec2 gamma;
uniform vec4 backgroundColor0;
uniform vec4 backgroundColor1;
uniform sampler2D positionTexture0;
uniform sampler2D positionTexture1;
uniform sampler2D smokeMaskTexture;
uniform vec3 origin;
uniform vec2 nearFar;
uniform vec2 sunPosition;
uniform vec2 enabled;
out vec4 fragColor;
void main() {
float fogMin = 0.00;
float fogMax = 0.97;
if (enabled.x != 1) { fragColor = vec4(0); return; }
vec2 texSize = textureSize(positionTexture0, 0).xy;
vec2 texCoord = gl_FragCoord.xy / texSize;
vec4 smokeMask = texture(smokeMaskTexture, texCoord);
vec4 position0 = texture(positionTexture0, texCoord);
position0.y -= origin.y;
float near = nearFar.x;
float far = nearFar.y;
vec4 position1 = texture(positionTexture1, texCoord);
position1.y -= origin.y;
if (position1.a <= 0) { position1.y = far; }
vec4 position = position1;
if (position0.a <= 0 && smokeMask.r > 0) {
position.y = mix(far, position1.y, smokeMask.r);
} else if (position0.a > 0 && smokeMask.r > 0) {
position.xyz = mix(position0.xyz, position1.xyz, smokeMask.r);
}
float random =
fract
( 10000
* sin
(
( gl_FragCoord.x
* 104729
+ gl_FragCoord.y
* 7639
)
* pi.y
)
);
vec4 backgroundColor0 = backgroundColor0;
vec4 backgroundColor1 = backgroundColor1;
backgroundColor0.rgb = pow(backgroundColor0.rgb, vec3(gamma.x));
backgroundColor1.rgb = pow(backgroundColor1.rgb, vec3(gamma.x));
vec4 color =
mix
( backgroundColor0
, backgroundColor1
, 1.0 - clamp(random * 0.1 + texCoord.y, 0.0, 1.0)
);
float sunPosition = max(0.2, -1 * sin(sunPosition.x * pi.y));
color.rgb *= sunPosition;
color.b = mix(color.b + 0.05, color.b, sunPosition);
float intensity =
clamp
( (position.y - near)
/ (far - near)
, fogMin
, fogMax
);
fragColor = vec4(color.rgb, intensity);
}