1
0
Fork 0

WS30: Combined land and water shader

Previously the water shader was separate and executed on a
different mesh.

This adds the water shader as a separate fragment function
(ws30-water.frag) and uses a material parameter passed in
as a Uniform to use it in preference to the usual texel
lookup.

Performance testing found a slight performance improvement
from having a single mesh, but a slight performance impact
from the extra fragment shader complexity.
This commit is contained in:
Stuart Buchanan 2022-02-05 15:24:02 +00:00
parent d3b667c853
commit 98fda02e65
13 changed files with 1629 additions and 821 deletions

View file

@ -5,28 +5,28 @@
<parameters>
<material>
<ambient type="vec4d">
0.2 .2 0.2 1.0
</ambient>
0.2 .2 0.2 1.0
</ambient>
<diffuse type="vec4d">
.8 .8 .8 1.0
</diffuse>
.8 .8 .8 1.0
</diffuse>
<specular type="vec4d">
0.0 0.0 0.0 1.0
</specular>
0.0 0.0 0.0 1.0
</specular>
<emissive type="vec4d">
0.0 0.0 0.0 1.0
</emissive>
0.0 0.0 0.0 1.0
</emissive>
<shininess>1.2</shininess>
</material>
<texture n="6">
<image>Textures/perlin.png</image>
<type>2d</type>
<filter>nearest</filter>
<wrap-s>repeat</wrap-s>
<wrap-t>repeat</wrap-t>
<internal-format>normalized</internal-format>
</texture>
<texture n="6">
<image>Textures/perlin.png</image>
<type>2d</type>
<filter>nearest</filter>
<wrap-s>repeat</wrap-s>
<wrap-t>repeat</wrap-t>
<internal-format>normalized</internal-format>
</texture>
<texture n="10">
<image>Textures/Terrain/snow3.png</image>
@ -107,6 +107,16 @@
<wind_effects><use>/sim/rendering/shaders/wind-effects</use></wind_effects>
<windE><use>/environment/sea/surface/wind-from-east-fps</use></windE>
<windN><use>/environment/sea/surface/wind-from-north-fps</use></windN>
<WaveFreq><use>/environment/wave/freq</use></WaveFreq>
<WaveAmp><use>/environment/wave/amp</use></WaveAmp>
<WaveSharp><use>/environment/wave/sharp</use></WaveSharp>
<WaveAngle><use>/environment/wave/angle</use></WaveAngle>
<WaveFactor><use>/environment/wave/factor</use></WaveFactor>
<WaveDAngle><use>/environment/wave/dangle</use></WaveDAngle>
<sea_r><use>/environment/sea/color_r</use></sea_r>
<sea_g><use>/environment/sea/color_g</use></sea_g>
<sea_b><use>/environment/sea/color_b</use></sea_b>
<saturation><use>/rendering/scene/saturation</use></saturation>
<display_xsize><use>/sim/startup/xsize</use></display_xsize>
<display_ysize><use>/sim/startup/ysize</use></display_ysize>
<!--
@ -289,6 +299,7 @@
<vertex-shader>Shaders/shadows-include.vert</vertex-shader>
<fragment-shader>Shaders/ws30-ALS-ultra.frag</fragment-shader>
<fragment-shader>Shaders/ws30-ALS-landclass-search-functions.frag</fragment-shader>
<fragment-shader>Shaders/ws30-water.frag</fragment-shader>
<fragment-shader>Shaders/noise.frag</fragment-shader>
<fragment-shader>Shaders/cloud-shadowfunc.frag</fragment-shader>
<fragment-shader>Shaders/hazes.frag</fragment-shader>
@ -347,12 +358,12 @@
<type>float</type>
<value><use>eye_alt</use></value>
</uniform>
<uniform>
<uniform>
<name>eye_lat</name>
<type>float</type>
<value><use>eye_lat</use></value>
</uniform>
<uniform>
<uniform>
<name>eye_lon</name>
<type>float</type>
<value><use>eye_lon</use></value>
@ -363,9 +374,9 @@
<value><use>snow_level</use></value>
</uniform>
<uniform>
<name>snow_thickness_factor</name>
<type>float</type>
<value><use>snow_thickness_factor</use></value>
<name>snow_thickness_factor</name>
<type>float</type>
<value><use>snow_thickness_factor</use></value>
</uniform>
<uniform>
<name>dust_cover_factor</name>
@ -373,9 +384,9 @@
<value><use>dust_cover_factor</use></value>
</uniform>
<uniform>
<name>lichen_cover_factor</name>
<type>float</type>
<value> <use>lichen_cover_factor</use></value>
<name>lichen_cover_factor</name>
<type>float</type>
<value> <use>lichen_cover_factor</use></value>
</uniform>
<uniform>
<name>wetness</name>
@ -490,220 +501,294 @@
<type>float</type>
<value><use>cloudpos1_y</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos2_x</name>
<type>float</type>
<value><use>cloudpos2_x</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos2_y</name>
<type>float</type>
<value><use>cloudpos2_y</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos3_x</name>
<type>float</type>
<value><use>cloudpos3_x</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos3_y</name>
<type>float</type>
<value><use>cloudpos3_y</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos4_x</name>
<type>float</type>
<value><use>cloudpos4_x</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos4_y</name>
<type>float</type>
<value><use>cloudpos4_y</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos5_x</name>
<type>float</type>
<value><use>cloudpos5_x</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos5_y</name>
<type>float</type>
<value><use>cloudpos5_y</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos6_x</name>
<type>float</type>
<value><use>cloudpos6_x</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos6_y</name>
<type>float</type>
<value><use>cloudpos6_y</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos7_x</name>
<type>float</type>
<value><use>cloudpos7_x</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos7_y</name>
<type>float</type>
<value><use>cloudpos7_y</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos8_x</name>
<type>float</type>
<value><use>cloudpos8_x</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos8_y</name>
<type>float</type>
<value><use>cloudpos8_y</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos9_x</name>
<type>float</type>
<value><use>cloudpos9_x</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos9_y</name>
<type>float</type>
<value><use>cloudpos9_y</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos10_x</name>
<type>float</type>
<value><use>cloudpos10_x</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos10_y</name>
<type>float</type>
<value><use>cloudpos10_y</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos11_x</name>
<type>float</type>
<value><use>cloudpos11_x</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos11_y</name>
<type>float</type>
<value><use>cloudpos11_y</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos12_x</name>
<type>float</type>
<value><use>cloudpos12_x</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos12_y</name>
<type>float</type>
<value><use>cloudpos12_y</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos13_x</name>
<type>float</type>
<value><use>cloudpos13_x</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos13_y</name>
<type>float</type>
<value><use>cloudpos13_y</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos14_x</name>
<type>float</type>
<value><use>cloudpos14_x</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos14_y</name>
<type>float</type>
<value><use>cloudpos14_y</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos15_x</name>
<type>float</type>
<value><use>cloudpos15_x</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos15_y</name>
<type>float</type>
<value><use>cloudpos15_y</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos16_x</name>
<type>float</type>
<value><use>cloudpos16_x</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos16_y</name>
<type>float</type>
<value><use>cloudpos16_y</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos17_x</name>
<type>float</type>
<value><use>cloudpos17_x</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos17_y</name>
<type>float</type>
<value><use>cloudpos17_y</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos18_x</name>
<type>float</type>
<value><use>cloudpos18_x</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos18_y</name>
<type>float</type>
<value><use>cloudpos18_y</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos19_x</name>
<type>float</type>
<value><use>cloudpos19_x</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos19_y</name>
<type>float</type>
<value><use>cloudpos19_y</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos20_x</name>
<type>float</type>
<value><use>cloudpos20_x</use></value>
</uniform>
<uniform>
<uniform>
<name>cloudpos20_y</name>
<type>float</type>
<value><use>cloudpos20_y</use></value>
</uniform>
<uniform>
<name>saturation</name>
<type>float</type>
<value><use>saturation</use></value>
</uniform>
<uniform>
<name>WindE</name>
<type>float</type>
<value><use>windE</use></value>
<name>WindE</name>
<type>float</type>
<value><use>windE</use></value>
</uniform>
<uniform>
<name>WindN</name>
<type>float</type>
<value><use>windN</use></value>
<name>WindN</name>
<type>float</type>
<value><use>windN</use></value>
</uniform>
<uniform>
<name>use_searchlight</name>
<type>int</type>
<value> <use>use_searchlight</use></value>
<name>WaveFreq</name>
<type>float</type>
<value>
<use>WaveFreq</use>
</value>
</uniform>
<uniform>
<name>use_landing_light</name>
<type>int</type>
<value> <use>use_landing_light</use></value>
<name>WaveAmp</name>
<type>float</type>
<value>
<use>WaveAmp</use>
</value>
</uniform>
<uniform>
<name>use_alt_landing_light</name>
<type>int</type>
<value> <use>use_alt_landing_light</use></value>
<name>WaveSharp</name>
<type>float</type>
<value>
<use>WaveSharp</use>
</value>
</uniform>
<uniform>
<name>WaveAngle</name>
<type>float</type>
<value>
<use>WaveAngle</use>
</value>
</uniform>
<uniform>
<name>WaveFactor</name>
<type>float</type>
<value>
<use>WaveFactor</use>
</value>
</uniform>
<uniform>
<name>WaveDAngle</name>
<type>float</type>
<value>
<use>WaveDAngle</use>
</value>
</uniform>
<!-- sea colors -->
<uniform>
<name>sea_r</name>
<type>float</type>
<value>
<use>sea_r</use>
</value>
</uniform>
<uniform>
<name>sea_g</name>
<type>float</type>
<value>
<use>sea_g</use>
</value>
</uniform>
<uniform>
<name>sea_b</name>
<type>float</type>
<value>
<use>sea_b</use>
</value>
</uniform>
<uniform>
<name>ocean_flag</name>
<type>int</type>
<value><use>ocean_flag</use></value>
</uniform>
<uniform>
<name>use_searchlight</name>
<type>int</type>
<value> <use>use_searchlight</use></value>
</uniform>
<uniform>
<name>use_landing_light</name>
<type>int</type>
<value> <use>use_landing_light</use></value>
</uniform>
<uniform>
<name>use_alt_landing_light</name>
<type>int</type>
<value> <use>use_alt_landing_light</use></value>
</uniform>
<uniform>
<name>display_xsize</name>
@ -726,9 +811,9 @@
<value><use>cloud_shadow_flag</use></value>
</uniform>
<uniform>
<name>raise_vertex</name>
<type>bool</type>
<value> <use>raise_vertex</use></value>
<name>raise_vertex</name>
<type>bool</type>
<value> <use>raise_vertex</use></value>
</uniform>
<uniform>
<name>landclass</name>
@ -891,6 +976,7 @@
<vertex-shader>Shaders/shadows-include.vert</vertex-shader>
<fragment-shader>Shaders/ws30-ALS-detailed.frag</fragment-shader>
<fragment-shader>Shaders/ws30-ALS-landclass-search-functions.frag</fragment-shader>
<fragment-shader>Shaders/ws30-water.frag</fragment-shader>
<fragment-shader>Shaders/noise.frag</fragment-shader>
<fragment-shader>Shaders/hazes.frag</fragment-shader>
<fragment-shader>Shaders/filters-ALS.frag</fragment-shader>
@ -948,9 +1034,9 @@
<value><use>snow_level</use></value>
</uniform>
<uniform>
<name>snow_thickness_factor</name>
<type>float</type>
<value><use>snow_thickness_factor</use></value>
<name>snow_thickness_factor</name>
<type>float</type>
<value><use>snow_thickness_factor</use></value>
</uniform>
<uniform>
<name>dust_cover_factor</name>
@ -958,9 +1044,9 @@
<value><use>dust_cover_factor</use></value>
</uniform>
<uniform>
<name>lichen_cover_factor</name>
<type>float</type>
<value> <use>lichen_cover_factor</use></value>
<name>lichen_cover_factor</name>
<type>float</type>
<value> <use>lichen_cover_factor</use></value>
</uniform>
<uniform>
<name>wetness</name>
@ -992,7 +1078,94 @@
<type>float</type>
<value><use>season</use></value>
</uniform>
<!-- filtering -->
<uniform>
<name>saturation</name>
<type>float</type>
<value><use>saturation</use></value>
</uniform>
<uniform>
<name>WindE</name>
<type>float</type>
<value>
<use>windE</use>
</value>
</uniform>
<uniform>
<name>WindN</name>
<type>float</type>
<value>
<use>windN</use>
</value>
</uniform>
<uniform>
<name>WaveFreq</name>
<type>float</type>
<value>
<use>WaveFreq</use>
</value>
</uniform>
<uniform>
<name>WaveAmp</name>
<type>float</type>
<value>
<use>WaveAmp</use>
</value>
</uniform>
<uniform>
<name>WaveSharp</name>
<type>float</type>
<value>
<use>WaveSharp</use>
</value>
</uniform>
<uniform>
<name>WaveAngle</name>
<type>float</type>
<value>
<use>WaveAngle</use>
</value>
</uniform>
<uniform>
<name>WaveFactor</name>
<type>float</type>
<value>
<use>WaveFactor</use>
</value>
</uniform>
<uniform>
<name>WaveDAngle</name>
<type>float</type>
<value>
<use>WaveDAngle</use>
</value>
</uniform>
<!-- sea colors -->
<uniform>
<name>sea_r</name>
<type>float</type>
<value>
<use>sea_r</use>
</value>
</uniform>
<uniform>
<name>sea_g</name>
<type>float</type>
<value>
<use>sea_g</use>
</value>
</uniform>
<uniform>
<name>sea_b</name>
<type>float</type>
<value>
<use>sea_b</use>
</value>
</uniform>
<uniform>
<name>ocean_flag</name>
<type>int</type>
<value><use>ocean_flag</use></value>
</uniform> <!-- filtering -->
<uniform>
<name>gamma</name>
<type>float</type>
@ -1191,6 +1364,7 @@
<vertex-shader>Shaders/shadows-include.vert</vertex-shader>
<fragment-shader>Shaders/ws30-ALS.frag</fragment-shader>
<fragment-shader>Shaders/ws30-ALS-landclass-search-functions.frag</fragment-shader>
<fragment-shader>Shaders/ws30-water.frag</fragment-shader>
<fragment-shader>Shaders/hazes.frag</fragment-shader>
<fragment-shader>Shaders/noise.frag</fragment-shader>
<fragment-shader>Shaders/filters-ALS.frag</fragment-shader>
@ -1267,6 +1441,94 @@
<type>float</type>
<value><use>air_pollution</use></value>
</uniform>
<uniform>
<name>saturation</name>
<type>float</type>
<value><use>saturation</use></value>
</uniform>
<uniform>
<name>WindE</name>
<type>float</type>
<value>
<use>windE</use>
</value>
</uniform>
<uniform>
<name>WindN</name>
<type>float</type>
<value>
<use>windN</use>
</value>
</uniform>
<uniform>
<name>WaveFreq</name>
<type>float</type>
<value>
<use>WaveFreq</use>
</value>
</uniform>
<uniform>
<name>WaveAmp</name>
<type>float</type>
<value>
<use>WaveAmp</use>
</value>
</uniform>
<uniform>
<name>WaveSharp</name>
<type>float</type>
<value>
<use>WaveSharp</use>
</value>
</uniform>
<uniform>
<name>WaveAngle</name>
<type>float</type>
<value>
<use>WaveAngle</use>
</value>
</uniform>
<uniform>
<name>WaveFactor</name>
<type>float</type>
<value>
<use>WaveFactor</use>
</value>
</uniform>
<uniform>
<name>WaveDAngle</name>
<type>float</type>
<value>
<use>WaveDAngle</use>
</value>
</uniform>
<!-- sea colors -->
<uniform>
<name>sea_r</name>
<type>float</type>
<value>
<use>sea_r</use>
</value>
</uniform>
<uniform>
<name>sea_g</name>
<type>float</type>
<value>
<use>sea_g</use>
</value>
</uniform>
<uniform>
<name>sea_b</name>
<type>float</type>
<value>
<use>sea_b</use>
</value>
</uniform>
<uniform>
<name>ocean_flag</name>
<type>int</type>
<value><use>ocean_flag</use></value>
</uniform>
<!-- filtering -->
<uniform>
<name>gamma</name>
@ -1440,7 +1702,7 @@
<vertex-shader>Shaders/include_fog.vert</vertex-shader>
<vertex-shader>Shaders/shadows-include.vert</vertex-shader>
<vertex-shader>Shaders/ws30.vert</vertex-shader>
<fragment-shader n="0">Shaders/include_fog.frag</fragment-shader>
<fragment-shader n="0">Shaders/include_fog.frag</fragment-shader>
<fragment-shader n="1">Shaders/ws30-q1.frag</fragment-shader>
<fragment-shader>Shaders/shadows-include.frag</fragment-shader>
<fragment-shader>Shaders/clustered-include.frag</fragment-shader>
@ -1534,7 +1796,7 @@
<vertex-shader>Shaders/include_fog.vert</vertex-shader>
<vertex-shader>Shaders/shadows-include.vert</vertex-shader>
<vertex-shader>Shaders/ws30.vert</vertex-shader>
<fragment-shader n="0">Shaders/include_fog.frag</fragment-shader>
<fragment-shader n="0">Shaders/include_fog.frag</fragment-shader>
<fragment-shader n="1">Shaders/ws30.frag</fragment-shader>
<fragment-shader>Shaders/shadows-include.frag</fragment-shader>
<fragment-shader>Shaders/clustered-include.frag</fragment-shader>

