Space Sim
Loading...
Searching...
No Matches
water.frag
Go to the documentation of this file.
1#version 460
2
8layout(location = 0)out vec4 fragColor;
9layout(std140, binding = 0) uniform CameraData {
10 vec4 cam_pos;
11 vec4 cam_vel;
13 mat4 view;
14 mat4 inv_projection;
15 mat4 inv_view;
16};
17layout(std140, binding = 2) uniform RenderingData {
18 ivec4 resolution;
19};
24in float vDist;
30
31// out vec4 fragColor;
32
33
34
35#define MAX_LAYERS 24
36
37struct WarpLayer
38{
40 float freq0;
42 float freq1;
44 float freq2;
45 float amp;
46 float pad0;
47 float pad1;
48 float pad3;
49};
50
51struct PerlinLayer
52{
53 float freq;
54 float amp;
55 float pad0;
56 float pad1;
57};
58
59struct FBMLayer
60{
61 float freq;
62 float amp;
63 float pad0;
64 float pad1;
65};
66
67layout(std430, binding=13) buffer PlanetData {
68
69 int warp_count;
70 int perlin_count;
71 int fbm_count;
72 int pad0;
75 WarpLayer warp_layers[MAX_LAYERS];
76 PerlinLayer perlin_layers[MAX_LAYERS];
77 FBMLayer fbm_layers[MAX_LAYERS];
78
79};
80
81
82const vec3 water_color = vec3(0.01, 0.1, 0.3);
83const vec3 sky_color = vec3(0.3, 0.3, 0.5);
84
85#define N 128
86
87uniform sampler2D tex;
88uniform sampler2D uFFT;
89uniform sampler2D uSlope;
90uniform sampler2D uSky;
91uniform sampler2D normal_map;
92uniform samplerCube uCubemap;
93uniform float uTime;
94uniform sampler2D uDepth;
95uniform sampler2D uScene;
96uniform sampler2D uExp;
97uniform sampler3D fbm_tex;
98uniform sampler2D shadow_map;
99
100const vec3 tX = vec3(0, 0, 1);
101const vec3 bX = vec3(0, 1, 0);
102const vec3 nXw = vec3(1, 0, 0);
103
104const vec3 tY = vec3(1, 0, 0);
105const vec3 bY = vec3(0, 0, 1);
106const vec3 nYw = vec3(0, 1, 0);
107
108const vec3 tZ = vec3(1, 0, 0);
109const vec3 bZ = vec3(0, 1, 0);
110const vec3 nZw = vec3(0, 0, 1);
111const int water_scale = 128;
112
113float getSun(vec3 dir, vec3 ld) {
114 return pow(max(0.0, dot(dir, normalize(ld))), 720.0) * 210.0;
115}
116
117int L = 2000;
119
120vec4 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}
132float 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
141float mod289(float x){return x - floor(x * (1.0 / 289.0)) * 289.0;}
142vec4 mod289(vec4 x){return x - floor(x * (1.0 / 289.0)) * 289.0;}
143vec4 perm(vec4 x){return mod289(((x * 34.0) + 1.0) * x);}
144
145float 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
168float 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
186float 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
215void 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}
out vec4 fragColor
in vec3 vPos
in vec3 vNormal
in vec3 vViewDir
layout(local_size_x=16, local_size_y=16, local_size_z=1) in
uniform samplerCube uCubemap
Definition clouds.frag:7
#define NUM_OCTAVES
Definition density.comp:111
float mod289(float x)
Definition density.comp:112
vec4 perm(vec4 x)
Definition density.comp:114
float fbm(vec3 x)
Definition density.comp:139
vec3 sun_dir
Definition main.c:237
vec3 sun_color
Definition main.c:238
float * fbm_tex
Definition init.c:346
int main(int argc, char *argv[])
Definition main.c:1726
float uTime
Definition main.c:775
in float vDist
Definition main.frag:14
uniform samplerCube uSky
Definition main.frag:22
float linearize(float z)
Definition main.frag:138
float clamp(float a, float b, float c)
Definition mathf.h:147
uniform sampler2D normal_map
Definition mesh.frag:61
in vec4 vFragPosLightSpace
Definition mesh.frag:12
float shadow_calculation(vec4 pos, float diff)
Definition mesh.frag:162
const vec3 tX
Definition mesh.frag:149
const vec3 tY
Definition mesh.frag:153
const vec3 bX
Definition mesh.frag:150
const vec3 nZw
Definition mesh.frag:159
const vec3 nYw
Definition mesh.frag:155
const vec3 bY
Definition mesh.frag:154
const vec3 tZ
Definition mesh.frag:157
uniform sampler2D shadow_map
Definition mesh.frag:65
const vec3 bZ
Definition mesh.frag:158
const vec3 nXw
Definition mesh.frag:151
uniform mat4 light_proj
Definition mesh.vert:18
uniform mat4 light_view
Definition mesh.vert:17
uniform sampler2D uScene
in vec2 vUV
uniform sampler2D uDepth
uniform mat4 projection
Definition shadow.vert:8
uniform mat4 view
Definition shadow.vert:9
float x
Definition types.h:45
float w
Definition types.h:47
float x
Definition main.h:5
float noise(vec3 p)
Definition density.comp:116
float y
Definition types.h:45
float y
Definition types.h:47
float y
Definition main.h:5
float z
Definition main.h:5
#define MAX_LAYERS
Definition types.h:407
Definition types.h:49
Definition types.h:51
Definition types.h:45
Definition main.h:5
Definition types.h:47
#define L
Definition water.comp:15
vec3 offset2
Definition types.h:378
float pad1
Definition types.h:403
const vec3 sky_color
Definition water.frag:83
float amp
Definition types.h:401
int NUM_TILES_PER_WATER_SCALE
Definition water.frag:118
uniform sampler2D uFFT
Definition water.frag:88
float freq0
Definition types.h:375
vec3 offset1
Definition types.h:376
float freq
Definition types.h:400
float amp
Definition types.h:391
in vec3 rvPos
Definition water.frag:22
float pad3
Definition types.h:383
float freq1
Definition types.h:377
in vec3 rvNormal
Definition water.frag:23
float pad1
Definition types.h:382
float pad0
Definition types.h:392
float freq2
Definition types.h:379
vec3 offset0
Definition types.h:374
float pad1
Definition types.h:393
vec4 sampleFFT(sampler2D tex, vec2 uv)
Definition water.frag:120
uniform sampler2D uExp
Definition water.frag:96
uniform sampler2D tex
Definition water.frag:87
float pad0
Definition types.h:381
const vec3 water_color
Definition water.frag:82
float amp
Definition types.h:380
uniform sampler2D uSlope
Definition water.frag:89
float freq
Definition types.h:390
float pad0
Definition types.h:402
float getSun(vec3 dir, vec3 ld)
Definition water.frag:113
const int water_scale
Definition water.frag:111