18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 21:45:16 +01:00

fix(render): old assets drawn as 26.05 drew them; the headlight beam spread sideways (#481)

* fix(render): old assets drawn as 26.05 drew them: metal, sun highlight, tonemapping, sun scale, diffuse colour

The shader changes since 26.05.2 were made for PBR-authored assets; the assets in the game
data were tuned for 26.05, and none of them is authored for the new combine.

- Metal keeps its diffuse term as far as its reflection mask is low (metal_diffuse_loss =
  metalic * reflectivity). Assets marking metal in the specgloss with a low mask went
  almost black (the gold EN63A-031); a full mask is drawn as before.
- The direct sun highlight is tinted by the albedo again, for dielectrics too. Old assets
  tuned param_specular / specgloss R with that multiply; untinted, dark glossy plastic broke
  into white glints along edges (Impuls fronts) and speckles on cab desks (SA132).
- ACES instead of AgX: AgX with its look crushed the dark end, a night scene lost half its
  brightness and the ground beside the track went black.
- SUN_DIFFUSE_SCALE 2.5 again (1.5 dimmed the moon at night as well).
- The Diffuse colour of submodels and shapes scales the light as it is, as in 26.05, not
  raised to 2.2: that took a quarter off the default 0.8 of every scenery shape and more off
  the ~64 000 textured submodels with a darker Diffuse.

Checked on screenshots of 10 vehicles by day (Ultra preset) and from cabs at night against
the 26.05.2 shaders on the same executable. No game data changes.

* feat(render): the headlight beam spread sideways, gfx.headlights.beamwidth (default 1.7)

The projected beam texture lit the track and the ballast, and the ground beside them only
with its dark edge (24-45 against 116 on the ballast close ahead, nearly 0 further on), so at
night only the ballast showed. The beam is spread sideways around its middle by
gfx.headlights.beamwidth: at 1.7 the ground gets most of the light of the ballast, the
brightness along the track stays as it was. 1 draws the narrow beam of 26.05; range 1-4.

The value reaches the shaders as HEADLIGHT_BEAM_WIDTH, written with the classic locale so
that a decimal comma can't break the glsl; the shader validator compiles them with and
without the spread. The key is written to the log with the other settings.

* fix(render): the sunless specgloss shader combines metal as apply_lights does

* fix(render): an untextured submodel takes its linearised colour as the texture again, as 26.05 did

mat_colored.frag gave apply_lights() a white base, so the albedo of a Map: none submodel was its diffuse colour c,
where 26.05.2 drew c^2.2 * c. A dark grey came out tens of times lighter: on the road vehicles of pctga the tyres
(25-40 grey) 60-165x, the glass 23x, whole untextured cars (a4, golf, passat, ka, arosa) 1.5-3x. With the
linearised colour as the texture a grey matches 26.05 in the ambient and the sun; a saturated colour is up to
a third darker in the sun, as 26.05 scaled the sun by the value of the colour, not by the colour.

* fix(render): the sun highlight is tinted by the texture alone, as 26.05 drew it
This commit is contained in:
maj00r
2026-11-24 19:44:46 +01:00
committed by GitHub
parent 827db54430
commit 571487612b
9 changed files with 81 additions and 26 deletions

View File

@@ -9,6 +9,7 @@ struct _global
bool gfx_shadergamma; bool gfx_shadergamma;
bool gfx_usegles; bool gfx_usegles;
int iMultisampling; int iMultisampling;
float gfx_headlights_beamwidth;
}; };
extern _global Global; extern _global Global;

View File

@@ -48,6 +48,7 @@ bool check_shader_allopts(std::string filename)
Global.gfx_shadergamma = true; Global.gfx_shadergamma = true;
// the default of the simulator, which compiles the alpha to coverage branches of the shaders // the default of the simulator, which compiles the alpha to coverage branches of the shaders
Global.iMultisampling = 2; Global.iMultisampling = 2;
Global.gfx_headlights_beamwidth = 2.f;
if (!check_shader(filename)) if (!check_shader(filename))
suc = false; suc = false;
@@ -59,6 +60,7 @@ bool check_shader_allopts(std::string filename)
Global.gfx_extraeffects = false; Global.gfx_extraeffects = false;
Global.gfx_shadergamma = false; Global.gfx_shadergamma = false;
Global.iMultisampling = 0; Global.iMultisampling = 0;
Global.gfx_headlights_beamwidth = 1.f;
if (!check_shader(filename)) if (!check_shader(filename))
suc = false; suc = false;

View File