View file

@ -22,6 +22,9 @@
// Possible values: 0:Normal, 1:Just the texture.
const int remove_haze_and_lighting = 0;
// Use built-in water shader. Use for testing impact of ws30-water.frag
const int water_shader = 1;
//
// End of test phase controls
//////////////////////////////////////////////////////////////////
@ -92,6 +95,7 @@ uniform vec4 fg_specularArray[128];
uniform vec4 fg_textureLookup1[128];
uniform vec4 fg_textureLookup2[128];
uniform vec4 fg_materialParams1[128];
uniform vec4 fg_materialParams3[128];
uniform mat4 fg_zUpTransform;
uniform vec3 fg_modelOffset;
@ -144,14 +148,15 @@ vec3 getClusteredLightsContribution(vec3 p, vec3 n, vec3 texel);
// From noise.frag
float rand2D(in vec2 co);
// These functions, and other function they depend on, are defined
// in ws30-ALS-landclass-search.frag.
// Create random landclasses without a texture lookup to stress test.
// Each square of square_size in m is assigned a random landclass value.
int get_random_landclass(in vec2 co, in vec2 tile_size);
// End Test-phase code
////////////////////////
// These functions, and other function they depend on, are defined
// in ws30-ALS-landclass-search.frag.
// Lookup a ground texture at a point based on the landclass at that point, without visible
// seams at coordinate discontinuities or at landclass boundaries where texture are switched.
@ -209,11 +214,8 @@ vec4 applyHaze(inout vec4 fragColor,
in float lightArg,
in float mie_angle);
// End Test-phase code
////////////////////////
// Procedurally generate a water texel for this fragment
vec4 generateWaterTexel();
void main()
{
@ -333,244 +335,251 @@ float noise_2000m = Noise3D(worldPos.xyz, 2000.0);
texel = get_mixed_texel(0, ground_tex_coord, lc, num_unique_neighbors, lc_n, mfact, dxdy_gc);
}
vec4 color = gl_Color * mat_ambient;
color.a = 1.0;
if ((water_shader == 1) && (fg_photoScenery == false) && fg_materialParams3[lc].x > 0.5) {
// This is a water fragment, so calculate the fragment color procedurally
fragColor = generateWaterTexel();
fragColor.rgb += getClusteredLightsContribution(eyePos.xyz, n, fragColor.rgb);
} else {
// Photoscenery or land fragment, so determine the shading and color normally
vec4 color = gl_Color * mat_ambient;
color.a = 1.0;
// Testing code: mix with green to show values of variables at each point
//vec4 green = vec4(0.0, 0.5, 0.0, 0.0);
//texel = mix(texel, green, (mfact[2]));
// Testing code: mix with green to show values of variables at each point
//vec4 green = vec4(0.0, 0.5, 0.0, 0.0);
//texel = mix(texel, green, (mfact[2]));
//mix_texel = texel;
//detail_texel = texel;
vec4 t = texel;
//mix_texel = texel;
//detail_texel = texel;
vec4 t = texel;
int flag = 1;
int mix_flag = 1;
int flag = 1;
int mix_flag = 1;
float local_autumn_factor = texel.a;
float local_autumn_factor = texel.a;
if (fg_photoScenery) {
flag = 0;
mix_flag = 0;
}
if (fg_photoScenery) {
flag = 0;
mix_flag = 0;
}
float distortion_factor = 1.0;
vec2 stprime;
float distortion_factor = 1.0;
vec2 stprime;
float noise_term;
float snow_alpha;
float noise_term;
float snow_alpha;
//float view_angle = abs(dot(normal, normalize(ecViewdir)));
//float view_angle = abs(dot(normal, normalize(ecViewdir)));
if ((quality_level > 3)&&(msl_altitude +500.0 > snowlevel)) {
float sfactor;
snow_texel = vec4 (0.95, 0.95, 0.95, 1.0) * (0.9 + 0.1* noise_500m + 0.1* (1.0 - noise_10m) );
snow_texel.r = snow_texel.r * (0.9 + 0.05 * (noise_10m + noise_5m));
snow_texel.g = snow_texel.g * (0.9 + 0.05 * (noise_10m + noise_5m));
snow_texel.a = 1.0;
noise_term = 0.1 * (noise_500m-0.5);
sfactor = sqrt(2.0 * (1.0-steepness)/0.03) + abs(ct)/0.15;
noise_term = noise_term + 0.2 * (noise_50m -0.5) * (1.0 - smoothstep(18000.0*sfactor, 40000.0*sfactor, dist) ) ;
noise_term = noise_term + 0.3 * (noise_10m -0.5) * (1.0 - smoothstep(4000.0 * sfactor, 8000.0 * sfactor, dist) ) ;
if ((quality_level > 3)&&(msl_altitude +500.0 > snowlevel)) {
float sfactor;
snow_texel = vec4 (0.95, 0.95, 0.95, 1.0) * (0.9 + 0.1* noise_500m + 0.1* (1.0 - noise_10m) );
snow_texel.r = snow_texel.r * (0.9 + 0.05 * (noise_10m + noise_5m));
snow_texel.g = snow_texel.g * (0.9 + 0.05 * (noise_10m + noise_5m));
snow_texel.a = 1.0;
noise_term = 0.1 * (noise_500m-0.5);
sfactor = sqrt(2.0 * (1.0-steepness)/0.03) + abs(ct)/0.15;
noise_term = noise_term + 0.2 * (noise_50m -0.5) * (1.0 - smoothstep(18000.0*sfactor, 40000.0*sfactor, dist) ) ;
noise_term = noise_term + 0.3 * (noise_10m -0.5) * (1.0 - smoothstep(4000.0 * sfactor, 8000.0 * sfactor, dist) ) ;
if (dist < 3000.0*sfactor) {
noise_term = noise_term + 0.3 * (noise_5m -0.5) * (1.0 - smoothstep(1000.0 * sfactor, 3000.0 *sfactor, dist) );
if (dist < 3000.0*sfactor) {
noise_term = noise_term + 0.3 * (noise_5m -0.5) * (1.0 - smoothstep(1000.0 * sfactor, 3000.0 *sfactor, dist) );
}
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) );
}
if ((tquality_level > 2) && (mix_flag == 1))
{
// Mix texture is material texture 15, which is mapped to the b channel of fg_textureLookup1
//int tex2 = int(fg_textureLookup1[lc].b * 255.0 + 0.5);
//mix_texel = texture(textureArray, vec3(gl_TexCoord[0].st * 1.3, tex2));
if (mix_texel.a < 0.1) { mix_flag = 0;}
//WS2: mix_texel = texture2D(mix_texture, gl_TexCoord[0].st * 1.3); // temp
mix_texel = lookup_ground_texture_array(4, st * 1.3, lc, dxdy * 1.3);
if (mix_texel.a <0.1) {mix_flag = 0;}
}
if (tquality_level > 3 && (flag == 1))
{
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);
//distortion_factor = 0.9375 + (1.0 * nvL[2]);
distortion_factor = 0.97 + 0.06 * noise_500m;
stprime = stprime * distortion_factor * 15.0;
if (quality_level > 4)
{
stprime = stprime + normalize(relPos).xy * 0.02 * (noise_10m + 0.5 * noise_5m - 0.75);
}
// Detail texture is material texture 11, which is mapped to the g channel of fg_textureLookup1
//int tex3 = int(fg_textureLookup1[lc].g * 255.0 + 0.5);
//detail_texel = texture(textureArray, vec3(stprime, tex3));
if (detail_texel.a < 0.1) { flag = 0;}
//WS2: detail_texel = texture2D(detail_texture, stprime); // temp
vec4 dxdy_prime = vec4(dFdx(stprime), dFdy(stprime));
detail_texel = lookup_ground_texture_array(5, stprime, lc, dxdy_prime);
}
// texture preparation according to detail level
// mix in hires texture patches
float dist_fact;
float nSum;
float mix_factor;
float transition_model = fg_materialParams1[lc].r;
float hires_overlay_bias = fg_materialParams1[lc].g;
if (tquality_level > 2) {
// first the second texture overlay
// transition model 0: random patch overlay without any gradient information
// transition model 1: only gradient-driven transitions, no randomness
if (mix_flag == 1) {
// Random patch overlay weighting with noise
nSum = 0.18 * (2.0 * noise_2000m + 2.0 * noise_1500m + noise_500m);
// Increase the weighting for the mix_texel if more gradient-driven.
nSum = mix(nSum, 0.5, max(0.0, 2.0 * (transition_model - 0.5)));
// Add the gradient element
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);
mix_factor = smoothstep(0.5, 0.54, nSum);
texel = mix(texel, mix_texel, mix_factor);
local_autumn_factor = texel.a;
}
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) );
}
if ((tquality_level > 2) && (mix_flag == 1))
{
// Mix texture is material texture 15, which is mapped to the b channel of fg_textureLookup1
//int tex2 = int(fg_textureLookup1[lc].b * 255.0 + 0.5);
//mix_texel = texture(textureArray, vec3(gl_TexCoord[0].st * 1.3, tex2));
if (mix_texel.a < 0.1) { mix_flag = 0;}
//WS2: mix_texel = texture2D(mix_texture, gl_TexCoord[0].st * 1.3); // temp
mix_texel = lookup_ground_texture_array(4, st * 1.3, lc, dxdy * 1.3);
if (mix_texel.a <0.1) {mix_flag = 0;}
}
if (tquality_level > 3 && (flag == 1))
{
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);
//distortion_factor = 0.9375 + (1.0 * nvL[2]);
distortion_factor = 0.97 + 0.06 * noise_500m;
stprime = stprime * distortion_factor * 15.0;
if (quality_level > 4)
{
stprime = stprime + normalize(relPos).xy * 0.02 * (noise_10m + 0.5 * noise_5m - 0.75);
}
// Detail texture is material texture 11, which is mapped to the g channel of fg_textureLookup1
//int tex3 = int(fg_textureLookup1[lc].g * 255.0 + 0.5);
//detail_texel = texture(textureArray, vec3(stprime, tex3));
if (detail_texel.a < 0.1) { flag = 0;}
//WS2: detail_texel = texture2D(detail_texture, stprime); // temp
vec4 dxdy_prime = vec4(dFdx(stprime), dFdy(stprime));
detail_texel = lookup_ground_texture_array(5, stprime, lc, dxdy_prime);
}
// texture preparation according to detail level
// mix in hires texture patches
float dist_fact;
float nSum;
float mix_factor;
float transition_model = fg_materialParams1[lc].r;
float hires_overlay_bias = fg_materialParams1[lc].g;
if (tquality_level > 2) {
// first the second texture overlay
// transition model 0: random patch overlay without any gradient information
// transition model 1: only gradient-driven transitions, no randomness
if (mix_flag == 1) {
// Random patch overlay weighting with noise
nSum = 0.18 * (2.0 * noise_2000m + 2.0 * noise_1500m + noise_500m);
// Increase the weighting for the mix_texel if more gradient-driven.
nSum = mix(nSum, 0.5, max(0.0, 2.0 * (transition_model - 0.5)));
// Add the gradient element
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);
mix_factor = smoothstep(0.5, 0.54, nSum);
texel = mix(texel, mix_texel, mix_factor);
local_autumn_factor = texel.a;
}
}
if (tquality_level > 3) {
// then the detail texture overlay
if (dist < 40000.0)
{
if (flag == 1) {
//noise_50m = Noise2D(rawPos.xy, 50.0);
//noise_250m = Noise2D(rawPos.xy, 250.0);
dist_fact = 0.1 * smoothstep(15000.0,40000.0, dist) - 0.03 * (1.0 - smoothstep(500.0,5000.0, dist));
nSum = ((1.0 -noise_2000m) + noise_1500m + 2.0 * noise_250m +noise_50m)/5.0;
nSum = nSum - 0.08 * (1.0 -smoothstep(0.9,0.95, abs(steepness)));
mix_factor = smoothstep(0.47, 0.54, nSum +hires_overlay_bias - dist_fact);
if (mix_factor > 0.8) {mix_factor = 0.8;}
texel = mix(texel, detail_texel,mix_factor);
local_autumn_factor = texel.a;
if (tquality_level > 3) {
// then the detail texture overlay
if (dist < 40000.0)
{
if (flag == 1) {
//noise_50m = Noise2D(rawPos.xy, 50.0);
//noise_250m = Noise2D(rawPos.xy, 250.0);
dist_fact = 0.1 * smoothstep(15000.0,40000.0, dist) - 0.03 * (1.0 - smoothstep(500.0,5000.0, dist));
nSum = ((1.0 -noise_2000m) + noise_1500m + 2.0 * noise_250m +noise_50m)/5.0;
nSum = nSum - 0.08 * (1.0 -smoothstep(0.9,0.95, abs(steepness)));
mix_factor = smoothstep(0.47, 0.54, nSum +hires_overlay_bias - dist_fact);
if (mix_factor > 0.8) {mix_factor = 0.8;}
texel = mix(texel, detail_texel,mix_factor);
local_autumn_factor = texel.a;
}
}
}
}
// autumn colors
// autumn colors
float autumn_factor = season * 2.0 * (1.0 - local_autumn_factor) ;
float autumn_factor = season * 2.0 * (1.0 - local_autumn_factor) ;
texel.r = min(1.0, (1.0 + 2.5 * autumn_factor) * texel.r);
texel.g = texel.g;
texel.b = max(0.0, (1.0 - 4.0 * autumn_factor) * texel.b);
texel.r = min(1.0, (1.0 + 2.5 * autumn_factor) * texel.r);
texel.g = texel.g;
texel.b = max(0.0, (1.0 - 4.0 * autumn_factor) * texel.b);
if (local_autumn_factor < 1.0)
{
intensity = length(texel.rgb) * (1.0 - 0.5 * smoothstep(1.1,2.0,season));
texel.rgb = intensity * normalize(mix(texel.rgb, vec3(0.23,0.17,0.08), smoothstep(1.1,2.0, season)));
}
if (local_autumn_factor < 1.0)
{
intensity = length(texel.rgb) * (1.0 - 0.5 * smoothstep(1.1,2.0,season));
texel.rgb = intensity * normalize(mix(texel.rgb, vec3(0.23,0.17,0.08), smoothstep(1.1,2.0, season)));
}
const vec4 dust_color = vec4 (0.76, 0.71, 0.56, 1.0);
const vec4 lichen_color = vec4 (0.17, 0.20, 0.06, 1.0);;
//float snow_alpha;
const vec4 dust_color = vec4 (0.76, 0.71, 0.56, 1.0);
const vec4 lichen_color = vec4 (0.17, 0.20, 0.06, 1.0);;
//float snow_alpha;
if (quality_level > 3)
{
if (quality_level > 3)
{
// mix vegetation
texel = mix(texel, lichen_color, 0.4 * lichen_cover_factor + 0.8 * lichen_cover_factor * 0.5 * (noise_10m + (1.0 - noise_5m)) );
// mix dust
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) );
// mix snow
if (msl_altitude +500.0 > snowlevel)
{
snow_alpha = smoothstep(0.75, 0.85, abs(steepness));
//texel = mix(texel, snow_texel, texel_snow_fraction);
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));
}
}
else if (msl_altitude +500.0 > snowlevel)
{
float snow_alpha = 0.5+0.5* smoothstep(0.2,0.8, 0.3 + snow_thickness_factor +0.0001*(msl_altitude -snowlevel) );
// texel = vec4(dot(vec3(0.2989, 0.5870, 0.1140), texel.rgb));
texel = mix(texel, vec4(1.0), snow_alpha* smoothstep(snowlevel, snowlevel+200.0, (msl_altitude)));
}
// mix vegetation
texel = mix(texel, lichen_color, 0.4 * lichen_cover_factor + 0.8 * lichen_cover_factor * 0.5 * (noise_10m + (1.0 - noise_5m)) );
// mix dust
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) );
// mix snow
if (msl_altitude +500.0 > snowlevel)
{
snow_alpha = smoothstep(0.75, 0.85, abs(steepness));
//texel = mix(texel, snow_texel, texel_snow_fraction);
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));
}
}
else if (msl_altitude +500.0 > snowlevel)
{
float snow_alpha = 0.5+0.5* smoothstep(0.2,0.8, 0.3 + snow_thickness_factor +0.0001*(msl_altitude -snowlevel) );
// texel = vec4(dot(vec3(0.2989, 0.5870, 0.1140), texel.rgb));
texel = mix(texel, vec4(1.0), snow_alpha* smoothstep(snowlevel, snowlevel+200.0, (msl_altitude)));
}
// get distribution of water when terrain is wet
// get distribution of water when terrain is wet
float water_threshold1;
float water_threshold2;
float water_factor =0.0;
float water_threshold1;
float water_threshold2;
float water_factor =0.0;
if ((dist < 5000.0)&& (quality_level > 3) && (wetness>0.0))
{
water_threshold1 = 1.0-0.5* wetness;
water_threshold2 = 1.0 - 0.3 * wetness;
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));
}
if ((dist < 5000.0)&& (quality_level > 3) && (wetness>0.0))
{
water_threshold1 = 1.0-0.5* wetness;
water_threshold2 = 1.0 - 0.3 * wetness;
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));
}
// darken wet terrain
// darken wet terrain
texel.rgb = texel.rgb * (1.0 - 0.6 * wetness);
texel.rgb = texel.rgb * (1.0 - 0.6 * wetness);
// light computations
// light computations
vec4 light_specular = gl_LightSource[0].specular;
vec4 light_specular = gl_LightSource[0].specular;
// If gl_Color.a == 0, this is a back-facing polygon and the
// normal should be reversed.
//n = (2.0 * gl_Color.a - 1.0) * normal;
n = normal;//vec3 (nvec.x, nvec.y, sqrt(1.0 -pow(nvec.x,2.0) - pow(nvec.y,2.0) ));
n = normalize(n);
// If gl_Color.a == 0, this is a back-facing polygon and the
// normal should be reversed.
//n = (2.0 * gl_Color.a - 1.0) * normal;
n = normal;//vec3 (nvec.x, nvec.y, sqrt(1.0 -pow(nvec.x,2.0) - pow(nvec.y,2.0) ));
n = normalize(n);
NdotL = dot(n, lightDir);
if ((tquality_level > 3) && (mix_flag ==1)&& (dist < 2000.0) && (quality_level > 4))
{
noisegrad_10m = (noise_10m - Noise2D(rawPos.xy+ 0.05 * normalize(lightDir.xy),10.0))/0.05;
noisegrad_5m = (noise_5m - Noise2D(rawPos.xy+ 0.05 * normalize(lightDir.xy),5.0))/0.05;
NdotL = NdotL + 1.0 * (noisegrad_10m + 0.5* noisegrad_5m) * mix_factor/0.8 * (1.0 - smoothstep(1000.0, 2000.0, dist));
}
NdotL = dot(n, lightDir);
if ((tquality_level > 3) && (mix_flag ==1)&& (dist < 2000.0) && (quality_level > 4))
{
noisegrad_10m = (noise_10m - Noise2D(rawPos.xy+ 0.05 * normalize(lightDir.xy),10.0))/0.05;
noisegrad_5m = (noise_5m - Noise2D(rawPos.xy+ 0.05 * normalize(lightDir.xy),5.0))/0.05;
NdotL = NdotL + 1.0 * (noisegrad_10m + 0.5* noisegrad_5m) * mix_factor/0.8 * (1.0 - smoothstep(1000.0, 2000.0, dist));
}
if (NdotL > 0.0) {
float shadowmap = getShadowing();
vec4 diffuse_term = light_diffuse_comp * mat_diffuse;
color += diffuse_term * NdotL * shadowmap;
NdotHV = max(dot(n, halfVector), 0.0);
if (mat_shininess > 0.0)
specular.rgb = (mat_specular.rgb
* light_specular.rgb
* pow(NdotHV, mat_shininess)
* shadowmap);
if (NdotL > 0.0) {
float shadowmap = getShadowing();
vec4 diffuse_term = light_diffuse_comp * mat_diffuse;
color += diffuse_term * NdotL * shadowmap;
NdotHV = max(dot(n, halfVector), 0.0);
if (mat_shininess > 0.0)
specular.rgb = (mat_specular.rgb
* light_specular.rgb
* pow(NdotHV, mat_shininess)
* shadowmap);
}
color.a = 1.0;//diffuse_term.a;
// This shouldn't be necessary, but our lighting becomes very
// saturated. Clamping the color before modulating by the texture
// is closer to what the OpenGL fixed function pipeline does.
color = clamp(color, 0.0, 1.0);
fragColor = color * texel + specular;
fragColor.rgb += getClusteredLightsContribution(eyePos.xyz, n, texel.rgb);
}
color.a = 1.0;//diffuse_term.a;
// This shouldn't be necessary, but our lighting becomes very
// saturated. Clamping the color before modulating by the texture
// is closer to what the OpenGL fixed function pipeline does.
color = clamp(color, 0.0, 1.0);
fragColor = color * texel + specular;
fragColor.rgb += getClusteredLightsContribution(eyePos.xyz, n, texel.rgb);
float lightArg = (terminator-yprime_alt)/100000.0;
vec3 hazeColor = get_hazeColor(lightArg);

