From 840a1e861fea17905b33c155d1c0d15580dfe17c Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Tue, 15 Feb 2022 09:02:42 -0500 Subject: [PATCH] Add back support for normal textures in material compiler --- engine/renderer/src/materialcompiler.cpp | 25 +++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/engine/renderer/src/materialcompiler.cpp b/engine/renderer/src/materialcompiler.cpp index a2e8c2d..34a97fb 100755 --- a/engine/renderer/src/materialcompiler.cpp +++ b/engine/renderer/src/materialcompiler.cpp @@ -215,7 +215,13 @@ ShaderSource MaterialCompiler::compile_material_fragment(Material& material, boo src += "layout(binding = " + std::to_string(sampler_index++) + ") uniform sampler2D colorTexture;\n"; } - + + if(material.normalProperty.type == DataType::AssetTexture) { + material.bound_textures[sampler_index] = material.normalProperty.value_tex; + + src += "layout(binding = " + std::to_string(sampler_index++) + ") uniform sampler2D normalTexture;\n"; + } + if(use_ibl) { src += "vec3 get_reflect(int i, vec3 final_normal) {\n \ const vec3 direction = normalize(in_frag_pos - scene.camPos.xyz);\n \ @@ -292,7 +298,16 @@ ShaderSource MaterialCompiler::compile_material_fragment(Material& material, boo src += "vec3 final_diffuse_color = from_srgb_to_linear(Color);\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"; + + if(material.normalProperty.type == DataType::AssetTexture) { + prism::log("enabling normal mapping on material.."); + + src += "vec3 final_normal = texture(normalTexture, in_uv).rgb;\n"; + src += "final_normal = final_normal * 2.0 - 1.0;\n"; + src += "final_normal = in_tbn * final_normal;\n"; + } else { + src += "vec3 final_normal = in_normal;\n"; + } src += "ComputedSurfaceInfo surface_info = compute_surface(final_diffuse_color.rgb, final_normal, final_metallic, final_roughness);\n \ @@ -312,7 +327,11 @@ ShaderSource MaterialCompiler::compile_material_fragment(Material& material, boo break;\n \ }\n \ SurfaceBRDF surface_brdf = brdf(light_info.direction, surface_info);\n"; - + + if(render_options.enable_normal_mapping && material.normalProperty.type == DataType::AssetTexture && render_options.enable_normal_shadowing) { + src += std::string("light_info.radiance *= calculate_normal_lighting(normalTexture, final_normal, light_info.direction);\n"); + } + src += "Lo += ((surface_brdf.specular + surface_brdf.diffuse) * light_info.radiance * surface_brdf.NdotL) * scene.lights[i].colorSize.rgb;\n \ }\n";