@@ -1,6 +1,23 @@
#include "stdafx.h" #include "stdafx.h"
#include "glsl_common.h" #include "glsl_common.h"
#include <locale>
#include <sstream>
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; std::string gl::glsl_common;
void gl::glsl_common_setup() 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 POSTFX_ENABLED " + std::to_string((int)!Global.gfx_skippipeline) + "\n" +
"#define EXTRAEFFECTS_ENABLED " + std::to_string((int)Global.gfx_extraeffects) + "\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 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_LIGHTS " + std::to_string(MAX_LIGHTS) + "U\n" +
"#define MAX_CASCADES " + std::to_string(MAX_CASCADES) + "U\n" + "#define MAX_CASCADES " + std::to_string(MAX_CASCADES) + "U\n" +
"#define MAX_PARAMS " + std::to_string(MAX_PARAMS) + "U\n" + "#define MAX_PARAMS " + std::to_string(MAX_PARAMS) + "U\n" +

View File

@@ -33,7 +33,7 @@ in vec4 UserData;
// lit and shaded faces of the same surface). // lit and shaded faces of the same surface).
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
const float AMBIENT_SCALE = 0.65; 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; const float SUN_NDOTL_SHARPNESS = 1.25;
float length2(vec3 v) 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 distance = length(light.pos - f_pos.xyz);
float atten = 1.0f / (1.0f + light.linear * distance + light.quadratic * (distance * distance)); 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)); 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); float lightintensity = max(max(lights.r, lights.g), lights.b);
return part * atten * lightintensity; 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). // param[1].x its intensity (HSV value unless a material overrides it).
vec3 material_diffuse() 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)); 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)); return (value > 0.0 ? diffusecolor * (param[1].x / value) : vec3(0.0));
} }
@@ -288,6 +296,8 @@ vec3 material_diffuse()
// do magic here // do magic here
vec3 apply_lights(vec3 fragcolor, vec3 fragnormal, vec3 texturecolor, float reflectivity, float specularity, float shadowtone) 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 // Diffuse colour is applied once, to the albedo; the light terms below are not scaled by it again
texturecolor *= material_diffuse(); texturecolor *= material_diffuse();
float weathered = (param[3].w > 0.0 ? clamp(param[3].w * UserData.x, 0.0, 1.0) : 0.0); 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); vec3 view_dir = normalize(-f_pos.xyz);
float NdotV = max(dot(fragnormal, view_dir), 0.0); float NdotV = max(dot(fragnormal, view_dir), 0.0);
vec3 F0 = mix(vec3(0.04), texturecolor, metalic); 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); vec3 fresnel = F0 + (1.0 - F0) * pow(1.0 - NdotV, 5.0);
const float MAX_REFLECTION_LOD = 8.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); texturecolor = mix(texturecolor, texturecolorfullv, envyuv.r * reflectivity * env_spec_w);
if (lights_count == 0U) 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. // (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 + emissioncolor * texturecolor
+ envcolor * env_spec * reflectivity; + 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); vec3 env_reflection = envcolor * env_spec * reflectivity * (1.0 - shadow1 * 0.5);
// --- Physically-based metal/rough combine (Substance 3D Painter parity) --- // --- Physically-based metal/rough combine (Substance 3D Painter parity) ---
// Dielectrics: keep the full diffuse albedo; the direct sun highlight stays // Dielectrics: keep the full diffuse albedo; the direct sun highlight is tinted
// light-coloured because dielectric F0 is achromatic (~0.04) and // by the albedo, as 26.05 drew it. The specular strength of the
// must NOT be tinted by the base colour. // assets (param_specular, the specgloss R) was tuned with that
// Metals: drop the diffuse term entirely and tint the direct highlight // multiply; an untinted highlight on dark glossy plastic of an old
// with the albedo (metal F0 == base colour). // 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 // The highlight *strength* (specularamount) is deliberately left untouched so
// existing material tuning is preserved — only the colour/energy split that // existing material tuning is preserved.
// was previously inverted gets corrected. vec3 diffuse_albedo = texturecolor * (1.0 - metal_diffuse_loss);
vec3 diffuse_albedo = texturecolor * (1.0 - metalic); vec3 spec_tint = highlight_tint;
vec3 spec_tint = mix(vec3(1.0), texturecolor, metalic);
vec3 result = fragcolor * diffuse_albedo // sun + ambient + headlight diffuse vec3 result = fragcolor * diffuse_albedo // sun + ambient + headlight diffuse
+ specularcolor * spec_tint // direct sun highlight + specularcolor * spec_tint // direct sun highlight

View File

