1 #include <msp/core/hash.h>
2 #include <msp/strings/format.h>
3 #include "basicmaterial.h"
5 #include "pbrmaterial.h"
9 #include "unlitmaterial.h"
16 const Program *Material::create_compatible_shader(DataFile::Collection &coll) const
19 map<string, int> spec_values;
20 fill_program_info(module_name, spec_values);
22 string info = module_name;
23 for(map<string, int>::const_iterator i=spec_values.begin(); i!=spec_values.end(); ++i)
24 info += format(",%s:%d", i->first, i->second);
26 string name = format("_material_%016x.shader", hash64(info));
27 Program *shprog = coll.find<Program>(name);
31 const Module &module = coll.get<Module>(module_name);
32 shprog = new Program(module, spec_values);
35 coll.add(name, shprog);
46 void Material::attach_texture_to(const Texture *tex, Texturing &texturing, ProgramData &tex_shdata, const string &name) const
53 if(const Uniform *uni = tex_shdata.find_uniform(name))
54 if(const Uniform1i *uni_int = dynamic_cast<const Uniform1i *>(uni))
55 unit = uni_int->get();
58 unit = texturing.find_free_unit(name);
60 throw runtime_error("no free texunit");
62 texturing.attach(unit, *tex, sampler);
63 tex_shdata.uniform(name, unit);
66 Material::MaterialRegistry &Material::get_material_registry()
68 static MaterialRegistry registry;
69 static bool initialized = false;
73 registry.register_type<BasicMaterial>("basic");
74 registry.register_type<PbrMaterial>("pbr");
75 registry.register_type<UnlitMaterial>("unlit");
81 Material::Loader::Loader(Material &m):
82 CollectionObjectLoader(m, 0)
85 Material::Loader::Loader(Material &m, Collection &c):
86 CollectionObjectLoader(m, &c)
89 void Material::Loader::init_actions()
91 add("sampler", &Loader::sampler);
94 void Material::Loader::sampler(const std::string &name)
96 obj.sampler = &get_collection().get<Sampler>(name);
100 DataFile::Loader::ActionMap Material::GenericLoader::shared_actions;
102 Material::GenericLoader::GenericLoader(DataFile::Collection *c):
105 set_actions(shared_actions);
108 void Material::GenericLoader::init_actions()
110 get_material_registry().add_all(*this);