From 2c75d51278887daad664731bc2fc718459d2ac44 Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Tue, 8 Feb 2022 08:50:10 -0500 Subject: [PATCH] Add parameters for metallic and roughness back This also makes them modifiable in the debug menu at runtime --- engine/asset/include/asset_types.hpp | 3 +++ engine/core/src/debug.cpp | 16 ++++++++++++++++ engine/renderer/src/materialcompiler.cpp | 9 +++++---- engine/renderer/src/renderer.cpp | 5 ++++- engine/shaders/mesh.vert.nocompile.glsl | 2 ++ 5 files changed, 30 insertions(+), 5 deletions(-) diff --git a/engine/asset/include/asset_types.hpp b/engine/asset/include/asset_types.hpp index da85ba4..f900674 100644 --- a/engine/asset/include/asset_types.hpp +++ b/engine/asset/include/asset_types.hpp @@ -43,6 +43,9 @@ class Material : public Asset { public: MaterialProperty colorProperty; + float metallic = 0.0f; + float roughness = 0.5f; + GFXPipeline* static_pipeline = nullptr; GFXPipeline* skinned_pipeline = nullptr; GFXPipeline* capture_pipeline = nullptr; diff --git a/engine/core/src/debug.cpp b/engine/core/src/debug.cpp index d317c77..23037b6 100644 --- a/engine/core/src/debug.cpp +++ b/engine/core/src/debug.cpp @@ -91,6 +91,22 @@ void draw_lighting() { ImGui::Separator(); } + ImGui::Text("Materials"); + ImGui::Separator(); + + for(auto& material : assetm->get_all()) { + ImGui::PushID(material); + + ImGui::TextDisabled("%s", material->path.c_str()); + + ImGui::DragFloat("Metallic", &material->metallic, 0.1f, 0.0f, 1.0f); + ImGui::DragFloat("Roughness", &material->roughness, 0.1f, 0.0f, 1.0f); + + ImGui::PopID(); + + ImGui::Separator(); + } + if(ImGui::Button("Reload shadows")) { engine->get_scene()->reset_shadows(); } diff --git a/engine/renderer/src/materialcompiler.cpp b/engine/renderer/src/materialcompiler.cpp index 3656488..a2e8c2d 100755 --- a/engine/renderer/src/materialcompiler.cpp +++ b/engine/renderer/src/materialcompiler.cpp @@ -201,8 +201,9 @@ ShaderSource MaterialCompiler::compile_material_fragment(Material& material, boo src += "layout(location = 4) in vec4 fragPosLightSpace;\n"; src += "layout(location = 5) in mat3 in_tbn;\n"; src += "layout(location = 14) in vec4 fragPostSpotLightSpace[max_spot_lights];\n"; - - src += "#include \"common.glsl\"\n"; + src += "layout(location = 13) in flat int inMaterialIndex;\n"; + + src += "#include \"common.glsl\"\n"; src += "#include \"rendering.glsl\"\n"; material.bound_textures.clear(); @@ -289,8 +290,8 @@ ShaderSource MaterialCompiler::compile_material_fragment(Material& material, boo } src += "vec3 final_diffuse_color = from_srgb_to_linear(Color);\n"; - src += "float final_roughness = 0.5;\n"; - src += "float final_metallic = 0.0;\n"; + src += "float final_roughness = scene.materials[inMaterialIndex].info.y;\n"; + src += "float final_metallic = scene.materials[inMaterialIndex].info.x;\n"; src += "vec3 final_normal = in_normal;\n"; src += diff --git a/engine/renderer/src/renderer.cpp b/engine/renderer/src/renderer.cpp index 7a58aea..c205215 100755 --- a/engine/renderer/src/renderer.cpp +++ b/engine/renderer/src/renderer.cpp @@ -422,6 +422,9 @@ void renderer::render_camera(GFXCommandBuffer* command_buffer, Scene& scene, Obj create_mesh_pipeline(*material.handle); if(!material_indices.count(material.handle)) { + sceneInfo.materials[numMaterialsInBuffer].info.x = material->metallic; + sceneInfo.materials[numMaterialsInBuffer].info.y = material->roughness; + material_indices[material.handle] = numMaterialsInBuffer++; } } @@ -479,7 +482,7 @@ void renderer::render_camera(GFXCommandBuffer* command_buffer, Scene& scene, Obj command_buffer->bind_texture(texture_to_bind, index); } - command_buffer->draw_indexed(part.index_count, part.index_offset, part.vertex_offset, 0); + command_buffer->draw_indexed(part.index_count, part.index_offset, part.vertex_offset, material_indices.at(*mesh.materials[material_index])); } } diff --git a/engine/shaders/mesh.vert.nocompile.glsl b/engine/shaders/mesh.vert.nocompile.glsl index 17c4506..f0bbaf3 100644 --- a/engine/shaders/mesh.vert.nocompile.glsl +++ b/engine/shaders/mesh.vert.nocompile.glsl @@ -20,6 +20,7 @@ layout (location = 2) out vec2 outUV; layout (location = 4) out vec4 fragPosLightSpace; layout (location = 5) out mat3 outTBN; layout (location = 14) out vec4 fragPostSpotLightSpace[max_spot_lights]; +layout (location = 13) out flat int outMaterialIndex; struct Material { vec4 color, info; @@ -120,4 +121,5 @@ void main() { #endif #endif outTBN = TBN; + outMaterialIndex = gl_BaseInstance; }