mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-10-24 19:11:52 +02:00
feat(track): rails and ballast blended into those of the tracks joined, where their textures differ
This commit is contained in:
@@ -756,6 +756,90 @@ material_manager::create_tinted( material_handle const Material, glm::vec3 const
|
||||
return tintedhandle;
|
||||
}
|
||||
|
||||
bool
|
||||
material_manager::blendable( material_handle const Material ) {
|
||||
|
||||
if( Material == null_handle || Material >= m_materials.size() ) {
|
||||
return false;
|
||||
}
|
||||
if( auto const lookup { m_blendables.find( Material ) }; lookup != m_blendables.end() ) {
|
||||
return lookup->second;
|
||||
}
|
||||
auto result { false };
|
||||
auto const &material { m_materials[ Material ] };
|
||||
if( material.is_good
|
||||
&& material.shader
|
||||
&& material.texture_variants.empty()
|
||||
&& false == material.update_on_weather_change
|
||||
&& false == material.update_on_season_change
|
||||
&& material.shader->texture_conf.size() == 1
|
||||
&& material.shader->texture_conf.count( "diffuse" ) == 1
|
||||
&& material.textures[ material.shader->texture_conf.at( "diffuse" ).id ] != null_handle ) {
|
||||
try {
|
||||
// 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( "trackblend" ) };
|
||||
result = std::all_of(
|
||||
std::begin( material.shader->param_conf ), std::end( material.shader->param_conf ),
|
||||
[&]( auto const &Entry ) {
|
||||
auto const found { blend->param_conf.find( Entry.first ) };
|
||||
return found != blend->param_conf.end()
|
||||
&& found->second.location == Entry.second.location
|
||||
&& found->second.offset == Entry.second.offset
|
||||
&& found->second.size == Entry.second.size; } );
|
||||
}
|
||||
catch( gl::shader_exception const &e ) {
|
||||
ErrorLog( "invalid shader: " + std::string( e.what() ) );
|
||||
}
|
||||
}
|
||||
m_blendables.emplace( Material, result );
|
||||
return result;
|
||||
}
|
||||
|
||||
material_handle
|
||||
material_manager::create_blend( material_handle const Material, std::array<material_handle, 3> const &Others, bool const Grain ) {
|
||||
|
||||
if( false == blendable( Material ) ) {
|
||||
return Material;
|
||||
}
|
||||
auto const key { std::make_tuple( Material, Others[ 0 ], Others[ 1 ], Others[ 2 ], Grain ) };
|
||||
if( auto const lookup { m_blends.find( key ) }; lookup != m_blends.end() ) {
|
||||
return lookup->second;
|
||||
}
|
||||
auto const diffuse_of = [&]( material_handle const Source ) -> texture_handle {
|
||||
if( false == blendable( Source ) ) {
|
||||
return null_handle;
|
||||
}
|
||||
auto const &source { m_materials[ Source ] };
|
||||
return source.textures[ source.shader->texture_conf.at( "diffuse" ).id ];
|
||||
};
|
||||
auto blend { variant_of( m_materials[ Material ], Grain ? ":blendgrain" : ":blend" ) };
|
||||
auto result { Material };
|
||||
try {
|
||||
blend.shader = GfxRenderer->Fetch_Shader( Grain ? "trackblend_grain" : "trackblend" );
|
||||
blend.texture_variants.clear();
|
||||
blend.textures.fill( null_handle );
|
||||
auto const own { diffuse_of( Material ) };
|
||||
blend.textures[ 0 ] = own;
|
||||
for( std::size_t other = 0; other < Others.size(); ++other ) {
|
||||
// the slots of the ends nothing is blended at take the own texture, the shader samples them only with a weight
|
||||
auto const texture { diffuse_of( Others[ other ] ) };
|
||||
blend.textures[ 1 + other ] = ( texture != null_handle ? texture : own );
|
||||
}
|
||||
// the parameters taken from the bindings again, by the shader
|
||||
blend.params_state.clear();
|
||||
blend.finalize( true );
|
||||
if( blend.is_good ) {
|
||||
result = static_cast<material_handle>( m_materials.size() );
|
||||
m_materials.emplace_back( std::move( blend ) );
|
||||
}
|
||||
}
|
||||
catch( gl::shader_exception const &e ) {
|
||||
ErrorLog( "invalid shader: " + std::string( e.what() ) );
|
||||
}
|
||||
m_blends.emplace( key, result );
|
||||
return result;
|
||||
}
|
||||
|
||||
void
|
||||
material_manager::on_weather_change() {
|
||||
|
||||
|
||||
@@ -147,6 +147,14 @@ public:
|
||||
create_matte( material_handle const Material );
|
||||
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
|
||||
material_handle
|
||||
create_blend( material_handle const Material, std::array<material_handle, 3> const &Others, bool const Grain );
|
||||
// whether the material can be blended so: one diffuse texture, lit as mat_default, not changing with the weather and seasons
|
||||
bool
|
||||
blendable( material_handle const Material );
|
||||
opengl_material const &
|
||||
material( material_handle const Material ) const { return m_materials[ Material ]; }
|
||||
opengl_material &
|
||||
@@ -177,6 +185,8 @@ private:
|
||||
index_map m_materialmappings;
|
||||
std::unordered_map<material_handle, material_handle> m_mattes;
|
||||
std::map<std::tuple<material_handle, float, float, float>, material_handle> m_tints;
|
||||
std::map<std::tuple<material_handle, material_handle, material_handle, material_handle, bool>, material_handle> m_blends;
|
||||
std::unordered_map<material_handle, bool> m_blendables;
|
||||
};
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user