View file

@ -30,6 +30,7 @@ varying vec2 rawPos;
varying vec3 worldPos;
//varying vec2 orthoTexCoord;
varying vec4 eyePos;
varying vec4 ecPosition;
// Sent packed into alpha channels
//varying float yprime_alt;
@ -37,6 +38,19 @@ varying float mie_angle;
varying float steepness;
// For water calculations
varying float earthShade;
varying vec3 lightdir;
varying vec4 waterTex1;
varying vec4 waterTex2;
varying vec4 waterTex4;
varying vec3 specular_light;
uniform float osg_SimulationTime;
uniform float WindN;
uniform float WindE;
uniform int colorMode;
uniform float hazeLayerAltitude;
uniform float terminator;
@ -56,7 +70,6 @@ uniform mat4 osg_ViewMatrixInverse;
uniform mat4 fg_zUpTransform;
uniform vec3 fg_modelOffset;
float earthShade;
float yprime_alt;
//float mie_angle;
@ -78,6 +91,14 @@ if (x < -15.0) {return 0.0;}
return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d));
}
void createRotationMatrix(in float angle, out mat4 rotmat)
{
rotmat = mat4( cos( angle ), -sin( angle ), 0.0, 0.0,
sin( angle ), cos( angle ), 0.0, 0.0,
0.0 , 0.0 , 1.0, 0.0,
0.0 , 0.0 , 0.0, 1.0 );
}
void main()
{
@ -105,6 +126,7 @@ void main()
// this code is copied from default.vert
//vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex;
ecPosition = gl_ModelViewMatrix * gl_Vertex;
gl_Position = ftransform();
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
//orthoTexCoord = orthophotoTexCoord;
@ -133,6 +155,26 @@ void main()
vertex_alt = max(relPos.z,100.0);
scattering = ground_scattering + (1.0 - ground_scattering) * smoothstep(hazeLayerAltitude -100.0, hazeLayerAltitude + 100.0, vertex_alt);
// Required for water calculations
lightdir = normalize(vec3(fg_zUpTransform * vec4(gl_ModelViewMatrixInverse * gl_LightSource[0].position)));
waterTex4 = vec4( ecPosition.xzy, 0.0 );
vec4 t1 = vec4(0.0, osg_SimulationTime * 0.005217, 0.0, 0.0);
vec4 t2 = vec4(0.0, osg_SimulationTime * -0.0012, 0.0, 0.0);
float Angle;
float windFactor = sqrt(WindE * WindE + WindN * WindN) * 0.05;
if (WindN == 0.0 && WindE == 0.0) {
Angle = 0.0;
} else {
Angle = atan(-WindN, WindE) - atan(1.0);
}
mat4 RotationMatrix;
createRotationMatrix(Angle, RotationMatrix);
waterTex1 = gl_MultiTexCoord0 * RotationMatrix - t1 * windFactor;
waterTex2 = gl_MultiTexCoord0 * RotationMatrix - t2 * windFactor;
// early culling of vertices which can't be seen due to ground haze despite being in aloft visibility range
@ -197,6 +239,14 @@ if (terminator < 1000000.0) // the full, sunrise and sunset computation
light_ambient.b = light_ambient.r * 0.5/0.33; //light_func(lightArg, 0.236, 0.253, 1.073, 0.572, 0.5);
light_ambient.a = 1.0;
// Water specular calculations
specular_light.b = light_func(lightArg, 1.330e-05, 0.264, 3.827, 1.08e-05, 1.0);
specular_light.g = light_func(lightArg, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0);
specular_light.r = light_func(lightArg, 8.305e-06, 0.161, 3.827, 3.04e-05, 1.0);
specular_light = max(specular_light * scattering, vec3 (0.05, 0.05, 0.05));
intensity = length(specular_light.rgb);
specular_light.rgb = intensity * normalize(mix(specular_light.rgb, shadedFogColor, 1.0 -smoothstep(0.1, 0.6,ground_scattering) ));
specular_light.rgb = intensity * normalize(mix(specular_light.rgb, shadedFogColor, 1.0 -smoothstep(0.5, 0.7,earthShade)));
@ -211,6 +261,13 @@ if (earthShade < 0.5)
light_diffuse.rgb = intensity * normalize(mix(light_diffuse.rgb, shadedFogColor, 1.0 -smoothstep(0.4, 0.7,earthShade) ));
}
// directional scattering for low sun
if (lightArg < 10.0) {
mie_angle = (0.5 * dot(normalize(relPos), lightdir) ) + 0.5;
} else {
mie_angle = 1.0;
}
// the haze gets the light at the altitude of the haze top if the vertex in view is below
// but the light at the vertex if the vertex is above
@ -241,7 +298,9 @@ else // the faster, full-day version without lightfields
if (terminator > 3000000.0)
{light_diffuse = vec4 (1.0, 1.0, 1.0, 1.0);
light_ambient = vec4 (0.33, 0.4, 0.5, 1.0); }
light_ambient = vec4 (0.33, 0.4, 0.5, 1.0);
specular_light = vec3 (1.0, 1.0, 1.0);
}
else
{
@ -257,9 +316,16 @@ else // the faster, full-day version without lightfields
light_ambient.g = light_ambient.r * 0.4/0.33;
light_ambient.b = light_ambient.r * 0.5/0.33;
light_ambient.a = 1.0;
specular_light.b = 0.78 + lightArg * 0.21;
specular_light.g = 0.907 + lightArg * 0.091;
specular_light.r = 0.904 + lightArg * 0.092;
}
light_diffuse = light_diffuse * scattering;
specular_light = specular_light * scattering;
yprime_alt = -sqrt(2.0 * EarthRadius * hazeLayerAltitude);
}
@ -275,7 +341,8 @@ float shade_depth = 1.0 * smoothstep (0.6,0.95,ground_scattering) * (1.0-smooth
light_ambient.rgb = light_ambient.rgb * (1.0 - shade_depth);
light_diffuse.rgb = light_diffuse.rgb * (1.0 + 1.2 * shade_depth);
specular_light.rgb *= (1.0 + 1.2 * shade_depth);
if (use_IR_vision)
{
light_ambient.rgb = max(light_ambient.rgb, vec3 (0.5, 0.5, 0.5));

View file

@ -24,6 +24,10 @@ const int remove_haze_and_lighting = 0;
// 1: enabled,
// 2: remove texture array lookups for 5 textures - only base texture + neighbour base textures
const int randomise_texture_lookups = 0;
// Use built-in water shader. Use for testing impact of ws30-water.frag
const int water_shader = 1;
//
// End of test phase controls
//////////////////////////////////////////////////////////////////
@ -97,6 +101,7 @@ uniform vec4 fg_textureLookup2[128];
// Each element of a vec4 contains a different materials parameter.
uniform vec4 fg_materialParams1[128];
uniform vec4 fg_materialParams2[128];
uniform vec4 fg_materialParams3[128];
uniform mat4 fg_zUpTransform;
uniform vec3 fg_modelOffset;
@ -188,14 +193,16 @@ float get6_rand_nums(in float PRNGseed1,
// These functions, and other function they depend on, are defined
// in ws30-ALS-landclass-search.frag.
// Create random landclasses without a texture lookup to stress test.
// Each square of square_size in m is assigned a random landclass value.
int get_random_landclass(in vec2 co, in vec2 tile_size);
// End Test-phase code
////////////////////////
// These functions, and other function they depend on, are defined
// in ws30-ALS-landclass-search.frag.
// Lookup a ground texture at a point based on the landclass at that point, without visible
// seams at coordinate discontinuities or at landclass boundaries where texture are switched.
@ -253,8 +260,8 @@ vec4 applyHaze(inout vec4 fragColor,
in float lightArg,
in float mie_angle);
// End Test-phase code
////////////////////////
// Procedurally generate a water texel for this fragment
vec4 generateWaterTexel();
void main()
@ -274,8 +281,10 @@ void main()
// this is taken from default.frag
float NdotL, NdotHV, fogFactor;
vec3 n = normalize(normal);
vec3 lightDir = gl_LightSource[0].position.xyz;
vec3 halfVector = normalize(normalize(lightDir) + normalize(ecViewdir));
vec3 secondary_light = vec3 (0.0,0.0,0.0);
// Material/texel properties
@ -432,373 +441,376 @@ void main()
//vec4 green = vec4(0.0, 0.5, 0.0, 0.0);
//texel = mix(texel, green, (mfact[2]));
if ((water_shader == 1) && (fg_photoScenery == false) && fg_materialParams3[lc].x > 0.5) {
// This is a water fragment, so calculate the fragment color procedurally
texel = generateWaterTexel();
fragColor = texel;
fragColor.rgb += getClusteredLightsContribution(ecPosition.xyz, n, fragColor.rgb);
} else {
// Lookup material parameters for the landclass at this fragment.
// Material parameters are from material definitions XML files (e.g. regional definitions in data/Materials/regions). They have the same names.
// These parameters are contained in arrays of uniforms fg_materialParams1 and fg_materialParams2.
// The uniforms are vec4s, and each parameter is mapped to a vec4 element (rgba channels).
// In WS2 these parameters were available as uniforms of the same name.
// Testing: The mapping is hardcoded at the moment.
float transition_model = fg_materialParams1[lc].r;
float hires_overlay_bias = fg_materialParams1[lc].g;
float grain_strength = fg_materialParams1[lc].b;
float intrinsic_wetness = fg_materialParams1[lc].a;
float dot_density = fg_materialParams2[lc].r;
float dot_size = fg_materialParams2[lc].g;
float dust_resistance = fg_materialParams2[lc].b;
int rock_strata = int(fg_materialParams2[lc].a);
// dot noise
float dotnoise_2m = DotNoise2D(rawPos.xy, 2.0 * dot_size,0.5, dot_density);
float dotnoise_10m = DotNoise2D(rawPos.xy, 10.0 * dot_size, 0.5, dot_density);
float dotnoise_15m = DotNoise2D(rawPos.xy, 15.0 * dot_size, 0.33, dot_density);
// Lookup material parameters for the landclass at this fragment.
// Material parameters are from material definitions XML files (e.g. regional definitions in data/Materials/regions). They have the same names.
// These parameters are contained in arrays of uniforms fg_materialParams1 and fg_materialParams2.
// The uniforms are vec4s, and each parameter is mapped to a vec4 element (rgba channels).
// In WS2 these parameters were available as uniforms of the same name.
// Testing: The mapping is hardcoded at the moment.
float transition_model = fg_materialParams1[lc].r;
float hires_overlay_bias = fg_materialParams1[lc].g;
float grain_strength = fg_materialParams1[lc].b;
float intrinsic_wetness = fg_materialParams1[lc].a;
float dot_density = fg_materialParams2[lc].r;
float dot_size = fg_materialParams2[lc].g;
float dust_resistance = fg_materialParams2[lc].b;
int rock_strata = int(fg_materialParams2[lc].a);
// dot noise
float dotnoise_2m = DotNoise2D(rawPos.xy, 2.0 * dot_size,0.5, dot_density);
float dotnoise_10m = DotNoise2D(rawPos.xy, 10.0 * dot_size, 0.5, dot_density);
float dotnoise_15m = DotNoise2D(rawPos.xy, 15.0 * dot_size, 0.33, dot_density);
// Testing code - set randomise_texture_lookups = 2 to only look up the base texture with no extra transitions.
detail_texel = texel;
mix_texel = texel;
grain_texel = texel;
dot_texel = texel;
gradient_texel = texel;
// Testing code - set randomise_texture_lookups = 2 to only look up the base texture with no extra transitions.
detail_texel = texel;
mix_texel = texel;
grain_texel = texel;
dot_texel = texel;
gradient_texel = texel;
/*
// Texture lookup testing code:
// To test this block, uncomment it and turn off normal and random texture lookups
// by setting randomise_texture_lookups = 2 or more.
int tex2;
/*
// Texture lookup testing code:
// To test this block, uncomment it and turn off normal and random texture lookups
// by setting randomise_texture_lookups = 2 or more.
int tex2;
// Grain texture is material texture 14, which is mapped to the r channel of fg_textureLookup2
tex2 = int(fg_textureLookup2[lc].r * 255.0 + 0.5);
grain_texel = texture(textureArray, vec3(gl_TexCoord[0].st * 1.3, tex2));
// Gradient texture is material texture 13, which is mapped to the a channel of fg_textureLookup1
tex2 = int(fg_textureLookup1[lc].a * 255.0 + 0.5);
gradient_texel = texture(textureArray, vec3(gl_TexCoord[0].st * 1.3, tex2));
// Dot texture is material texture 15, which is mapped to the g channel of fg_textureLookup2
tex2 = int(fg_textureLookup2[lc].g * 255.0 + 0.5);
dot_texel = texture(textureArray, vec3(gl_TexCoord[0].st * 1.3, tex2));
// Mix texture is material texture 12, which is mapped to the b channel of fg_textureLookup1
tex2 = int(fg_textureLookup1[lc].b * 255.0 + 0.5);
mix_texel = texture(textureArray, vec3(gl_TexCoord[0].st * 1.3, tex2));
if (mix_texel.a < 0.1) { mix_flag = 0;} // Disable if no index found
// Detail texture is material texture 11, which is mapped to the g channel of fg_textureLookup1
tex2 = int(fg_textureLookup1[lc].g * 255.0 + 0.5);
detail_texel = texture(textureArray, vec3(gl_TexCoord[0].st * 1.3, tex2));
if (detail_texel.a < 0.1) { flag = 0;} // Disable if no index found
//Examples of how lookup_ground_texture array is used with the above grain/gradient texture lookups:
//grain_texel = lookup_ground_texture_array(1, st * 1.3, lc, dxdy * 1.3);
//gradient_texel = lookup_ground_texture_array(2, st * 1.3, lc, dxdy * 1.3);
*/
// Grain texture is material texture 14, which is mapped to the r channel of fg_textureLookup2
tex2 = int(fg_textureLookup2[lc].r * 255.0 + 0.5);
grain_texel = texture(textureArray, vec3(gl_TexCoord[0].st * 1.3, tex2));
// Gradient texture is material texture 13, which is mapped to the a channel of fg_textureLookup1
tex2 = int(fg_textureLookup1[lc].a * 255.0 + 0.5);
gradient_texel = texture(textureArray, vec3(gl_TexCoord[0].st * 1.3, tex2));
// Dot texture is material texture 15, which is mapped to the g channel of fg_textureLookup2
tex2 = int(fg_textureLookup2[lc].g * 255.0 + 0.5);
dot_texel = texture(textureArray, vec3(gl_TexCoord[0].st * 1.3, tex2));
// Mix texture is material texture 12, which is mapped to the b channel of fg_textureLookup1
tex2 = int(fg_textureLookup1[lc].b * 255.0 + 0.5);
mix_texel = texture(textureArray, vec3(gl_TexCoord[0].st * 1.3, tex2));
if (mix_texel.a < 0.1) { mix_flag = 0;} // Disable if no index found
// Detail texture is material texture 11, which is mapped to the g channel of fg_textureLookup1
tex2 = int(fg_textureLookup1[lc].g * 255.0 + 0.5);
detail_texel = texture(textureArray, vec3(gl_TexCoord[0].st * 1.3, tex2));
if (detail_texel.a < 0.1) { flag = 0;} // Disable if no index found
//Examples of how lookup_ground_texture array is used with the above grain/gradient texture lookups:
//grain_texel = lookup_ground_texture_array(1, st * 1.3, lc, dxdy * 1.3);
//gradient_texel = lookup_ground_texture_array(2, st * 1.3, lc, dxdy * 1.3);
*/
// Generate 6 random numbers
float pseed2 = 1.0;
int tex_id_lc[6];
float rn[6];
if (randomise_texture_lookups == 1)
{
get6_rand_nums(float(lc)*33245.31, pseed2, 47.0, rn);
for (int i=0;i<6;i++) tex_id_lc[i] = int(mod( (float(lc)+(rn[i]*47.0)+1.0) , 48.0));
}
//texel = mix(vec4(vec3(0.0),1.0), vec4(0.0,0.5,0.0,1.0), float(tex_id_lc[2])/48.0);
// WS2:
//grain_texel = texture2D(grain_texture, gl_TexCoord[0].st * 25.0);
//gradient_texel = texture2D(gradient_texture, gl_TexCoord[0].st * 4.0);
//stprime = gl_TexCoord[0].st * 80.0;
//stprime = stprime + normalize(relPos).xy * 0.01 * (dotnoise_10m + dotnoise_15m);
//dot_texel = texture2D(dot_texture, vec2 (stprime.y, stprime.x) );
if (randomise_texture_lookups == 0)
{
grain_texel = lookup_ground_texture_array(1, st * 25.0, lc, dxdy * 25.0);
gradient_texel = lookup_ground_texture_array(2, st * 4.0, lc, dxdy * 4.0);
}
else if (randomise_texture_lookups == 1)
{
grain_texel = lookup_ground_texture_array(0, st * 25.0, tex_id_lc[0], dxdy * 25.0);
gradient_texel = lookup_ground_texture_array(0, st * 4.0, tex_id_lc[1], dxdy * 4.0);
}
stprime = st * 80.0;
stprime = stprime + normalize(relPos).xy * 0.01 * (dotnoise_10m + dotnoise_15m);
vec4 dxdy_prime = vec4(dFdx(stprime), dFdy(stprime));
if (randomise_texture_lookups == 0)
{
dot_texel = lookup_ground_texture_array(3, stprime.ts, lc, dxdy_prime.tsqp);
}
else if (randomise_texture_lookups == 1)
{
dot_texel = lookup_ground_texture_array(0, stprime.ts, tex_id_lc[2], dxdy_prime.tsqp);
}
// Testing: WS2 code after this, except for random texture lookups and partial derivatives
float local_autumn_factor = texel.a;
// we need to fade procedural structures when they get smaller than a single pixel, for this we need
// to know under what angle we see the surface
float view_angle = abs(dot(normalize(normal), normalize(ecViewdir)));
// the snow texel is generated procedurally
if (msl_altitude +500.0 > snowlevel)
{
snow_texel = vec4 (0.95, 0.95, 0.95, 1.0) * (0.9 + 0.1* noise_500m + 0.1* (1.0 - noise_10m) );
snow_texel.r = snow_texel.r * (0.9 + 0.05 * (noise_10m + noise_5m));
snow_texel.g = snow_texel.g * (0.9 + 0.05 * (noise_10m + noise_5m));
snow_texel.a = 1.0;
noise_term = 0.1 * (noise_500m-0.5) ;
noise_term = noise_term + 0.2 * (snownoise_50m -0.5) * detail_fade(50.0, view_angle, 0.5*dist) ;
noise_term = noise_term + 0.2 * (snownoise_25m -0.5) * detail_fade(25.0, view_angle, 0.5*dist) ;
noise_term = noise_term + 0.3 * (noise_10m -0.5) * detail_fade(10.0, view_angle, 0.8*dist) ;
noise_term = noise_term + 0.3 * (noise_5m - 0.5) * detail_fade(5.0, view_angle, dist);
noise_term = noise_term + 0.15 * (noise_2m -0.5) * detail_fade(2.0, view_angle, dist);
noise_term = noise_term + 0.08 * (noise_1m -0.5) * detail_fade(1.0, view_angle, dist);
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) );
}
if (mix_flag == 1)
{
//WS2: mix_texel = texture2D(mix_texture, gl_TexCoord[0].st * 1.3);
// Generate 6 random numbers
float pseed2 = 1.0;
int tex_id_lc[6];
float rn[6];
if (randomise_texture_lookups == 1)
{
get6_rand_nums(float(lc)*33245.31, pseed2, 47.0, rn);
for (int i=0;i<6;i++) tex_id_lc[i] = int(mod( (float(lc)+(rn[i]*47.0)+1.0) , 48.0));
}
//texel = mix(vec4(vec3(0.0),1.0), vec4(0.0,0.5,0.0,1.0), float(tex_id_lc[2])/48.0);
// WS2:
//grain_texel = texture2D(grain_texture, gl_TexCoord[0].st * 25.0);
//gradient_texel = texture2D(gradient_texture, gl_TexCoord[0].st * 4.0);
//stprime = gl_TexCoord[0].st * 80.0;
//stprime = stprime + normalize(relPos).xy * 0.01 * (dotnoise_10m + dotnoise_15m);
//dot_texel = texture2D(dot_texture, vec2 (stprime.y, stprime.x) );
if (randomise_texture_lookups == 0)
{
mix_texel = lookup_ground_texture_array(4, st * 1.3, lc, dxdy * 1.3);
grain_texel = lookup_ground_texture_array(1, st * 25.0, lc, dxdy * 25.0);
gradient_texel = lookup_ground_texture_array(2, st * 4.0, lc, dxdy * 4.0);
}
else if (randomise_texture_lookups == 1)
{
mix_texel = lookup_ground_texture_array(0, st * 1.3, tex_id_lc[3], dxdy * 1.3);
grain_texel = lookup_ground_texture_array(0, st * 25.0, tex_id_lc[0], dxdy * 25.0);
gradient_texel = lookup_ground_texture_array(0, st * 4.0, tex_id_lc[1], dxdy * 4.0);
}
if (mix_texel.a <0.1) {mix_flag = 0;}
}
// the hires overlay texture is loaded with parallax mapping
if (flag == 1)
{
stprime = vec2 (0.86*st.s + 0.5*st.t, 0.5*st.s - 0.86*st.t);
distortion_factor = 0.97 + 0.06 * noise_500m;
stprime = stprime * distortion_factor * 15.0;
stprime = stprime + normalize(relPos).xy * 0.022 * (noise_10m + 0.5 * noise_5m +0.25 * noise_2m - 0.875 );
//WS2: detail_texel = texture2D(detail_texture, stprime); // temp
stprime = st * 80.0;
stprime = stprime + normalize(relPos).xy * 0.01 * (dotnoise_10m + dotnoise_15m);
vec4 dxdy_prime = vec4(dFdx(stprime), dFdy(stprime));
dxdy_prime = vec4(dFdx(stprime), dFdy(stprime));
if (randomise_texture_lookups == 0)
{
detail_texel = lookup_ground_texture_array(5, stprime , lc, dxdy_prime);
dot_texel = lookup_ground_texture_array(3, stprime.ts, lc, dxdy_prime.tsqp);
}
else if (randomise_texture_lookups == 1)
{
detail_texel = lookup_ground_texture_array(0, stprime, tex_id_lc[4], dxdy_prime);
else if (randomise_texture_lookups == 1)
{
dot_texel = lookup_ground_texture_array(0, stprime.ts, tex_id_lc[2], dxdy_prime.tsqp);
}
if (detail_texel.a <0.1) {flag = 0;}
} // End if (flag == 1)
// texture preparation according to detail level
// mix in hires texture patches
// Testing: WS2 code after this, except for random texture lookups and partial derivatives
float local_autumn_factor = texel.a;
float dist_fact;
float nSum;
float mix_factor;
// first the second texture overlay
// transition model 0: random patch overlay without any gradient information
// transition model 1: only gradient-driven transitions, no randomness
if (mix_flag == 1)
{
nSum = 0.167 * (noise_4000m + 2.0 * noise_2000m + 2.0 * noise_1500m + noise_500m);
nSum = mix(nSum, 0.5, max(0.0, 2.0 * (transition_model - 0.5)));
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);
mix_factor = smoothstep(0.5, 0.54, nSum);
texel = mix(texel, mix_texel, mix_factor);
local_autumn_factor = texel.a;
// we need to fade procedural structures when they get smaller than a single pixel, for this we need
// to know under what angle we see the surface
float view_angle = abs(dot(normalize(normal), normalize(ecViewdir)));
// the snow texel is generated procedurally
if (msl_altitude +500.0 > snowlevel)
{
snow_texel = vec4 (0.95, 0.95, 0.95, 1.0) * (0.9 + 0.1* noise_500m + 0.1* (1.0 - noise_10m) );
snow_texel.r = snow_texel.r * (0.9 + 0.05 * (noise_10m + noise_5m));
snow_texel.g = snow_texel.g * (0.9 + 0.05 * (noise_10m + noise_5m));
snow_texel.a = 1.0;
noise_term = 0.1 * (noise_500m-0.5) ;
noise_term = noise_term + 0.2 * (snownoise_50m -0.5) * detail_fade(50.0, view_angle, 0.5*dist) ;
noise_term = noise_term + 0.2 * (snownoise_25m -0.5) * detail_fade(25.0, view_angle, 0.5*dist) ;
noise_term = noise_term + 0.3 * (noise_10m -0.5) * detail_fade(10.0, view_angle, 0.8*dist) ;
noise_term = noise_term + 0.3 * (noise_5m - 0.5) * detail_fade(5.0, view_angle, dist);
noise_term = noise_term + 0.15 * (noise_2m -0.5) * detail_fade(2.0, view_angle, dist);
noise_term = noise_term + 0.08 * (noise_1m -0.5) * detail_fade(1.0, view_angle, dist);
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) );
}
if (mix_flag == 1)
{
//WS2: mix_texel = texture2D(mix_texture, gl_TexCoord[0].st * 1.3);
if (randomise_texture_lookups == 0)
{
mix_texel = lookup_ground_texture_array(4, st * 1.3, lc, dxdy * 1.3);
}
else if (randomise_texture_lookups == 1)
{
mix_texel = lookup_ground_texture_array(0, st * 1.3, tex_id_lc[3], dxdy * 1.3);
}
if (mix_texel.a <0.1) {mix_flag = 0;}
}
// the hires overlay texture is loaded with parallax mapping
if (flag == 1)
{
stprime = vec2 (0.86*st.s + 0.5*st.t, 0.5*st.s - 0.86*st.t);
distortion_factor = 0.97 + 0.06 * noise_500m;
stprime = stprime * distortion_factor * 15.0;
stprime = stprime + normalize(relPos).xy * 0.022 * (noise_10m + 0.5 * noise_5m +0.25 * noise_2m - 0.875 );
//WS2: detail_texel = texture2D(detail_texture, stprime); // temp
dxdy_prime = vec4(dFdx(stprime), dFdy(stprime));
if (randomise_texture_lookups == 0)
{
detail_texel = lookup_ground_texture_array(5, stprime , lc, dxdy_prime);
}
else if (randomise_texture_lookups == 1)
{
detail_texel = lookup_ground_texture_array(0, stprime, tex_id_lc[4], dxdy_prime);
}
if (detail_texel.a <0.1) {flag = 0;}
} // End if (flag == 1)
// texture preparation according to detail level
// mix in hires texture patches
float dist_fact;
float nSum;
float mix_factor;
// first the second texture overlay
// transition model 0: random patch overlay without any gradient information
// transition model 1: only gradient-driven transitions, no randomness
if (mix_flag == 1)
{
nSum = 0.167 * (noise_4000m + 2.0 * noise_2000m + 2.0 * noise_1500m + noise_500m);
nSum = mix(nSum, 0.5, max(0.0, 2.0 * (transition_model - 0.5)));
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);
mix_factor = smoothstep(0.5, 0.54, nSum);
texel = mix(texel, mix_texel, mix_factor);
local_autumn_factor = texel.a;
}
// then the detail texture overlay
mix_factor = 0.0;
//WS2: condition was broken up - does it matter for dynamic branching?
if ((flag == 1) && (dist < 40000.0))
{
dist_fact = 0.1 * smoothstep(15000.0,40000.0, dist) - 0.03 * (1.0 - smoothstep(500.0,5000.0, dist));
nSum = ((1.0 -noise_2000m) + noise_1500m + 2.0 * noise_250m +noise_50m)/5.0;
nSum = nSum - 0.08 * (1.0 -smoothstep(0.9,0.95, abs(steepness)));
mix_factor = smoothstep(0.47, 0.54, nSum +hires_overlay_bias- dist_fact);
if (mix_factor > 0.8) {mix_factor = 0.8;}
texel = mix(texel, detail_texel,mix_factor);
}
// rock for very steep gradients
if (gradient_texel.a > 0.0)
{
texel = mix(texel, gradient_texel, 1.0 - smoothstep(0.75,0.8,abs(steepness)+ 0.00002* msl_altitude + 0.05 * (noise_50m - 0.5)));
local_autumn_factor = texel.a;
}
// strata noise
float stratnoise_50m;
float stratnoise_10m;
// Testing: if rock_strata parameter is not cast into int, need (rock_strata > 0.99)
if (rock_strata==1)
{
stratnoise_50m = Strata3D(vec3 (rawPos.x, rawPos.y, msl_altitude), 50.0, 0.2);
stratnoise_10m = Strata3D(vec3 (rawPos.x, rawPos.y, msl_altitude), 10.0, 0.2);
stratnoise_50m = mix(stratnoise_50m, 1.0, smoothstep(0.8,0.9, steepness));
stratnoise_10m = mix(stratnoise_10m, 1.0, smoothstep(0.8,0.9, steepness));
texel *= (0.4 + 0.4 * stratnoise_50m + 0.2 * stratnoise_10m);
}
// the dot vegetation texture overlay
texel.rgb = mix(texel.rgb, dot_texel.rgb, dot_texel.a * (dotnoise_10m + dotnoise_15m) * detail_fade(1.0 * (dot_size * (1.0 +0.1*dot_size)), view_angle,dist));
texel.rgb = mix(texel.rgb, dot_texel.rgb, dot_texel.a * dotnoise_2m * detail_fade(0.1 * dot_size, view_angle,dist));
// then the grain texture overlay
texel.rgb = mix(texel.rgb, grain_texel.rgb, grain_strength * grain_texel.a * (1.0 - mix_factor) * (1.0-smoothstep(2000.0,5000.0, dist)));
// for really hires, add procedural noise overlay
texel.rgb = texel.rgb * (1.0 + 0.4 * (noise_01m-0.5) * detail_fade(0.1, view_angle, dist)) ;
// autumn colors
float autumn_factor = season * 2.0 * (1.0 - local_autumn_factor) ;
texel.r = min(1.0, (1.0 + 2.5 * autumn_factor) * texel.r);
texel.g = texel.g;
texel.b = max(0.0, (1.0 - 4.0 * autumn_factor) * texel.b);
if (local_autumn_factor < 1.0)
{
intensity = length(texel.rgb) * (1.0 - 0.5 * smoothstep(1.1,2.0,season));
texel.rgb = intensity * normalize(mix(texel.rgb, vec3(0.23,0.17,0.08), smoothstep(1.1,2.0, season)));
}
// slope line overlay
texel.rgb = texel.rgb * (1.0 - 0.12 * slopenoise_50m - 0.08 * slopenoise_100m);
//const vec4 dust_color = vec4 (0.76, 0.71, 0.56, 1.0);
const vec4 dust_color = vec4 (0.76, 0.65, 0.45, 1.0);
const vec4 lichen_color = vec4 (0.17, 0.20, 0.06, 1.0);
// mix vegetation
float gradient_factor = smoothstep(0.5, 1.0, steepness);
texel = mix(texel, lichen_color, gradient_factor * (0.4 * lichen_cover_factor + 0.8 * lichen_cover_factor * 0.5 * (noise_10m + (1.0 - noise_5m))) );
// mix dust
texel = mix(texel, dust_color, clamp(0.5 * dust_cover_factor *dust_resistance + 3.0 * dust_cover_factor * dust_resistance *(((noise_1500m - 0.5) * 0.125)+0.125 ),0.0, 1.0) );
// mix snow
float snow_mix_factor = 0.0;
if (msl_altitude + 500.0 > snowlevel)
{
snow_alpha = smoothstep(0.75, 0.85, abs(steepness));
snow_mix_factor = snow_texel.a* smoothstep(snowlevel, snowlevel+200.0, snow_alpha * msl_altitude+ (noise_2000m + 0.1 * noise_10m -0.55) *400.0);
texel = mix(texel, snow_texel, snow_mix_factor);
}
// get distribution of water when terrain is wet
float combined_wetness = min(1.0, wetness + intrinsic_wetness);
float water_threshold1;
float water_threshold2;
float water_factor =0.0;
if ((dist < 5000.0) && (combined_wetness>0.0))
{
water_threshold1 = 1.0-0.5* combined_wetness;
water_threshold2 = 1.0 - 0.3 * combined_wetness;
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));
}
// darken wet terrain
texel.rgb = texel.rgb * (1.0 - 0.6 * combined_wetness);
// light computations
vec4 light_specular = gl_LightSource[0].specular;
// If gl_Color.a == 0, this is a back-facing polygon and the
// normal should be reversed.
//n = (2.0 * gl_Color.a - 1.0) * normal;
vec3 n = normal;//vec3 (nvec.x, nvec.y, sqrt(1.0 -pow(nvec.x,2.0) - pow(nvec.y,2.0) ));
n = normalize(n);
NdotL = dot(n, lightDir);
float noisegrad_10m = (noise_10m - Noise2D(rawPos.xy+ 0.05 * normalize(lightDir.xy),10.0))/0.05;
float noisegrad_5m = (noise_5m - Noise2D(rawPos.xy+ 0.05 * normalize(lightDir.xy),5.0))/0.05;
float noisegrad_2m = (noise_2m - Noise2D(rawPos.xy+ 0.05 * normalize(lightDir.xy),2.0))/0.05;
float noisegrad_1m = (noise_1m - Noise2D(rawPos.xy+ 0.05 * normalize(lightDir.xy),1.0))/0.05;
dotnoisegrad_10m = (dotnoise_10m - DotNoise2D(rawPos.xy+ 0.05 * normalize(lightDir.xy),10.0 * dot_size,0.5, dot_density))/0.05;
NdotL = NdotL + (noisegrad_10m * detail_fade(10.0, view_angle,dist) + 0.5* noisegrad_5m * detail_fade(5.0, view_angle,dist)) * mix_factor/0.8;
NdotL = NdotL + 0.15 * noisegrad_2m * mix_factor/0.8 * detail_fade(2.0,view_angle,dist);
NdotL = NdotL + 0.1 * noisegrad_2m * detail_fade(2.0,view_angle,dist);
NdotL = NdotL + 0.05 * noisegrad_1m * detail_fade(1.0, view_angle,dist);
NdotL = NdotL + (1.0-snow_mix_factor) * 0.3* dot_texel.a * (0.5* dotnoisegrad_10m * detail_fade(1.0 * dot_size, view_angle, dist) +0.5 * dotnoisegrad_10m * noise_01m * detail_fade(0.1, view_angle, dist)) ;
// Testing: Very temporary - reduce procedural normal map features with photoscenery active without breaking profiling as the controls are default (by request)
if (fg_photoScenery) NdotL = mix(dot(n, lightDir), NdotL, 0.00001);
if (NdotL > 0.0)
{
float shadowmap = getShadowing();
if (cloud_shadow_flag == 1) {NdotL = NdotL * shadow_func(relPos.x, relPos.y, 0.3 * noise_250m + 0.5 * noise_500m+0.2 * noise_1500m, dist);}
vec4 diffuse_term = light_diffuse_comp * mat_diffuse;
color += diffuse_term * NdotL * shadowmap;
NdotHV = max(dot(n, halfVector), 0.0);
if (mat_shininess > 0.0)
specular.rgb = ((mat_specular.rgb * 0.1 + (water_factor * vec3 (1.0, 1.0, 1.0)))
* light_specular.rgb
* pow(NdotHV, mat_shininess + (20.0 * water_factor))
* shadowmap);
}
color.a = 1.0;//diffuse_term.a; // as gl_Color.a and light_diffuse.comp.a were packed with other values
// This shouldn't be necessary, but our lighting becomes very
// saturated. Clamping the color before modulating by the texture
// is closer to what the OpenGL fixed function pipeline does.
color = clamp(color, 0.0, 1.0);
if (use_searchlight == 1) {
secondary_light += searchlight();
}
if (use_landing_light == 1) {
secondary_light += landing_light(landing_light1_offset, landing_light3_offset);
}
if (use_alt_landing_light == 1) {
secondary_light += landing_light(landing_light2_offset, landing_light3_offset);
}
color.rgb += secondary_light * light_distance_fading(dist);
fragColor = color * texel + specular;
fragColor.rgb += getClusteredLightsContribution(ecPosition.xyz, n, texel.rgb);
}
// then the detail texture overlay
mix_factor = 0.0;
//WS2: condition was broken up - does it matter for dynamic branching?
if ((flag == 1) && (dist < 40000.0))
{
dist_fact = 0.1 * smoothstep(15000.0,40000.0, dist) - 0.03 * (1.0 - smoothstep(500.0,5000.0, dist));
nSum = ((1.0 -noise_2000m) + noise_1500m + 2.0 * noise_250m +noise_50m)/5.0;
nSum = nSum - 0.08 * (1.0 -smoothstep(0.9,0.95, abs(steepness)));
mix_factor = smoothstep(0.47, 0.54, nSum +hires_overlay_bias- dist_fact);
if (mix_factor > 0.8) {mix_factor = 0.8;}
texel = mix(texel, detail_texel,mix_factor);
}
// rock for very steep gradients
if (gradient_texel.a > 0.0)
{
texel = mix(texel, gradient_texel, 1.0 - smoothstep(0.75,0.8,abs(steepness)+ 0.00002* msl_altitude + 0.05 * (noise_50m - 0.5)));
local_autumn_factor = texel.a;
}
// strata noise
float stratnoise_50m;
float stratnoise_10m;
// Testing: if rock_strata parameter is not cast into int, need (rock_strata > 0.99)
if (rock_strata==1)
{
stratnoise_50m = Strata3D(vec3 (rawPos.x, rawPos.y, msl_altitude), 50.0, 0.2);
stratnoise_10m = Strata3D(vec3 (rawPos.x, rawPos.y, msl_altitude), 10.0, 0.2);
stratnoise_50m = mix(stratnoise_50m, 1.0, smoothstep(0.8,0.9, steepness));
stratnoise_10m = mix(stratnoise_10m, 1.0, smoothstep(0.8,0.9, steepness));
texel *= (0.4 + 0.4 * stratnoise_50m + 0.2 * stratnoise_10m);
}
// the dot vegetation texture overlay
texel.rgb = mix(texel.rgb, dot_texel.rgb, dot_texel.a * (dotnoise_10m + dotnoise_15m) * detail_fade(1.0 * (dot_size * (1.0 +0.1*dot_size)), view_angle,dist));
texel.rgb = mix(texel.rgb, dot_texel.rgb, dot_texel.a * dotnoise_2m * detail_fade(0.1 * dot_size, view_angle,dist));
// then the grain texture overlay
texel.rgb = mix(texel.rgb, grain_texel.rgb, grain_strength * grain_texel.a * (1.0 - mix_factor) * (1.0-smoothstep(2000.0,5000.0, dist)));
// for really hires, add procedural noise overlay
texel.rgb = texel.rgb * (1.0 + 0.4 * (noise_01m-0.5) * detail_fade(0.1, view_angle, dist)) ;
// autumn colors
float autumn_factor = season * 2.0 * (1.0 - local_autumn_factor) ;
texel.r = min(1.0, (1.0 + 2.5 * autumn_factor) * texel.r);
texel.g = texel.g;
texel.b = max(0.0, (1.0 - 4.0 * autumn_factor) * texel.b);
if (local_autumn_factor < 1.0)
{
intensity = length(texel.rgb) * (1.0 - 0.5 * smoothstep(1.1,2.0,season));
texel.rgb = intensity * normalize(mix(texel.rgb, vec3(0.23,0.17,0.08), smoothstep(1.1,2.0, season)));
}
// slope line overlay
texel.rgb = texel.rgb * (1.0 - 0.12 * slopenoise_50m - 0.08 * slopenoise_100m);
//const vec4 dust_color = vec4 (0.76, 0.71, 0.56, 1.0);
const vec4 dust_color = vec4 (0.76, 0.65, 0.45, 1.0);
const vec4 lichen_color = vec4 (0.17, 0.20, 0.06, 1.0);
// mix vegetation
float gradient_factor = smoothstep(0.5, 1.0, steepness);
texel = mix(texel, lichen_color, gradient_factor * (0.4 * lichen_cover_factor + 0.8 * lichen_cover_factor * 0.5 * (noise_10m + (1.0 - noise_5m))) );
// mix dust
texel = mix(texel, dust_color, clamp(0.5 * dust_cover_factor *dust_resistance + 3.0 * dust_cover_factor * dust_resistance *(((noise_1500m - 0.5) * 0.125)+0.125 ),0.0, 1.0) );
// mix snow
float snow_mix_factor = 0.0;
if (msl_altitude + 500.0 > snowlevel)
{
snow_alpha = smoothstep(0.75, 0.85, abs(steepness));
snow_mix_factor = snow_texel.a* smoothstep(snowlevel, snowlevel+200.0, snow_alpha * msl_altitude+ (noise_2000m + 0.1 * noise_10m -0.55) *400.0);
texel = mix(texel, snow_texel, snow_mix_factor);
}
// get distribution of water when terrain is wet
float combined_wetness = min(1.0, wetness + intrinsic_wetness);
float water_threshold1;
float water_threshold2;
float water_factor =0.0;
if ((dist < 5000.0) && (combined_wetness>0.0))
{
water_threshold1 = 1.0-0.5* combined_wetness;
water_threshold2 = 1.0 - 0.3 * combined_wetness;
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));
}
// darken wet terrain
texel.rgb = texel.rgb * (1.0 - 0.6 * combined_wetness);
// light computations
vec4 light_specular = gl_LightSource[0].specular;
// If gl_Color.a == 0, this is a back-facing polygon and the
// normal should be reversed.
//n = (2.0 * gl_Color.a - 1.0) * normal;
vec3 n = normal;//vec3 (nvec.x, nvec.y, sqrt(1.0 -pow(nvec.x,2.0) - pow(nvec.y,2.0) ));
n = normalize(n);
NdotL = dot(n, lightDir);
float noisegrad_10m = (noise_10m - Noise2D(rawPos.xy+ 0.05 * normalize(lightDir.xy),10.0))/0.05;
float noisegrad_5m = (noise_5m - Noise2D(rawPos.xy+ 0.05 * normalize(lightDir.xy),5.0))/0.05;
float noisegrad_2m = (noise_2m - Noise2D(rawPos.xy+ 0.05 * normalize(lightDir.xy),2.0))/0.05;
float noisegrad_1m = (noise_1m - Noise2D(rawPos.xy+ 0.05 * normalize(lightDir.xy),1.0))/0.05;
dotnoisegrad_10m = (dotnoise_10m - DotNoise2D(rawPos.xy+ 0.05 * normalize(lightDir.xy),10.0 * dot_size,0.5, dot_density))/0.05;
NdotL = NdotL + (noisegrad_10m * detail_fade(10.0, view_angle,dist) + 0.5* noisegrad_5m * detail_fade(5.0, view_angle,dist)) * mix_factor/0.8;
NdotL = NdotL + 0.15 * noisegrad_2m * mix_factor/0.8 * detail_fade(2.0,view_angle,dist);
NdotL = NdotL + 0.1 * noisegrad_2m * detail_fade(2.0,view_angle,dist);
NdotL = NdotL + 0.05 * noisegrad_1m * detail_fade(1.0, view_angle,dist);
NdotL = NdotL + (1.0-snow_mix_factor) * 0.3* dot_texel.a * (0.5* dotnoisegrad_10m * detail_fade(1.0 * dot_size, view_angle, dist) +0.5 * dotnoisegrad_10m * noise_01m * detail_fade(0.1, view_angle, dist)) ;
// Testing: Very temporary - reduce procedural normal map features with photoscenery active without breaking profiling as the controls are default (by request)
if (fg_photoScenery) NdotL = mix(dot(n, lightDir), NdotL, 0.00001);
if (NdotL > 0.0)
{
float shadowmap = getShadowing();
if (cloud_shadow_flag == 1) {NdotL = NdotL * shadow_func(relPos.x, relPos.y, 0.3 * noise_250m + 0.5 * noise_500m+0.2 * noise_1500m, dist);}
vec4 diffuse_term = light_diffuse_comp * mat_diffuse;
color += diffuse_term * NdotL * shadowmap;
NdotHV = max(dot(n, halfVector), 0.0);
if (mat_shininess > 0.0)
specular.rgb = ((mat_specular.rgb * 0.1 + (water_factor * vec3 (1.0, 1.0, 1.0)))
* light_specular.rgb
* pow(NdotHV, mat_shininess + (20.0 * water_factor))
* shadowmap);
}
color.a = 1.0;//diffuse_term.a; // as gl_Color.a and light_diffuse.comp.a were packed with other values
// This shouldn't be necessary, but our lighting becomes very
// saturated. Clamping the color before modulating by the texture
// is closer to what the OpenGL fixed function pipeline does.
color = clamp(color, 0.0, 1.0);
vec3 secondary_light = vec3 (0.0,0.0,0.0);
if (use_searchlight == 1) {
secondary_light += searchlight();
}
if (use_landing_light == 1) {
secondary_light += landing_light(landing_light1_offset, landing_light3_offset);
}
if (use_alt_landing_light == 1) {
secondary_light += landing_light(landing_light2_offset, landing_light3_offset);
}
color.rgb += secondary_light * light_distance_fading(dist);
fragColor = color * texel + specular;
fragColor.rgb += getClusteredLightsContribution(ecPosition.xyz, n, texel.rgb);
float lightArg = (terminator-yprime_alt)/100000.0;
vec3 hazeColor = get_hazeColor(lightArg);

