Debian Codegraph @

Shaders/ws30-water-ALS-detailed.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 //////////////////////////////////////////////////////////////////
10 // TEST PHASE TOGGLES AND CONTROLS
11 //
12
13 // Development tools:
14 // Reduce haze to almost zero, while preserving lighting. Useful for observing distant tiles.
15 // Keeps the calculation overhead. This can be used for profiling.
16 // Possible values: 0:Normal, 1:Reduced haze.
17 const int reduce_haze_without_removing_calculation_overhead = 0;
18
19 // Remove haze and lighting and shows just the texture.
20 // Useful for checking texture rendering and scenery.
21 // The compiler will likely optimise out the haze and lighting calculations.
22 // Possible values: 0:Normal, 1:Just the texture.
23 const int remove_haze_and_lighting = 0;
24
25 // Use built-in water shader. Use for testing impact of ws30-water.frag
26 const int water_shader = 1;
27
28 //
29 // End of test phase controls
30 //////////////////////////////////////////////////////////////////
31
32
33
34
35
36
37
38
39
40
41
42
43
44 // written by Thorsten Renk, Oct 2011, based on default.frag
45 // Ambient term comes in gl_Color.rgb.
46 varying vec4 light_diffuse_comp;
47 varying vec3 normal;
48 varying vec3 relPos;
49 varying vec2 ground_tex_coord;
50 varying vec2 rawPos;
51 varying vec3 worldPos;
52 // Testing code:
53 //vec3 worldPos = vec3(5000.0, 6000.0, 7000.0) + vec3(vec2(rawPos), 600.0); // vec3(100.0, 10.0, 3.0);
54 varying vec4 eyePos;
55
56
57 uniform sampler2D landclass;
58 uniform sampler2DArray textureArray;
59 uniform sampler2D perlin;
60
61 //varying float yprime_alt;
62 //varying float mie_angle;
63 varying float steepness;
64
65
66 uniform float visibility;
67 uniform float avisibility;
68 uniform float scattering;
69 uniform float terminator;
70 uniform float terrain_alt;
71 uniform float hazeLayerAltitude;
72 uniform float overcast;
73 uniform float eye_alt;
74 uniform float snowlevel;
75 uniform float dust_cover_factor;
76 uniform float lichen_cover_factor;
77 uniform float wetness;
78 uniform float fogstructure;
79 uniform float snow_thickness_factor;
80 uniform float cloud_self_shading;
81 uniform float season;
82
83 uniform int quality_level;
84 uniform int tquality_level;
85
86 // Passed from VPBTechnique, not the Effect
87 uniform float fg_tileWidth;
88 uniform float fg_tileHeight;
89 uniform bool fg_photoScenery;
90 uniform vec4 fg_dimensionsArray[128];
91 uniform vec4 fg_ambientArray[128];
92 uniform vec4 fg_diffuseArray[128];
93 uniform vec4 fg_specularArray[128];
94 uniform vec4 fg_textureLookup1[128];
95 uniform vec4 fg_textureLookup2[128];
96 uniform vec4 fg_materialParams1[128];
97 uniform vec4 fg_materialParams3[128];
98 uniform mat4 fg_zUpTransform;
99 uniform vec3 fg_modelOffset;
100
101 const float EarthRadius = 5800000.0;
102 const float terminator_width = 200000.0;
103
104 //Testing phase: Why are these in global scope in WS2 shaders?
105 float alt;
106 float eShade;
107 float yprime_alt;
108 float mie_angle;
109
110 float shadow_func (in float x, in float y, in float noise, in float dist);
111 float DotNoise2D(in vec2 coord, in float wavelength, in float fractionalMaxDotSize, in float dot_density);
112 float Noise2D(in vec2 coord, in float wavelength);
113 float Noise3D(in vec3 coord, in float wavelength);
114 float SlopeLines2D(in vec2 coord, in vec2 gradDir, in float wavelength, in float steepness);
115 float Strata3D(in vec3 coord, in float wavelength, in float variation);
116 float fog_func (in float targ, in float alt);
117 float rayleigh_in_func(in float dist, in float air_pollution, in float avisibility, in float eye_alt, in float vertex_alt);
118 float alt_factor(in float eye_alt, in float vertex_alt);
119 float light_distance_fading(in float dist);
120 float fog_backscatter(in float avisibility);
121
122 vec3 rayleigh_out_shift(in vec3 color, in float outscatter);
123 vec3 get_hazeColor(in float light_arg);
124 vec3 searchlight();
125 vec3 landing_light(in float offset, in float offsetv);
126 vec3 filter_combined (in vec3 color) ;
127
128 float getShadowing();
129 vec3 getClusteredLightsContribution(vec3 p, vec3 n, vec3 texel);
130
131
132
133
134 //////////////////////////
135 // Test-phase code:
136
137
138 // These should be sent as uniforms
139
140 // Tile dimensions in meters
141 // vec2 tile_size = vec2(tile_width , tile_height);
142 // Testing: texture coords are sent flipped right now:
143
144 // Note tile_size is defined in the shader include: ws30-landclass-search-functions.frag.
145 // vec2 tile_size = vec2(tile_height , tile_width);
146
147 // From noise.frag
148 float rand2D(in vec2 co);
149
150 // Create random landclasses without a texture lookup to stress test.
151 // Each square of square_size in m is assigned a random landclass value.
152 int get_random_landclass(in vec2 co, in vec2 tile_size);
153
154 // End Test-phase code
155 ////////////////////////
156
157 // These functions, and other function they depend on, are defined
158 // in ws30-ALS-landclass-search.frag.
159
160 // Lookup a ground texture at a point based on the landclass at that point, without visible
161 // seams at coordinate discontinuities or at landclass boundaries where texture are switched.
162 // The partial derivatives of the tile_coord at the fragment is needed to adjust for
163 // the stretching of different textures, so that the correct mip-map level is looked
164 // up and there are no seams.
165 // Texture types: 0: base texture, 1: grain texture, 2: gradient texture, 3 dot texture,
166 // 4: mix texture, 5: detail texture.
167
168 vec4 lookup_ground_texture_array(in int texture_type, in vec2 ground_texture_coord, in int landclass_id,
169 in vec4 dFdx_and_dFdy);
170
171
172 // Look up the landclass id [0 .. 255] for this particular fragment.
173 // Lookup id of any neighbouring landclass that is within the search distance.
174 // Searches are performed in upto 4 directions right now, but only one landclass is looked up
175 // Create a mix factor weighting the influences of nearby landclasses
176 void get_landclass_id(in vec2 tile_coord, in vec4 dFdx_and_dFdy,
177 out int landclass_id, out ivec4 neighbor_landclass_ids,
178 out int num_unique_neighbors,out vec4 mix_factor
179 );
180
181
182 // Look up the texel of the specified texture type (e.g. grain or detail textures) for this fragment
183 // and any neighbor texels, then mix.
184
185 vec4 get_mixed_texel(in int texture_type, in vec2 g_texture_coord,
186 in int landclass_id, in int num_unique_neighbors,
187 in ivec4 neighbor_texel_landclass_ids, in vec4 neighbor_mix_factors,
188 in vec4 dFdx_and_dFdy
189 );
190
191 // Determine the texel and material parameters for a particular fragment,
192 // Taking into account photoscenery etc.
193 void get_material(in int landclass,
194 in vec2 ground_tex_coord,
195 in vec4 dxdy_gc,
196 out float mat_shininess,
197 out vec4 mat_ambient,
198 out vec4 mat_diffuse,
199 out vec4 mat_specular,
200 out vec4 dxdy,
201 out vec2 st
202 );
203
204 // Apply the ALS haze model to a specific fragment
205 vec4 applyHaze(inout vec4 fragColor,
206 inout vec3 hazeColor,
207 in vec3 secondary_light,
208 in float ct,
209 in float hazeLayerAltitude,
210 in float visibility,
211 in float avisibility,
212 in float dist,
213 in float lightArg,
214 in float mie_angle);
215
216 // Procedurally generate a water texel for this fragment
217 vec4 generateWaterTexel();
218
219 void main()
220 {
221
222
223 yprime_alt = light_diffuse_comp.a;
224 //diffuse_term.a = 1.0;
225 mie_angle = gl_Color.a;
226 float effective_scattering = min(scattering, cloud_self_shading);
227
228 // distance to fragment
229
230 float dist = length(relPos);
231 // angle of view vector with horizon
232 float ct = dot(vec3(0.0, 0.0, 1.0), relPos)/dist;
233
234 // Altitude of fragment above sea level
235 float msl_altitude = (relPos.z + eye_alt);
236
237
238 vec3 shadedFogColor = vec3(0.55, 0.67, 0.88);
239 // this is taken from default.frag
240 vec3 n;
241 float NdotL, NdotHV, fogFactor;
242 vec3 lightDir = gl_LightSource[0].position.xyz;
243 vec3 halfVector = gl_LightSource[0].halfVector.xyz;
244 //vec3 halfVector = normalize(normalize(lightDir) + normalize(ecViewdir));
245 vec4 texel;
246 vec4 snow_texel;
247 vec4 detail_texel;
248 vec4 mix_texel;
249 vec4 fragColor;
250 vec4 specular = vec4(0.0);
251 float intensity;
252
253 // Material/texel properties
254 float mat_shininess;
255 vec2 st;
256 vec4 mat_ambient, mat_diffuse, mat_specular, dxdy;
257
258 // get noise at different wavelengths
259
260 // used: 5m, 5m gradient, 10m, 10m gradient: heightmap of the closeup terrain, 10m also snow
261 // 50m: detail texel
262 // 250m: detail texel
263 // 500m: distortion and overlay
264 // 1500m: overlay, detail, dust, fog
265 // 2000m: overlay, detail, snow, fog
266
267 float noise_10m;
268 float noise_5m;
269 noise_10m = Noise2D(rawPos.xy, 10.0);
270 noise_5m = Noise2D(rawPos.xy ,5.0);
271
272 float noisegrad_10m;
273 float noisegrad_5m;
274
275 float noise_50m = Noise2D(rawPos.xy, 50.0);;
276
277
278 float noise_250m = Noise3D(worldPos.xyz,250.0);
279 float noise_500m = Noise3D(worldPos.xyz, 500.0);
280 float noise_1500m = Noise3D(worldPos.xyz, 1500.0);
281 float noise_2000m = Noise3D(worldPos.xyz, 2000.0);
282
283
284
285 //
286
287
288 // get the texels
289
290 // Oct 27 2021:
291 // Geometry is in the form of roughly rectangular 'tiles'
292 // with a mesh forming a grid with regular spacing.
293 // Each vertex in the mesh is given an elevation
294
295 // Tile dimensions in m
296 // Testing: created from two float uniforms in global scope. Should be sent as a vec2
297 // vec2 tile_size
298
299 // Tile texture coordinates range [0..1] over the tile 'rectangle'
300
301 // This is a water fragment, so calculate the fragment color procedurally
302 fragColor = generateWaterTexel();
303 fragColor.rgb += getClusteredLightsContribution(eyePos.xyz, n, fragColor.rgb);
304
305 float lightArg = (terminator-yprime_alt)/100000.0;
306 vec3 hazeColor = get_hazeColor(lightArg);
307 gl_FragColor = applyHaze(fragColor, hazeColor, vec3(0.0), ct, hazeLayerAltitude, visibility, avisibility, dist, lightArg, mie_angle);
308
309
310 // Testing phase controls:
311 if (remove_haze_and_lighting == 1)
312 {
313 gl_FragColor = texel;
314 }
315
316
317
318 }