1 #include "framebuffer.h"
3 #include "pbrmaterial.h"
13 const Tag PbrMaterial::texture_tags[] =
15 Tag("base_color_map"),
21 Tag("fresnel_lookup"),
25 PbrMaterial::PbrMaterial():
26 fresnel_lookup(get_or_create_fresnel_lookup()),
27 fresnel_sampler(Resources::get_global().get<Sampler>("_linear_clamp.samp"))
35 const Texture2D &PbrMaterial::get_or_create_fresnel_lookup()
37 Resources &resources = Resources::get_global();
39 static const string name = "_pbr_fresnel_lookup.tex";
40 Texture2D *fresnel_lookup = resources.find<Texture2D>(name);
42 return *fresnel_lookup;
44 fresnel_lookup = new Texture2D;
45 fresnel_lookup->storage(RG8, 128, 128, 1);
46 resources.add(name, fresnel_lookup);
48 const Program &shprog = resources.get<Program>("_pbr_fresnel_lookup.glsl.shader");
50 shdata.uniform("n_samples", 1024);
51 // Not actually used here, but put it in to satisfy the interface
52 shdata.uniform("roughness", 0.0f);
54 const Mesh &mesh = resources.get<Mesh>("_fullscreen_quad.mesh");
55 Framebuffer fresnel_lookup_fbo((COLOR_ATTACHMENT,RG8));
56 fresnel_lookup_fbo.attach(COLOR_ATTACHMENT, *fresnel_lookup);
59 renderer.set_framebuffer(&fresnel_lookup_fbo);
60 renderer.set_shader_program(&shprog, &shdata);
64 return *fresnel_lookup;
67 void PbrMaterial::fill_program_info(string &module_name, map<string, int> &spec_values) const
69 module_name = "pbr_material.glsl";
70 spec_values["use_base_color_map"] = (base_color.texture!=0);
71 spec_values["use_normal_map"] = (normal.texture!=0);
72 spec_values["use_metalness_map"] = (metalness.texture!=0);
73 spec_values["use_roughness_map"] = (roughness.texture!=0);
74 spec_values["use_occlusion_map"] = (occlusion.texture!=0);
75 bool use_emission = (emission.texture || emission.value.r || emission.value.g || emission.value.b);
76 spec_values["use_emission"] = use_emission;
77 spec_values["use_emission_map"] = (emission.texture!=0);
80 const Texture *PbrMaterial::get_texture(Tag tag) const
82 if(tag==texture_tags[0])
83 return base_color.texture;
84 else if(tag==texture_tags[1])
85 return normal.texture;
86 else if(tag==texture_tags[2])
87 return metalness.texture;
88 else if(tag==texture_tags[3])
89 return roughness.texture;
90 else if(tag==texture_tags[4])
91 return occlusion.texture;
92 else if(tag==texture_tags[5])
93 return emission.texture;
94 else if(tag==texture_tags[6])
95 return &fresnel_lookup;
100 const Sampler *PbrMaterial::get_sampler(Tag tag) const
102 if(tag==texture_tags[6])
103 return &fresnel_sampler;
108 void PbrMaterial::set_base_color(const Color &color)
110 base_color.value = color;
111 shdata.uniform("pbr_material.base_color", color);
114 void PbrMaterial::set_base_color_map(const Texture *tex)
116 base_color.texture = tex;
119 void PbrMaterial::set_tint(const Color &color)
122 shdata.uniform("pbr_material.tint", color);
125 void PbrMaterial::set_normal_map(const Texture *tex)
127 normal.texture = tex;
130 void PbrMaterial::set_metalness(float value)
132 metalness.value = value;
133 shdata.uniform("pbr_material.metalness", value);
136 void PbrMaterial::set_metalness_map(const Texture *tex)
138 metalness.texture = tex;
141 void PbrMaterial::set_roughness(float value)
143 roughness.value = value;
144 shdata.uniform("pbr_material.roughness", value);
147 void PbrMaterial::set_roughness_map(const Texture *tex)
149 roughness.texture = tex;
152 void PbrMaterial::set_occlusion_map(const Texture *tex)
154 occlusion.texture = tex;
157 void PbrMaterial::set_emission(const Color &color)
159 emission.value = color;
160 shdata.uniform("pbr_material.emission", color);
163 void PbrMaterial::set_emission_map(const Texture *tex)
165 emission.texture = tex;
169 DataFile::Loader::ActionMap PbrMaterial::Loader::shared_actions;
171 PbrMaterial::Loader::Loader(PbrMaterial &m, Collection &c):
172 DerivedObjectLoader<PbrMaterial, Material::PropertyLoader<PbrMaterial> >(m, c)
174 set_actions(shared_actions);
177 void PbrMaterial::Loader::init_actions()
179 Material::PropertyLoader<PbrMaterial>::init_actions();
180 add_property("base_color", &PbrMaterial::set_base_color, &PbrMaterial::set_base_color_map, true);
181 add_property("normal", &PbrMaterial::set_normal_map);
182 add_property("metalness", &PbrMaterial::set_metalness, &PbrMaterial::set_metalness_map);
183 add_property("roughness", &PbrMaterial::set_roughness, &PbrMaterial::set_roughness_map);
184 add_property("occlusion", &PbrMaterial::set_occlusion_map);
185 add_property("emission", &PbrMaterial::set_emission, &PbrMaterial::set_emission_map, false);
186 add_property("tint", &PbrMaterial::set_tint, 0, true);