17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-25 11:39:47 +01:00

refactor(track): the ballast fades into that of the tracks joined, as the rails do

This commit is contained in:
2026-10-21 23:36:03 +02:00
parent b8eb5c71a1
commit 925f5910bb
18 changed files with 32 additions and 166 deletions

View File

@@ -507,8 +507,8 @@ std::vector<track_property> make_properties()
}
{
// the ends of a plain track are p1 and p2, a switch has p4 as the end of its second path
char const *const ends[][3] = {{"blend1", "Texture blending at p1 (m)", "How far from the end p1 the rails and the ballast blend into those of the track joined there, where their textures differ.\nThe ballast comes in as stones mixed together, the rails fade into each other; 0: not blended"},
{"blend2", "Texture blending at p2 (m)", "How far from the end p2 the rails and the ballast blend into those of the track joined there, where their textures differ.\nThe ballast comes in as stones mixed together, the rails fade into each other; 0: not blended"},
char const *const ends[][3] = {{"blend1", "Texture blending at p1 (m)", "How far from the end p1 the rails and the ballast blend into those of the track joined there, where their textures differ, fading into each other; 0: not blended"},
{"blend2", "Texture blending at p2 (m)", "How far from the end p2 the rails and the ballast blend into those of the track joined there, where their textures differ, fading into each other; 0: not blended"},
{"blend3", "Texture blending at p4 (m)", "How far from the end p4 of the switch its trackbed blends into the ballast of the track joined there, where their textures differ; 0: not blended"}};
for (int end = 0; end < 3; ++end)
{

View File

@@ -10270,11 +10270,11 @@ msgstr "Mieszanie tekstur przy p2 (m)"
msgid "Texture blending at p4 (m)"
msgstr "Mieszanie tekstur przy p4 (m)"
msgid "How far from the end p1 the rails and the ballast blend into those of the track joined there, where their textures differ.\nThe ballast comes in as stones mixed together, the rails fade into each other; 0: not blended"
msgstr "Na jakiej długości od końca p1 szyny i podsypka przechodzą w te z toru tam dołączonego, gdy ich tekstury są różne.\nPodsypka miesza się kamieniami, szyny przenikają się płynnie; 0: bez mieszania"
msgid "How far from the end p1 the rails and the ballast blend into those of the track joined there, where their textures differ, fading into each other; 0: not blended"
msgstr "Na jakiej długości od końca p1 szyny i podsypka płynnie przechodzą w te z toru tam dołączonego, gdy ich tekstury są różne; 0: bez mieszania"
msgid "How far from the end p2 the rails and the ballast blend into those of the track joined there, where their textures differ.\nThe ballast comes in as stones mixed together, the rails fade into each other; 0: not blended"
msgstr "Na jakiej długości od końca p2 szyny i podsypka przechodzą w te z toru tam dołączonego, gdy ich tekstury są różne.\nPodsypka miesza się kamieniami, szyny przenikają się płynnie; 0: bez mieszania"
msgid "How far from the end p2 the rails and the ballast blend into those of the track joined there, where their textures differ, fading into each other; 0: not blended"
msgstr "Na jakiej długości od końca p2 szyny i podsypka płynnie przechodzą w te z toru tam dołączonego, gdy ich tekstury są różne; 0: bez mieszania"
msgid "How far from the end p4 of the switch its trackbed blends into the ballast of the track joined there, where their textures differ; 0: not blended"
msgstr "Na jakiej długości od końca p4 rozjazdu jego podsypka przechodzi w podsypkę toru tam dołączonego, gdy ich tekstury są różne; 0: bez mieszania"

View File

@@ -766,13 +766,13 @@ int constexpr blend_specgloss { 4 }; // added to the lighting for a material wit
int constexpr blend_parallax { 8 }; // added to the lighting (1, normalmap) of a parallax material
std::string
blend_shader( int const Lighting, bool const Grain ) {
blend_shader( int const Lighting ) {
std::string name { "trackblend" };
if( Lighting & blend_specgloss ) { name += "_specgloss"; }
else if( Lighting & blend_parallax ) { name += "_parallax"; }
else if( Lighting > 0 ) { name += "_normalmap"; }
return ( Grain ? name + "_grain" : name );
return name;
}
} // namespace
@@ -820,7 +820,7 @@ material_manager::blend_lighting( material_handle const Material ) {
}
if( lighting >= 0 ) {
// the parameters of the material go to the blending one as they are, so they have to be in the same places in both
auto const blend { GfxRenderer->Fetch_Shader( blend_shader( lighting, false ) ) };
auto const blend { GfxRenderer->Fetch_Shader( blend_shader( lighting ) ) };
auto const fits { std::all_of(
std::begin( material.shader->param_conf ), std::end( material.shader->param_conf ),
[&]( auto const &Entry ) {
@@ -864,7 +864,6 @@ material_manager::build_blend( blend_key const &Key, opengl_material &Blend ) {
auto const material { std::get<0>( Key ) };
std::array<material_handle, 3> const others { std::get<1>( Key ), std::get<2>( Key ), std::get<3>( Key ) };
auto const grain { std::get<4>( Key ) };
auto const lighting { blend_lighting( material ) };
if( lighting < 0 ) {
return false;
@@ -885,8 +884,8 @@ material_manager::build_blend( blend_key const &Key, opengl_material &Blend ) {
int const normalslots[] = { ( specgloss ? 3 : 4 ), ( specgloss ? 4 : 5 ), ( specgloss ? 5 : 6 ), 7 };
auto const ends { specgloss ? 2 : 3 };
try {
Blend = variant_of( m_materials[ material ], grain ? ":blendgrain" : ":blend" );
Blend.shader = GfxRenderer->Fetch_Shader( blend_shader( lighting, grain ) );
Blend = variant_of( m_materials[ material ], ":blend" );
Blend.shader = GfxRenderer->Fetch_Shader( blend_shader( lighting ) );
Blend.texture_variants.clear();
Blend.textures.fill( null_handle );
auto const owndiffuse { texture_of( material, "diffuse" ) };
@@ -922,12 +921,12 @@ material_manager::build_blend( blend_key const &Key, opengl_material &Blend ) {
}
material_handle
material_manager::create_blend( material_handle const Material, std::array<material_handle, 3> const &Others, bool const Grain ) {
material_manager::create_blend( material_handle const Material, std::array<material_handle, 3> const &Others ) {
if( false == blendable( Material ) ) {
return Material;
}
blend_key const key { Material, Others[ 0 ], Others[ 1 ], Others[ 2 ], Grain };
blend_key const key { Material, Others[ 0 ], Others[ 1 ], Others[ 2 ] };
if( auto const lookup { m_blends.find( key ) }; lookup != m_blends.end() ) {
return lookup->second;
}

View File

@@ -148,10 +148,9 @@ public:
material_handle
create_tinted( material_handle const Material, glm::vec3 const &Colour );
// the material of a track blending its texture with those of up to three others (null: none) by the weights in y, z and w of
// the user data of the vertices (mat_trackblend), as grains with Grain. returns: handle to it, made once, or the material
// itself where it can't be blended
// the user data of the vertices (mat_trackblend). returns: handle to it, made once, or the material itself where it can't be blended
material_handle
create_blend( material_handle const Material, std::array<material_handle, 3> const &Others, bool const Grain );
create_blend( material_handle const Material, std::array<material_handle, 3> const &Others );
// whether the material can be blended so: a diffuse texture lit as mat_default, or with a normal map (and a specgloss map)
// lit as normalmap, sunlessnormalmap or shadowlessnormalmap (and their _specgloss), or parallax; not tinted. the blending materials take the
// textures of another season or weather with the materials they blend
@@ -172,8 +171,8 @@ private:
// types
typedef std::vector<opengl_material> material_sequence;
typedef std::unordered_map<std::string, std::size_t> index_map;
// a blending material: the material of the track, those it blends into at the ends, whether in grains
using blend_key = std::tuple<material_handle, material_handle, material_handle, material_handle, bool>;
// a blending material: the material of the track, those it blends into at the ends
using blend_key = std::tuple<material_handle, material_handle, material_handle, material_handle>;
// methods:
// checks whether specified texture is in the texture bank. returns texture id, or npos.
material_handle

View File

@@ -2850,9 +2850,9 @@ material_handle opengl46_renderer::Tinted_Material(material_handle const Materia
return m_materials.create_tinted(Material, Colour);
}
material_handle opengl46_renderer::Blend_Material(material_handle const Material, std::array<material_handle, 3> const &Others, bool const Grain)
material_handle opengl46_renderer::Blend_Material(material_handle const Material, std::array<material_handle, 3> const &Others)
{
return m_materials.create_blend(Material, Others, Grain);
return m_materials.create_blend(Material, Others);
}
bool opengl46_renderer::Blendable_Material(material_handle const Material)
@@ -4405,7 +4405,7 @@ int opengl46_renderer::Prepass_Class( material_handle const Material )
"detail_normalmap", "detail_normalmap_specgloss", "normalmap", "normalmap_specgloss",
"reflmap", "reflmap_specgloss", "shadowlessnormalmap", "shadowlessnormalmap_specgloss",
"sunlessnormalmap", "sunlessnormalmap_specgloss",
"trackblend", "trackblend_grain", "trackblend_normalmap", "trackblend_normalmap_grain", "trackblend_specgloss", "trackblend_specgloss_grain" };
"trackblend", "trackblend_normalmap", "trackblend_specgloss" };
static std::unordered_set<std::string> const solid { "colored", "default_0", "terrain", "water", "water_specgloss", "invalid" };
auto prepassclass { 0 };
for( auto const &shader : m_shaders ) {

View File

@@ -102,7 +102,7 @@ class opengl46_renderer : public gfx_renderer {
material_handle
Tinted_Material( material_handle const Material, glm::vec3 const &Colour ) override;
material_handle
Blend_Material( material_handle const Material, std::array<material_handle, 3> const &Others, bool const Grain ) override;
Blend_Material( material_handle const Material, std::array<material_handle, 3> const &Others ) override;
bool
Blendable_Material( material_handle const Material ) override;
void

View File

@@ -112,8 +112,8 @@ public:
virtual auto Matte_Material( material_handle const Material ) -> material_handle { return Material; }
virtual auto Tinted_Material( material_handle const Material, glm::vec3 const &Colour ) -> material_handle { return Material; }
// the material of a track blending its texture with those of up to three others (null: none) by the weights in y, z and w of the user data
// of the vertices, the others as grains with Grain (world/TrackBlend.h). backends which can't make one give back the material itself
virtual auto Blend_Material( material_handle const Material, std::array<material_handle, 3> const &Others, bool const Grain ) -> material_handle { return Material; }
// of the vertices (world/TrackBlend.h). backends which can't make one give back the material itself
virtual auto Blend_Material( material_handle const Material, std::array<material_handle, 3> const &Others ) -> material_handle { return Material; }
// whether a material can be blended so
virtual auto Blendable_Material( material_handle const Material ) -> bool { return false; }
// heightmap terrain: material of a chunk blending the materials of up to 8 layers by their weights.

View File

@@ -1,4 +1,4 @@
// the rails of a track faded into those of the tracks joined at its ends, see trackblend.glsl
// the rails and the ballast of a track faded into those of the tracks joined at its ends, see trackblend.glsl
#texture (diffuse, 0, sRGB_A)
uniform sampler2D diffuse;
#texture (diffuse1, 1, sRGB_A)

View File

@@ -1,11 +0,0 @@
// the ballast of a track mixed in grains into that of the tracks joined at its ends, see trackblend.glsl
#define TRACKBLEND_GRAIN 1
#texture (diffuse, 0, sRGB_A)
uniform sampler2D diffuse;
#texture (diffuse1, 1, sRGB_A)
uniform sampler2D diffuse1;
#texture (diffuse2, 2, sRGB_A)
uniform sampler2D diffuse2;
#texture (diffuse3, 3, sRGB_A)
uniform sampler2D diffuse3;
#include <trackblend.glsl>

View File

@@ -1,4 +1,4 @@
// the rails of a track of a normal mapped material faded into those of the tracks joined at its ends, see trackblend.glsl
// the rails and the ballast of a track of a normal mapped material faded into those of the tracks joined at its ends, see trackblend.glsl
#define TRACKBLEND_NORMALMAP 1
#texture (diffuse, 0, sRGB_A)
uniform sampler2D diffuse;

View File

@@ -1,20 +0,0 @@
// the ballast of a track of a normal mapped material mixed in grains into that of the tracks joined at its ends, see trackblend.glsl
#define TRACKBLEND_GRAIN 1
#define TRACKBLEND_NORMALMAP 1
#texture (diffuse, 0, sRGB_A)
uniform sampler2D diffuse;
#texture (diffuse1, 1, sRGB_A)
uniform sampler2D diffuse1;
#texture (diffuse2, 2, sRGB_A)
uniform sampler2D diffuse2;
#texture (diffuse3, 3, sRGB_A)
uniform sampler2D diffuse3;
#texture (normalmap, 4, RGBA)
uniform sampler2D normalmap;
#texture (normalmap1, 5, RGBA)
uniform sampler2D normalmap1;
#texture (normalmap2, 6, RGBA)
uniform sampler2D normalmap2;
#texture (normalmap3, 7, RGBA)
uniform sampler2D normalmap3;
#include <trackblend.glsl>

View File

@@ -1,4 +1,4 @@
// the rails of a track of a parallax material faded into those of the tracks joined at its ends, see trackblend.glsl
// the rails and the ballast of a track of a parallax material faded into those of the tracks joined at its ends, see trackblend.glsl
#define TRACKBLEND_NORMALMAP 1
#define TRACKBLEND_PARALLAX 1
#param (height_scale, 2, 1, 1, zero)

View File

@@ -1,23 +0,0 @@
// the ballast of a track of a parallax material mixed in grains into that of the tracks joined at its ends, see trackblend.glsl
#define TRACKBLEND_GRAIN 1
#define TRACKBLEND_NORMALMAP 1
#define TRACKBLEND_PARALLAX 1
#param (height_scale, 2, 1, 1, zero)
#param (height_offset, 2, 2, 1, zero)
#texture (diffuse, 0, sRGB_A)
uniform sampler2D diffuse;
#texture (diffuse1, 1, sRGB_A)
uniform sampler2D diffuse1;
#texture (diffuse2, 2, sRGB_A)
uniform sampler2D diffuse2;
#texture (diffuse3, 3, sRGB_A)
uniform sampler2D diffuse3;
#texture (normalmap, 4, RGBA)
uniform sampler2D normalmap;
#texture (normalmap1, 5, RGBA)
uniform sampler2D normalmap1;
#texture (normalmap2, 6, RGBA)
uniform sampler2D normalmap2;
#texture (normalmap3, 7, RGBA)
uniform sampler2D normalmap3;
#include <trackblend.glsl>

View File

@@ -1,4 +1,4 @@
// the rails of a track of a material with normal and specgloss maps faded into those of the tracks joined at its ends, see trackblend.glsl
// the rails and the ballast of a track of a material with normal and specgloss maps faded into those of the tracks joined at its ends, see trackblend.glsl
#define TRACKBLEND_NORMALMAP 1
#define TRACKBLEND_SPECGLOSS 1
#texture (diffuse, 0, sRGB_A)

View File

@@ -1,19 +0,0 @@
// the ballast of a track of a material with normal and specgloss maps mixed in grains into that of the tracks joined at its ends, see trackblend.glsl
#define TRACKBLEND_GRAIN 1
#define TRACKBLEND_NORMALMAP 1
#define TRACKBLEND_SPECGLOSS 1
#texture (diffuse, 0, sRGB_A)
uniform sampler2D diffuse;
#texture (diffuse1, 1, sRGB_A)
uniform sampler2D diffuse1;
#texture (diffuse2, 2, sRGB_A)
uniform sampler2D diffuse2;
#texture (normalmap, 3, RGBA)
uniform sampler2D normalmap;
#texture (normalmap1, 4, RGBA)
uniform sampler2D normalmap1;
#texture (normalmap2, 5, RGBA)
uniform sampler2D normalmap2;
#texture (specgloss, 6, RGBA)
uniform sampler2D specgloss;
#include <trackblend.glsl>

View File

@@ -1,7 +1,6 @@
// a track drawn with the textures of the tracks joined at its ends blended in near them (world/TrackBlend.h): the weights of the
// tracks joined at the ends p1, p2 and p4 of a switch in UserData.yzw, their textures in diffuse1..3, normalmap1..3. the
// including shader declares the textures and sets:
// TRACKBLEND_GRAIN the others come in as grains, as the stones of two ballasts would mix, rather than faded in
// tracks joined at the ends p1, p2 and p4 of a switch in UserData.yzw, their textures in diffuse1..3, normalmap1..3, faded into
// the texture of the track. the including shader declares the textures and sets:
// TRACKBLEND_NORMALMAP with the normal maps of the four, lit as the shader of the material of the track: param[2].x 0 as
// mat_default, 1 normalmap, 2 sunlessnormalmap, 3 shadowlessnormalmap
// TRACKBLEND_SPECGLOSS the specgloss map of the track itself as well, the textures of the ends p1 and p2 alone (no room
@@ -38,49 +37,6 @@ layout(location = 1) out vec4 out_motion;
#include <apply_fog.glsl>
#include <tonemapping.glsl>
#ifdef TRACKBLEND_GRAIN
// grains per repeat of the texture: a ballast texture repeats every few metres, a grain is a handful of its stones
const float GRAIN_CELLS = 28.0;
vec2 grain_hash2(vec2 p)
{
vec3 q = fract(vec3(p.xyx) * vec3(0.1031, 0.1030, 0.0973));
q += dot(q, q.yzx + 33.33);
return fract((q.xx + q.yz) * q.zy);
}
float grain_hash1(vec2 p)
{
vec3 q = fract(vec3(p.xyx) * 0.1031);
q += dot(q, q.yzx + 33.33);
return fract((q.x + q.y) * q.z);
}
// the value of the grain the point lies in, 0..1: cells around points jittered on a grid, each of one value
float grain_value(vec2 p)
{
vec2 cell = floor(p);
vec2 inside = fract(p);
float nearest = 8.0;
float value = 0.0;
for (int y = -1; y <= 1; ++y)
{
for (int x = -1; x <= 1; ++x)
{
vec2 offset = vec2(float(x), float(y));
vec2 r = offset + grain_hash2(cell + offset) - inside;
float d = dot(r, r);
if (d < nearest)
{
nearest = d;
value = grain_hash1(cell + offset + 17.31);
}
}
}
return value;
}
#endif
#ifdef TRACKBLEND_PARALLAX
// the texture coordinates shifted by the heights (b) of the normal map, as ParallaxMapping() of mat_parallax.frag
vec2 parallax_coord(sampler2D heights, vec2 coord, vec3 viewdir, vec2 dcoorddx, vec2 dcoorddy)
@@ -142,12 +98,6 @@ void main()
vec2 dy = dFdy(f_coord);
#ifdef TRACKBLEND_PARALLAX
vec3 viewdir = normalize(vec3(0.0) - TangentFragPos);
#endif
#ifdef TRACKBLEND_GRAIN
vec2 grainpos = f_coord * GRAIN_CELLS;
// grains smaller than a pixel would shimmer, there the textures fade into each other instead
float footprint = max(length(dx), length(dy)) * GRAIN_CELLS;
float grained = 1.0 - smoothstep(0.35, 0.9, footprint);
#endif
vec2 coord = TRACKBLEND_COORD(normalmap);
vec4 tex_color = textureGrad(diffuse, coord, dx, dy);
@@ -156,15 +106,6 @@ void main()
#endif
if (total > 0.0)
{
#ifdef TRACKBLEND_GRAIN
// the grain is of the track joined at the end whose share its value falls in, of the track itself past them
float pick = grain_value(grainpos);
float first = weights.x;
float second = weights.x + weights.y;
vec3 grain = vec3(float(pick < first), float(pick >= first && pick < second), float(pick >= second && pick < total));
weights = mix(weights, grain, grained);
total = weights.x + weights.y + weights.z;
#endif
vec4 blended = tex_color * (1.0 - total);
#ifdef TRACKBLEND_NORMALMAP
vec4 blendednormal = tex_normal * (1.0 - total);

View File

@@ -230,7 +230,7 @@ blending setup( TTrack const &Track, kind const Kind ) {
return {};
}
auto const own { material_of( Track, Kind, 0 ) };
auto const material { GfxRenderer->Blend_Material( own, others, Kind == kind::ballast ) };
auto const material { GfxRenderer->Blend_Material( own, others ) };
if( material == own ) {
// the renderer can't blend them
return {};

View File

@@ -21,8 +21,8 @@ class TTrack;
// the rails and the ballast of a track blended into those of the tracks joined at its ends, where their textures differ: near
// an end the vertices carry the weight of the track joined there (y, z, w of their user data for the ends p1, p2 and p4 of a
// switch) and the track is drawn with a material blending its texture with theirs. the ballast comes in as grains, the rails
// fade into each other. a plain track blends its rails and its ballast, a switch the trackbed made for it
// switch) and the track is drawn with a material fading its texture into theirs. a plain track blends its rails and its
// ballast, a switch the trackbed made for it
namespace track_blend {
// how far from an end the textures blend, by default