Debian Codegraph @

Shaders/ws30-water.frag

raw
1 // SPDX-FileCopyrightText: (C) 2022 Stuart Buchanan [email protected]
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 // Helper functions for WS30 water implementation, heavily based on the
5 // water-ALS-base.frag and waterr_ALS-high.frag
6
7 #version 130
8 #extension GL_EXT_texture_array : enable
9
10 // Hardcoded indexes into the texture atlas
11 const int ATLAS_INDEX_WATER = 0;
12 const int ATLAS_INDEX_WATER_REFLECTION = 1;
13 const int ATLAS_INDEX_WAVES_VERT10_NM = 2;
14 const int ATLAS_INDEX_WATER_SINE_NMAP = 3;
15 const int ATLAS_INDEX_WATER_REFLECTION_GREY = 4;
16 const int ATLAS_INDEX_SEA_FOAM = 5;
17 const int ATLAS_INDEX_PERLIN_NOISE_NM = 6;
18 const int ATLAS_INDEX_OCEAN_DEPTH = 7;
19 const int ATLAS_INDEX_GLOBAL_COLORS = 8;
20 const int ATLAS_INDEX_PACKICE_OVERLAY = 9;
21
22 // WS30 uniforms
23 uniform sampler2DArray textureArray;
24 uniform float ground_scattering;
25 uniform float overcast;
26 uniform float fg_tileWidth;
27 uniform float fg_tileHeight;
28
29 // Water.eff uniforms
30 uniform float sea_r;
31 uniform float sea_g;
32 uniform float sea_b;
33 uniform float osg_SimulationTime;
34 uniform float WindN;
35 uniform float WindE;
36 uniform float WaveFreq;
37 uniform float WaveAmp;
38 uniform float WaveSharp;
39 uniform float WaveAngle;
40 uniform float WaveFactor;
41 uniform float saturation;
42
43 // WS30 varying
44 varying vec3 relPos;
45
46 // Water.eff varying
47 varying float earthShade;
48 varying vec3 lightdir;
49 varying vec4 waterTex1;
50 varying vec4 waterTex2;
51 varying vec4 waterTex4;
52 varying vec3 specular_light;
53
54 //WS3.0 varying
55 varying float steepness;
56
57
58 /////// functions /////////
59
60 float getShadowing();
61 vec3 getClusteredLightsContribution(vec3 p, vec3 n, vec3 texel);
62
63 void rotationmatrix(in float angle, out mat4 rotmat)
64 {
65 rotmat = mat4( cos( angle ), -sin( angle ), 0.0, 0.0,
66 sin( angle ), cos( angle ), 0.0, 0.0,
67 0.0 , 0.0 , 1.0, 0.0,
68 0.0 , 0.0 , 0.0, 1.0 );
69 }
70
71 // wave functions ///////////////////////
72 struct Wave {
73 float freq; // 2*PI / wavelength
74 float amp; // amplitude
75 float phase; // speed * 2*PI / wavelength
76 vec2 dir;
77 };
78
79 Wave wave0 = Wave(1.0, 1.0, 0.5, vec2(0.97, 0.25));
80 Wave wave1 = Wave(2.0, 0.5, 1.3, vec2(0.97, -0.25));
81 Wave wave2 = Wave(1.0, 1.0, 0.6, vec2(0.95, -0.3));
82 Wave wave3 = Wave(2.0, 0.5, 1.4, vec2(0.99, 0.1));
83
84 float evaluateWave(in Wave w, in vec2 pos, in float t) {
85 return w.amp * sin( dot(w.dir, pos) * w.freq + t * w.phase);
86 }
87
88 // derivative of wave function
89 float evaluateWaveDeriv(in Wave w, in vec2 pos, in float t) {
90 return w.freq * w.amp * cos( dot(w.dir, pos)*w.freq + t*w.phase);
91 }
92
93 // sharp wave functions
94 float evaluateWaveSharp(in Wave w, in vec2 pos, in float t, in float k) {
95 return w.amp * pow(sin( dot(w.dir, pos)*w.freq + t*w.phase)* 0.5 + 0.5 , k);
96 }
97
98 float evaluateWaveDerivSharp(in Wave w, in vec2 pos, in float t, in float k) {
99 return k*w.freq*w.amp * pow(sin( dot(w.dir, pos)*w.freq + t*w.phase)* 0.5 + 0.5 , k - 1) * cos( dot(w.dir, pos)*w.freq + t*w.phase);
100 }
101
102 void sumWaves(in float angle, in float dangle, in float windScale, in float factor, out float ddx, float ddy) {
103 mat4 RotationMatrix;
104 float deriv;
105 vec4 P = waterTex1 * 1024;
106
107 rotationmatrix(radians(angle + dangle * windScale + 0.6 * sin(P.x * factor)), RotationMatrix);
108 P *= RotationMatrix;
109
110 P.y += evaluateWave(wave0, P.xz, osg_SimulationTime);
111 deriv = evaluateWaveDeriv(wave0, P.xz, osg_SimulationTime );
112 ddx = deriv * wave0.dir.x;
113 ddy = deriv * wave0.dir.y;
114
115 //P.y += evaluateWave(wave1, P.xz, osg_SimulationTime);
116 //deriv = evaluateWaveDeriv(wave1, P.xz, osg_SimulationTime);
117 //ddx += deriv * wave1.dir.x;
118 //ddy += deriv * wave1.dir.y;
119
120 P.y += evaluateWaveSharp(wave2, P.xz, osg_SimulationTime, WaveSharp);
121 deriv = evaluateWaveDerivSharp(wave2, P.xz, osg_SimulationTime, WaveSharp);
122 ddx += deriv * wave2.dir.x;
123 ddy += deriv * wave2.dir.y;
124
125 //P.y += evaluateWaveSharp(wave3, P.xz, osg_SimulationTime, WaveSharp);
126 //deriv = evaluateWaveDerivSharp(wave3, P.xz, osg_SimulationTime, WaveSharp);
127 //ddx += deriv * wave3.dir.x;
128 //ddy += deriv * wave3.dir.y;
129 }
130
131 vec4 generateWaterTexel()
132 {
133 vec4 texel;
134 float dist = length(relPos);
135 float tileScale = 1 / (fg_tileHeight + fg_tileWidth) / 2.0;
136
137 vec4 sca = vec4(0.005, 0.005, 0.005, 0.005) * tileScale;
138 vec4 sca2 = vec4(0.02, 0.02, 0.02, 0.02) * tileScale;
139 vec4 tscale = vec4(0.25, 0.25, 0.25, 0.25) / 10000.0 * tileScale;
140
141 mat4 RotationMatrix;
142
143 // compute direction to viewer
144 vec3 E = normalize(-relPos);
145
146 // compute direction to light source
147 vec3 L = normalize(lightdir);
148
149 // half vector
150 vec3 Hv = normalize(L + E);
151
152 vec3 Normal = vec3 (0.0, 0.0, 1.0);
153
154 const float water_shininess = 240.0;
155
156 float windEffect = sqrt( WindE*WindE + WindN*WindN ) * 0.6; //wind speed in kt
157 float windScale = 15.0/(3.0 + windEffect); //wave scale
158 float windEffect_low = 0.3 + 0.7 * smoothstep(0.0, 5.0, windEffect); //low windspeed wave filter
159 float waveRoughness = 0.01 + smoothstep(0.0, 40.0, windEffect); //wave roughness filter
160
161 float mixFactor = 0.2 + 0.02 * smoothstep(0.0, 50.0, windEffect);
162 mixFactor = clamp(mixFactor, 0.3, 0.8);
163
164 // there's no need to do wave patterns or foam for pixels which are so far away that we can't actually see them
165 // we only need detail in the near zone or where the sun reflection is
166
167 int detail_flag;
168 if ((dist > 15000.0) && (dot(normalize(vec3 (lightdir.x, lightdir.y, 0.0) ), normalize(relPos)) < 0.7 )) {detail_flag = 0;}
169 else {detail_flag = 1;}
170
171 // sine waves
172 float ddx, ddx1, ddx2, ddx3, ddy, ddy1, ddy2, ddy3;
173 float angle;
174
175 ddx = 0.0, ddy = 0.0;
176 ddx1 = 0.0, ddy1 = 0.0;
177 ddx2 = 0.0, ddy2 = 0.0;
178 ddx3 = 0.0, ddy3 = 0.0;
179
180 if (detail_flag == 1)
181 {
182 angle = 0.0;
183
184 wave0.freq = WaveFreq ;
185 wave0.amp = WaveAmp;
186 wave0.dir = vec2 (0.0, 1.0); //vec2(cos(radians(angle)), sin(radians(angle)));
187
188 angle -= 45;
189 wave1.freq = WaveFreq * 2.0 ;
190 wave1.amp = WaveAmp * 1.25;
191 wave1.dir = vec2(0.70710, -0.7071); //vec2(cos(radians(angle)), sin(radians(angle)));
192
193 angle += 30;
194 wave2.freq = WaveFreq * 3.5;
195 wave2.amp = WaveAmp * 0.75;
196 wave2.dir = vec2(0.96592, -0.2588);// vec2(cos(radians(angle)), sin(radians(angle)));
197
198 angle -= 50;
199 wave3.freq = WaveFreq * 3.0 ;
200 wave3.amp = WaveAmp * 0.75;
201 wave3.dir = vec2(0.42261, -0.9063); //vec2(cos(radians(angle)), sin(radians(angle)));
202
203 // sum waves
204 sumWaves(WaveAngle, -1.5, windScale, WaveFactor, ddx, ddy);
205 sumWaves(WaveAngle, 1.5, windScale, WaveFactor, ddx1, ddy1);
206
207 //reset the waves
208 angle = 0.0;
209 float waveamp = WaveAmp * 0.75;
210
211 wave0.freq = WaveFreq ;
212 wave0.amp = waveamp;
213 wave0.dir = vec2 (0.0, 1.0); //vec2(cos(radians(angle)), sin(radians(angle)));
214
215 angle -= 20;
216 wave1.freq = WaveFreq * 2.0 ;
217 wave1.amp = waveamp * 1.25;
218 wave1.dir = vec2(0.93969, -0.34202);// vec2(cos(radians(angle)), sin(radians(angle)));
219
220 angle += 35;
221 wave2.freq = WaveFreq * 3.5;
222 wave2.amp = waveamp * 0.75;
223 wave2.dir = vec2(0.965925, 0.25881); //vec2(cos(radians(angle)), sin(radians(angle)));
224
225 angle -= 45;
226 wave3.freq = WaveFreq * 3.0 ;
227 wave3.amp = waveamp * 0.75;
228 wave3.dir = vec2(0.866025, -0.5); //vec2(cos(radians(angle)), sin(radians(angle)));
229 }
230 // end sine stuff
231
232 //cover = 5.0 * smoothstep(0.6, 1.0, scattering);
233 //cover = 5.0 * ground_scattering;
234
235 vec4 viewt = normalize(waterTex4);
236 vec2 st = vec2(waterTex2 * tscale * windScale);
237 vec4 disdis = texture(textureArray, vec3(st, ATLAS_INDEX_WATER_SINE_NMAP)) * 2.0 - 1.0;
238
239 vec4 vNorm;
240
241 //normalmaps
242 st = vec2(waterTex1 + disdis * sca2) * windScale;
243 vec4 nmap = texture(textureArray, vec3(st, ATLAS_INDEX_WAVES_VERT10_NM)) * 2.0 - 1.0;
244 vec4 nmap1 = texture(textureArray, vec3(st, ATLAS_INDEX_PERLIN_NOISE_NM)) * 2.0 - 1.0;
245
246 rotationmatrix(radians(3.0 * sin(osg_SimulationTime * 0.0075)), RotationMatrix);
247 st = vec2(waterTex2 * RotationMatrix * tscale) * windScale;
248 nmap += texture(textureArray, vec3(st, ATLAS_INDEX_WAVES_VERT10_NM)) * 2.0 - 1.0;
249
250 nmap *= windEffect_low;
251 nmap1 *= windEffect_low;
252
253 // mix water and noise, modulated by factor
254 vNorm = normalize(mix(nmap, nmap1, mixFactor) * waveRoughness);
255 vNorm.r += ddx + ddx1 + ddx2 + ddx3;
256
257 //if (normalmap_dds > 0) {vNorm = -vNorm;} //dds fix
258
259 //load reflection
260 vec4 refl ;
261 refl.r = sea_r;
262 refl.g = sea_g;
263 refl.b = sea_b;
264 refl.a = 1.0;
265
266 float intensity;
267 // de-saturate for reduced light
268 refl.rgb = mix(refl.rgb, vec3 (0.248, 0.248, 0.248), 1.0 - smoothstep(0.1, 0.8, ground_scattering));
269
270 // de-saturate light for overcast haze
271 intensity = length(refl.rgb);
272 refl.rgb = mix(refl.rgb, intensity * vec3 (1.0, 1.0, 1.0), 0.5 * smoothstep(0.1, 0.9, overcast));
273
274 vec3 N;
275 st = vec2(waterTex1 + disdis * sca2) * windScale;
276 vec3 N0 = vec3(texture(textureArray, vec3(st, ATLAS_INDEX_WAVES_VERT10_NM))) * 2.0 - 1.0;
277 st = vec2(waterTex1 + disdis * sca) * windScale;
278 vec3 N1 = vec3(texture(textureArray, vec3(st, ATLAS_INDEX_PERLIN_NOISE_NM))) * 2.0 - 1.0;
279
280 st = vec2(waterTex1 * tscale) * windScale;
281 N0 += vec3(texture(textureArray, vec3(st, ATLAS_INDEX_WAVES_VERT10_NM))) * 2.0 - 1.0;
282 N1 += vec3(texture(textureArray, vec3(st, ATLAS_INDEX_PERLIN_NOISE_NM))) * 2.0 - 1.0;
283
284 rotationmatrix(radians(2.0 * sin(osg_SimulationTime * 0.005)), RotationMatrix);
285 st = vec2(waterTex2 * RotationMatrix * (tscale + sca2)) * windScale;
286 N0 += vec3(texture(textureArray, vec3(st, ATLAS_INDEX_WAVES_VERT10_NM))) * 2.0 - 1.0;
287 N1 += vec3(texture(textureArray, vec3(st, ATLAS_INDEX_PERLIN_NOISE_NM))) * 2.0 - 1.0;
288
289 rotationmatrix(radians(-4.0 * sin(osg_SimulationTime * 0.003)), RotationMatrix);
290 st = vec2(waterTex1 * RotationMatrix + disdis * sca2) * windScale;
291 N0 += vec3(texture(textureArray, vec3(st, ATLAS_INDEX_WAVES_VERT10_NM))) * 2.0 - 1.0;
292 st = vec2(waterTex1 * RotationMatrix + disdis * sca) * windScale;
293 N1 += vec3(texture(textureArray, vec3(st, ATLAS_INDEX_PERLIN_NOISE_NM))) * 2.0 - 1.0;
294
295 N0 *= windEffect_low;
296 N1 *= windEffect_low;
297
298 N0.r += (ddx + ddx1 + ddx2 + ddx3);
299 N0.g += (ddy + ddy1 + ddy2 + ddy3);
300
301 N = normalize(mix(Normal + N0, Normal + N1, mixFactor) * waveRoughness);
302
303 vec3 specular_color = vec3(specular_light * earthShade) * pow(max(0.0, dot(N, Hv)), water_shininess) * 6.0;
304
305 // secondary reflection of sky irradiance
306 vec3 ER = E - 2.0 * N * dot(E,N);
307 float ctrefl = dot(vec3(0.0,0.0,1.0), -normalize(ER));
308 //float fresnel = -0.5 + 8.0 * (1.0-smoothstep(0.0,0.4, dot(E,N)));
309 float fresnel = 8.0 * (1.0-smoothstep(0.0,0.4, dot(E,N)));
310 //specular_color += (ctrefl*ctrefl) * fresnel* specular_light.rgb;
311
312 specular_color += ((0.15*(1.0-ctrefl* ctrefl) * fresnel) - 0.3) * specular_light.rgb * earthShade;
313 vec4 specular = vec4(specular_color, 0.5);
314
315 specular = specular * saturation * 0.3 * earthShade ;
316
317 //calculate fresnel
318 vec4 invfres = vec4( dot(vNorm, viewt) );
319 vec4 fres = vec4(1.0) + invfres;
320 refl *= fres;
321
322 vec4 ambient_light;
323 //intensity = length(specular_light.rgb);
324 ambient_light.rgb = max(specular_light.rgb * earthShade, vec3(0.05, 0.05, 0.05));
325 //ambient_light.rgb = max(intensity * normalize(vec3 (0.33, 0.4, 0.5)), vec3 (0.1,0.1,0.1));
326 ambient_light.a = 1.0;
327
328 // compute object shadow effect
329
330 float shadowValue = getShadowing();
331 specular = specular * shadowValue;
332 refl = refl * (0.7 + 0.3 *shadowValue);
333
334
335 texel = refl + specular * smoothstep(0.3, 0.6, ground_scattering);
336
337 // For the clustered lighting function we use the simple up direction (Normal) to get an
338 // approximate lighting contribution, as the procedural normal map is done afterwards.
339 //texel += vec4(getClusteredLightsContribution(ecPosition.xyz, Normal, vec3(1.0)), 0.0) * light_distance_fading(dist) * 2.0 * pow(max(0.0,dot(E,N)), water_shininess);
340
341 if (dist < 10000.0)
342 {
343 float foamSlope = 0.10 + 0.1 * windScale;
344 float waveSlope = N.g + min(50.0*(1.0 - steepness), 0.1);
345
346 if ((steepness < 0.9999) || (windEffect >= 8.0) && (waveSlope >= foamSlope)) {
347 //add foam
348 st = vec2(waterTex2 * tscale) * 25.0;
349 vec4 foam_texel = texture(textureArray, vec3(st, ATLAS_INDEX_SEA_FOAM) );
350
351 texel = mix(texel, max(texel, texel + foam_texel), smoothstep(0.01, 0.50, waveSlope));
352 }
353 }
354
355 texel *= ambient_light;
356
357 return texel;
358 }