View file

@ -32,6 +32,18 @@ varying vec2 grad_dir;
varying vec4 ecPosition;
varying vec3 vertVec;
// For water calculations
varying float earthShade;
varying vec3 lightdir;
varying vec4 waterTex1;
varying vec4 waterTex2;
varying vec4 waterTex4;
varying vec3 specular_light;
uniform float osg_SimulationTime;
uniform float WindN;
uniform float WindE;
// Sent packed into alpha channels
//varying float yprime_alt;
varying float mie_angle;
@ -60,7 +72,6 @@ uniform mat4 osg_ViewMatrixInverse;
uniform mat4 fg_zUpTransform;
uniform vec3 fg_modelOffset;
float earthShade;
float yprime_alt;
vec3 moonlight_perception (in vec3 light);
@ -82,6 +93,13 @@ float light_func (in float x, in float a, in float b, in float c, in float d, in
return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d));
}
void createRotationMatrix(in float angle, out mat4 rotmat)
{
rotmat = mat4( cos( angle ), -sin( angle ), 0.0, 0.0,
sin( angle ), cos( angle ), 0.0, 0.0,
0.0 , 0.0 , 1.0, 0.0,
0.0 , 0.0 , 0.0, 1.0 );
}
void main()
{
@ -130,6 +148,28 @@ void main()
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
normal = gl_NormalMatrix * gl_Normal;
// Required for water calculations
lightdir = normalize(vec3(fg_zUpTransform * vec4(gl_ModelViewMatrixInverse * gl_LightSource[0].position)));
waterTex4 = vec4( ecPosition.xzy, 0.0 );
vec4 t1 = vec4(0.0, osg_SimulationTime * 0.005217, 0.0, 0.0);
vec4 t2 = vec4(0.0, osg_SimulationTime * -0.0012, 0.0, 0.0);
float Angle;
float windFactor = sqrt(WindE * WindE + WindN * WindN) * 0.05;
if (WindN == 0.0 && WindE == 0.0) {
Angle = 0.0;
} else {
Angle = atan(-WindN, WindE) - atan(1.0);
}
mat4 RotationMatrix;
createRotationMatrix(Angle, RotationMatrix);
waterTex1 = gl_MultiTexCoord0 * RotationMatrix - t1 * windFactor;
waterTex2 = gl_MultiTexCoord0 * RotationMatrix - t2 * windFactor;
///////////////////////////////////////////
// Test phase code:
//
@ -256,6 +296,14 @@ if (terminator < 1000000.0) // the full, sunrise and sunset computation
light_ambient.b = light_ambient.r * 0.5/0.33; //light_func(lightArg, 0.236, 0.253, 1.073, 0.572, 0.5);
light_ambient.a = 1.0;
// Water specular calculations
specular_light.b = light_func(lightArg, 1.330e-05, 0.264, 3.827, 1.08e-05, 1.0);
specular_light.g = light_func(lightArg, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0);
specular_light.r = light_func(lightArg, 8.305e-06, 0.161, 3.827, 3.04e-05, 1.0);
specular_light = max(specular_light * scattering, vec3 (0.05, 0.05, 0.05));
intensity = length(specular_light.rgb);
specular_light.rgb = intensity * normalize(mix(specular_light.rgb, shadedFogColor, 1.0 -smoothstep(0.1, 0.6,ground_scattering) ));
specular_light.rgb = intensity * normalize(mix(specular_light.rgb, shadedFogColor, 1.0 -smoothstep(0.5, 0.7,earthShade)));
@ -270,6 +318,13 @@ if (terminator < 1000000.0) // the full, sunrise and sunset computation
light_diffuse.rgb = intensity * normalize(mix(light_diffuse.rgb, shadedFogColor, 1.0 -smoothstep(0.4, 0.7,earthShade) ));
}
// directional scattering for low sun
if (lightArg < 10.0) {
mie_angle = (0.5 * dot(normalize(relPos), lightdir) ) + 0.5;
} else {
mie_angle = 1.0;
}
// the haze gets the light at the altitude of the haze top if the vertex in view is below
// but the light at the vertex if the vertex is above
@ -302,6 +357,7 @@ if (terminator < 1000000.0) // the full, sunrise and sunset computation
{
light_diffuse = vec4 (1.0, 1.0, 1.0, 1.0);
light_ambient = vec4 (0.33, 0.4, 0.5, 1.0);
specular_light = vec3 (1.0, 1.0, 1.0);
}
else
{
@ -317,9 +373,15 @@ if (terminator < 1000000.0) // the full, sunrise and sunset computation
light_ambient.g = light_ambient.r * 0.4/0.33;
light_ambient.b = light_ambient.r * 0.5/0.33;
light_ambient.a = 1.0;
specular_light.b = 0.78 + lightArg * 0.21;
specular_light.g = 0.907 + lightArg * 0.091;
specular_light.r = 0.904 + lightArg * 0.092;
}
light_diffuse = light_diffuse * scattering;
specular_light = specular_light * scattering;
yprime_alt = -sqrt(2.0 * EarthRadius * hazeLayerAltitude);
} //End the faster, full-day version without lightfields
@ -336,6 +398,7 @@ if (terminator < 1000000.0) // the full, sunrise and sunset computation
light_ambient.rgb = light_ambient.rgb * (1.0 - shade_depth);
light_diffuse.rgb = light_diffuse.rgb * (1.0 + 1.2 * shade_depth);
specular_light.rgb *= (1.0 + 1.2 * shade_depth);
if (use_IR_vision)
{

View file

@ -22,6 +22,9 @@
// Possible values: 0:Normal, 1:Just the texture.
const int remove_haze_and_lighting = 0;
// Use built-in water shader. Use for testing impact of ws30-water.frag
const int water_shader = 1;
//
// End of test phase controls
//////////////////////////////////////////////////////////////////
@ -73,6 +76,8 @@ uniform vec4 fg_diffuseArray[128];
uniform vec4 fg_specularArray[128];
uniform vec4 fg_textureLookup1[128];
uniform vec4 fg_textureLookup2[128];
uniform vec4 fg_materialParams3[128];
#define MAX_TEXTURES 8
uniform mat4 fg_zUpTransform;
uniform vec3 fg_modelOffset;
@ -107,6 +112,7 @@ vec3 filter_combined (in vec3 color) ;
float getShadowing();
vec3 getClusteredLightsContribution(vec3 p, vec3 n, vec3 texel);
vec4 generateWaterTexel();
// Not used
float luminance(vec3 color)
@ -118,7 +124,6 @@ float luminance(vec3 color)
//////////////////////////
// Test-phase code:
// These should be sent as uniforms
// Tile dimensions in meters
@ -131,14 +136,15 @@ float luminance(vec3 color)
// From noise.frag
float rand2D(in vec2 co);
// These functions, and other function they depend on, are defined
// in ws30-ALS-landclass-search.frag.
// Create random landclasses without a texture lookup to stress test.
// Each square of square_size in m is assigned a random landclass value.
int get_random_landclass(in vec2 co, in vec2 tile_size);
// End Test-phase code
////////////////////////
// These functions, and other function they depend on, are defined
// in ws30-ALS-landclass-search.frag.
// Lookup a ground texture at a point based on the landclass at that point, without visible
// seams at coordinate discontinuities or at landclass boundaries where texture are switched.
@ -196,18 +202,15 @@ vec4 applyHaze(inout vec4 fragColor,
in float lightArg,
in float mie_angle);
// End Test-phase code
////////////////////////
// Procedurally generate a water texel for this fragment
vec4 generateWaterTexel();
void main()
{
vec3 shadedFogColor = vec3(0.55, 0.67, 0.88);
// this is taken from default.frag
vec3 n;
float NdotL, NdotHV, fogFactor;
vec3 n = normalize(normal);
vec3 lightDir = gl_LightSource[0].position.xyz;
vec3 halfVector = gl_LightSource[0].halfVector.xyz;
vec4 texel;
@ -220,26 +223,13 @@ void main()
vec2 st;
vec4 mat_ambient, mat_diffuse, mat_specular, dxdy;
// Oct 27 2021:
// Geometry is in the form of roughly rectangular 'tiles'
// with a mesh forming a grid with regular spacing.
// Each vertex in the mesh is given an elevation
// Tile dimensions in m
// Testing: created from two float uniforms in global scope. Should be sent as a vec2
// vec2 tile_size
// Tile texture coordinates range [0..1] over the tile 'rectangle'
vec2 tile_coord = gl_TexCoord[0].st;
// Test phase: Constants and toggles for transitions between landlcasses are defined at
// the top of this file.
// Look up the landclass id [0 .. 255] for this particular fragment
// and any neighbouring landclass that is close.
// Each tile has 1 texture containing landclass ids stetched over it.
// Landclass for current fragment, and up-to 4 neighboring landclasses - 2 used currently
int lc;
ivec4 lc_n;
@ -266,67 +256,70 @@ void main()
texel = lookup_ground_texture_array(0, st, lc, dxdy);
}
vec4 color = mat_ambient * (gl_LightModel.ambient + gl_LightSource[0].ambient);
if ((water_shader == 1) && (fg_photoScenery == false) && fg_materialParams3[lc].x > 0.5) {
// This is a water fragment, so calculate the fragment color procedurally
fragColor = generateWaterTexel();
fragColor.rgb += getClusteredLightsContribution(ecPosition.xyz, n, fragColor.rgb);
} else {
// Photoscenery or land fragment, so determine the shading and color normally
vec4 color = mat_ambient * (gl_LightModel.ambient + gl_LightSource[0].ambient);
// Testing code:
// Use rlc even when looking up textures to recreate the extra performance hit
// so any performance difference between the two is due to the texture lookup
// color = color+0.00001*float(get_random_landclass(tile_coord.st, tile_size));
// Testing code:
// Use rlc even when looking up textures to recreate the extra performance hit
// so any performance difference between the two is due to the texture lookup
// color = color+0.00001*float(get_random_landclass(tile_coord.st, tile_size));
float effective_scattering = min(scattering, cloud_self_shading);
float effective_scattering = min(scattering, cloud_self_shading);
vec4 light_specular = gl_LightSource[0].specular;
vec4 light_specular = gl_LightSource[0].specular;
// If gl_Color.a == 0, this is a back-facing polygon and the
// normal should be reversed.
//n = (2.0 * gl_Color.a - 1.0) * normal;
n = normalize(normal);
// If gl_Color.a == 0, this is a back-facing polygon and the
// normal should be reversed.
//n = (2.0 * gl_Color.a - 1.0) * normal;
NdotL = dot(n, lightDir);
vec4 diffuse_term = light_diffuse_comp * mat_diffuse;
if (NdotL > 0.0) {
float shadowmap = getShadowing();
vec4 diffuse_term = light_diffuse_comp * mat_diffuse;
color += diffuse_term * NdotL * shadowmap;
NdotHV = max(dot(n, halfVector), 0.0);
if (mat_shininess > 0.0)
specular.rgb = (mat_specular.rgb
* light_specular.rgb
* pow(NdotHV, gl_FrontMaterial.shininess)
* shadowmap);
NdotL = dot(n, lightDir);
vec4 diffuse_term = light_diffuse_comp * mat_diffuse;
if (NdotL > 0.0) {
float shadowmap = getShadowing();
vec4 diffuse_term = light_diffuse_comp * mat_diffuse;
color += diffuse_term * NdotL * shadowmap;
NdotHV = max(dot(n, halfVector), 0.0);
if (mat_shininess > 0.0)
specular.rgb = (mat_specular.rgb
* light_specular.rgb
* pow(NdotHV, gl_FrontMaterial.shininess)
* shadowmap);
}
color.a = diffuse_term.a;
// This shouldn't be necessary, but our lighting becomes very
// saturated. Clamping the color before modulating by the texture
// is closer to what the OpenGL fixed function pipeline does.
color = clamp(color, 0.0, 1.0);
// Testing code: mix with green to show values of variables at each point
//vec4 green = vec4(0.0, 0.5, 0.0, 0.0);
//texel = mix(texel, green, (mfact[2]));
fragColor = color * texel + specular;
fragColor.rgb += getClusteredLightsContribution(ecPosition.xyz, n, texel.rgb);
}
color.a = diffuse_term.a;
// This shouldn't be necessary, but our lighting becomes very
// saturated. Clamping the color before modulating by the texture
// is closer to what the OpenGL fixed function pipeline does.
color = clamp(color, 0.0, 1.0);
// Testing code: mix with green to show values of variables at each point
//vec4 green = vec4(0.0, 0.5, 0.0, 0.0);
//texel = mix(texel, green, (mfact[2]));
fragColor = color * texel + specular;
fragColor.rgb += getClusteredLightsContribution(ecPosition.xyz, n, texel.rgb);
float dist = length(relPos);
// angle with horizon
float dist = length(relPos);
float ct = dot(vec3(0.0, 0.0, 1.0), relPos)/dist;
float lightArg = (terminator-yprime_alt)/100000.0;
vec3 hazeColor = get_hazeColor(lightArg);
gl_FragColor = applyHaze(fragColor, hazeColor, vec3(0.0), ct, hazeLayerAltitude, visibility, avisibility, dist, lightArg, mie_angle);
// Testing phase controls:
if (remove_haze_and_lighting == 1)
{
gl_FragColor = texel;
}
// Testing phase controls:
if (remove_haze_and_lighting == 1)
{
gl_FragColor = texel;
}
}

View file

@ -33,6 +33,18 @@ varying vec4 ecPosition;
varying float yprime_alt;
varying float mie_angle;
// For water calculations
varying float earthShade;
varying vec3 lightdir;
varying vec4 waterTex1;
varying vec4 waterTex2;
varying vec4 waterTex4;
varying vec3 specular_light;
uniform float osg_SimulationTime;
uniform float WindN;
uniform float WindE;
uniform int colorMode;
uniform float hazeLayerAltitude;
uniform float terminator;
@ -42,6 +54,7 @@ uniform float visibility;
uniform float overcast;
uniform float ground_scattering;
uniform float moonlight;
uniform float eye_alt;
void setupShadows(vec4 eyeSpacePos);
@ -49,19 +62,20 @@ void setupShadows(vec4 eyeSpacePos);
const float EarthRadius = 5800000.0;
const float terminator_width = 200000.0;
float earthShade;
float light_func (in float x, in float a, in float b, in float c, in float d, in float e)
{
//x = x - 0.5;
// use the asymptotics to shorten computations
if (x < -15.0) {return 0.0;}
return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d));
// use the asymptotics to shorten computations
if (x < -15.0) {return 0.0;}
return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d));
}
void rotationmatrix(in float angle, out mat4 rotmat)
{
rotmat = mat4( cos( angle ), -sin( angle ), 0.0, 0.0,
sin( angle ), cos( angle ), 0.0, 0.0,
0.0 , 0.0 , 1.0, 0.0,
0.0 , 0.0 , 0.0, 1.0 );
}
void main()
{
@ -86,6 +100,28 @@ void main()
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
normal = gl_NormalMatrix * gl_Normal;
// Required for water calculations
lightdir = normalize(vec3(fg_zUpTransform * vec4(gl_ModelViewMatrixInverse * gl_LightSource[0].position)));
waterTex4 = vec4( ecPosition.xzy, 0.0 );
vec4 t1 = vec4(0.0, osg_SimulationTime * 0.005217, 0.0, 0.0);
vec4 t2 = vec4(0.0, osg_SimulationTime * -0.0012, 0.0, 0.0);
float Angle;
float windFactor = sqrt(WindE * WindE + WindN * WindN) * 0.05;
if (WindN == 0.0 && WindE == 0.0) {
Angle = 0.0;
} else {
Angle = atan(-WindN, WindE) - atan(1.0);
}
mat4 RotationMatrix;
rotationmatrix(Angle, RotationMatrix);
waterTex1 = gl_MultiTexCoord0 * RotationMatrix - t1 * windFactor;
rotationmatrix(Angle, RotationMatrix);
waterTex2 = gl_MultiTexCoord0 * RotationMatrix - t2 * windFactor;
// Temporary value:
ground_tex_coord = gl_TexCoord[0].st;
@ -103,125 +139,131 @@ void main()
float dist = length(relPos);
// altitude of the vertex in question, somehow zero leads to artefacts, so ensure it is at least 100m
vertex_alt = max(relPos.z,100.0);
vertex_alt = max(relPos.z + eye_alt, 100.0);
scattering = ground_scattering + (1.0 - ground_scattering) * smoothstep(hazeLayerAltitude -100.0, hazeLayerAltitude + 100.0, vertex_alt);
// branch dependent on daytime
if (terminator < 1000000.0) // the full, sunrise and sunset computation
{
// establish coordinates relative to sun position
if (terminator < 1000000.0) // the full, sunrise and sunset computation
{
vec3 lightFull = (gl_ModelViewMatrixInverse * gl_LightSource[0].position).xyz;
vec3 lightHorizon = normalize(vec3(lightFull.x,lightFull.y, 0.0));
// yprime is the distance of the vertex into sun direction
yprime = -dot(relPos, lightHorizon);
// this gets an altitude correction, higher terrain gets to see the sun earlier
yprime_alt = yprime - sqrt(2.0 * EarthRadius * vertex_alt);
// two times terminator width governs how quickly light fades into shadow
// now the light-dimming factor
earthShade = 0.6 * (1.0 - smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt)) + 0.4;
// parametrized version of the Flightgear ground lighting function
lightArg = (terminator-yprime_alt)/100000.0;
// establish coordinates relative to sun position
// directional scattering for low sun
if (lightArg < 10.0) {
mie_angle = (0.5 * dot(normalize(relPos), normalize(lightFull)) ) + 0.5;
} else {
mie_angle = 1.0;
}
vec3 lightFull = (gl_ModelViewMatrixInverse * gl_LightSource[0].position).xyz;
vec3 lightHorizon = normalize(vec3(lightFull.x,lightFull.y, 0.0));
// yprime is the distance of the vertex into sun direction
yprime = -dot(relPos, lightHorizon);
// this gets an altitude correction, higher terrain gets to see the sun earlier
yprime_alt = yprime - sqrt(2.0 * EarthRadius * vertex_alt);
// two times terminator width governs how quickly light fades into shadow
// now the light-dimming factor
earthShade = 0.6 * (1.0 - smoothstep(-terminator_width+ terminator, terminator_width + terminator, yprime_alt)) + 0.4;
// parametrized version of the Flightgear ground lighting function
lightArg = (terminator-yprime_alt)/100000.0;
// directional scattering for low sun
if (lightArg < 10.0)
{mie_angle = (0.5 * dot(normalize(relPos), normalize(lightFull)) ) + 0.5;}
else
{mie_angle = 1.0;}
light_diffuse.b = light_func(lightArg, 1.330e-05, 0.264, 3.827, 1.08e-05, 1.0);
light_diffuse.g = light_func(lightArg, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0);
light_diffuse.r = light_func(lightArg, 8.305e-06, 0.161, 3.827, 3.04e-05, 1.0);
light_diffuse.a = 1.0;
light_diffuse = light_diffuse * scattering;
light_ambient.r = light_func(lightArg, 0.236, 0.253, 1.073, 0.572, 0.33);
light_ambient.g = light_ambient.r * 0.4/0.33;
light_ambient.b = light_ambient.r * 0.5/0.33;
light_ambient.a = 1.0;
// Water specular calculations
specular_light.b = light_func(lightArg, 1.330e-05, 0.264, 3.827, 1.08e-05, 1.0);
specular_light.g = light_func(lightArg, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0);
specular_light.r = light_func(lightArg, 8.305e-06, 0.161, 3.827, 3.04e-05, 1.0);
specular_light = max(specular_light * scattering, vec3 (0.05, 0.05, 0.05));
intensity = length(specular_light.rgb);
specular_light.rgb = intensity * normalize(mix(specular_light.rgb, shadedFogColor, 1.0 -smoothstep(0.1, 0.6,ground_scattering) ));
specular_light.rgb = intensity * normalize(mix(specular_light.rgb, shadedFogColor, 1.0 -smoothstep(0.5, 0.7,earthShade)));
light_diffuse.b = light_func(lightArg, 1.330e-05, 0.264, 3.827, 1.08e-05, 1.0);
light_diffuse.g = light_func(lightArg, 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0);
light_diffuse.r = light_func(lightArg, 8.305e-06, 0.161, 3.827, 3.04e-05, 1.0);
light_diffuse.a = 1.0;
light_diffuse = light_diffuse * scattering;
// correct ambient light intensity and hue before sunrise
if (earthShade < 0.5) {
//light_ambient = light_ambient * (0.7 + 0.3 * smoothstep(0.2, 0.5, earthShade));
intensity = length(light_ambient.xyz);
light_ambient.rgb = intensity * normalize(mix(light_ambient.rgb, shadedFogColor, 1.0 -smoothstep(0.4, 0.8,earthShade) ));
light_ambient.rgb = light_ambient.rgb + moonLightColor * (1.0 - smoothstep(0.4, 0.5, earthShade));
light_ambient.r = light_func(lightArg, 0.236, 0.253, 1.073, 0.572, 0.33);
light_ambient.g = light_ambient.r * 0.4/0.33;
light_ambient.b = light_ambient.r * 0.5/0.33;
light_ambient.a = 1.0;
intensity = length(light_diffuse.xyz);
light_diffuse.rgb = intensity * normalize(mix(light_diffuse.rgb, shadedFogColor, 1.0 -smoothstep(0.4, 0.7,earthShade) ));
}
// directional scattering for low sun
if (lightArg < 10.0) {
mie_angle = (0.5 * dot(normalize(relPos), lightdir) ) + 0.5;
} else {
mie_angle = 1.0;
}
// the haze gets the light at the altitude of the haze top if the vertex in view is below
// but the light at the vertex if the vertex is above
vertex_alt = max(vertex_alt,hazeLayerAltitude);
// correct ambient light intensity and hue before sunrise
if (earthShade < 0.5)
{
//light_ambient = light_ambient * (0.7 + 0.3 * smoothstep(0.2, 0.5, earthShade));
intensity = length(light_ambient.xyz);
if (vertex_alt > hazeLayerAltitude)
{
if (dist > 0.8 * avisibility)
{
vertex_alt = mix(vertex_alt, hazeLayerAltitude, smoothstep(0.8*avisibility, avisibility, dist));
yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt);
}
} else {
vertex_alt = hazeLayerAltitude;
yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt);
}
}
else // the faster, full-day version without lightfields
{
earthShade = 1.0;
mie_angle = 1.0;
light_ambient.rgb = intensity * normalize(mix(light_ambient.rgb, shadedFogColor, 1.0 -smoothstep(0.4, 0.8,earthShade) ));
light_ambient.rgb = light_ambient.rgb + moonLightColor * (1.0 - smoothstep(0.4, 0.5, earthShade));
if (terminator > 3000000.0) {
light_diffuse = vec4 (1.0, 1.0, 1.0, 0.0);
light_ambient = vec4 (0.33, 0.4, 0.5, 0.0);
specular_light = vec3 (1.0, 1.0, 1.0);
intensity = length(light_diffuse.xyz);
light_diffuse.rgb = intensity * normalize(mix(light_diffuse.rgb, shadedFogColor, 1.0 -smoothstep(0.4, 0.7,earthShade) ));
}
} else {
lightArg = (terminator/100000.0 - 10.0)/20.0;
light_diffuse.b = 0.78 + lightArg * 0.21;
light_diffuse.g = 0.907 + lightArg * 0.091;
light_diffuse.r = 0.904 + lightArg * 0.092;
light_diffuse.a = 1.0;
light_ambient.r = 0.316 + lightArg * 0.016;
light_ambient.g = light_ambient.r * 0.4/0.33;
light_ambient.b = light_ambient.r * 0.5/0.33;
light_ambient.a = 1.0;
// the haze gets the light at the altitude of the haze top if the vertex in view is below
// but the light at the vertex if the vertex is above
specular_light.b = 0.78 + lightArg * 0.21;
specular_light.g = 0.907 + lightArg * 0.091;
specular_light.r = 0.904 + lightArg * 0.092;
}
light_diffuse = light_diffuse * scattering;
specular_light = specular_light * scattering;
float shade_depth = 1.0 * smoothstep (0.6,0.95,ground_scattering) * (1.0-smoothstep(0.1,0.5,overcast)) * smoothstep(0.4,1.5,earthShade);
specular_light.rgb *= (1.0 + 1.2 * shade_depth);
vertex_alt = max(vertex_alt,hazeLayerAltitude);
if (vertex_alt > hazeLayerAltitude)
{
if (dist > 0.8 * avisibility)
{
vertex_alt = mix(vertex_alt, hazeLayerAltitude, smoothstep(0.8*avisibility, avisibility, dist));
yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt);
}
}
else
{
vertex_alt = hazeLayerAltitude;
yprime_alt = yprime -sqrt(2.0 * EarthRadius * vertex_alt);
}
}
else // the faster, full-day version without lightfields
{
//vertex_alt = max(gl_Vertex.z,100.0);
earthShade = 1.0;
mie_angle = 1.0;
if (terminator > 3000000.0)
{light_diffuse = vec4 (1.0, 1.0, 1.0, 0.0);
light_ambient = vec4 (0.33, 0.4, 0.5, 0.0); }
else
{
lightArg = (terminator/100000.0 - 10.0)/20.0;
light_diffuse.b = 0.78 + lightArg * 0.21;
light_diffuse.g = 0.907 + lightArg * 0.091;
light_diffuse.r = 0.904 + lightArg * 0.092;
light_diffuse.a = 1.0;
light_ambient.r = 0.316 + lightArg * 0.016;
light_ambient.g = light_ambient.r * 0.4/0.33;
light_ambient.b = light_ambient.r * 0.5/0.33;
light_ambient.a = 1.0;
}
light_diffuse = light_diffuse * scattering;
yprime_alt = -sqrt(2.0 * EarthRadius * hazeLayerAltitude);
}
yprime_alt = -sqrt(2.0 * EarthRadius * hazeLayerAltitude);
}
// default lighting based on texture and material using the light we have just computed
// default lighting based on texture and material using the light we have just computed
light_diffuse_comp = light_diffuse;
//Testing phase code: ambient colours are not sent to fragement shader yet.

