17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-08 13:19:17 +01:00

Merge pull request #245 from MaSzyna-EU07/split/2-materials

Split/2 materials
This commit is contained in:
2026-11-07 11:20:50 +00:00
committed by GitHub
12 changed files with 292 additions and 3 deletions

View File

@@ -662,12 +662,176 @@ material_manager::create_from_text( std::string const &Name, std::string const &
return materialhandle;
}
namespace {
opengl_material
variant_of( opengl_material const &Source, std::string const &Suffix ) {
opengl_material variant;
variant.textures = Source.textures;
variant.texture_variants = Source.texture_variants;
variant.params = Source.params;
variant.params_state = Source.params_state;
variant.shader = Source.shader;
variant.opacity = Source.opacity;
variant.selfillum = Source.selfillum;
variant.shadow_rank = Source.shadow_rank;
variant.glossiness = Source.glossiness;
variant.name = Source.name + Suffix;
variant.size = Source.size;
variant.is_good = Source.is_good;
variant.path = Source.path;
return variant;
}
// the params of a shader, as the shader keeps them
using param_confs = decltype( gl::shader::param_conf );
// whether another param of the shader lies on a component of the named one, which a value set for that one would change too
bool
shares_slot( param_confs const &Conf, std::string const &Name ) {
auto const own { Conf.find( Name ) };
if( own == Conf.end() ) {
return false;
}
return std::ranges::any_of(
Conf,
[ &Name, &own ]( auto const &Entry ) {
return Entry.first != Name
&& Entry.second.location == own->second.location
&& Entry.second.offset < own->second.offset + own->second.size
&& own->second.offset < Entry.second.offset + Entry.second.size; } );
}
// the highlights of a copy of a material damped and its weathering on
void
make_matte( opengl_material &Variant ) {
Variant.glossiness = 0.1f;
if( !Variant.shader ) {
return;
}
for( auto const &[ key, value ] : std::vector<std::pair<std::string, glm::vec4>>{ { "weathering", glm::vec4{ 1.f } } } ) {
auto const entry { Variant.shader->param_conf.find( key ) };
if( entry == Variant.shader->param_conf.end()
|| shares_slot( Variant.shader->param_conf, key ) ) {
continue;
}
auto &slot { Variant.params[ entry->second.location ] };
auto const offset { static_cast<glm::length_t>( entry->second.offset ) };
auto const size { static_cast<glm::length_t>( entry->second.size ) };
for( glm::length_t i = 0; i < size; ++i ) {
slot[ offset + i ] = value[ i ];
}
std::erase_if(
Variant.params_state,
[ &entry ]( gl::shader::param_entry const &State ) {
return State.location == entry->second.location && State.offset == entry->second.offset; } );
}
}
// the colour in the rgb of the slot of the weathering of a copy of a material, which tells the shader to tint by its sign. returns:
// whether the shader can tint it, the weathering and the tint alone in that slot
bool
make_tinted( opengl_material &Variant, glm::vec3 const &Colour ) {
if( !Variant.shader ) {
return false;
}
auto const &conf { Variant.shader->param_conf };
auto const weathering { conf.find( "weathering" ) };
if( auto const tint { conf.find( "tint" ) };
weathering == conf.end()
|| tint == conf.end()
|| weathering->second.offset != 3
|| tint->second.location != weathering->second.location
|| tint->second.offset != 0
|| std::ranges::any_of(
conf,
[ &weathering, &tint ]( auto const &Entry ) {
return Entry.first != weathering->first && Entry.first != tint->first && Entry.second.location == weathering->second.location; } ) ) {
return false;
}
auto const location { weathering->second.location };
Variant.params[ location ] = glm::vec4{ Colour, -1.f };
std::erase_if(
Variant.params_state,
[ location ]( gl::shader::param_entry const &State ) {
return State.location == location; } );
return true;
}
} // namespace
material_handle
material_manager::create_matte( material_handle const Material ) {
if( Material == null_handle || Material >= m_materials.size() ) {
return Material;
}
if( auto const lookup { m_mattes.find( Material ) }; lookup != m_mattes.end() ) {
return lookup->second;
}
auto matte { variant_of( m_materials[ Material ], ":matte" ) };
make_matte( matte );
auto const mattehandle { static_cast<material_handle>( m_materials.size() ) };
m_materials.emplace_back( std::move( matte ) );
m_mattes.emplace( Material, mattehandle );
return mattehandle;
}
material_handle
material_manager::create_tinted( material_handle const Material, glm::vec3 const &Colour ) {
if( Material == null_handle || Material >= m_materials.size() || !m_materials[ Material ].shader ) {
return Material;
}
auto const key { std::make_tuple( Material, Colour.r, Colour.g, Colour.b ) };
if( auto const lookup { m_tints.find( key ) }; lookup != m_tints.end() ) {
return lookup->second;
}
auto tinted { variant_of( m_materials[ Material ], ":tinted" ) };
if( false == make_tinted( tinted, Colour ) ) {
return Material;
}
auto const tintedhandle { static_cast<material_handle>( m_materials.size() ) };
m_materials.emplace_back( std::move( tinted ) );
m_tints.emplace( key, tintedhandle );
return tintedhandle;
}
void
material_manager::update_variants( bool const Weather ) {
// the copies don't update themselves, their names aren't files to read again
auto const changed = [&]( material_handle const Source ) {
auto const &source { m_materials[ Source ] };
return Weather ? source.update_on_weather_change : source.update_on_season_change; };
for( auto const &[ source, matte ] : m_mattes ) {
if( changed( source ) ) {
auto variant { variant_of( m_materials[ source ], ":matte" ) };
make_matte( variant );
m_materials[ matte ] = std::move( variant );
}
}
for( auto const &[ key, tinted ] : m_tints ) {
auto const source { std::get<0>( key ) };
if( changed( source ) ) {
auto variant { variant_of( m_materials[ source ], ":tinted" ) };
make_tinted( variant, glm::vec3 { std::get<1>( key ), std::get<2>( key ), std::get<3>( key ) } );
m_materials[ tinted ] = std::move( variant );
}
}
}
void
material_manager::on_weather_change() {
for( auto &material : m_materials ) {
if( material.update_on_weather_change ) { material.update(); }
}
update_variants( true );
}
void
@@ -676,6 +840,7 @@ material_manager::on_season_change() {
for( auto &material : m_materials ) {
if( material.update_on_season_change ) { material.update(); }
}
update_variants( false );
}
// checks whether specified material is in the material bank. returns handle to the material, or a null handle

View File

@@ -142,6 +142,13 @@ public:
// material described by provided text (the content of a .mat file), not kept in a file. returns: handle to the material or null_handle
material_handle
create_from_text( std::string const &Name, std::string const &Definition );
// copy of provided material with its specular highlights and reflections damped. returns: handle to the copy, made once per material
material_handle
create_matte( material_handle const Material );
// copy of provided material with its texture tinted to the colour as far as the userdata of the vertices says. returns: handle to
// the copy, made once per material and colour; the material itself when its shader can't tint it
material_handle
create_tinted( material_handle const Material, glm::vec3 const &Colour );
opengl_material const &
material( material_handle const Material ) const { return m_materials[ Material ]; }
opengl_material &
@@ -161,6 +168,9 @@ private:
// checks whether specified texture is in the texture bank. returns texture id, or npos.
material_handle
find_in_databank( std::string const &Materialname ) const;
// the matte and tinted copies of the materials changed with the weather or the season made again from them, under their handles
void
update_variants( bool const Weather );
public:
// checks whether specified file exists. returns name of the located file, or empty string.
static std::pair<std::string, std::string>
@@ -170,6 +180,8 @@ private:
// members:
material_sequence m_materials;
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;
};
//---------------------------------------------------------------------------