Commit d65f2840 authored by Florian Oetke's avatar Florian Oetke
Browse files

better emissive material handling [#26]

parent a194bb8f
......@@ -49,13 +49,6 @@ void main() {
vec3 albedo = albedo_mat_id.rgb;
int material = int(albedo_mat_id.a*255);
// material 255 (unlit)
if(material==255) {
out_color = out_color_diff = vec4(albedo*100.0, 1.0);
return;
}
// material 0 (default)
vec3 N = decode_normal(mat_data.rg);
float roughness = mat_data.b;
float metallic = mat_data.a;
......@@ -77,8 +70,6 @@ void main() {
out_color = vec4(brdf(albedo, F0, roughness, N, V, L, radiance, diffuse) * shadow, 1.0);
out_color_diff = vec4(diffuse * shadow, 1.0);
}
// out_color.rgb += albedo * vec3(0.92, 0.95, 1) * 0.001;
}
......
......@@ -13,14 +13,17 @@ layout(location = 2) in vec2 tex_coords;
layout(location = 0) out vec4 depth_out;
layout(location = 1) out vec4 albedo_mat_id;
layout(location = 2) out vec4 mat_data;
layout(location = 3) out vec4 color_out;
layout(location = 4) out vec4 color_diffuse_out;
layout(set=1, binding = 0) uniform sampler2D albedo_sampler;
layout(set=1, binding = 1) uniform sampler2D mat_data_sampler;
layout(set=1, binding = 2) uniform sampler2D mat_data2_sampler;
layout(push_constant) uniform Per_model_uniforms {
mat4 model;
vec4 light_color;
vec4 options;
vec4 material_properties;
} model_uniforms;
......@@ -33,4 +36,6 @@ void main() {
depth_out = vec4(-view_pos.z / global_uniforms.proj_planes.y, 0,0,1);
albedo_mat_id = vec4(albedo.rgb, 1.0);
mat_data = vec4(encode_normal(normalize(normal)), 0.0, 0.0);
color_out = vec4(albedo.rgb * texture(mat_data2_sampler, tex_coords).r * model_uniforms.material_properties.rgb, 1.0);
color_diffuse_out = color_out;
}
......@@ -3,7 +3,7 @@
},
"Directional_light": {
"source_radius": 0.8,
"intensity": 500.0,
"intensity": 5000.0,
"temperature": 4500,
"shadow_size": 24,
"near_plane": 1.0,
......
......@@ -331,8 +331,16 @@ namespace mirrage {
material_file.albedo_aid = albedo_name;
switch(substance_id) {
case "emissive"_strid: // TODO: anything else required?
break;
case "emissive"_strid: {
auto emission_texture_name = find_texture(name, material, cfg, Texture_type::emission);
auto emission = load_texture2d(resolve_path(name, base_dir, emission_texture_name), false);
generate_mip_maps(emission,
[](auto a, auto b, auto c, auto d) { return (a + b + c + d) / 4.f; });
material_file.mat_data2_aid = name + "_mat_data2.ktx";
store_texture(emission, texture_dir + material_file.mat_data2_aid, false);
}
[[fallthrough]];
case "default"_strid:
default:
......@@ -376,7 +384,6 @@ namespace mirrage {
material_file.mat_data_aid = name + "_mat_data.ktx";
store_texture(normal, texture_dir + material_file.mat_data_aid, false);
break;
}
......
......@@ -158,6 +158,8 @@ namespace mirrage::graphic {
return *this;
}
auto color_mask(std::uint32_t index, vk::ColorComponentFlags) -> Stage_builder&;
auto add_push_constant(util::Str_id id, std::uint32_t offset, std::uint32_t size, vk::ShaderStageFlags)
-> Stage_builder&;
......
......@@ -217,6 +217,12 @@ namespace mirrage::graphic {
return *this;
}
auto Stage_builder::color_mask(std::uint32_t index, vk::ColorComponentFlags mask) -> Stage_builder&
{
pipeline().color_blend_attachments.at(index).setColorWriteMask(mask);
return *this;
}
auto Stage_builder::add_push_constant(util::Str_id id,
std::uint32_t offset,
std::uint32_t size,
......
......@@ -21,7 +21,8 @@ namespace mirrage::renderer {
struct Material_data {
util::Str_id substance_id = "default"_strid;
std::string albedo_aid;
std::string mat_data_aid;
std::string mat_data_aid; // RG: normal, B:roughness, A:metallic
std::string mat_data2_aid; // R:emissive intensity
};
#ifdef sf2_structDef
......@@ -37,6 +38,7 @@ namespace mirrage::renderer {
vk::Sampler,
graphic::Texture_ptr albedo,
graphic::Texture_ptr mat_data,
graphic::Texture_ptr mat_data2,
util::Str_id substance_id);
void bind(graphic::Render_pass& pass) const;
......@@ -47,6 +49,7 @@ namespace mirrage::renderer {
graphic::DescriptorSet _descriptor_set;
graphic::Texture_ptr _albedo;
graphic::Texture_ptr _mat_data;
graphic::Texture_ptr _mat_data2;
util::Str_id _substance_id;
};
using Material_ptr = asset::Ptr<Material>;
......
......@@ -12,6 +12,18 @@ namespace mirrage::renderer {
// Client code should only care about the Model_comp class as it represents an actually usable model.
class Material_property_comp : public ecs::Component<Material_property_comp> {
public:
static constexpr const char* name() { return "Material_property"; }
Material_property_comp() = default;
Material_property_comp(ecs::Entity_handle owner, ecs::Entity_manager& em) : Component(owner, em) {}
glm::vec3 emissive_color = glm::vec3(200, 200, 200);
};
sf2_structDef(Material_property_comp, emissive_color);
class Model_comp : public ecs::Component<Model_comp> {
public:
static constexpr const char* name() { return "Model_loaded"; }
......
......@@ -21,7 +21,7 @@ namespace mirrage {
namespace mirrage::ecs {
class Entity_manager;
namespace components {
class Transform_comp;
struct Transform_comp;
}
} // namespace mirrage::ecs
......
......@@ -11,7 +11,7 @@ using namespace mirrage::graphic;
namespace mirrage::renderer {
namespace {
constexpr auto material_textures = std::uint32_t(2);
constexpr auto material_textures = std::uint32_t(3);
} // namespace
auto create_material_descriptor_set_layout(Device& device, vk::Sampler sampler)
......@@ -42,10 +42,12 @@ namespace mirrage::renderer {
vk::Sampler sampler,
graphic::Texture_ptr albedo,
graphic::Texture_ptr mat_data,
graphic::Texture_ptr mat_data2,
util::Str_id substance_id)
: _descriptor_set(std::move(descriptor_set))
, _albedo(std::move(albedo))
, _mat_data(std::move(mat_data))
, _mat_data2(std::move(mat_data2))
, _substance_id(substance_id ? substance_id : "default"_strid)
{
......@@ -54,6 +56,9 @@ namespace mirrage::renderer {
vk::DescriptorImageInfo{sampler, _albedo->view(), vk::ImageLayout::eShaderReadOnlyOptimal};
desc_images[1] =
vk::DescriptorImageInfo{sampler, _mat_data->view(), vk::ImageLayout::eShaderReadOnlyOptimal};
desc_images[2] =
vk::DescriptorImageInfo{sampler, _mat_data2->view(), vk::ImageLayout::eShaderReadOnlyOptimal};
auto desc_writes = std::array<vk::WriteDescriptorSet, 1>();
desc_writes[0] = vk::WriteDescriptorSet{*_descriptor_set,
......@@ -133,14 +138,17 @@ namespace mirrage::asset {
auto desc_set =
_descriptor_set_pool.create_descriptor(_descriptor_set_layout, renderer::material_textures);
auto albedo = load_tex(data.albedo_aid);
auto mat_data = load_tex(data.mat_data_aid);
auto albedo = load_tex(data.albedo_aid);
auto mat_data = load_tex(data.mat_data_aid);
auto mat_data2 = !data.mat_data2_aid.empty() ? load_tex(data.mat_data2_aid) : mat_data;
auto all_loaded = async::when_all(albedo.internal_task(), mat_data.internal_task());
auto all_loaded =
async::when_all(albedo.internal_task(), mat_data.internal_task(), mat_data2.internal_task());
using Task_type = decltype(all_loaded)::result_type;
return all_loaded.then([=, desc_set = std::move(desc_set)](const Task_type&) mutable {
return renderer::Material(_device, std::move(desc_set), _sampler, albedo, mat_data, sub_id);
return renderer::Material(
_device, std::move(desc_set), _sampler, albedo, mat_data, mat_data2, sub_id);
});
}
......
......@@ -34,15 +34,21 @@ namespace mirrage::renderer {
{
pass.stage("default"_strid)
.shader("frag_shader:model"_aid, graphic::Shader_stage::fragment)
.shader("vert_shader:model"_aid, graphic::Shader_stage::vertex);
.shader("vert_shader:model"_aid, graphic::Shader_stage::vertex)
.color_mask(3, vk::ColorComponentFlags{})
.color_mask(4, vk::ColorComponentFlags{});
pass.stage("emissive"_strid)
.shader("frag_shader:model_emissive"_aid, graphic::Shader_stage::fragment)
.shader("vert_shader:model"_aid, graphic::Shader_stage::vertex);
.shader("vert_shader:model"_aid, graphic::Shader_stage::vertex)
.color_mask(3, graphic::all_color_components)
.color_mask(4, graphic::all_color_components);
pass.stage("alphatest"_strid)
.shader("frag_shader:model_alphatest"_aid, graphic::Shader_stage::fragment)
.shader("vert_shader:model"_aid, graphic::Shader_stage::vertex);
.shader("vert_shader:model"_aid, graphic::Shader_stage::vertex)
.color_mask(3, vk::ColorComponentFlags{})
.color_mask(4, vk::ColorComponentFlags{});
}
void Deferred_geometry_subpass::configure_animation_pipeline(Deferred_renderer& renderer,
......@@ -70,28 +76,40 @@ namespace mirrage::renderer {
{
pass.stage("default"_strid)
.shader("frag_shader:model"_aid, graphic::Shader_stage::fragment)
.shader("vert_shader:model_animated"_aid, graphic::Shader_stage::vertex);
.shader("vert_shader:model_animated"_aid, graphic::Shader_stage::vertex)
.color_mask(3, vk::ColorComponentFlags{})
.color_mask(4, vk::ColorComponentFlags{});
pass.stage("emissive"_strid)
.shader("frag_shader:model_emissive"_aid, graphic::Shader_stage::fragment)
.shader("vert_shader:model_animated"_aid, graphic::Shader_stage::vertex);
.shader("vert_shader:model_animated"_aid, graphic::Shader_stage::vertex)
.color_mask(3, graphic::all_color_components)
.color_mask(4, graphic::all_color_components);
pass.stage("alphatest"_strid)
.shader("frag_shader:model_alphatest"_aid, graphic::Shader_stage::fragment)
.shader("vert_shader:model_animated"_aid, graphic::Shader_stage::vertex);
.shader("vert_shader:model_animated"_aid, graphic::Shader_stage::vertex)
.color_mask(3, vk::ColorComponentFlags{})
.color_mask(4, vk::ColorComponentFlags{});
pass.stage("dq_default"_strid)
.shader("frag_shader:model"_aid, graphic::Shader_stage::fragment)
.shader("vert_shader:model_animated_dqs"_aid, graphic::Shader_stage::vertex);
.shader("vert_shader:model_animated_dqs"_aid, graphic::Shader_stage::vertex)
.color_mask(3, vk::ColorComponentFlags{})
.color_mask(4, vk::ColorComponentFlags{});
pass.stage("dq_emissive"_strid)
.shader("frag_shader:model_emissive"_aid, graphic::Shader_stage::fragment)
.shader("vert_shader:model_animated_dqs"_aid, graphic::Shader_stage::vertex);
.shader("vert_shader:model_animated_dqs"_aid, graphic::Shader_stage::vertex)
.color_mask(3, graphic::all_color_components)
.color_mask(4, graphic::all_color_components);
pass.stage("dq_alphatest"_strid)
.shader("frag_shader:model_alphatest"_aid, graphic::Shader_stage::fragment)
.shader("vert_shader:model_animated_dqs"_aid, graphic::Shader_stage::vertex);
.shader("vert_shader:model_animated_dqs"_aid, graphic::Shader_stage::vertex)
.color_mask(3, vk::ColorComponentFlags{})
.color_mask(4, vk::ColorComponentFlags{});
}
void Deferred_geometry_subpass::update(util::Time) {}
......@@ -139,6 +157,16 @@ namespace mirrage::renderer {
dpc.model = glm::toMat4(geo.orientation) * glm::scale(glm::mat4(1.f), geo.scale);
dpc.model[3] = glm::vec4(geo.position, 1.f);
dpc.model = _renderer.global_uniforms().view_mat * dpc.model;
if(sub_mesh.material->substance_id() == "emissive"_strid) {
auto emissive_color =
_ecs.get(geo.entity)
.get_or_throw()
.template get<Material_property_comp>()
.process(glm::vec3(200, 200, 200), [](auto& m) { return m.emissive_color; });
dpc.light_data = glm::vec4(emissive_color / 10.f, 1.f);
}
};
......
......@@ -120,7 +120,7 @@ namespace mirrage::renderer {
auto& light_data = **ll;
dpc.model = light_data.calc_shadowmap_view_proj(*light.transform) * inv_view;
dpc.light_color = glm::vec4(light_data.color(), light_data.intensity());
dpc.light_color = glm::vec4(light_data.color(), light_data.intensity() / 10.f);
dpc.light_data.r = light_data.source_radius() / 1_m;
auto dir =
_renderer.global_uniforms().view_mat * glm::vec4(-light.transform->direction(), 0.f);
......
......@@ -108,6 +108,8 @@ namespace mirrage::renderer {
.color_attachment(depth_sampleable)
.color_attachment(albedo_mat_id)
.color_attachment(mat_data)
.color_attachment(color)
.color_attachment(color_diffuse)
.depth_stencil_attachment(depth);
gpass.configure_subpass(renderer, geometry_pass);
......@@ -120,6 +122,8 @@ namespace mirrage::renderer {
.color_attachment(depth_sampleable)
.color_attachment(albedo_mat_id)
.color_attachment(mat_data)
.color_attachment(color)
.color_attachment(color_diffuse)
.depth_stencil_attachment(depth);
gpass.configure_animation_subpass(renderer, animated_geometry_pass);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment