From 7df5690f083fab223f06fb71b6edf40aef7d39d9 Mon Sep 17 00:00:00 2001 From: Thorsten Renk Date: Sat, 16 Sep 2017 09:44:18 +0300 Subject: [PATCH] ALS terrain overlay shaders, not yet used, terrain definitions will follow post-release --- Effects/terrain-overlay.eff | 918 ++++++++++++++++++ .../local_weather/weather_tile_management.nas | 22 +- Shaders/terrain-overlay-ALS.frag | 173 ++++ Shaders/terrain-overlay-ALS.geom | 62 ++ 4 files changed, 1173 insertions(+), 2 deletions(-) create mode 100644 Effects/terrain-overlay.eff create mode 100644 Shaders/terrain-overlay-ALS.frag create mode 100644 Shaders/terrain-overlay-ALS.geom diff --git a/Effects/terrain-overlay.eff b/Effects/terrain-overlay.eff new file mode 100644 index 000000000..8a00ba418 --- /dev/null +++ b/Effects/terrain-overlay.eff @@ -0,0 +1,918 @@ + + + Effects/terrain-overlay + Effects/terrain-default + + + + + Textures/Terrain/void.png + 2d + nearest-mipmap-nearest + nearest + repeat + repeat + normalized + + + + Textures/Terrain/void.png + 2d + nearest-mipmap-nearest + nearest + repeat + repeat + normalized + + + 1.5 + 1.0 + 1.0 + 0.5 + 0.5 + 0.5 + 0.5 + 0.8 + 0.0 + 0 + 0 + + + + + + + + + + + /sim/rendering/shaders/skydome + + + 6.0 + /sim/rendering/shaders/landmass + + + 6.0 + /sim/rendering/shaders/transition + + + 1.0 + /sim/rendering/shaders/vegetation-effects + + + + + 2.0 + + + + GL_ARB_shader_objects + GL_ARB_shading_language_100 + GL_ARB_vertex_shader + GL_ARB_fragment_shader + GL_EXT_geometry_shader4 + + + + + + + true + + material/ambient + material/diffuse + material/specular + material/emissive + material/shininess + ambient-and-diffuse + + transparent + transparent + smooth + back + + render-bin/bin-number + render-bin/bin-name + + + 0 + texture[0]/image + texture[0]/type + texture[0]/filter + texture[0]/wrap-s + texture[0]/wrap-t + texture[0]/internal-format + + + 2 + texture[13]/image + texture[13]/type + texture[13]/filter + texture[13]/wrap-s + texture[13]/wrap-t + texture[13]/internal-format + + + 3 + texture[15]/type + texture[15]/image + texture[15]/filter + texture[15]/wrap-s + texture[15]/wrap-t + texture[15]/internal-format + + + 4 + texture[14]/type + texture[14]/image + texture[14]/filter + texture[14]/wrap-s + texture[14]/wrap-t + texture[14]/internal-format + + + 5 + texture[12]/type + texture[12]/image + texture[12]/filter + texture[12]/wrap-s + texture[12]/wrap-t + texture[12]/internal-format + + + 7 + texture[11]/type + texture[11]/image + texture[11]/filter + texture[11]/wrap-s + texture[11]/wrap-t + texture[11]/internal-format + + + Shaders/terrain-ALS-ultra.vert + Shaders/terrain-ALS-ultra.frag + Shaders/noise.frag + Shaders/cloud-shadowfunc.frag + Shaders/hazes.frag + Shaders/secondary_lights.frag + Shaders/filters-ALS.frag + + + grain_strength + float + grain_strength + + + intrinsic_wetness + float + intrinsic_wetness + + + transition_model + float + transition_model + + + hires_overlay_bias + float + hires_overlay_bias + + + dot_density + float + dot_density + + + dot_size + float + dot_size + + + dust_resistance + float + dust_resistance + + + visibility + float + visibility + + + avisibility + float + avisibility + + + hazeLayerAltitude + float + lthickness + + + scattering + float + scattering + + + ground_scattering + float + ground_scattering + + + terminator + float + terminator + + + terrain_alt + float + terrain_alt + + + overcast + float + overcast + + + eye_alt + float + eye_alt + + + eye_lat + float + eye_lat + + + eye_lon + float + eye_lon + + + snowlevel + float + snow_level + + + snow_thickness_factor + float + snow_thickness_factor + + + dust_cover_factor + float + dust_cover_factor + + + lichen_cover_factor + float + lichen_cover_factor + + + wetness + float + wetness + + + fogstructure + float + fogstructure + + + cloud_self_shading + float + cloud_self_shading + + + moonlight + float + moonlight + + + season + float + season + + + air_pollution + float + air_pollution + + + + view_pitch_offset + float + view_pitch_offset + + + view_heading_offset + float + view_heading_offset + + + field_of_view + float + view_fov + + + landing_light1_offset + float + landing_light1_offset + + + landing_light2_offset + float + landing_light2_offset + + + landing_light3_offset + float + landing_light3_offset + + + + gamma + float + gamma + + + brightness + float + brightness + + + use_night_vision + bool + use_night_vision + + + use_IR_vision + bool + use_IR_vision + + + use_filtering + bool + use_filtering + + + delta_T + float + delta_T + + + fact_grey + float + fact_grey + + + fact_black + float + fact_black + + + + cloudpos1_x + float + cloudpos1_x + + + cloudpos1_y + float + cloudpos1_y + + + cloudpos2_x + float + cloudpos2_x + + + cloudpos2_y + float + cloudpos2_y + + + cloudpos3_x + float + cloudpos3_x + + + cloudpos3_y + float + cloudpos3_y + + + cloudpos4_x + float + cloudpos4_x + + + cloudpos4_y + float + cloudpos4_y + + + cloudpos5_x + float + cloudpos5_x + + + cloudpos5_y + float + cloudpos5_y + + + cloudpos6_x + float + cloudpos6_x + + + cloudpos6_y + float + cloudpos6_y + + + cloudpos7_x + float + cloudpos7_x + + + cloudpos7_y + float + cloudpos7_y + + + cloudpos8_x + float + cloudpos8_x + + + cloudpos8_y + float + cloudpos8_y + + + cloudpos9_x + float + cloudpos9_x + + + cloudpos9_y + float + cloudpos9_y + + + cloudpos10_x + float + cloudpos10_x + + + cloudpos10_y + float + cloudpos10_y + + + cloudpos11_x + float + cloudpos11_x + + + cloudpos11_y + float + cloudpos11_y + + + cloudpos12_x + float + cloudpos12_x + + + cloudpos12_y + float + cloudpos12_y + + + cloudpos13_x + float + cloudpos13_x + + + cloudpos13_y + float + cloudpos13_y + + + cloudpos14_x + float + cloudpos14_x + + + cloudpos14_y + float + cloudpos14_y + + + cloudpos15_x + float + cloudpos15_x + + + cloudpos15_y + float + cloudpos15_y + + + cloudpos16_x + float + cloudpos16_x + + + cloudpos16_y + float + cloudpos16_y + + + cloudpos17_x + float + cloudpos17_x + + + cloudpos17_y + float + cloudpos17_y + + + cloudpos18_x + float + cloudpos18_x + + + cloudpos18_y + float + cloudpos18_y + + + cloudpos19_x + float + cloudpos19_x + + + cloudpos19_y + float + cloudpos19_y + + + cloudpos20_x + float + cloudpos20_x + + + cloudpos20_y + float + cloudpos20_y + + + WindE + float + windE + + + WindN + float + windN + + + use_searchlight + int + use_searchlight + + + use_landing_light + int + use_landing_light + + + use_alt_landing_light + int + use_alt_landing_light + + + display_xsize + int + display_xsize + + + display_ysize + int + display_ysize + + + wind_effects + int + wind_effects + + + cloud_shadow_flag + int + cloud_shadow_flag + + + rock_strata + int + rock_strata + + + raise_vertex + bool + raise_vertex + + + texture + sampler-2d + 0 + + + mix_texture + sampler-2d + 5 + + + detail_texture + sampler-2d + 7 + + + grain_texture + sampler-2d + 4 + + + dot_texture + sampler-2d + 3 + + + gradient_texture + sampler-2d + 2 + + + colorMode + int + 2 + + + + lequal + + + + + true + + material/ambient + material/diffuse + material/specular + material/emissive + material/shininess + ambient-and-diffuse + + transparent + smooth + + + back + + + 111 + DepthSortedBin + + + 7 + texture[20]/type + texture[20]/image + texture[20]/filter + texture[20]/mag-filter + texture[20]/wrap-s + texture[20]/wrap-t + texture[20]/internal-format + + + 8 + texture[21]/type + texture[21]/image + texture[21]/filter + texture[21]/mag-filter + texture[21]/wrap-s + texture[21]/wrap-t + texture[21]/internal-format + + + Shaders/grass-ALS.vert + Shaders/terrain-overlay-ALS.geom + Shaders/terrain-overlay-ALS.frag + Shaders/noise.frag + Shaders/filters-ALS.frag + Shaders/cloud-shadowfunc.frag + 96 + triangles + triangle-strip + + + overlayPrimaryTex + sampler-2d + 7 + + + overlaySecondaryTex + sampler-2d + 8 + + + visibility + float + visibility + + + avisibility + float + avisibility + + + scattering + float + scattering + + + overlay_bias + float + overlay_bias + + + overlay_max_height + float + max_overlay_height + + + overlay_hardness + float + overlay_hardness + + + overlay_secondary_hardness + float + overlay_secondary_hardness + + + overlay_density + float + overlay_density + + + overlay_secondary_density + float + overlay_secondary_density + + + overlay_scale + float + overlay_scale + + + overlay_brightness_top + float + overlay_brightness_top + + + overlay_brightness_bottom + float + overlay_brightness_bottom + + + overlay_steepness_factor + float + overlay_steepness_factor + + + season + float + season + + + dust_cover_factor + float + dust_cover_factor + + + wetness + float + wetness + + + snowlevel + float + snow_level + + + snow_thickness_factor + float + snow_thickness_factor + + + + + cloudpos_n_x + float + cloudpos_n_x + + + cloudpos_n_y + float + cloudpos_n_y + + + + + + gamma + float + gamma + + + brightness + float + brightness + + + use_night_vision + bool + use_night_vision + + + use_IR_vision + bool + use_IR_vision + + + use_filtering + bool + use_filtering + + + delta_T + float + delta_T + + + fact_grey + float + fact_grey + + + fact_black + float + fact_black + + + + display_xsize + int + display_xsize + + + display_ysize + int + display_ysize + + + overlay_autumn_flag + int + overlay_autumn_flag + + + overlay_secondary_flag + int + overlay_secondary_flag + + + cloud_shadow_flag + int + cloud_shadow_flag + + + + + true + src-alpha + one-minus-src-alpha + + + + false + + + + diff --git a/Nasal/local_weather/weather_tile_management.nas b/Nasal/local_weather/weather_tile_management.nas index 9da5d565f..3bf608e2c 100644 --- a/Nasal/local_weather/weather_tile_management.nas +++ b/Nasal/local_weather/weather_tile_management.nas @@ -1455,14 +1455,19 @@ for (var i = index; i < i_max; i = i+1) } var index_max = -1; +var index_min = -1; var dist_max = -1.0; +var dist_min = 1000000.0; var counter = 0; +var diffx = 0.0; +var diffy = 0.0; + foreach(s; cloudShadowArray) { - var diffx = (s.lat - eyeLat) * local_weather.lat_to_m + offset_x; - var diffy = -(s.lon - eyeLon) * local_weather.lon_to_m + offset_y; + diffx = (s.lat - eyeLat) * local_weather.lat_to_m + offset_x; + diffy = -(s.lon - eyeLon) * local_weather.lon_to_m + offset_y; var dist = math.sqrt(diffx*diffx + diffy*diffy); if (getprop("/local-weather/cloud-shadows/cloud-shadow-fov-flag")==1) @@ -1471,11 +1476,24 @@ foreach(s; cloudShadowArray) if (viewDotPos <0.7) {dist = dist -(viewDotPos - 0.7) * 10000.0;} } if (dist > dist_max) {dist_max = dist; index_max = counter;} + if (dist < dist_min) {dist_min = dist; index_min = counter;} setprop("/local-weather/cloud-shadows/cloudpos-x["~counter~"]",int(diffx) + s.size); setprop("/local-weather/cloud-shadows/cloudpos-y["~counter~"]",int(diffy) + s.strength ); counter = counter+1; } + + # now write out the closest cloud for the detail effects + + var s = cloudShadowArray[index_min]; + + diffx = (s.lat - eyeLat) * local_weather.lat_to_m + offset_x; + diffy = -(s.lon - eyeLon) * local_weather.lon_to_m + offset_y; + + setprop("/local-weather/cloud-shadows/nearest-cloudpos-x",int(diffx) + s.size); + setprop("/local-weather/cloud-shadows/nearest-cloudpos-y",int(diffy) + s.strength ); + + #print("Dist_max:", dist_max, " index_max: ", index_max); cloudShadowMinIndex = index_max; if (dist_max > 0.0) {cloudShadowMaxDist = dist_max;} diff --git a/Shaders/terrain-overlay-ALS.frag b/Shaders/terrain-overlay-ALS.frag new file mode 100644 index 000000000..1d6b0e50e --- /dev/null +++ b/Shaders/terrain-overlay-ALS.frag @@ -0,0 +1,173 @@ +// -*-C++-*- +#version 120 + +#define MAX_LAYERS 8 +#define MAX_DISTANCE 3000.0 + +uniform float visibility; +uniform float avisibility; +uniform float scattering; +uniform float overlay_bias; +uniform float season; +uniform float dust_cover_factor; +uniform float overlay_max_height; +uniform float overlay_hardness; +uniform float overlay_density; +uniform float overlay_scale; +uniform float overlay_steepness_factor; +uniform float overlay_brightness_bottom; +uniform float overlay_brightness_top; +uniform float overlay_secondary_hardness; +uniform float overlay_secondary_density; +uniform float snowlevel; +uniform float wetness; +uniform float snow_thickness_factor; + +uniform int overlay_autumn_flag; +uniform int overlay_secondary_flag; +uniform int cloud_shadow_flag; + +uniform sampler2D overlayPrimaryTex; +uniform sampler2D overlaySecondaryTex; + + +uniform float osg_SimulationTime; + +varying vec2 g_rawpos; // Horizontal position in model space +varying float g_distance_to_eye; // Distance to the camera. Layers were disregarded +varying vec3 g_normal; +varying in float g_altitude; + + +flat in float g_layer; // The layer where the fragment lives (0-1 range) + + +float rand2D(in vec2 co); +float Noise2D(in vec2 co, in float wavelength); +vec3 filter_combined (in vec3 color) ; + +float shadow_func_nearest (in float x, in float y, in float noise, in float dist); + + +void main() +{ + + if (g_distance_to_eye > MAX_DISTANCE) {discard;} + + vec2 texCoord = gl_TexCoord[0].st; + + vec2 pos_rotated = vec2 (0.707 * g_rawpos.x + 0.707 * g_rawpos.y, 0.707 * g_rawpos.x - 0.707 * g_rawpos.y); + + + //float noise_1m = 0.5 * Noise2D(pos_rotated.xy, 1.0 * overlay_scale); + //noise_1m += 0.5 * Noise2D(g_rawpos.xy, 1.1 * overlay_scale); + + float noise_1m = Noise2D(pos_rotated.xy, 1.0 * overlay_scale); + + float noise_2m = Noise2D(g_rawpos.xy, 2.0 * overlay_scale); ; + float noise_10m = Noise2D(g_rawpos.xy, 10.0 * overlay_scale); + + + + float value = 0.0; + + float d_fade =smoothstep(100.0, MAX_DISTANCE, g_distance_to_eye); + + float steepness = dot (normalize(g_normal), vec3 (0.0, 0.0, 1.0)); + float steepness_bias = smoothstep(overlay_steepness_factor, overlay_steepness_factor + 0.1, steepness); + + float overlayPattern = 0.2 * noise_10m + 0.3 * noise_2m + 0.5 * noise_1m - 0.2 * g_layer - 0.1 + 0.2 * overlay_density ; + overlayPattern *= steepness_bias; + + float secondaryPattern = 0.2 * (1.0-noise_10m) + 0.3 * (1.0-noise_2m) + 0.5 * (1.0-noise_1m) - 0.4 * g_layer - 0.2 + 0.2 * overlay_secondary_density ; + secondaryPattern *= overlay_secondary_flag; + float secondaryMix = 0.0; + + if (overlayPattern > 0.5) + { + value = smoothstep(0.5, (0.8 - 0.25 * overlay_hardness), overlayPattern); + } + else if (secondaryPattern > 0.5) + { + value = smoothstep(0.5, (0.8 - 0.25 * overlay_secondary_hardness), secondaryPattern); + secondaryMix = 1.0; + } + else {discard;} + + vec3 texel = texture2D(overlayPrimaryTex, texCoord * 20.0).rgb; + vec3 secondary_texel = texture2D(overlaySecondaryTex, texCoord * 20.0).rgb; + + // autumn coloring + + if (overlay_autumn_flag == 1) + { + texel.r = min(1.0, (1.0 + 2.5 * 0.1 * season) * texel.r); + texel.g = texel.g; + texel.b = max(0.0, (1.0 - 4.0 * 0.1 * season) * texel.b); + float 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))); + } + + texel = mix (texel, secondary_texel, secondaryMix); + + + float layer_arg = mix(g_layer, 1.0, smoothstep(250.0, 5000.0, g_distance_to_eye)); + texel.rgb *= (overlay_brightness_bottom + (overlay_brightness_top - overlay_brightness_bottom) * g_layer); + texel.rgb *= (1.0 - 0.5 * wetness); + + // dust overlay + + const vec3 dust_color = vec3 (0.76, 0.65, 0.45); + texel = mix (texel, dust_color, 0.7 * dust_cover_factor); + + // snow overlay + + //vec3 snow_texel = vec3 (0.95, 0.95, 0.95); + + float snow_factor = 0.2+0.8* smoothstep(0.2,0.8, 0.3 + 0.5 * snow_thickness_factor +0.0001*(g_altitude -snowlevel) ); + snow_factor *= smoothstep(0.5, 0.7, steepness); + + snow_factor *= smoothstep(g_layer - 0.1, g_layer , snow_factor + 0.2); + + //texel.rgb = mix(texel.rgb, snow_texel.rgb, snow_factor * smoothstep(snowlevel, snowlevel+200.0, g_altitude - 100.0)); + value *= (1.0 - snow_factor * smoothstep(snowlevel, snowlevel+200.0, g_altitude - 100.0)); + + // do a conservative simple fog model, fading to alpha + + float min_visibility = min(visibility, avisibility); + + float base_alpha = clamp(0.4 * overlay_max_height/0.3, 0.4, 1.0); + value*= base_alpha * (1.0 - d_fade); + + float targ = 8.0 * g_distance_to_eye/min_visibility; + value *= exp(-targ - targ * targ * targ * targ); + value= clamp(value, 0.0, 1.0); + + // cloud shadows + + float cloud_shade = 1.0; + + if (cloud_shadow_flag == 1) + { + vec2 eyePos = (gl_ModelViewMatrixInverse * vec4 (0.0, 0.0, 0.0, 1.0)).xy; + vec2 relPos = g_rawpos - eyePos; + + + cloud_shade = shadow_func_nearest(relPos.x, relPos.y, 1.0, g_distance_to_eye); + } + + // lighting is very simple, the ground underneath should do most of it + + vec3 N = normalize (gl_NormalMatrix * g_normal); + float NdotL = 0.5 + 1.0 * clamp(dot (N, gl_LightSource[0].position.xyz), 0.0, 1.0) * cloud_shade; + + texel *= length(gl_LightSource[0].diffuse.rgb)/1.73 * scattering * NdotL; + texel = clamp(texel, 0.0, 1.0); + + + vec4 fragColor = vec4 (texel, value); + fragColor.rgb = filter_combined(fragColor.rgb); + fragColor = clamp(fragColor, 0.0, 1.0); + + gl_FragColor = fragColor; +} diff --git a/Shaders/terrain-overlay-ALS.geom b/Shaders/terrain-overlay-ALS.geom new file mode 100644 index 000000000..c7eb5dde3 --- /dev/null +++ b/Shaders/terrain-overlay-ALS.geom @@ -0,0 +1,62 @@ +// -*-C++-*- +#version 120 +#extension GL_EXT_geometry_shader4 : enable + +#define MAX_LAYERS 8 +#define MIN_LAYERS 2 +#define MAX_MINUS_MIN_LAYERS 6 + +uniform float overlay_max_height; + +varying in vec3 v_normal[3]; + +varying out vec2 g_rawpos; +varying out float g_distance_to_eye; +varying out vec3 g_normal; +varying out float g_altitude; + + +flat varying out float g_layer; + + + +float min3(in float a, in float b, in float c) +{ + float m = a; + if (m > b) m = b; + if (m > c) m = c; + return m; +} + +void main() +{ + float distances[3]; + distances[0] = -(gl_ModelViewMatrix * gl_PositionIn[0]).z; + distances[1] = -(gl_ModelViewMatrix * gl_PositionIn[1]).z; + distances[2] = -(gl_ModelViewMatrix * gl_PositionIn[2]).z; + float minDistance = min3(distances[0], distances[1], distances[2]); + //float avgDistance = (distances[0]+distances[1]+distances[2])*0.33; + + int numLayers = MIN_LAYERS + int((1.0 - smoothstep(250.0, 5000.0, minDistance)) * float(MAX_MINUS_MIN_LAYERS)); + + float deltaLayer = 1.0 / float(numLayers); + float currDeltaLayer = 1.5 * deltaLayer;// * 0.5; + + for (int layer = 0; layer < numLayers; ++layer) { + for (int i = 0; i < 3; ++i) { + vec4 pos = gl_PositionIn[i] + vec4(v_normal[i] * currDeltaLayer * overlay_max_height, 0.0); + g_rawpos = gl_PositionIn[i].xy; + g_distance_to_eye = distances[i]; + g_layer = currDeltaLayer; + g_normal = v_normal[i]; + g_altitude = gl_PositionIn[i].z; + + gl_Position = gl_ModelViewProjectionMatrix * pos; + gl_TexCoord[0] = gl_TexCoordIn[i][0]; + EmitVertex(); + } + EndPrimitive(); + + currDeltaLayer += deltaLayer; + } +}