From: Mikko Rasa Date: Wed, 25 Apr 2018 11:13:26 +0000 (+0300) Subject: Use an explicit material slot name in RenderPass X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=commitdiff_plain;h=cfd4d36c2b6b6095ada3aef8082e5d409a233a21 Use an explicit material slot name in RenderPass The previous method of overriding materials with pass name seemed a bit too hacky to my liking. This is more in line with how overriding textures works. The Blender exporter now expects the base technique to have a material slot "surface". --- diff --git a/blender/io_mspgl/export_object.py b/blender/io_mspgl/export_object.py index f0197235..2d2c25ed 100644 --- a/blender/io_mspgl/export_object.py +++ b/blender/io_mspgl/export_object.py @@ -141,7 +141,7 @@ class ObjectExporter: mat_name = material.name+".mat" mat_out = open_output(os.path.join(path, mat_name)) self.export_material(material, mat_out) - out_file.write("material", '""', '"{}"'.format(mat_name)) + out_file.write("material", '"surface"', '"{}"'.format(mat_name)) out_file.end() out_file.end() else: diff --git a/source/renderpass.cpp b/source/renderpass.cpp index 09c049a8..b588ff2d 100644 --- a/source/renderpass.cpp +++ b/source/renderpass.cpp @@ -104,6 +104,7 @@ void RenderPass::Loader::init() add("shader", &RenderPass::shprog); add("material", &Loader::material_inline); add("material", &Loader::material); + add("material_slot", &RenderPass::material_slot); add("back_faces",&RenderPass::back_faces); add("texunit", &Loader::texunit); add("texunit", &Loader::texunit_named); diff --git a/source/renderpass.h b/source/renderpass.h index 50cefb76..c90108d1 100644 --- a/source/renderpass.h +++ b/source/renderpass.h @@ -58,6 +58,7 @@ private: const Program *shprog; ProgramData *shdata; RefPtr material; + std::string material_slot; Texturing *texturing; std::map tex_names; bool back_faces; @@ -73,6 +74,7 @@ public: const ProgramData *get_shader_data() const { return shdata; } void set_material(const Material *); const Material *get_material() const { return material.get(); } + const std::string &get_material_slot_name() const { return material_slot; } void set_texture(unsigned, const Texture *); const Texturing *get_texturing() const { return texturing; } int get_texture_index(const std::string &) const; diff --git a/source/technique.cpp b/source/technique.cpp index def0a84a..928c4ff6 100644 --- a/source/technique.cpp +++ b/source/technique.cpp @@ -44,6 +44,22 @@ bool Technique::replace_texture(const string &slot, const Texture &tex) return replaced; } +bool Technique::replace_material(const string &slot, const Material &mat) +{ + bool replaced = false; + for(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i) + { + const string &pass_slot = i->second.get_material_slot_name(); + if(!pass_slot.empty() && pass_slot==slot) + { + i->second.set_material(&mat); + replaced = true; + } + } + + return replaced; +} + bool Technique::has_shaders() const { for(PassMap::const_iterator i=passes.begin(); i!=passes.end(); ++i) @@ -97,10 +113,14 @@ Technique::InheritLoader::InheritLoader(Technique &t, Collection &c): add("texture", &InheritLoader::texture); } -void Technique::InheritLoader::material(const string &pass_tag, const string &name) +void Technique::InheritLoader::material(const string &slot, const string &name) { - RenderPass &pass = get_item(obj.passes, pass_tag); const Material &mat = get_collection().get(name); + if(obj.replace_material(slot, mat)) + return; + + // For backwards compatibility + RenderPass &pass = get_item(obj.passes, slot); if(const Material *base_mat = pass.get_material()) { for(PassMap::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i) diff --git a/source/technique.h b/source/technique.h index bed1db33..1078433f 100644 --- a/source/technique.h +++ b/source/technique.h @@ -51,6 +51,7 @@ public: const RenderPass &get_pass(const Tag &) const; const PassMap &get_passes() const { return passes; } bool replace_texture(const std::string &, const Texture &); + bool replace_material(const std::string &, const Material &); bool has_shaders() const; };