]> git.tdb.fi Git - libs/gl.git/commitdiff
Rearrange type specification in material datafiles
authorMikko Rasa <tdb@tdb.fi>
Mon, 12 Apr 2021 12:38:54 +0000 (15:38 +0300)
committerMikko Rasa <tdb@tdb.fi>
Mon, 12 Apr 2021 12:50:58 +0000 (15:50 +0300)
The type is now specified as a separate statement with no substatements.
This is what I wanted to do originally but couldn't figure out a clean
way to do it.

No legacy support because the new material system as a whole is still
fairly new and hasn't been used in many places.

blender/io_mspgl/export_material.py
source/materials/material.cpp
source/materials/material.h

index c7ca765cb0736c9387c5742b0b9e76a3f0e7c106..10be7dcba60057665792b7291ab550122901e3e7 100644 (file)
@@ -67,24 +67,23 @@ class MaterialExporter:
                return create_technique_resource(material, resources)
 
        def export_material(self, material, *, resources):
-               from .datafile import Resource, Statement
+               from .datafile import Resource, Statement, Token
                mat_res = Resource(material.name+".mat", "material")
 
                if material.type!="pbr" and material.type!="unlit":
                        raise Exception("Can't export unknown material type "+material.type)
 
-               st = Statement(material.type)
+               mat_res.statements.append(Statement("type", Token(material.type)));
                for p in material.properties:
-                       ss = self.create_property_statement(mat_res, p, resources)
-                       if ss:
-                               st.sub.append(ss)
+                       st = self.create_property_statement(mat_res, p, resources)
+                       if st:
+                               mat_res.statements.append(st)
                if self.use_textures:
                        textures = [p.texture for p in material.properties if p.texture]
                        if textures and not textures[0].default_filter:
                                from .export_texture import SamplerExporter
                                sampler_export = SamplerExporter()
-                               st.sub.append(mat_res.create_reference_statement("sampler", resources[sampler_export.get_sampler_name(textures[0])]))
-               mat_res.statements.append(st)
+                               mat_res.statements.append(mat_res.create_reference_statement("sampler", resources[sampler_export.get_sampler_name(textures[0])]))
 
                return mat_res
 
@@ -134,10 +133,9 @@ class MaterialAtlasExporter:
                mat_name = material_atlas.name+".mat"
                if mat_name not in resources:
                        mat_res = Resource(mat_name, "material")
-                       st = Statement("pbr")
-                       st.sub.append(mat_res.create_reference_statement("base_color_map", base_color_res))
-                       st.sub.append(mat_res.create_reference_statement("sampler", sampler_res))
-                       mat_res.statements.append(st)
+                       mat_res.statements.append(Statement("type", Token('pbr')))
+                       mat_res.statements.append(mat_res.create_reference_statement("base_color_map", base_color_res))
+                       mat_res.statements.append(mat_res.create_reference_statement("sampler", sampler_res))
 
                        resources[mat_name] = mat_res
 
index d87f2f830afd7583df377b641a0a70873829e5e6..36d0958f5afb18aaa5270750f7c8e031193583a4 100644 (file)
@@ -103,14 +103,27 @@ void Material::Loader::sampler(const string &name)
 DataFile::Loader::ActionMap Material::GenericLoader::shared_actions;
 
 Material::GenericLoader::GenericLoader(DataFile::Collection *c):
-       coll(c)
+       coll(c),
+       material(0),
+       mat_loader(0)
 {
        set_actions(shared_actions);
 }
 
+Material::GenericLoader::~GenericLoader()
+{
+       delete material;
+       delete mat_loader;
+}
+
 void Material::GenericLoader::init_actions()
 {
-       get_material_registry().invoke_all(*this);
+       add("type", &GenericLoader::type);
+}
+
+void Material::GenericLoader::type(const DataFile::Symbol &sym)
+{
+       get_material_registry().invoke(sym.name, *this);
 }
 
 } // namespace GL
index 2f8f38ab7ce183c40492b3d75f2b11c2904939a6..278fc4dc0ee8ba7da8ce27fcd392d5c4bf0c2b46 100644 (file)
@@ -62,31 +62,32 @@ public:
        {
        private:
                template<typename T>
-               struct AddType
+               struct CreateMaterial
                {
-                       void operator()(const std::string &kw, GenericLoader &ldr) const { ldr.add(kw, &GenericLoader::typed_material<T>); }
+                       void operator()(const std::string &, GenericLoader &) const;
                };
 
                DataFile::Collection *coll;
-               RefPtr<Material> material;
+               Material *material;
+               Loader *mat_loader;
 
                static ActionMap shared_actions;
 
        public:
                GenericLoader(DataFile::Collection * = 0);
+               ~GenericLoader();
 
-               Material *get_material() { return material.release(); }
+               Material *get_material() { Material *m = material; material = 0; return m; }
        private:
                virtual void init_actions();
 
-               template<typename T>
-               void typed_material();
+               void type(const DataFile::Symbol &);
 
                friend class Material;
        };
 
 private:
-       typedef TypeRegistry<GenericLoader::AddType, GenericLoader &> MaterialRegistry;
+       typedef TypeRegistry<GenericLoader::CreateMaterial, GenericLoader &> MaterialRegistry;
 
 protected:
        const Sampler *sampler;
@@ -194,16 +195,18 @@ void Material::PropertyLoader<C>::property_texture(void (C::*set_texture)(const
 
 
 template<typename T>
-void Material::GenericLoader::typed_material()
+void Material::GenericLoader::CreateMaterial<T>::operator()(const std::string &, GenericLoader &ldr) const
 {
-       if(material)
-               throw std::logic_error("Material was already loaded");
-       RefPtr<T> mat = new T;
-       if(coll)
-               load_sub(*mat, *coll);
+       if(ldr.material)
+               throw std::logic_error("Material type was already specified");
+
+       T *mat = new T;
+       ldr.material = mat;
+       if(ldr.coll)
+               ldr.mat_loader = new typename T::Loader(*mat, *ldr.coll);
        else
-               load_sub(*mat);
-       material = mat;
+               ldr.mat_loader = new typename T::Loader(*mat);
+       ldr.add_auxiliary_loader(*ldr.mat_loader);
 }
 
 } // namespace GL