360
Shaders/ws30-water.frag Normal file
View file

@ -0,0 +1,360 @@
// SPDX-FileCopyrightText: (C) 2022 Stuart Buchanan stuart13@gmail.com
// SPDX-License-Identifier: GPL-2.0-or-later
// Helper functions for WS30 water implementation, heavily based on the
// water-ALS-base.frag and waterr_ALS-high.frag
#version 130
#extension GL_EXT_texture_array : enable
// Hardcoded indexes into the texture atlas
const int ATLAS_INDEX_WATER = 0;
const int ATLAS_INDEX_WATER_REFLECTION = 1;
const int ATLAS_INDEX_WAVES_VERT10_NM = 2;
const int ATLAS_INDEX_WATER_SINE_NMAP = 3;
const int ATLAS_INDEX_WATER_REFLECTION_GREY = 4;
const int ATLAS_INDEX_SEA_FOAM = 5;
const int ATLAS_INDEX_PERLIN_NOISE_NM = 6;
const int ATLAS_INDEX_OCEAN_DEPTH = 7;
const int ATLAS_INDEX_GLOBAL_COLORS = 8;
const int ATLAS_INDEX_PACKICE_OVERLAY = 9;
// WS30 uniforms
uniform sampler2DArray textureArray;
uniform float ground_scattering;
uniform float overcast;
uniform float fg_tileWidth;
uniform float fg_tileHeight;
// Water.eff uniforms
uniform float sea_r;
uniform float sea_g;
uniform float sea_b;
uniform float osg_SimulationTime;
uniform float WindN;
uniform float WindE;
uniform float WaveFreq;
uniform float WaveAmp;
uniform float WaveSharp;
uniform float WaveAngle;
uniform float WaveFactor;
uniform float WaveDAngle;
uniform float saturation;
// WS30 varying
varying vec3 relPos;
// Water.eff varying
varying float earthShade;
varying vec3 lightdir;
varying vec4 waterTex1;
varying vec4 waterTex2;
varying vec4 waterTex4;
varying vec3 specular_light;
/////// functions /////////
float getShadowing();
vec3 getClusteredLightsContribution(vec3 p, vec3 n, vec3 texel);
void rotationmatrix(in float angle, out mat4 rotmat)
{
rotmat = mat4( cos( angle ), -sin( angle ), 0.0, 0.0,
sin( angle ), cos( angle ), 0.0, 0.0,
0.0 , 0.0 , 1.0, 0.0,
0.0 , 0.0 , 0.0, 1.0 );
}
// wave functions ///////////////////////
struct Wave {
float freq; // 2*PI / wavelength
float amp; // amplitude
float phase; // speed * 2*PI / wavelength
vec2 dir;
};
Wave wave0 = Wave(1.0, 1.0, 0.5, vec2(0.97, 0.25));
Wave wave1 = Wave(2.0, 0.5, 1.3, vec2(0.97, -0.25));
Wave wave2 = Wave(1.0, 1.0, 0.6, vec2(0.95, -0.3));
Wave wave3 = Wave(2.0, 0.5, 1.4, vec2(0.99, 0.1));
float evaluateWave(in Wave w, in vec2 pos, in float t) {
return w.amp * sin( dot(w.dir, pos) * w.freq + t * w.phase);
}
// derivative of wave function
float evaluateWaveDeriv(in Wave w, in vec2 pos, in float t) {
return w.freq * w.amp * cos( dot(w.dir, pos)*w.freq + t*w.phase);
}
// sharp wave functions
float evaluateWaveSharp(in Wave w, in vec2 pos, in float t, in float k) {
return w.amp * pow(sin( dot(w.dir, pos)*w.freq + t*w.phase)* 0.5 + 0.5 , k);
}
float evaluateWaveDerivSharp(in Wave w, in vec2 pos, in float t, in float k) {
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);
}
void sumWaves(in float angle, in float dangle, in float windScale, in float factor, out float ddx, float ddy) {
mat4 RotationMatrix;
float deriv;
vec4 P = waterTex1 * 1024;
rotationmatrix(radians(angle + dangle * windScale + 0.6 * sin(P.x * factor)), RotationMatrix);
P *= RotationMatrix;
P.y += evaluateWave(wave0, P.xz, osg_SimulationTime);
deriv = evaluateWaveDeriv(wave0, P.xz, osg_SimulationTime );
ddx = deriv * wave0.dir.x;
ddy = deriv * wave0.dir.y;
//P.y += evaluateWave(wave1, P.xz, osg_SimulationTime);
//deriv = evaluateWaveDeriv(wave1, P.xz, osg_SimulationTime);
//ddx += deriv * wave1.dir.x;
//ddy += deriv * wave1.dir.y;
P.y += evaluateWaveSharp(wave2, P.xz, osg_SimulationTime, WaveSharp);
deriv = evaluateWaveDerivSharp(wave2, P.xz, osg_SimulationTime, WaveSharp);
ddx += deriv * wave2.dir.x;
ddy += deriv * wave2.dir.y;
//P.y += evaluateWaveSharp(wave3, P.xz, osg_SimulationTime, WaveSharp);
//deriv = evaluateWaveDerivSharp(wave3, P.xz, osg_SimulationTime, WaveSharp);
//ddx += deriv * wave3.dir.x;
//ddy += deriv * wave3.dir.y;
}
vec4 generateWaterTexel()
{
vec4 texel;
float dist = length(relPos);
float tileScale = 1 / (fg_tileHeight + fg_tileWidth) / 2.0;
vec4 sca = vec4(0.005, 0.005, 0.005, 0.005) * tileScale;
vec4 sca2 = vec4(0.02, 0.02, 0.02, 0.02) * tileScale;
vec4 tscale = vec4(0.25, 0.25, 0.25, 0.25) / 10000.0 * tileScale;
mat4 RotationMatrix;
// compute direction to viewer
vec3 E = normalize(-relPos);
// compute direction to light source
vec3 L = normalize(lightdir);
// half vector
vec3 Hv = normalize(L + E);
vec3 Normal = vec3 (0.0, 0.0, 1.0);
const float water_shininess = 240.0;
float windEffect = sqrt( WindE*WindE + WindN*WindN ) * 0.6; //wind speed in kt
float windScale = 15.0/(3.0 + windEffect); //wave scale
float windEffect_low = 0.3 + 0.7 * smoothstep(0.0, 5.0, windEffect); //low windspeed wave filter
float waveRoughness = 0.01 + smoothstep(0.0, 40.0, windEffect); //wave roughness filter
float mixFactor = 0.2 + 0.02 * smoothstep(0.0, 50.0, windEffect);
mixFactor = clamp(mixFactor, 0.3, 0.8);
// there's no need to do wave patterns or foam for pixels which are so far away that we can't actually see them
// we only need detail in the near zone or where the sun reflection is
int detail_flag;
if ((dist > 15000.0) && (dot(normalize(vec3 (lightdir.x, lightdir.y, 0.0) ), normalize(relPos)) < 0.7 )) {detail_flag = 0;}
else {detail_flag = 1;}
// sine waves
float ddx, ddx1, ddx2, ddx3, ddy, ddy1, ddy2, ddy3;
float angle;
ddx = 0.0, ddy = 0.0;
ddx1 = 0.0, ddy1 = 0.0;
ddx2 = 0.0, ddy2 = 0.0;
ddx3 = 0.0, ddy3 = 0.0;
if (detail_flag == 1)
{
angle = 0.0;
wave0.freq = WaveFreq ;
wave0.amp = WaveAmp;
wave0.dir = vec2 (0.0, 1.0); //vec2(cos(radians(angle)), sin(radians(angle)));
angle -= 45;
wave1.freq = WaveFreq * 2.0 ;
wave1.amp = WaveAmp * 1.25;
wave1.dir = vec2(0.70710, -0.7071); //vec2(cos(radians(angle)), sin(radians(angle)));
angle += 30;
wave2.freq = WaveFreq * 3.5;
wave2.amp = WaveAmp * 0.75;
wave2.dir = vec2(0.96592, -0.2588);// vec2(cos(radians(angle)), sin(radians(angle)));
angle -= 50;
wave3.freq = WaveFreq * 3.0 ;
wave3.amp = WaveAmp * 0.75;
wave3.dir = vec2(0.42261, -0.9063); //vec2(cos(radians(angle)), sin(radians(angle)));
// sum waves
sumWaves(WaveAngle, -1.5, windScale, WaveFactor, ddx, ddy);
sumWaves(WaveAngle, 1.5, windScale, WaveFactor, ddx1, ddy1);
//reset the waves
angle = 0.0;
float waveamp = WaveAmp * 0.75;
wave0.freq = WaveFreq ;
wave0.amp = waveamp;
wave0.dir = vec2 (0.0, 1.0); //vec2(cos(radians(angle)), sin(radians(angle)));
angle -= 20;
wave1.freq = WaveFreq * 2.0 ;
wave1.amp = waveamp * 1.25;
wave1.dir = vec2(0.93969, -0.34202);// vec2(cos(radians(angle)), sin(radians(angle)));
angle += 35;
wave2.freq = WaveFreq * 3.5;
wave2.amp = waveamp * 0.75;
wave2.dir = vec2(0.965925, 0.25881); //vec2(cos(radians(angle)), sin(radians(angle)));
angle -= 45;
wave3.freq = WaveFreq * 3.0 ;
wave3.amp = waveamp * 0.75;
wave3.dir = vec2(0.866025, -0.5); //vec2(cos(radians(angle)), sin(radians(angle)));
//sumWaves(WaveAngle + WaveDAngle, -1.5, windScale, WaveFactor, ddx2, ddy2);
//sumWaves(WaveAngle + WaveDAngle, 1.5, windScale, WaveFactor, ddx3, ddy3);
}
// end sine stuff
//cover = 5.0 * smoothstep(0.6, 1.0, scattering);
//cover = 5.0 * ground_scattering;
vec4 viewt = normalize(waterTex4);
vec2 st = vec2(waterTex2 * tscale * windScale);
vec4 disdis = texture(textureArray, vec3(st, ATLAS_INDEX_WATER_SINE_NMAP)) * 2.0 - 1.0;
vec4 vNorm;
//normalmaps
st = vec2(waterTex1 + disdis * sca2) * windScale;
vec4 nmap = texture(textureArray, vec3(st, ATLAS_INDEX_WAVES_VERT10_NM)) * 2.0 - 1.0;
vec4 nmap1 = texture(textureArray, vec3(st, ATLAS_INDEX_PERLIN_NOISE_NM)) * 2.0 - 1.0;
rotationmatrix(radians(3.0 * sin(osg_SimulationTime * 0.0075)), RotationMatrix);
st = vec2(waterTex2 * RotationMatrix * tscale) * windScale;
nmap += texture(textureArray, vec3(st, ATLAS_INDEX_WAVES_VERT10_NM)) * 2.0 - 1.0;
nmap *= windEffect_low;
nmap1 *= windEffect_low;
// mix water and noise, modulated by factor
vNorm = normalize(mix(nmap, nmap1, mixFactor) * waveRoughness);
vNorm.r += ddx + ddx1 + ddx2 + ddx3;
//if (normalmap_dds > 0) {vNorm = -vNorm;} //dds fix
//load reflection
vec4 refl ;
refl.r = sea_r;
refl.g = sea_g;
refl.b = sea_b;
refl.a = 1.0;
float intensity;
// de-saturate for reduced light
refl.rgb = mix(refl.rgb, vec3 (0.248, 0.248, 0.248), 1.0 - smoothstep(0.1, 0.8, ground_scattering));
// de-saturate light for overcast haze
intensity = length(refl.rgb);
refl.rgb = mix(refl.rgb, intensity * vec3 (1.0, 1.0, 1.0), 0.5 * smoothstep(0.1, 0.9, overcast));
vec3 N;
st = vec2(waterTex1 + disdis * sca2) * windScale;
vec3 N0 = vec3(texture(textureArray, vec3(st, ATLAS_INDEX_WAVES_VERT10_NM))) * 2.0 - 1.0;
st = vec2(waterTex1 + disdis * sca) * windScale;
vec3 N1 = vec3(texture(textureArray, vec3(st, ATLAS_INDEX_PERLIN_NOISE_NM))) * 2.0 - 1.0;
st = vec2(waterTex1 * tscale) * windScale;
N0 += vec3(texture(textureArray, vec3(st, ATLAS_INDEX_WAVES_VERT10_NM))) * 2.0 - 1.0;
N1 += vec3(texture(textureArray, vec3(st, ATLAS_INDEX_PERLIN_NOISE_NM))) * 2.0 - 1.0;
rotationmatrix(radians(2.0 * sin(osg_SimulationTime * 0.005)), RotationMatrix);
st = vec2(waterTex2 * RotationMatrix * (tscale + sca2)) * windScale;
N0 += vec3(texture(textureArray, vec3(st, ATLAS_INDEX_WAVES_VERT10_NM))) * 2.0 - 1.0;
N1 += vec3(texture(textureArray, vec3(st, ATLAS_INDEX_PERLIN_NOISE_NM))) * 2.0 - 1.0;
rotationmatrix(radians(-4.0 * sin(osg_SimulationTime * 0.003)), RotationMatrix);
st = vec2(waterTex1 * RotationMatrix + disdis * sca2) * windScale;
N0 += vec3(texture(textureArray, vec3(st, ATLAS_INDEX_WAVES_VERT10_NM))) * 2.0 - 1.0;
st = vec2(waterTex1 * RotationMatrix + disdis * sca) * windScale;
N1 += vec3(texture(textureArray, vec3(st, ATLAS_INDEX_PERLIN_NOISE_NM))) * 2.0 - 1.0;
N0 *= windEffect_low;
N1 *= windEffect_low;
N0.r += (ddx + ddx1 + ddx2 + ddx3);
N0.g += (ddy + ddy1 + ddy2 + ddy3);
N = normalize(mix(Normal + N0, Normal + N1, mixFactor) * waveRoughness);
vec3 specular_color = vec3(specular_light * earthShade) * pow(max(0.0, dot(N, Hv)), water_shininess) * 6.0;
// secondary reflection of sky irradiance
vec3 ER = E - 2.0 * N * dot(E,N);
float ctrefl = dot(vec3(0.0,0.0,1.0), -normalize(ER));
//float fresnel = -0.5 + 8.0 * (1.0-smoothstep(0.0,0.4, dot(E,N)));
float fresnel = 8.0 * (1.0-smoothstep(0.0,0.4, dot(E,N)));
//specular_color += (ctrefl*ctrefl) * fresnel* specular_light.rgb;
specular_color += ((0.15*(1.0-ctrefl* ctrefl) * fresnel) - 0.3) * specular_light.rgb * earthShade;
vec4 specular = vec4(specular_color, 0.5);
specular = specular * saturation * 0.3 * earthShade ;
//calculate fresnel
vec4 invfres = vec4( dot(vNorm, viewt) );
vec4 fres = vec4(1.0) + invfres;
refl *= fres;
vec4 ambient_light;
//intensity = length(specular_light.rgb);
ambient_light.rgb = max(specular_light.rgb * earthShade, vec3(0.05, 0.05, 0.05));
//ambient_light.rgb = max(intensity * normalize(vec3 (0.33, 0.4, 0.5)), vec3 (0.1,0.1,0.1));
ambient_light.a = 1.0;
// compute object shadow effect
float shadowValue = getShadowing();
specular = specular * shadowValue;
refl = refl * (0.7 + 0.3 *shadowValue);
texel = refl + specular * smoothstep(0.3, 0.6, ground_scattering);
// For the clustered lighting function we use the simple up direction (Normal) to get an
// approximate lighting contribution, as the procedural normal map is done afterwards.
//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);
if (dist < 10000.0)
{
float foamSlope = 0.10 + 0.1 * windScale;
float waveSlope = N.g;
if ((windEffect >= 8.0) && (waveSlope >= foamSlope)) {
//add foam
st = vec2(waterTex2 * tscale) * 25.0;
vec4 foam_texel = texture(textureArray, vec3(st, ATLAS_INDEX_SEA_FOAM) );
texel = mix(texel, max(texel, texel + foam_texel), smoothstep(0.01, 0.50, N.g));
}
}
texel *= ambient_light;
return texel;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 137 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 564 KiB