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
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
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
{
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;
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