| 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 |
// Constants controlling the transition from water |
| 33 |
// to terrain depending on the terrain normal |
| 34 |
// A normal of 1.0 is completely horizontal. |
| 35 |
const float WATER_START = 0.995; // Deeper water. |
| 36 |
const float WATER_BEACH_TO_WATER = 0.99; // Transition point between the shoreline and the shallow water |
| 37 |
const float WATER_STEEP_TO_BEACH = 0.985; // The transition point between the shoreline and the land |
| 38 |
const float WATER_STEEP = 0.98; // Anything with less than this value is considered not water or shoreline |
| 39 |
|
| 40 |
// written by Thorsten Renk, Oct 2011, based on default.frag |
| 41 |
// Ambient term comes in gl_Color.rgb. |
| 42 |
varying vec4 light_diffuse_comp; |
| 43 |
varying vec3 normal; |
| 44 |
varying vec3 relPos; |
| 45 |
varying vec2 ground_tex_coord; |
| 46 |
varying vec2 rawPos; |
| 47 |
varying vec3 worldPos; |
| 48 |
// Testing code: |
| 49 |
//vec3 worldPos = vec3(5000.0, 6000.0, 7000.0) + vec3(vec2(rawPos), 600.0); // vec3(100.0, 10.0, 3.0); |
| 50 |
varying vec4 eyePos; |
| 51 |
|
| 52 |
|
| 53 |
uniform sampler2D landclass; |
| 54 |
uniform sampler2DArray textureArray; |
| 55 |
uniform sampler2D perlin; |
| 56 |
|
| 57 |
//varying float yprime_alt; |
| 58 |
//varying float mie_angle; |
| 59 |
varying float steepness; |
| 60 |
|
| 61 |
|
| 62 |
uniform float visibility; |
| 63 |
uniform float avisibility; |
| 64 |
uniform float scattering; |
| 65 |
uniform float terminator; |
| 66 |
uniform float terrain_alt; |
| 67 |
uniform float hazeLayerAltitude; |
| 68 |
uniform float overcast; |
| 69 |
uniform float eye_alt; |
| 70 |
uniform float snowlevel; |
| 71 |
uniform float dust_cover_factor; |
| 72 |
uniform float lichen_cover_factor; |
| 73 |
uniform float wetness; |
| 74 |
uniform float fogstructure; |
| 75 |
uniform float snow_thickness_factor; |
| 76 |
uniform float cloud_self_shading; |
| 77 |
uniform float season; |
| 78 |
|
| 79 |
uniform int quality_level; |
| 80 |
uniform int tquality_level; |
| 81 |
|
| 82 |
// Passed from VPBTechnique, not the Effect |
| 83 |
uniform float fg_tileWidth; |
| 84 |
uniform float fg_tileHeight; |
| 85 |
uniform bool fg_photoScenery; |
| 86 |
uniform vec4 fg_dimensionsArray[128]; |
| 87 |
uniform vec4 fg_ambientArray[128]; |
| 88 |
uniform vec4 fg_diffuseArray[128]; |
| 89 |
uniform vec4 fg_specularArray[128]; |
| 90 |
uniform vec4 fg_textureLookup1[128]; |
| 91 |
uniform vec4 fg_textureLookup2[128]; |
| 92 |
uniform vec4 fg_materialParams1[128]; |
| 93 |
uniform vec4 fg_materialParams3[128]; |
| 94 |
uniform mat4 fg_zUpTransform; |
| 95 |
uniform vec3 fg_modelOffset; |
| 96 |
|
| 97 |
// Coastline texture - generated from VPBTechnique |
| 98 |
uniform sampler2D coastline; |
| 99 |
|
| 100 |
// Index into the material definition for shorelines |
| 101 |
uniform int fg_shoreAtlasIndex; |
| 102 |
|
| 103 |
const float EarthRadius = 5800000.0; |
| 104 |
const float terminator_width = 200000.0; |
| 105 |
|
| 106 |
//Testing phase: Why are these in global scope in WS2 shaders? |
| 107 |
float alt; |
| 108 |
float eShade; |
| 109 |
float yprime_alt; |
| 110 |
float mie_angle; |
| 111 |
|
| 112 |
float shadow_func (in float x, in float y, in float noise, in float dist); |
| 113 |
float DotNoise2D(in vec2 coord, in float wavelength, in float fractionalMaxDotSize, in float dot_density); |
| 114 |
float Noise2D(in vec2 coord, in float wavelength); |
| 115 |
float Noise3D(in vec3 coord, in float wavelength); |
| 116 |
float SlopeLines2D(in vec2 coord, in vec2 gradDir, in float wavelength, in float steepness); |
| 117 |
float Strata3D(in vec3 coord, in float wavelength, in float variation); |
| 118 |
float fog_func (in float targ, in float alt); |
| 119 |
float rayleigh_in_func(in float dist, in float air_pollution, in float avisibility, in float eye_alt, in float vertex_alt); |
| 120 |
float alt_factor(in float eye_alt, in float vertex_alt); |
| 121 |
float light_distance_fading(in float dist); |
| 122 |
float fog_backscatter(in float avisibility); |
| 123 |
|
| 124 |
vec3 rayleigh_out_shift(in vec3 color, in float outscatter); |
| 125 |
vec3 get_hazeColor(in float light_arg); |
| 126 |
vec3 searchlight(); |
| 127 |
vec3 landing_light(in float offset, in float offsetv); |
| 128 |
vec3 filter_combined (in vec3 color) ; |
| 129 |
|
| 130 |
float getShadowing(); |
| 131 |
vec3 getClusteredLightsContribution(vec3 p, vec3 n, vec3 texel); |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
////////////////////////// |
| 137 |
// Test-phase code: |
| 138 |
|
| 139 |
|
| 140 |
// These should be sent as uniforms |
| 141 |
|
| 142 |
// Tile dimensions in meters |
| 143 |
// vec2 tile_size = vec2(tile_width , tile_height); |
| 144 |
// Testing: texture coords are sent flipped right now: |
| 145 |
|
| 146 |
// Note tile_size is defined in the shader include: ws30-landclass-search-functions.frag. |
| 147 |
// vec2 tile_size = vec2(tile_height , tile_width); |
| 148 |
|
| 149 |
// From noise.frag |
| 150 |
float rand2D(in vec2 co); |
| 151 |
|
| 152 |
// Create random landclasses without a texture lookup to stress test. |
| 153 |
// Each square of square_size in m is assigned a random landclass value. |
| 154 |
int get_random_landclass(in vec2 co, in vec2 tile_size); |
| 155 |
|
| 156 |
// End Test-phase code |
| 157 |
//////////////////////// |
| 158 |
|
| 159 |
// These functions, and other function they depend on, are defined |
| 160 |
// in ws30-ALS-landclass-search.frag. |
| 161 |
|
| 162 |
// Lookup a ground texture at a point based on the landclass at that point, without visible |
| 163 |
// seams at coordinate discontinuities or at landclass boundaries where texture are switched. |
| 164 |
// The partial derivatives of the tile_coord at the fragment is needed to adjust for |
| 165 |
// the stretching of different textures, so that the correct mip-map level is looked |
| 166 |
// up and there are no seams. |
| 167 |
// Texture types: 0: base texture, 1: grain texture, 2: gradient texture, 3 dot texture, |
| 168 |
// 4: mix texture, 5: detail texture. |
| 169 |
|
| 170 |
vec4 lookup_ground_texture_array(in int texture_type, in vec2 ground_texture_coord, in int landclass_id, |
| 171 |
in vec4 dFdx_and_dFdy); |
| 172 |
|
| 173 |
|
| 174 |
// Look up the landclass id [0 .. 255] for this particular fragment. |
| 175 |
// Lookup id of any neighbouring landclass that is within the search distance. |
| 176 |
// Searches are performed in upto 4 directions right now, but only one landclass is looked up |
| 177 |
// Create a mix factor weighting the influences of nearby landclasses |
| 178 |
void get_landclass_id(in vec2 tile_coord, in vec4 dFdx_and_dFdy, |
| 179 |
out int landclass_id, out ivec4 neighbor_landclass_ids, |
| 180 |
out int num_unique_neighbors,out vec4 mix_factor |
| 181 |
); |
| 182 |
|
| 183 |
|
| 184 |
// Look up the texel of the specified texture type (e.g. grain or detail textures) for this fragment |
| 185 |
// and any neighbor texels, then mix. |
| 186 |
|
| 187 |
vec4 get_mixed_texel(in int texture_type, in vec2 g_texture_coord, |
| 188 |
in int landclass_id, in int num_unique_neighbors, |
| 189 |
in ivec4 neighbor_texel_landclass_ids, in vec4 neighbor_mix_factors, |
| 190 |
in vec4 dFdx_and_dFdy |
| 191 |
); |
| 192 |
|
| 193 |
// Determine the texel and material parameters for a particular fragment, |
| 194 |
// Taking into account photoscenery etc. |
| 195 |
void get_material(in int landclass, |
| 196 |
in vec2 ground_tex_coord, |
| 197 |
in vec4 dxdy_gc, |
| 198 |
out float mat_shininess, |
| 199 |
out vec4 mat_ambient, |
| 200 |
out vec4 mat_diffuse, |
| 201 |
out vec4 mat_specular, |
| 202 |
out vec4 dxdy, |
| 203 |
out vec2 st |
| 204 |
); |
| 205 |
|
| 206 |
// Apply the ALS haze model to a specific fragment |
| 207 |
vec4 applyHaze(inout vec4 fragColor, |
| 208 |
inout vec3 hazeColor, |
| 209 |
in vec3 secondary_light, |
| 210 |
in float ct, |
| 211 |
in float hazeLayerAltitude, |
| 212 |
in float visibility, |
| 213 |
in float avisibility, |
| 214 |
in float dist, |
| 215 |
in float lightArg, |
| 216 |
in float mie_angle); |
| 217 |
|
| 218 |
// Procedurally generate a water texel for this fragment |
| 219 |
vec4 generateWaterTexel(); |
| 220 |
|
| 221 |
void main() |
| 222 |
{ |
| 223 |
|
| 224 |
|
| 225 |
yprime_alt = light_diffuse_comp.a; |
| 226 |
//diffuse_term.a = 1.0; |
| 227 |
mie_angle = gl_Color.a; |
| 228 |
float effective_scattering = min(scattering, cloud_self_shading); |
| 229 |
|
| 230 |
// distance to fragment |
| 231 |
|
| 232 |
float dist = length(relPos); |
| 233 |
// angle of view vector with horizon |
| 234 |
float ct = dot(vec3(0.0, 0.0, 1.0), relPos)/dist; |
| 235 |
|
| 236 |
// Altitude of fragment above sea level |
| 237 |
float msl_altitude = (relPos.z + eye_alt); |
| 238 |
|
| 239 |
|
| 240 |
vec3 shadedFogColor = vec3(0.55, 0.67, 0.88); |
| 241 |
// this is taken from default.frag |
| 242 |
vec3 n; |
| 243 |
float NdotL, NdotHV, fogFactor; |
| 244 |
vec3 lightDir = gl_LightSource[0].position.xyz; |
| 245 |
vec3 halfVector = gl_LightSource[0].halfVector.xyz; |
| 246 |
//vec3 halfVector = normalize(normalize(lightDir) + normalize(ecViewdir)); |
| 247 |
vec4 texel; |
| 248 |
vec4 snow_texel; |
| 249 |
vec4 detail_texel; |
| 250 |
vec4 mix_texel; |
| 251 |
vec4 fragColor; |
| 252 |
vec4 specular = vec4(0.0); |
| 253 |
float intensity; |
| 254 |
|
| 255 |
// Material/texel properties |
| 256 |
float mat_shininess; |
| 257 |
vec2 st; |
| 258 |
vec4 mat_ambient, mat_diffuse, mat_specular, dxdy; |
| 259 |
|
| 260 |
// get noise at different wavelengths |
| 261 |
|
| 262 |
// used: 5m, 5m gradient, 10m, 10m gradient: heightmap of the closeup terrain, 10m also snow |
| 263 |
// 50m: detail texel |
| 264 |
// 250m: detail texel |
| 265 |
// 500m: distortion and overlay |
| 266 |
// 1500m: overlay, detail, dust, fog |
| 267 |
// 2000m: overlay, detail, snow, fog |
| 268 |
|
| 269 |
float noise_10m; |
| 270 |
float noise_5m; |
| 271 |
noise_10m = Noise2D(rawPos.xy, 10.0); |
| 272 |
noise_5m = Noise2D(rawPos.xy ,5.0); |
| 273 |
|
| 274 |
float noisegrad_10m; |
| 275 |
float noisegrad_5m; |
| 276 |
|
| 277 |
float noise_50m = Noise2D(rawPos.xy, 50.0);; |
| 278 |
|
| 279 |
|
| 280 |
float noise_250m = Noise3D(worldPos.xyz,250.0); |
| 281 |
float noise_500m = Noise3D(worldPos.xyz, 500.0); |
| 282 |
float noise_1500m = Noise3D(worldPos.xyz, 1500.0); |
| 283 |
float noise_2000m = Noise3D(worldPos.xyz, 2000.0); |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
// |
| 288 |
|
| 289 |
|
| 290 |
// get the texels |
| 291 |
|
| 292 |
// Oct 27 2021: |
| 293 |
// Geometry is in the form of roughly rectangular 'tiles' |
| 294 |
// with a mesh forming a grid with regular spacing. |
| 295 |
// Each vertex in the mesh is given an elevation |
| 296 |
|
| 297 |
// Tile dimensions in m |
| 298 |
// Testing: created from two float uniforms in global scope. Should be sent as a vec2 |
| 299 |
// vec2 tile_size |
| 300 |
|
| 301 |
// Tile texture coordinates range [0..1] over the tile 'rectangle' |
| 302 |
vec2 tile_coord = gl_TexCoord[0].st; |
| 303 |
|
| 304 |
// Test phase: Constants and toggles for transitions between landlcasses are defined at |
| 305 |
// the top of this file. |
| 306 |
|
| 307 |
// Look up the landclass id [0 .. 255] for this particular fragment |
| 308 |
// and any neighbouring landclass that is close. |
| 309 |
// Each tile has 1 texture containing landclass ids stetched over it. |
| 310 |
|
| 311 |
// Landclass for current fragment, and up-to 4 neighboring landclasses - 2 used currently |
| 312 |
int lc; |
| 313 |
ivec4 lc_n; |
| 314 |
|
| 315 |
int num_unique_neighbors = 0; |
| 316 |
|
| 317 |
// Mix factor of base textures for 2 neighbour landclass(es) |
| 318 |
vec4 mfact; |
| 319 |
|
| 320 |
bool water_lc = false; |
| 321 |
bool mix_water_texel = false; |
| 322 |
|
| 323 |
// Partial derivatives of s and t of ground texture coords for this fragment, |
| 324 |
// with respect to window (screen space) x and y axes. |
| 325 |
// Used to pick mipmap LoD levels, and turn off unneeded procedural detail |
| 326 |
// dFdx and dFdy are packed in a vec4 so multiplying everything |
| 327 |
// to scale takes 1 instruction slot. |
| 328 |
vec4 dxdy_gc = vec4(dFdx(ground_tex_coord) , dFdy(ground_tex_coord)); |
| 329 |
|
| 330 |
get_landclass_id(tile_coord, dxdy_gc, lc, lc_n, num_unique_neighbors, mfact); |
| 331 |
get_material(lc, ground_tex_coord, dxdy_gc, mat_shininess, mat_ambient, mat_diffuse, mat_specular, dxdy, st); |
| 332 |
|
| 333 |
if (fg_photoScenery) { |
| 334 |
texel = texture(landclass, vec2(gl_TexCoord[0].s, 1.0 - gl_TexCoord[0].t)); |
| 335 |
water_lc = (texture(coastline, vec2(tile_coord.s, tile_coord.t)).r > 0.1); |
| 336 |
} else { |
| 337 |
// Lookup the base texture texel for this fragment and any neighbors, with mixing |
| 338 |
texel = get_mixed_texel(0, ground_tex_coord, lc, num_unique_neighbors, lc_n, mfact, dxdy_gc); |
| 339 |
water_lc = texture(landclass, vec2(tile_coord.s, tile_coord.t)).z > 0.9; |
| 340 |
} |
| 341 |
|
| 342 |
float steepness_modifier = texture2D(coastline, tile_coord).g * 0.1; |
| 343 |
bool water = water_lc || texture2D(coastline, tile_coord).b > 0.05; |
| 344 |
|
| 345 |
if ((water_shader == 1) && (water && (steepness + steepness_modifier)< WATER_START)) { |
| 346 |
|
| 347 |
// This is a water fragment, so calculate the fragment color procedurally, but mix in the steep and beach |
| 348 |
if (water_lc) { |
| 349 |
lc = fg_shoreAtlasIndex; |
| 350 |
get_material(lc, ground_tex_coord, dxdy_gc, mat_shininess, mat_ambient, mat_diffuse, mat_specular, dxdy, st); |
| 351 |
} |
| 352 |
|
| 353 |
vec4 steep_texel = lookup_ground_texture_array(0, ground_tex_coord, lc, dxdy_gc); // Uses the same index as the gradient texture, which it is |
| 354 |
vec4 beach_texel = lookup_ground_texture_array(0, ground_tex_coord, fg_shoreAtlasIndex, dxdy_gc); // Use the shore texture |
| 355 |
texel = mix(steep_texel, beach_texel, smoothstep(WATER_STEEP, WATER_STEEP_TO_BEACH, steepness + steepness_modifier)); |
| 356 |
|
| 357 |
// Flag that we need to mix in a water texel later if appropriate. |
| 358 |
mix_water_texel = (steepness + steepness_modifier > WATER_BEACH_TO_WATER); |
| 359 |
water = false; |
| 360 |
} |
| 361 |
|
| 362 |
if ((water_shader == 1) && water) { |
| 363 |
fragColor = generateWaterTexel(); |
| 364 |
fragColor.rgb += getClusteredLightsContribution(eyePos.xyz, n, fragColor.rgb); |
| 365 |
} else { |
| 366 |
// Photoscenery or land fragment, so determine the shading and color normally |
| 367 |
vec4 color = gl_Color * mat_ambient; |
| 368 |
color.a = 1.0; |
| 369 |
|
| 370 |
// Testing code: mix with green to show values of variables at each point |
| 371 |
//vec4 green = vec4(0.0, 0.5, 0.0, 0.0); |
| 372 |
//texel = mix(texel, green, (mfact[2])); |
| 373 |
|
| 374 |
//mix_texel = texel; |
| 375 |
//detail_texel = texel; |
| 376 |
vec4 t = texel; |
| 377 |
|
| 378 |
int flag = 1; |
| 379 |
int mix_flag = 1; |
| 380 |
|
| 381 |
float local_autumn_factor = texel.a; |
| 382 |
|
| 383 |
if (fg_photoScenery) { |
| 384 |
flag = 0; |
| 385 |
mix_flag = 0; |
| 386 |
} |
| 387 |
|
| 388 |
float distortion_factor = 1.0; |
| 389 |
vec2 stprime; |
| 390 |
|
| 391 |
float noise_term; |
| 392 |
float snow_alpha; |
| 393 |
|
| 394 |
//float view_angle = abs(dot(normal, normalize(ecViewdir))); |
| 395 |
|
| 396 |
if ((quality_level > 3)&&(msl_altitude +500.0 > snowlevel)) { |
| 397 |
float sfactor; |
| 398 |
snow_texel = vec4 (0.95, 0.95, 0.95, 1.0) * (0.9 + 0.1* noise_500m + 0.1* (1.0 - noise_10m) ); |
| 399 |
snow_texel.r = snow_texel.r * (0.9 + 0.05 * (noise_10m + noise_5m)); |
| 400 |
snow_texel.g = snow_texel.g * (0.9 + 0.05 * (noise_10m + noise_5m)); |
| 401 |
snow_texel.a = 1.0; |
| 402 |
noise_term = 0.1 * (noise_500m-0.5); |
| 403 |
sfactor = sqrt(2.0 * (1.0-steepness)/0.03) + abs(ct)/0.15; |
| 404 |
noise_term = noise_term + 0.2 * (noise_50m -0.5) * (1.0 - smoothstep(18000.0*sfactor, 40000.0*sfactor, dist) ) ; |
| 405 |
noise_term = noise_term + 0.3 * (noise_10m -0.5) * (1.0 - smoothstep(4000.0 * sfactor, 8000.0 * sfactor, dist) ) ; |
| 406 |
|
| 407 |
if (dist < 3000.0*sfactor) { |
| 408 |
noise_term = noise_term + 0.3 * (noise_5m -0.5) * (1.0 - smoothstep(1000.0 * sfactor, 3000.0 *sfactor, dist) ); |
| 409 |
} |
| 410 |
|
| 411 |
snow_texel.a = snow_texel.a * 0.2+0.8* smoothstep(0.2,0.8, 0.3 +noise_term + snow_thickness_factor +0.0001*(msl_altitude -snowlevel) ); |
| 412 |
|
| 413 |
} |
| 414 |
|
| 415 |
if ((tquality_level > 2) && (mix_flag == 1)) |
| 416 |
{ |
| 417 |
// Mix texture is material texture 15, which is mapped to the b channel of fg_textureLookup1 |
| 418 |
//int tex2 = int(fg_textureLookup1[lc].b * 255.0 + 0.5); |
| 419 |
//mix_texel = texture(textureArray, vec3(gl_TexCoord[0].st * 1.3, tex2)); |
| 420 |
if (mix_texel.a < 0.1) { mix_flag = 0;} |
| 421 |
//WS2: mix_texel = texture2D(mix_texture, gl_TexCoord[0].st * 1.3); // temp |
| 422 |
|
| 423 |
mix_texel = lookup_ground_texture_array(4, st * 1.3, lc, dxdy * 1.3); |
| 424 |
if (mix_texel.a <0.1) {mix_flag = 0;} |
| 425 |
} |
| 426 |
|
| 427 |
if (tquality_level > 3 && (flag == 1)) |
| 428 |
{ |
| 429 |
stprime = vec2 (0.86*gl_TexCoord[0].s + 0.5*gl_TexCoord[0].t, 0.5*gl_TexCoord[0].s - 0.86*gl_TexCoord[0].t); |
| 430 |
//distortion_factor = 0.9375 + (1.0 * nvL[2]); |
| 431 |
distortion_factor = 0.97 + 0.06 * noise_500m; |
| 432 |
stprime = stprime * distortion_factor * 15.0; |
| 433 |
if (quality_level > 4) |
| 434 |
{ |
| 435 |
stprime = stprime + normalize(relPos).xy * 0.02 * (noise_10m + 0.5 * noise_5m - 0.75); |
| 436 |
} |
| 437 |
|
| 438 |
// Detail texture is material texture 11, which is mapped to the g channel of fg_textureLookup1 |
| 439 |
//int tex3 = int(fg_textureLookup1[lc].g * 255.0 + 0.5); |
| 440 |
//detail_texel = texture(textureArray, vec3(stprime, tex3)); |
| 441 |
if (detail_texel.a < 0.1) { flag = 0;} |
| 442 |
//WS2: detail_texel = texture2D(detail_texture, stprime); // temp |
| 443 |
|
| 444 |
vec4 dxdy_prime = vec4(dFdx(stprime), dFdy(stprime)); |
| 445 |
detail_texel = lookup_ground_texture_array(5, stprime, lc, dxdy_prime); |
| 446 |
} |
| 447 |
|
| 448 |
// texture preparation according to detail level |
| 449 |
|
| 450 |
// mix in hires texture patches |
| 451 |
|
| 452 |
float dist_fact; |
| 453 |
float nSum; |
| 454 |
float mix_factor; |
| 455 |
float transition_model = fg_materialParams1[lc].r; |
| 456 |
float hires_overlay_bias = fg_materialParams1[lc].g; |
| 457 |
|
| 458 |
if (tquality_level > 2) { |
| 459 |
// first the second texture overlay |
| 460 |
// transition model 0: random patch overlay without any gradient information |
| 461 |
// transition model 1: only gradient-driven transitions, no randomness |
| 462 |
|
| 463 |
|
| 464 |
if (mix_flag == 1) { |
| 465 |
// Random patch overlay weighting with noise |
| 466 |
nSum = 0.18 * (2.0 * noise_2000m + 2.0 * noise_1500m + noise_500m); |
| 467 |
|
| 468 |
// Increase the weighting for the mix_texel if more gradient-driven. |
| 469 |
nSum = mix(nSum, 0.5, max(0.0, 2.0 * (transition_model - 0.5))); |
| 470 |
|
| 471 |
// Add the gradient element |
| 472 |
nSum = nSum + 0.4 * (1.0 -smoothstep(0.9,0.95, abs(steepness)+ 0.05 * (noise_50m - 0.5))) * min(1.0, 2.0 * transition_model); |
| 473 |
mix_factor = smoothstep(0.5, 0.54, nSum); |
| 474 |
texel = mix(texel, mix_texel, mix_factor); |
| 475 |
local_autumn_factor = texel.a; |
| 476 |
} |
| 477 |
|
| 478 |
} |
| 479 |
|
| 480 |
if (tquality_level > 3) { |
| 481 |
// then the detail texture overlay |
| 482 |
if (dist < 40000.0) |
| 483 |
{ |
| 484 |
if (flag == 1) { |
| 485 |
//noise_50m = Noise2D(rawPos.xy, 50.0); |
| 486 |
//noise_250m = Noise2D(rawPos.xy, 250.0); |
| 487 |
dist_fact = 0.1 * smoothstep(15000.0,40000.0, dist) - 0.03 * (1.0 - smoothstep(500.0,5000.0, dist)); |
| 488 |
nSum = ((1.0 -noise_2000m) + noise_1500m + 2.0 * noise_250m +noise_50m)/5.0; |
| 489 |
nSum = nSum - 0.08 * (1.0 -smoothstep(0.9,0.95, abs(steepness))); |
| 490 |
mix_factor = smoothstep(0.47, 0.54, nSum +hires_overlay_bias - dist_fact); |
| 491 |
if (mix_factor > 0.8) {mix_factor = 0.8;} |
| 492 |
texel = mix(texel, detail_texel,mix_factor); |
| 493 |
local_autumn_factor = texel.a; |
| 494 |
} |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
// autumn colors |
| 501 |
|
| 502 |
float autumn_factor = season * 2.0 * (1.0 - local_autumn_factor) ; |
| 503 |
|
| 504 |
|
| 505 |
texel.r = min(1.0, (1.0 + 2.5 * autumn_factor) * texel.r); |
| 506 |
texel.g = texel.g; |
| 507 |
texel.b = max(0.0, (1.0 - 4.0 * autumn_factor) * texel.b); |
| 508 |
|
| 509 |
|
| 510 |
if (local_autumn_factor < 1.0) |
| 511 |
{ |
| 512 |
intensity = length(texel.rgb) * (1.0 - 0.5 * smoothstep(1.1,2.0,season)); |
| 513 |
texel.rgb = intensity * normalize(mix(texel.rgb, vec3(0.23,0.17,0.08), smoothstep(1.1,2.0, season))); |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
const vec4 dust_color = vec4 (0.76, 0.71, 0.56, 1.0); |
| 519 |
const vec4 lichen_color = vec4 (0.17, 0.20, 0.06, 1.0);; |
| 520 |
//float snow_alpha; |
| 521 |
|
| 522 |
if (quality_level > 3) |
| 523 |
{ |
| 524 |
|
| 525 |
// mix vegetation |
| 526 |
texel = mix(texel, lichen_color, 0.4 * lichen_cover_factor + 0.8 * lichen_cover_factor * 0.5 * (noise_10m + (1.0 - noise_5m)) ); |
| 527 |
// mix dust |
| 528 |
texel = mix(texel, dust_color, clamp(0.5 * dust_cover_factor + 3.0 * dust_cover_factor * (((noise_1500m - 0.5) * 0.125)+0.125 ),0.0, 1.0) ); |
| 529 |
|
| 530 |
// mix snow |
| 531 |
if (msl_altitude +500.0 > snowlevel) |
| 532 |
{ |
| 533 |
snow_alpha = smoothstep(0.75, 0.85, abs(steepness)); |
| 534 |
//texel = mix(texel, snow_texel, texel_snow_fraction); |
| 535 |
texel = mix(texel, snow_texel, snow_texel.a* smoothstep(snowlevel, snowlevel+200.0, snow_alpha * (msl_altitude)+ (noise_2000m + 0.1 * noise_10m -0.55) *400.0)); |
| 536 |
} |
| 537 |
} |
| 538 |
else if (msl_altitude +500.0 > snowlevel) |
| 539 |
{ |
| 540 |
float snow_alpha = 0.5+0.5* smoothstep(0.2,0.8, 0.3 + snow_thickness_factor +0.0001*(msl_altitude -snowlevel) ); |
| 541 |
// texel = vec4(dot(vec3(0.2989, 0.5870, 0.1140), texel.rgb)); |
| 542 |
texel = mix(texel, vec4(1.0), snow_alpha* smoothstep(snowlevel, snowlevel+200.0, (msl_altitude))); |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
// get distribution of water when terrain is wet |
| 548 |
|
| 549 |
float water_threshold1; |
| 550 |
float water_threshold2; |
| 551 |
float water_factor =0.0; |
| 552 |
|
| 553 |
|
| 554 |
if ((dist < 5000.0)&& (quality_level > 3) && (wetness>0.0)) |
| 555 |
{ |
| 556 |
water_threshold1 = 1.0-0.5* wetness; |
| 557 |
water_threshold2 = 1.0 - 0.3 * wetness; |
| 558 |
water_factor = smoothstep(water_threshold1, water_threshold2 , (0.3 * (2.0 * (1.0-noise_10m) + (1.0 -noise_5m)) * (1.0 - smoothstep(2000.0, 5000.0, dist))) - 5.0 * (1.0 -steepness)); |
| 559 |
} |
| 560 |
|
| 561 |
// darken wet terrain |
| 562 |
|
| 563 |
texel.rgb = texel.rgb * (1.0 - 0.6 * wetness); |
| 564 |
|
| 565 |
|
| 566 |
// light computations |
| 567 |
|
| 568 |
|
| 569 |
vec4 light_specular = gl_LightSource[0].specular; |
| 570 |
|
| 571 |
// If gl_Color.a == 0, this is a back-facing polygon and the |
| 572 |
// normal should be reversed. |
| 573 |
//n = (2.0 * gl_Color.a - 1.0) * normal; |
| 574 |
n = normal;//vec3 (nvec.x, nvec.y, sqrt(1.0 -pow(nvec.x,2.0) - pow(nvec.y,2.0) )); |
| 575 |
n = normalize(n); |
| 576 |
|
| 577 |
NdotL = dot(n, lightDir); |
| 578 |
if ((tquality_level > 3) && (mix_flag ==1)&& (dist < 2000.0) && (quality_level > 4)) |
| 579 |
{ |
| 580 |
noisegrad_10m = (noise_10m - Noise2D(rawPos.xy+ 0.05 * normalize(lightDir.xy),10.0))/0.05; |
| 581 |
noisegrad_5m = (noise_5m - Noise2D(rawPos.xy+ 0.05 * normalize(lightDir.xy),5.0))/0.05; |
| 582 |
NdotL = NdotL + 1.0 * (noisegrad_10m + 0.5* noisegrad_5m) * mix_factor/0.8 * (1.0 - smoothstep(1000.0, 2000.0, dist)); |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
if (NdotL > 0.0) { |
| 587 |
float shadowmap = getShadowing(); |
| 588 |
vec4 diffuse_term = light_diffuse_comp * mat_diffuse; |
| 589 |
color += diffuse_term * NdotL * shadowmap; |
| 590 |
NdotHV = max(dot(n, halfVector), 0.0); |
| 591 |
if (mat_shininess > 0.0) |
| 592 |
specular.rgb = (mat_specular.rgb |
| 593 |
* light_specular.rgb |
| 594 |
* pow(NdotHV, mat_shininess) |
| 595 |
* shadowmap); |
| 596 |
} |
| 597 |
color.a = 1.0;//diffuse_term.a; |
| 598 |
// This shouldn't be necessary, but our lighting becomes very |
| 599 |
// saturated. Clamping the color before modulating by the texture |
| 600 |
// is closer to what the OpenGL fixed function pipeline does. |
| 601 |
color = clamp(color, 0.0, 1.0); |
| 602 |
|
| 603 |
fragColor = color * texel + specular; |
| 604 |
|
| 605 |
if (mix_water_texel) { fragColor = mix(fragColor, generateWaterTexel(), smoothstep(WATER_BEACH_TO_WATER, WATER_START, steepness + steepness_modifier)); } |
| 606 |
|
| 607 |
fragColor.rgb += getClusteredLightsContribution(eyePos.xyz, n, texel.rgb); |
| 608 |
} |
| 609 |
|
| 610 |
float lightArg = (terminator-yprime_alt)/100000.0; |
| 611 |
vec3 hazeColor = get_hazeColor(lightArg); |
| 612 |
gl_FragColor = applyHaze(fragColor, hazeColor, vec3(0.0), ct, hazeLayerAltitude, visibility, avisibility, dist, lightArg, mie_angle); |
| 613 |
|
| 614 |
|
| 615 |
// Testing phase controls: |
| 616 |
if (remove_haze_and_lighting == 1) |
| 617 |
{ |
| 618 |
gl_FragColor = texel; |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
} |