Debian Codegraph @

Shaders/ws30-water-ALS-ultra.frag

raw
1 // WS30 FRAGMENT SHADER
2
3 // -*-C++-*-
4 #version 130
5 #extension GL_EXT_texture_array : enable
6 // written by Thorsten Renk, Oct 2011, based on default.frag
7
8 //////////////////////////////////////////////////////////////////
9 // TEST PHASE TOGGLES AND CONTROLS
10 //
11 // Remove haze and lighting and shows just the texture.
12 // Useful for checking texture rendering and scenery.
13 // The compiler will likely optimise out the haze and lighting calculations.
14 // Possible values: 0:Normal, 1:Just the texture.
15 const int remove_haze_and_lighting = 0;
16
17 // Randomise texture lookups for 5 non-base textures e.g. mix_texture, detaile_texture etc.
18 // Each landclass is assigned 5 random textures from the ground texture array.
19 // This simulates a worst case possible texture lookup scenario, without needing access to material parameters.
20 // This does not simulate multiple texture sets, of which there may be up-to 4.
21 // The performance will likely be worse than in a real situation - there might be fewer textures
22 // for mix, detail and other textures. This might be easier on the GPUs texture caches.
23 // Possible values: 0: disabled (default),
24 // 1: enabled,
25 // 2: remove texture array lookups for 5 textures - only base texture + neighbour base textures
26 const int randomise_texture_lookups = 0;
27
28 // Use built-in water shader. Use for testing impact of ws30-water.frag
29 const int water_shader = 1;
30
31 //
32 // End of test phase controls
33 //////////////////////////////////////////////////////////////////
34
35 // Ambient term comes in gl_Color.rgb.
36 varying vec4 light_diffuse_comp;
37 varying vec3 normal;
38 varying vec3 relPos;
39 varying vec2 ground_tex_coord;
40 varying vec3 worldPos;
41 varying vec2 rawPos;
42 varying vec3 ecViewdir;
43 varying vec2 grad_dir;
44 varying vec4 ecPosition;
45 varying float steepness;
46
47 uniform sampler2D landclass;
48 uniform sampler2DArray textureArray;
49 uniform sampler2D perlin;
50
51 uniform float visibility;
52 uniform float avisibility;
53 uniform float scattering;
54 uniform float terminator;
55 uniform float terrain_alt;
56 uniform float hazeLayerAltitude;
57 uniform float overcast;
58 uniform float eye_alt;
59 uniform float snowlevel;
60 uniform float dust_cover_factor;
61 uniform float lichen_cover_factor;
62 uniform float wetness;
63 uniform float fogstructure;
64 uniform float snow_thickness_factor;
65 uniform float cloud_self_shading;
66 uniform float season;
67 uniform float air_pollution;
68
69 uniform float WindE;
70 uniform float WindN;
71 uniform float landing_light1_offset;
72 uniform float landing_light2_offset;
73 uniform float landing_light3_offset;
74 uniform float osg_SimulationTime;
75
76 uniform int wind_effects;
77 uniform int cloud_shadow_flag;
78 uniform int use_searchlight;
79 uniform int use_landing_light;
80 uniform int use_alt_landing_light;
81 // Testing code: Currently hardcoded to 2000, to allow noise functions to run while waiting for landclass lookup(s)
82 uniform int swatch_size; //in metres, typically 1000 or 2000
83
84 // Passed from VPBTechnique, not the Effect
85 uniform float fg_tileWidth;
86 uniform float fg_tileHeight;
87 uniform bool fg_photoScenery;
88 // Material parameters, from material definitions and effect defaults, for each landclass.
89 // xsize and ysize
90 uniform vec4 fg_dimensionsArray[128];
91 // RGBA ambient color
92 uniform vec4 fg_ambientArray[128];
93 // RGBA diffuse color
94 uniform vec4 fg_diffuseArray[128];
95 // RGBA specular color
96 uniform vec4 fg_specularArray[128];
97 // Indicies of textures in the ground texture array for different
98 // texture slots (grain, gradient, dot, mix, detail) for each landclass
99 uniform vec4 fg_textureLookup1[128];
100 uniform vec4 fg_textureLookup2[128];
101 // Each element of a vec4 contains a different materials parameter.
102 uniform vec4 fg_materialParams1[128];
103 uniform vec4 fg_materialParams2[128];
104 uniform vec4 fg_materialParams3[128];
105
106 // Coastline texture - generated from VPBTechnique
107 uniform sampler2D coastline;
108
109 // Sand texture
110 uniform sampler2D sand;
111
112 uniform mat4 fg_zUpTransform;
113 uniform vec3 fg_modelOffset;
114
115 const float terminator_width = 200000.0;
116
117 float shadow_func (in float x, in float y, in float noise, in float dist);
118 float DotNoise2D(in vec2 coord, in float wavelength, in float fractionalMaxDotSize, in float dot_density);
119 float Noise2D(in vec2 coord, in float wavelength);
120 float Noise3D(in vec3 coord, in float wavelength);
121 float SlopeLines2D(in vec2 coord, in vec2 gradDir, in float wavelength, in float steepness);
122 float Strata3D(in vec3 coord, in float wavelength, in float variation);
123 float fog_func (in float targ, in float alt);
124 float rayleigh_in_func(in float dist, in float air_pollution, in float avisibility, in float eye_alt, in float vertex_alt);
125 float alt_factor(in float eye_alt, in float vertex_alt);
126 float light_distance_fading(in float dist);
127 float fog_backscatter(in float avisibility);
128
129 vec3 rayleigh_out_shift(in vec3 color, in float outscatter);
130 vec3 get_hazeColor(in float light_arg);
131 vec3 searchlight();
132 vec3 landing_light(in float offset, in float offsetv);
133 vec3 filter_combined (in vec3 color) ;
134
135 float getShadowing();
136 vec3 getClusteredLightsContribution(vec3 p, vec3 n, vec3 texel);
137
138
139 // a fade function for procedural scales which are smaller than a pixel
140
141 float detail_fade (in float scale, in float angle, in float dist)
142 {
143 float fade_dist = 2000.0 * scale * angle/max(pow(steepness,4.0), 0.1);
144
145 return 1.0 - smoothstep(0.5 * fade_dist, fade_dist, dist);
146 }
147
148 //////////////////////////
149 // Test-phase code:
150
151
152 // These should be sent as uniforms
153
154 // Tile dimensions in meters
155 // vec2 tile_size = vec2(fg_tileWidth , fg_tileHeight);
156 // Testing: texture coords are sent flipped right now:
157
158 // Note tile_size is defined in the shader include: ws30-landclass-search-functions.frag.
159 //vec2 tile_size = vec2(fg_tileHeight , fg_tileWidth);
160
161
162 // Uniform array lookup functions example:
163 // Access data[] as if it was a 1-d array of floats
164 // with data sorted as rows of data values for each row of texture variants
165 // using index for the relevant value
166 /*
167 float getFloatFromArrayData(int i)
168 {
169 int n = int(floor(float(i/4.0)));
170 vec4 v4 = someArray[n];
171 int index_within_v4 = int(mod(float(i),4.0));
172 float value = v4[index_within_v4];
173 return value;
174 }
175
176
177 vec4 getVec4FromArrayData(int i)
178 {
179 return (vec4(getFloatFromArrayData(i), getFloatFromArrayData(i+1), getFloatFromArrayData(i+2), getFloatFromArrayData(i+3)));
180 }
181 */
182
183
184
185 // From noise.frag
186 float rand2D(in vec2 co);
187
188
189 // Generates a full precision 32 bit random number from 2 seeds
190 // as well as 6 random integers between 0 and factor that are rescaled 0.0-1.0
191 // by re-using the significant figures from the full precision number.
192 // This avoids having to generate 6 random numbers when
193 // limited variation is needed: say 6 numbers with 100 levels (i.e between 0 and 100).
194 // Seed 2 is incremented so the function can be called again to generate
195 // a different set of numbers
196 float get6_rand_nums(in float PRNGseed1,
197 inout float PRNGseed2, float factor, out float [6] random_integers
198 );
199
200
201
202 // Create random landclasses without a texture lookup to stress test.
203 // Each square of square_size in m is assigned a random landclass value.
204 int get_random_landclass(in vec2 co, in vec2 tile_size);
205
206 // End Test-phase code
207 ////////////////////////
208
209
210 // These functions, and other function they depend on, are defined
211 // in ws30-ALS-landclass-search.frag.
212
213 // Lookup a ground texture at a point based on the landclass at that point, without visible
214 // seams at coordinate discontinuities or at landclass boundaries where texture are switched.
215 // The partial derivatives of the tile_coord at the fragment is needed to adjust for
216 // the stretching of different textures, so that the correct mip-map level is looked
217 // up and there are no seams.
218 // Texture types: 0: base texture, 1: grain texture, 2: gradient texture, 3 dot texture,
219 // 4: mix texture, 5: detail texture.
220
221 vec4 lookup_ground_texture_array(in int texture_type, in vec2 ground_texture_coord, in int landclass_id,
222 in vec4 dFdx_and_dFdy);
223
224
225 // Look up the landclass id [0 .. 255] for this particular fragment.
226 // Lookup id of any neighbouring landclass that is within the search distance.
227 // Searches are performed in upto 4 directions right now, but only one landclass is looked up
228 // Create a mix factor werighting the influences of nearby landclasses
229 void get_landclass_id(in vec2 tile_coord, in vec4 dFdx_and_dFdy,
230 out int landclass_id, out ivec4 neighbor_landclass_ids,
231 out int num_unique_neighbors,out vec4 mix_factor
232 );
233
234
235 // Look up the texel of the specified texture type (e.g. grain or detail textures) for this fragment
236 // and any neighbor texels, then mix.
237
238 vec4 get_mixed_texel(in int texture_type, in vec2 g_texture_coord,
239 in int landclass_id, in int num_unique_neighbors,
240 in ivec4 neighbor_texel_landclass_ids, in vec4 neighbor_mix_factors,
241 in vec4 dFdx_and_dFdy
242 );
243
244 // Determine the texel and material parameters for a particular fragment,
245 // Taking into account photoscenery etc.
246 void get_material(in int landclass,
247 in vec2 ground_tex_coord,
248 in vec4 dxdy_gc,
249 out float mat_shininess,
250 out vec4 mat_ambient,
251 out vec4 mat_diffuse,
252 out vec4 mat_specular,
253 out vec4 dxdy,
254 out vec2 st
255 );
256
257 // Apply the ALS haze model to a specific fragment
258 vec4 applyHaze(inout vec4 fragColor,
259 inout vec3 hazeColor,
260 in vec3 secondary_light,
261 in float ct,
262 in float hazeLayerAltitude,
263 in float visibility,
264 in float avisibility,
265 in float dist,
266 in float lightArg,
267 in float mie_angle);
268
269 // Procedurally generate a water texel for this fragment
270 vec4 generateWaterTexel();
271
272
273 void main()
274 {
275 float yprime_alt = light_diffuse_comp.a;
276 //diffuse_term.a = 1.0;
277 float mie_angle = gl_Color.a;
278 float effective_scattering = min(scattering, cloud_self_shading);
279
280 // distance to fragment
281 float dist = length(relPos);
282 // angle of view vector with horizon
283 float ct = dot(vec3(0.0, 0.0, 1.0), relPos)/dist;
284 // float altitude of fragment above sea level
285 float msl_altitude = (relPos.z + eye_alt);
286
287
288 // this is taken from default.frag
289 float NdotL, NdotHV, fogFactor;
290 vec3 n = normalize(normal);
291 vec3 lightDir = gl_LightSource[0].position.xyz;
292 vec3 halfVector = normalize(normalize(lightDir) + normalize(ecViewdir));
293 vec3 secondary_light = vec3 (0.0,0.0,0.0);
294
295
296 // Material/texel properties
297 float mat_shininess;
298 vec2 st;
299 vec4 mat_ambient, mat_diffuse, mat_specular, dxdy;
300
301 vec4 texel;
302 vec4 snow_texel;
303 vec4 detail_texel;
304 vec4 mix_texel;
305 vec4 grain_texel;
306 vec4 dot_texel;
307 vec4 gradient_texel;
308
309 vec4 fragColor;
310 vec4 specular = vec4(0.0);
311 float intensity;
312
313 // Wind motion of the overlay noise simulating movement of vegetation and loose debris
314
315 vec2 windPos;
316
317 if (wind_effects > 1)
318 {
319 float windSpeed = length(vec2 (WindE,WindN)) /3.0480;
320 // interfering sine wave wind pattern
321 float sineTerm = sin(0.35 * windSpeed * osg_SimulationTime + 0.05 * (rawPos.x + rawPos.y));
322 sineTerm = sineTerm + sin(0.3 * windSpeed * osg_SimulationTime + 0.04 * (rawPos.x + rawPos.y));
323 sineTerm = sineTerm + sin(0.22 * windSpeed * osg_SimulationTime + 0.05 * (rawPos.x + rawPos.y));
324 sineTerm = sineTerm/3.0;
325 // non-linear amplification to simulate gusts
326 sineTerm = sineTerm * sineTerm;//smoothstep(0.2, 1.0, sineTerm);
327
328 // wind starts moving dust and leaves at around 8 m/s
329 float timeArg = 0.01 * osg_SimulationTime * windSpeed * smoothstep(8.0, 15.0, windSpeed);
330 timeArg = timeArg + 0.02 * sineTerm;
331
332 windPos = vec2 (rawPos.x + WindN * timeArg, rawPos.y + WindE * timeArg);
333 }
334 else
335 {
336 windPos = rawPos.xy;
337 }
338
339
340 // get noise at different wavelengths in units of swatch_size
341 // original assumed 4km texture.
342
343 // used: 5m, 5m gradient, 10m, 10m gradient: heightmap of the closeup terrain, 10m also snow
344 // 50m: detail texel
345 // 250m: detail texel
346 // 500m: distortion and overlay
347 // 1500m: overlay, detail, dust, fog
348 // 2000m: overlay, detail, snow, fog
349
350 // Perlin noise
351
352 float noise_10m = Noise2D(rawPos.xy, 10.0);
353 float noise_5m = Noise2D(rawPos.xy ,5.0);
354 float noise_2m = Noise2D(rawPos.xy ,2.0);
355 float noise_1m = Noise2D(rawPos.xy ,1.0);
356 float noise_01m = Noise2D(windPos.xy, 0.1);
357
358 // Noise relative to swatch size
359 float noise_25m = Noise2D(rawPos.xy, swatch_size*0.000625);
360 float noise_50m = Noise2D(rawPos.xy, swatch_size*0.00125);
361 float noise_250m = Noise3D(worldPos.xyz,swatch_size*0.0625);
362 float noise_500m = Noise3D(worldPos.xyz, swatch_size*0.125);
363 float noise_1500m = Noise3D(worldPos.xyz, swatch_size*0.3750);
364 float noise_2000m = Noise3D(worldPos.xyz, swatch_size*0.5);
365 float noise_4000m = Noise3D(worldPos.xyz, swatch_size);
366
367 float dotnoisegrad_10m;
368
369
370 // slope noise
371 float slopenoise_50m = SlopeLines2D(rawPos.xy, grad_dir, 50.0, steepness);
372 float slopenoise_100m = SlopeLines2D(rawPos.xy, grad_dir, 100.0, steepness);
373
374 float snownoise_25m = mix(noise_25m, slopenoise_50m, clamp(3.0*(1.0-steepness),0.0,1.0));
375 float snownoise_50m = mix(noise_50m, slopenoise_100m, clamp(3.0*(1.0-steepness),0.0,1.0));
376
377 // get the texels
378
379
380
381 float distortion_factor = 1.0;
382 vec2 stprime;
383 int flag = 1;
384 int mix_flag = 1;
385 float noise_term;
386 float snow_alpha;
387
388 // Oct 27 2021:
389 // Geometry is in the form of roughly rectangular 'tiles'
390 // with a mesh forming a grid with regular spacing.
391 // Each vertex in the mesh is given an elevation
392
393 // Tile dimensions in m
394 // Testing: created from two float uniforms in global scope. Should be sent as a vec2
395 // vec2 tile_size
396
397 // This is a water fragment, so calculate the fragment color procedurally
398 // and mix with some sand and cliff colour depending on steepness
399 //vec4 steep_texel = lookup_ground_texture_array(2, ground_tex_coord, lc, dxdy_gc); // Uses the same index as the gradient texture, which it is
400 //vec4 beach_texel = lookup_ground_texture_array(3, ground_tex_coord, lc, dxdy_gc); // Use the dot texture, which is overloaded to be the beach texture
401
402 // Mix from a rocky texture to beach for steep slopes, which invariably represent the elevation mesh not being perfectly
403 // aligned with the landclass mesh.
404 // texel = mix(steep_texel, beach_texel, smoothstep(waterline_max_steepness - 0.1, waterline_max_steepness - 0.03, steepness));
405
406 // Mix from the beach into the water, which produces a pleasing translucent shallow water effect.
407 //fragColor = mix(texel, generateWaterTexel(), smoothstep(waterline_min_steepness,waterline_max_steepness,steepness));
408 fragColor = generateWaterTexel();
409 fragColor.rgb += getClusteredLightsContribution(ecPosition.xyz, n, fragColor.rgb);
410
411 float lightArg = (terminator-yprime_alt)/100000.0;
412 vec3 hazeColor = get_hazeColor(lightArg);
413
414 // Rayleigh color shift due to out-scattering
415 float rayleigh_length = 0.5 * avisibility * (2.5 - 1.9 * air_pollution)/alt_factor(eye_alt, msl_altitude);
416 float outscatter = 1.0-exp(-dist/rayleigh_length);
417 fragColor.rgb = rayleigh_out_shift(fragColor.rgb,outscatter);
418
419 // Rayleigh color shift due to in-scattering
420
421 float rShade = 1.0 - 0.9 * smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt + 420000.0);
422 //float lightIntensity = length(diffuse_term.rgb)/1.73 * rShade;
423 float lightIntensity = length(hazeColor * effective_scattering) * rShade;
424 vec3 rayleighColor = vec3 (0.17, 0.52, 0.87) * lightIntensity;
425 float rayleighStrength = rayleigh_in_func(dist, air_pollution, avisibility/max(lightIntensity,0.05), eye_alt, msl_altitude);
426 fragColor.rgb = mix(fragColor.rgb, rayleighColor,rayleighStrength);
427
428 gl_FragColor = applyHaze(fragColor, hazeColor, secondary_light, ct, hazeLayerAltitude, visibility, avisibility, dist, lightArg, mie_angle);
429
430 // Testing phase controls:
431 if (remove_haze_and_lighting == 1)
432 {
433 gl_FragColor = texel;
434 }
435 }