diff --git a/ci_shadervalidator/Globals.h b/ci_shadervalidator/Globals.h index e8f548dc3..943ff0adc 100644 --- a/ci_shadervalidator/Globals.h +++ b/ci_shadervalidator/Globals.h @@ -9,6 +9,7 @@ struct _global bool gfx_shadergamma; bool gfx_usegles; int iMultisampling; + float gfx_headlights_beamwidth; }; extern _global Global; diff --git a/ci_shadervalidator/main.cpp b/ci_shadervalidator/main.cpp index 65ad54b2a..d11a013bf 100644 --- a/ci_shadervalidator/main.cpp +++ b/ci_shadervalidator/main.cpp @@ -48,6 +48,7 @@ bool check_shader_allopts(std::string filename) Global.gfx_shadergamma = true; // the default of the simulator, which compiles the alpha to coverage branches of the shaders Global.iMultisampling = 2; + Global.gfx_headlights_beamwidth = 2.f; if (!check_shader(filename)) suc = false; @@ -59,6 +60,7 @@ bool check_shader_allopts(std::string filename) Global.gfx_extraeffects = false; Global.gfx_shadergamma = false; Global.iMultisampling = 0; + Global.gfx_headlights_beamwidth = 1.f; if (!check_shader(filename)) suc = false; diff --git a/gl/glsl_common.cpp b/gl/glsl_common.cpp index e621fdc6b..9c3cfcb1f 100644 --- a/gl/glsl_common.cpp +++ b/gl/glsl_common.cpp @@ -1,6 +1,23 @@ #include "stdafx.h" #include "glsl_common.h" +#include +#include + +namespace +{ +// a float the way glsl reads it, whatever the locale of the program +std::string glsl_float(float const Value) +{ + std::ostringstream text; + text.imbue(std::locale::classic()); + text.setf(std::ios::fixed); + text.precision(3); + text << Value; + return text.str(); +} +} // namespace + std::string gl::glsl_common; void gl::glsl_common_setup() @@ -12,6 +29,7 @@ void gl::glsl_common_setup() "#define POSTFX_ENABLED " + std::to_string((int)!Global.gfx_skippipeline) + "\n" + "#define EXTRAEFFECTS_ENABLED " + std::to_string((int)Global.gfx_extraeffects) + "\n" + "#define ALPHA_TO_COVERAGE_ENABLED " + std::to_string((int)(Global.iMultisampling > 0)) + "\n" + + "#define HEADLIGHT_BEAM_WIDTH " + glsl_float(Global.gfx_headlights_beamwidth) + "\n" + "#define MAX_LIGHTS " + std::to_string(MAX_LIGHTS) + "U\n" + "#define MAX_CASCADES " + std::to_string(MAX_CASCADES) + "U\n" + "#define MAX_PARAMS " + std::to_string(MAX_PARAMS) + "U\n" + diff --git a/shaders/light_common.glsl b/shaders/light_common.glsl index 6261a090d..ddc7c39d3 100644 --- a/shaders/light_common.glsl +++ b/shaders/light_common.glsl @@ -33,7 +33,7 @@ in vec4 UserData; // lit and shaded faces of the same surface). // --------------------------------------------------------------------- const float AMBIENT_SCALE = 0.65; -const float SUN_DIFFUSE_SCALE = 1.5; +const float SUN_DIFFUSE_SCALE = 2.5; // 26.05; at 1.5 the moon left the night black around the headlights const float SUN_NDOTL_SHARPNESS = 1.25; float length2(vec3 v) @@ -242,7 +242,13 @@ vec2 calc_headlights(light_s light, vec3 fragnormal) float distance = length(light.pos - f_pos.xyz); float atten = 1.0f / (1.0f + light.linear * distance + light.quadratic * (distance * distance)); atten *= mix(1.0, 0.0, clamp((coords.z - 0.998) * 500.0, 0.0, 1.0)); - vec3 lights = textureProj(headlightmap, headlightpos).rgb * light.headlight_weights.rgb; + // gfx.headlights.beamwidth spreads the beam sideways around its middle (0.5 of the projected x), so the + // ground beside the track gets the light of the ballast; the brightness along the track stays as it was + vec4 beampos = headlightpos; +#ifdef HEADLIGHT_BEAM_WIDTH + beampos.x = 0.5 * beampos.w + (beampos.x - 0.5 * beampos.w) / HEADLIGHT_BEAM_WIDTH; +#endif + vec3 lights = textureProj(headlightmap, beampos).rgb * light.headlight_weights.rgb; float lightintensity = max(max(lights.r, lights.g), lights.b); return part * atten * lightintensity; } @@ -279,7 +285,9 @@ vec3 EnvBRDFApprox(vec3 F0, float roughness, float NoV) // param[1].x its intensity (HSV value unless a material overrides it). vec3 material_diffuse() { - vec3 diffusecolor = pow(max(param[0].rgb, vec3(0.0)), vec3(2.2)); + // the colour as it is, as 26.05 scaled the light by it; linearised (^2.2) it took a quarter off the default + // 0.8 of every scenery shape and more off darker submodels, and the night went black + vec3 diffusecolor = max(param[0].rgb, vec3(0.0)); float value = max(param[0].r, max(param[0].g, param[0].b)); return (value > 0.0 ? diffusecolor * (param[1].x / value) : vec3(0.0)); } @@ -288,6 +296,8 @@ vec3 material_diffuse() // do magic here vec3 apply_lights(vec3 fragcolor, vec3 fragnormal, vec3 texturecolor, float reflectivity, float specularity, float shadowtone) { + // the sun highlight is tinted by the texture alone, as 26.05 drew it, not by the diffuse colour too + vec3 highlight_tint = texturecolor; // Diffuse colour is applied once, to the albedo; the light terms below are not scaled by it again texturecolor *= material_diffuse(); float weathered = (param[3].w > 0.0 ? clamp(param[3].w * UserData.x, 0.0, 1.0) : 0.0); @@ -313,6 +323,10 @@ vec3 apply_lights(vec3 fragcolor, vec3 fragnormal, vec3 texturecolor, float refl vec3 view_dir = normalize(-f_pos.xyz); float NdotV = max(dot(fragnormal, view_dir), 0.0); vec3 F0 = mix(vec3(0.04), texturecolor, metalic); + // A metal gives up its diffuse term only as far as its reflection mask lets the environment take its place. + // Assets made before the metal/rough combine mark metal in the specgloss but keep a low mask, and without + // this their metal parts went almost black; a metal with the full mask is drawn as before. + float metal_diffuse_loss = metalic * clamp(reflectivity, 0.0, 1.0); vec3 fresnel = F0 + (1.0 - F0) * pow(1.0 - NdotV, 5.0); const float MAX_REFLECTION_LOD = 8.0; @@ -333,9 +347,9 @@ vec3 apply_lights(vec3 fragcolor, vec3 fragnormal, vec3 texturecolor, float refl texturecolor = mix(texturecolor, texturecolorfullv, envyuv.r * reflectivity * env_spec_w); if (lights_count == 0U) - // Metals carry no diffuse term; env reflection is gated by the env BRDF + // Metals carry no diffuse term where they reflect; env reflection is gated by the env BRDF // (F0-tinted, roughness-attenuated) so matte surfaces barely reflect. - return fragcolor * texturecolor * (1.0 - metalic) + return fragcolor * texturecolor * (1.0 - metal_diffuse_loss) + emissioncolor * texturecolor + envcolor * env_spec * reflectivity; @@ -375,16 +389,19 @@ vec3 apply_lights(vec3 fragcolor, vec3 fragnormal, vec3 texturecolor, float refl vec3 env_reflection = envcolor * env_spec * reflectivity * (1.0 - shadow1 * 0.5); // --- Physically-based metal/rough combine (Substance 3D Painter parity) --- - // Dielectrics: keep the full diffuse albedo; the direct sun highlight stays - // light-coloured because dielectric F0 is achromatic (~0.04) and - // must NOT be tinted by the base colour. - // Metals: drop the diffuse term entirely and tint the direct highlight - // with the albedo (metal F0 == base colour). + // Dielectrics: keep the full diffuse albedo; the direct sun highlight is tinted + // by the albedo, as 26.05 drew it. The specular strength of the + // assets (param_specular, the specgloss R) was tuned with that + // multiply; an untinted highlight on dark glossy plastic of an old + // asset turned into white glints along edges and speckles on cab + // desks. + // Metals: drop the diffuse term where the reflection mask is full (see + // metal_diffuse_loss) and tint the direct highlight with the + // albedo (metal F0 == base colour). // The highlight *strength* (specularamount) is deliberately left untouched so - // existing material tuning is preserved — only the colour/energy split that - // was previously inverted gets corrected. - vec3 diffuse_albedo = texturecolor * (1.0 - metalic); - vec3 spec_tint = mix(vec3(1.0), texturecolor, metalic); + // existing material tuning is preserved. + vec3 diffuse_albedo = texturecolor * (1.0 - metal_diffuse_loss); + vec3 spec_tint = highlight_tint; vec3 result = fragcolor * diffuse_albedo // sun + ambient + headlight diffuse + specularcolor * spec_tint // direct sun highlight diff --git a/shaders/mat_colored.frag b/shaders/mat_colored.frag index 46de50f09..59ee3e8f8 100644 --- a/shaders/mat_colored.frag +++ b/shaders/mat_colored.frag @@ -24,8 +24,10 @@ layout(location = 1) out vec4 out_motion; void main() { - // no texture: the diffuse colour alone makes the albedo, apply_lights() applies it to the white base - vec4 tex_color = vec4(1.0, 1.0, 1.0, param[0].a); + // no texture: the diffuse colour, linearised, stands in for the texture, and apply_lights() scales it by the + // colour once more, as 26.05 did (c^2.2 * c); on a white base a dark grey of an untextured car came out + // tens of times lighter than in 26.05: tyres, bumpers and glass grey instead of near black + vec4 tex_color = vec4(pow(max(param[0].rgb, vec3(0.0)), vec3(2.2)), param[0].a); // if (tex_color.a < opacity) // discard; @@ -33,7 +35,7 @@ void main() vec3 fragcolor = ambient; vec3 fragnormal = normalize(f_normal); float reflectivity = param[1].z; - float specularity = dot(pow(param[0].rgb, vec3(2.2)), vec3(0.5)); + float specularity = dot(tex_color.rgb, vec3(0.5)); glossiness = abs(param[1].w); fragcolor = apply_lights(fragcolor, fragnormal, tex_color.rgb, reflectivity, specularity, shadow_tone); diff --git a/shaders/mat_sunlessnormalmap_specgloss.frag b/shaders/mat_sunlessnormalmap_specgloss.frag index 4e2abe1d9..eb1f9b916 100644 --- a/shaders/mat_sunlessnormalmap_specgloss.frag +++ b/shaders/mat_sunlessnormalmap_specgloss.frag @@ -37,6 +37,7 @@ uniform sampler2D specgloss; vec3 apply_lights_sunless(vec3 fragcolor, vec3 fragnormal, vec3 texturecolor, float reflectivity, float specularity, float shadowtone) { // diffuse colour applied once, to the albedo (see material_diffuse() in light_common.glsl) + vec3 highlight_tint = texturecolor; texturecolor *= material_diffuse(); // Cab interior: dim ambient less than the exterior path - ambient // is the dominant indoor illumination, but it was still too bright. @@ -47,6 +48,9 @@ vec3 apply_lights_sunless(vec3 fragcolor, vec3 fragnormal, vec3 texturecolor, fl vec3 view_dir = normalize(-f_pos.xyz); float NdotV = max(dot(fragnormal, view_dir), 0.0); vec3 F0 = mix(vec3(0.04), texturecolor, metalic); + // A metal gives up its diffuse term only as far as its reflection mask lets the environment take its place + // (the same as apply_lights in light_common.glsl). + float metal_diffuse_loss = metalic * clamp(reflectivity, 0.0, 1.0); vec3 fresnel = F0 + (1.0 - F0) * pow(1.0 - NdotV, 5.0); const float MAX_REFLECTION_LOD = 8.0; @@ -64,7 +68,7 @@ vec3 apply_lights_sunless(vec3 fragcolor, vec3 fragnormal, vec3 texturecolor, fl texturecolor = mix(texturecolor, texturecolorfullv, envyuv.r * reflectivity * env_spec_w); if (lights_count == 0U) - return fragcolor * texturecolor * (1.0 - metalic) + return fragcolor * texturecolor * (1.0 - metal_diffuse_loss) + emissioncolor * texturecolor + envcolor * env_spec * reflectivity; @@ -95,11 +99,11 @@ vec3 apply_lights_sunless(vec3 fragcolor, vec3 fragnormal, vec3 texturecolor, fl // Env reflection gated by env BRDF (roughness-attenuated, F0-tinted). vec3 env_reflection = envcolor * env_spec * reflectivity; - // Physically-based metal/rough combine (matches apply_lights / Substance): - // dielectric -> light-coloured highlight + full diffuse; metal -> albedo-tinted - // highlight, no diffuse term. Highlight strength left unchanged. - vec3 diffuse_albedo = texturecolor * (1.0 - metalic); - vec3 spec_tint = mix(vec3(1.0), texturecolor, metalic); + // Metal/rough combine, the same as apply_lights: the direct highlight is tinted by + // the albedo (as 26.05 drew it), a metal drops its diffuse term only where its + // reflection mask is full. Highlight strength left unchanged. + vec3 diffuse_albedo = texturecolor * (1.0 - metal_diffuse_loss); + vec3 spec_tint = highlight_tint; vec3 result = fragcolor * diffuse_albedo + specularcolor * spec_tint diff --git a/shaders/tonemapping.glsl b/shaders/tonemapping.glsl index 863fe0fe1..701f9d5fd 100644 --- a/shaders/tonemapping.glsl +++ b/shaders/tonemapping.glsl @@ -139,14 +139,15 @@ vec3 ApplyAgX(vec3 linearColorRec709) vec4 tonemap(vec4 x) { - // Use AgX by default. Reinhard and ACES above are kept for reference. + // ACES, as 26.05 drew it. AgX and Reinhard above are kept for reference: AgX with its look crushed the dark + // end, so the night and everything in shadow went black (a night scene lost half its brightness). // Last-line-of-defense sanitize so NaN/Inf/negative HDR values do not // escape into log2()/pow() and produce black flashes or invalid output. vec3 hdr = x.rgb; hdr = mix(hdr, vec3(0.0), vec3(any(isnan(hdr)) || any(isinf(hdr)))); hdr = max(hdr, vec3(0.0)); - return FBOUT(vec4(ApplyAgX(hdr), x.a)); - //return FBOUT(vec4(ACESFilm(hdr), x.a)); + return FBOUT(vec4(ACESFilm(hdr), x.a)); + //return FBOUT(vec4(ApplyAgX(hdr), x.a)); //return FBOUT(vec4(reinhard(x.rgb), x.a)); } diff --git a/utilities/Globals.cpp b/utilities/Globals.cpp index abfa6d5e7..d5974f934 100644 --- a/utilities/Globals.cpp +++ b/utilities/Globals.cpp @@ -1708,6 +1708,12 @@ global_settings::ConfigParse_gfx( cParser &Parser, std::string_view const Token Parser >> gfx_shadow_rank_cutoff; gfx_shadow_rank_cutoff = std::clamp(gfx_shadow_rank_cutoff, 1, 3); } + else if (Token == "gfx.headlights.beamwidth") + { + Parser.getTokens(1); + Parser >> gfx_headlights_beamwidth; + gfx_headlights_beamwidth = std::clamp(gfx_headlights_beamwidth, 1.f, 4.f); + } else { tokenparsed = false; @@ -1978,6 +1984,7 @@ global_settings::export_as_text( std::ostream &Output ) const { export_as_text( Output, "gfx.shadergamma", gfx_shadergamma ); export_as_text( Output, "gfx.shadow.angle.min", gfx_shadow_angle_min ); export_as_text( Output, "gfx.shadow.rank.cutoff", gfx_shadow_rank_cutoff ); + export_as_text( Output, "gfx.headlights.beamwidth", gfx_headlights_beamwidth ); export_as_text( Output, "python.enabled", python_enabled ); export_as_text( Output, "python.threadedupload", python_threadedupload ); export_as_text( Output, "python.uploadmain", python_uploadmain ); diff --git a/utilities/Globals.h b/utilities/Globals.h index cdbab2c74..a754819da 100644 --- a/utilities/Globals.h +++ b/utilities/Globals.h @@ -352,6 +352,9 @@ struct global_settings { float gfx_distance_factor_max { 3.f }; float gfx_shadow_angle_min { -0.2f }; int gfx_shadow_rank_cutoff { 3 }; + // how many times wider sideways the headlight beam is spread than its texture: at 1.7 the ground beside the track gets + // most of the light of the ballast; 1 draws the narrow beam of 26.05 + float gfx_headlights_beamwidth { 1.7f }; float ui_fontsize = 13.0f; float ui_scale = 1.0f;