Space Sim
Toggle main menu visibility
Loading...
Searching...
No Matches
src
render
water.frag
Go to the documentation of this file.
1
#version 460
2
8
layout
(location = 0)out
vec4
fragColor
;
9
layout
(std140, binding = 0) uniform CameraData {
10
vec4
cam_pos;
11
vec4
cam_vel;
12
mat4
projection
;
13
mat4
view
;
14
mat4
inv_projection;
15
mat4
inv_view;
16
};
17
layout
(std140, binding = 2) uniform RenderingData {
18
ivec4
resolution;
19
};
20
in
vec3
vPos
;
21
in
vec3
vNormal
;
22
in
vec3
rvPos
;
23
in
vec3
rvNormal
;
24
in
float
vDist
;
25
in
vec2
vUV
;
26
in
vec3
vViewDir
;
27
uniform
mat4
light_proj
;
28
uniform
mat4
light_view
;
29
in
vec4
vFragPosLightSpace
;
30
31
// out vec4 fragColor;
32
33
34
35
#define MAX_LAYERS 24
36
37
struct
WarpLayer
38
{
39
vec3
offset0
;
40
float
freq0
;
41
vec3
offset1
;
42
float
freq1
;
43
vec3
offset2
;
44
float
freq2
;
45
float
amp
;
46
float
pad0
;
47
float
pad1
;
48
float
pad3
;
49
};
50
51
struct
PerlinLayer
52
{
53
float
freq
;
54
float
amp
;
55
float
pad0
;
56
float
pad1
;
57
};
58
59
struct
FBMLayer
60
{
61
float
freq
;
62
float
amp
;
63
float
pad0
;
64
float
pad1
;
65
};
66
67
layout
(std430, binding=13) buffer
PlanetData
{
68
69
int
warp_count;
70
int
perlin_count;
71
int
fbm_count;
72
int
pad0;
73
vec4
sun_dir
;
74
vec4
sun_color
;
75
WarpLayer
warp_layers[
MAX_LAYERS
];
76
PerlinLayer
perlin_layers[
MAX_LAYERS
];
77
FBMLayer
fbm_layers[
MAX_LAYERS
];
78
79
};
80
81
82
const
vec3
water_color
=
vec3
(0.01, 0.1, 0.3);
83
const
vec3
sky_color
=
vec3
(0.3, 0.3, 0.5);
84
85
#define N 128
86
87
uniform sampler2D
tex
;
88
uniform sampler2D
uFFT
;
89
uniform sampler2D
uSlope
;
90
uniform sampler2D
uSky
;
91
uniform sampler2D
normal_map
;
92
uniform samplerCube
uCubemap
;
93
uniform
float
uTime
;
94
uniform sampler2D
uDepth
;
95
uniform sampler2D
uScene
;
96
uniform sampler2D
uExp
;
97
uniform sampler3D
fbm_tex
;
98
uniform sampler2D
shadow_map
;
99
100
const
vec3
tX
=
vec3
(0, 0, 1);
101
const
vec3
bX
=
vec3
(0, 1, 0);
102
const
vec3
nXw
=
vec3
(1, 0, 0);
103
104
const
vec3
tY
=
vec3
(1, 0, 0);
105
const
vec3
bY
=
vec3
(0, 0, 1);
106
const
vec3
nYw
=
vec3
(0, 1, 0);
107
108
const
vec3
tZ
=
vec3
(1, 0, 0);
109
const
vec3
bZ
=
vec3
(0, 1, 0);
110
const
vec3
nZw
=
vec3
(0, 0, 1);
111
const
int
water_scale
= 128;
112
113
float
getSun
(
vec3
dir,
vec3
ld) {
114
return
pow(max(0.0, dot(dir, normalize(ld))), 720.0) * 210.0;
115
}
116
117
int
L
= 2000;
118
int
NUM_TILES_PER_WATER_SCALE
= 4;
119
120
vec4
sampleFFT
(sampler2D
tex
,
vec2
uv)
121
{
122
vec2
texel = 1.0 / textureSize(
tex
, 0);
123
124
vec4
c = texture(
tex
, uv) * 0.5;
125
c += texture(
tex
, uv +
vec2
(texel.
x
, 0)) * 0.125;
126
c += texture(
tex
, uv -
vec2
(texel.
x
, 0)) * 0.125;
127
c += texture(
tex
, uv +
vec2
(0, texel.
y
)) * 0.125;
128
c += texture(
tex
, uv -
vec2
(0, texel.
y
)) * 0.125;
129
130
return
c;
131
}
132
float
linearize
(
float
z)
133
{
134
float
near = 0.01;
135
float
far = 100000.0;
136
return
(2.0 * near) / (far + near - z * (far - near));
137
}
138
139
140
#define NUM_OCTAVES 5
141
float
mod289
(
float
x){
return
x - floor(x * (1.0 / 289.0)) * 289.0;}
142
vec4
mod289
(
vec4
x){
return
x - floor(x * (1.0 / 289.0)) * 289.0;}
143
vec4
perm
(
vec4
x){
return
mod289
(((x * 34.0) + 1.0) * x);}
144
145
float
noise
(
vec3
p){
146
vec3
a = floor(p);
147
vec3
d = p - a;
148
d = d * d * (3.0 - 2.0 * d);
149
150
vec4
b = a.xxyy +
vec4
(0.0, 1.0, 0.0, 1.0);
151
vec4
k1 =
perm
(b.xyxy);
152
vec4
k2 =
perm
(k1.xyxy + b.zzww);
153
154
vec4
c = k2 + a.zzzz;
155
vec4
k3 =
perm
(c);
156
vec4
k4 =
perm
(c + 1.0);
157
158
vec4
o1 = fract(k3 * (1.0 / 41.0));
159
vec4
o2 = fract(k4 * (1.0 / 41.0));
160
161
vec4
o3 = o2 * d.
z
+ o1 * (1.0 - d.
z
);
162
vec2
o4 = o3.yw * d.
x
+ o3.xz * (1.0 - d.
x
);
163
164
return
o4.
y
* d.
y
+ o4.x * (1.0 - d.
y
);
165
}
166
167
168
float
fbm
(
vec3
x) {
169
float
v = 0.0;
170
float
a = 0.5;
171
// float f = 1.0;
172
vec3
shift =
vec3
(100);
173
for
(
int
i = 0; i <
NUM_OCTAVES
; ++i) {
174
v += a *
noise
(x);
175
x = x * 2.0 + shift;
176
// f *= 2.5;
177
a *= 0.45;
178
}
179
return
v;
180
}
181
182
183
184
185
186
float
shadow_calculation
(
vec4
pos,
float
diff)
187
{
188
vec3
proj_coords = pos.xyz / pos.
w
;
189
proj_coords = proj_coords * 0.5 + 0.5;
190
float
closest_depth = texture(
shadow_map
, proj_coords.xy).r;
191
float
current_depth = proj_coords.
z
;
192
193
194
float
bias = max(0.05f * (diff), 0.005f);
195
float
shadow = 0.0f;
196
vec2
texel_size = 1.0 / textureSize(
shadow_map
, 0);
197
for
(
int
y = -1; y <= 1; y++)
198
{
199
200
for
(
int
x = -1; x <= 1; x++)
201
{
202
203
float
pcfDepth = texture(
shadow_map
, proj_coords.xy +
vec2
(x, y) * texel_size).r;
204
205
shadow += (current_depth - bias > pcfDepth ? 1.0 : 0.0);
206
}
207
}
208
shadow /= 9;
209
if
(proj_coords.
z
> 1.0) shadow = 0.0;
210
211
return
shadow;
212
}
213
214
215
void
main
()
216
{
217
218
vec2
uv =
vUV
;
219
vec3
n =
vNormal
;
220
vec2
screenUV = gl_FragCoord.xy / resolution.xy;
221
vec4
sc_offset = texture(
uScene
, screenUV + n.xy * 0.02);
222
float
waterDepth =
linearize
(gl_FragCoord.z);
223
float
depth_diff =
clamp
((sc_offset.a - waterDepth) * 1000, 0, 500);
224
float
absorption_coeff = 0.2;
225
float
absorption = exp2((-depth_diff * absorption_coeff) * 1.442695);
226
vec3
view_dir =
vViewDir
;
227
float
f = 1.0 -
clamp
(dot(n, view_dir), 0.0, 1.0);
228
float
fresnel = f * f;
229
fresnel *= fresnel;
230
231
232
vec3
ld =
sun_dir
.xyz;
233
ld = normalize(-ld);
234
235
float
diffuse = max(dot(n, ld), 0.0);
236
237
vec3
halfway = normalize(ld + view_dir);
238
float
ndh = max(dot(n, halfway), 0.0);
239
float
r = 0.05;
240
float
spec = ndh * ndh;
241
spec *= spec;
242
spec *= spec;
243
vec3
R = reflect(-view_dir, n);
244
245
246
R.
z
= abs(R.
z
);
247
mat3 rotation = mat3(
248
vec3
(1, 0, 0),
249
vec3
(0, 0, 1),
250
vec3
(0, -1, 0)
251
);
252
R *= rotation;
253
vec3
reflection = texture(
uCubemap
, R).rgb;
254
vec3
refraction = mix(
water_color
, sc_offset.rgb, 0.1);
255
float
thin = smoothstep(0.7, 1.0, 1.0 - dot(n,
vec3
(0,0,1)));
256
vec3
thinColor =
vec3
(0.1, 0.5, 0.3);
257
258
refraction += thin * thinColor;
259
260
reflection += spec *
sun_color
.rgb;
261
262
float
shadow =
shadow_calculation
(
vFragPosLightSpace
, diffuse);
263
vec3
color = refraction * (1-fresnel) + reflection * fresnel;
264
265
fragColor
=
vec4
(color * (1-shadow), waterDepth);
266
}
fragColor
out vec4 fragColor
Definition
atmosphere.frag:9
vPos
in vec3 vPos
Definition
atmosphere.frag:10
vNormal
in vec3 vNormal
Definition
atmosphere.frag:11
vViewDir
in vec3 vViewDir
Definition
atmosphere.frag:15
layout
layout(local_size_x=16, local_size_y=16, local_size_z=1) in
uCubemap
uniform samplerCube uCubemap
Definition
clouds.frag:7
NUM_OCTAVES
#define NUM_OCTAVES
Definition
density.comp:111
mod289
float mod289(float x)
Definition
density.comp:112
perm
vec4 perm(vec4 x)
Definition
density.comp:114
fbm
float fbm(vec3 x)
Definition
density.comp:139
sun_dir
vec3 sun_dir
Definition
main.c:237
sun_color
vec3 sun_color
Definition
main.c:238
fbm_tex
float * fbm_tex
Definition
init.c:346
main
int main(int argc, char *argv[])
Definition
main.c:1726
uTime
float uTime
Definition
main.c:775
vDist
in float vDist
Definition
main.frag:14
uSky
uniform samplerCube uSky
Definition
main.frag:22
linearize
float linearize(float z)
Definition
main.frag:138
clamp
float clamp(float a, float b, float c)
Definition
mathf.h:147
normal_map
uniform sampler2D normal_map
Definition
mesh.frag:61
vFragPosLightSpace
in vec4 vFragPosLightSpace
Definition
mesh.frag:12
shadow_calculation
float shadow_calculation(vec4 pos, float diff)
Definition
mesh.frag:162
tX
const vec3 tX
Definition
mesh.frag:149
tY
const vec3 tY
Definition
mesh.frag:153
bX
const vec3 bX
Definition
mesh.frag:150
nZw
const vec3 nZw
Definition
mesh.frag:159
nYw
const vec3 nYw
Definition
mesh.frag:155
bY
const vec3 bY
Definition
mesh.frag:154
tZ
const vec3 tZ
Definition
mesh.frag:157
shadow_map
uniform sampler2D shadow_map
Definition
mesh.frag:65
bZ
const vec3 bZ
Definition
mesh.frag:158
nXw
const vec3 nXw
Definition
mesh.frag:151
light_proj
uniform mat4 light_proj
Definition
mesh.vert:18
light_view
uniform mat4 light_view
Definition
mesh.vert:17
uScene
uniform sampler2D uScene
Definition
postprocess.frag:6
vUV
in vec2 vUV
Definition
postprocess.frag:3
uDepth
uniform sampler2D uDepth
Definition
postprocess.frag:7
projection
uniform mat4 projection
Definition
shadow.vert:8
view
uniform mat4 view
Definition
shadow.vert:9
vec2::x
float x
Definition
types.h:45
vec4::w
float w
Definition
types.h:47
vec3::x
float x
Definition
main.h:5
noise
float noise(vec3 p)
Definition
density.comp:116
vec2::y
float y
Definition
types.h:45
vec4::y
float y
Definition
types.h:47
vec3::y
float y
Definition
main.h:5
vec3::z
float z
Definition
main.h:5
MAX_LAYERS
#define MAX_LAYERS
Definition
types.h:407
PlanetData
Definition
types.h:409
ivec4
Definition
types.h:49
mat4
Definition
types.h:51
vec2
Definition
types.h:45
vec3
Definition
main.h:5
vec4
Definition
types.h:47
L
#define L
Definition
water.comp:15
WarpLayer::offset2
vec3 offset2
Definition
types.h:378
FBMLayer::pad1
float pad1
Definition
types.h:403
sky_color
const vec3 sky_color
Definition
water.frag:83
FBMLayer::amp
float amp
Definition
types.h:401
NUM_TILES_PER_WATER_SCALE
int NUM_TILES_PER_WATER_SCALE
Definition
water.frag:118
uFFT
uniform sampler2D uFFT
Definition
water.frag:88
WarpLayer::freq0
float freq0
Definition
types.h:375
WarpLayer::offset1
vec3 offset1
Definition
types.h:376
FBMLayer::freq
float freq
Definition
types.h:400
PerlinLayer::amp
float amp
Definition
types.h:391
rvPos
in vec3 rvPos
Definition
water.frag:22
WarpLayer::pad3
float pad3
Definition
types.h:383
WarpLayer::freq1
float freq1
Definition
types.h:377
rvNormal
in vec3 rvNormal
Definition
water.frag:23
WarpLayer::pad1
float pad1
Definition
types.h:382
PerlinLayer::pad0
float pad0
Definition
types.h:392
WarpLayer::freq2
float freq2
Definition
types.h:379
WarpLayer::offset0
vec3 offset0
Definition
types.h:374
PerlinLayer::pad1
float pad1
Definition
types.h:393
sampleFFT
vec4 sampleFFT(sampler2D tex, vec2 uv)
Definition
water.frag:120
uExp
uniform sampler2D uExp
Definition
water.frag:96
tex
uniform sampler2D tex
Definition
water.frag:87
WarpLayer::pad0
float pad0
Definition
types.h:381
water_color
const vec3 water_color
Definition
water.frag:82
WarpLayer::amp
float amp
Definition
types.h:380
uSlope
uniform sampler2D uSlope
Definition
water.frag:89
PerlinLayer::freq
float freq
Definition
types.h:390
FBMLayer::pad0
float pad0
Definition
types.h:402
getSun
float getSun(vec3 dir, vec3 ld)
Definition
water.frag:113
water_scale
const int water_scale
Definition
water.frag:111
FBMLayer
Definition
types.h:398
PerlinLayer
Definition
types.h:388
WarpLayer
Definition
types.h:372
Generated by
1.18.0