@@ -24,8 +24,10 @@ layout(location = 1) out vec4 out_motion;
void main() void main()
{ {
// no texture: the diffuse colour alone makes the albedo, apply_lights() applies it to the white base // no texture: the diffuse colour, linearised, stands in for the texture, and apply_lights() scales it by the
vec4 tex_color = vec4(1.0, 1.0, 1.0, param[0].a); // 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) // if (tex_color.a < opacity)
// discard; // discard;
@@ -33,7 +35,7 @@ void main()
vec3 fragcolor = ambient; vec3 fragcolor = ambient;
vec3 fragnormal = normalize(f_normal); vec3 fragnormal = normalize(f_normal);
float reflectivity = param[1].z; 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); glossiness = abs(param[1].w);
fragcolor = apply_lights(fragcolor, fragnormal, tex_color.rgb, reflectivity, specularity, shadow_tone); fragcolor = apply_lights(fragcolor, fragnormal, tex_color.rgb, reflectivity, specularity, shadow_tone);

View File

@@ -37,6 +37,7 @@ uniform sampler2D specgloss;
vec3 apply_lights_sunless(vec3 fragcolor, vec3 fragnormal, vec3 texturecolor, float reflectivity, float specularity, float shadowtone) 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) // diffuse colour applied once, to the albedo (see material_diffuse() in light_common.glsl)
vec3 highlight_tint = texturecolor;
texturecolor *= material_diffuse(); texturecolor *= material_diffuse();
// Cab interior: dim ambient less than the exterior path - ambient // Cab interior: dim ambient less than the exterior path - ambient
// is the dominant indoor illumination, but it was still too bright. // 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); vec3 view_dir = normalize(-f_pos.xyz);
float NdotV = max(dot(fragnormal, view_dir), 0.0); float NdotV = max(dot(fragnormal, view_dir), 0.0);
vec3 F0 = mix(vec3(0.04), texturecolor, metalic); 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); vec3 fresnel = F0 + (1.0 - F0) * pow(1.0 - NdotV, 5.0);
const float MAX_REFLECTION_LOD = 8.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); texturecolor = mix(texturecolor, texturecolorfullv, envyuv.r * reflectivity * env_spec_w);
if (lights_count == 0U) if (lights_count == 0U)
return fragcolor * texturecolor * (1.0 - metalic) return fragcolor * texturecolor * (1.0 - metal_diffuse_loss)
+ emissioncolor * texturecolor + emissioncolor * texturecolor
+ envcolor * env_spec * reflectivity; + 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). // Env reflection gated by env BRDF (roughness-attenuated, F0-tinted).
vec3 env_reflection = envcolor * env_spec * reflectivity; vec3 env_reflection = envcolor * env_spec * reflectivity;
// Physically-based metal/rough combine (matches apply_lights / Substance): // Metal/rough combine, the same as apply_lights: the direct highlight is tinted by
// dielectric -> light-coloured highlight + full diffuse; metal -> albedo-tinted // the albedo (as 26.05 drew it), a metal drops its diffuse term only where its
// highlight, no diffuse term. Highlight strength left unchanged. // reflection mask is full. Highlight strength left unchanged.
vec3 diffuse_albedo = texturecolor * (1.0 - metalic); vec3 diffuse_albedo = texturecolor * (1.0 - metal_diffuse_loss);
vec3 spec_tint = mix(vec3(1.0), texturecolor, metalic); vec3 spec_tint = highlight_tint;
vec3 result = fragcolor * diffuse_albedo vec3 result = fragcolor * diffuse_albedo
+ specularcolor * spec_tint + specularcolor * spec_tint

View File

@@ -139,14 +139,15 @@ vec3 ApplyAgX(vec3 linearColorRec709)
vec4 tonemap(vec4 x) 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 // Last-line-of-defense sanitize so NaN/Inf/negative HDR values do not
// escape into log2()/pow() and produce black flashes or invalid output. // escape into log2()/pow() and produce black flashes or invalid output.
vec3 hdr = x.rgb; vec3 hdr = x.rgb;
hdr = mix(hdr, vec3(0.0), vec3(any(isnan(hdr)) || any(isinf(hdr)))); hdr = mix(hdr, vec3(0.0), vec3(any(isnan(hdr)) || any(isinf(hdr))));
hdr = max(hdr, vec3(0.0)); 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)); //return FBOUT(vec4(reinhard(x.rgb), x.a));
} }

View File

@@ -1708,6 +1708,12 @@ global_settings::ConfigParse_gfx( cParser &Parser, std::string_view const Token
Parser >> gfx_shadow_rank_cutoff; Parser >> gfx_shadow_rank_cutoff;
gfx_shadow_rank_cutoff = std::clamp(gfx_shadow_rank_cutoff, 1, 3); 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 else
{ {
tokenparsed = false; 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.shadergamma", gfx_shadergamma );
export_as_text( Output, "gfx.shadow.angle.min", gfx_shadow_angle_min ); 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.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.enabled", python_enabled );
export_as_text( Output, "python.threadedupload", python_threadedupload ); export_as_text( Output, "python.threadedupload", python_threadedupload );
export_as_text( Output, "python.uploadmain", python_uploadmain ); export_as_text( Output, "python.uploadmain", python_uploadmain );

View File

@@ -352,6 +352,9 @@ struct global_settings {
float gfx_distance_factor_max { 3.f }; float gfx_distance_factor_max { 3.f };
float gfx_shadow_angle_min { -0.2f }; float gfx_shadow_angle_min { -0.2f };
int gfx_shadow_rank_cutoff { 3 }; 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_fontsize = 13.0f;
float ui_scale = 1.0f; float ui_scale = 1.0f;