Use an explicit material slot name in RenderPass
authorMikko Rasa <tdb@tdb.fi>
Wed, 25 Apr 2018 11:13:26 +0000 (14:13 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 25 Apr 2018 11:18:51 +0000 (14:18 +0300)
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".

blender/io_mspgl/export_object.py
source/renderpass.cpp
source/renderpass.h
source/technique.cpp
source/technique.h

index f01972351549dcb60ba5bc8c45ff140180498008..2d2c25ed8013369c74661ba7b1f3c6b087193bec 100644 (file)
@@ -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:
index 09c049a8cee8eb1983bf279b084b592e498d7f75..b588ff2dc8a119624bcf5297ad5483f8fef972dc 100644 (file)
@@ -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);
index 50cefb76698ea48c63b965e79489816b1d239657..c90108d1c27be58632c313e77f491012dcfb8b79 100644 (file)
@@ -58,6 +58,7 @@ private:
        const Program *shprog;
        ProgramData *shdata;
        RefPtr<const Material> material;
+       std::string material_slot;
        Texturing *texturing;
        std::map<std::string, unsigned> 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;
index def0a84a9dd019ea385919a883ff4c7e15fd3d62..928c4ff6ea8cb4f00d5c6d73af7c1c263e339678 100644 (file)
@@ -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<Material>(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)
index bed1db3385046e23af3311fc7dd649cd8fc8c0a1..1078433f39f7d5c028b80ca622150882446ac3e4 100644 (file)
@@ -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;
 };