From 3d5ad2e13146865c35ffe6e4fb9e24f7de95fe38 Mon Sep 17 00:00:00 2001 From: antopilo Date: Sat, 4 Jan 2025 21:39:46 -0500 Subject: [PATCH] Fixed normal TBN matrix --- Editor/src/ComponentsPanel/MaterialEditor.cpp | 8 ++- Resources/Shaders/Vulkan/triangle.frag | 61 +++++++++++++++++-- Resources/Shaders/Vulkan/triangle.vert | 10 ++- 3 files changed, 68 insertions(+), 11 deletions(-) diff --git a/Editor/src/ComponentsPanel/MaterialEditor.cpp b/Editor/src/ComponentsPanel/MaterialEditor.cpp index 22928088..45d52e16 100644 --- a/Editor/src/ComponentsPanel/MaterialEditor.cpp +++ b/Editor/src/ComponentsPanel/MaterialEditor.cpp @@ -89,13 +89,15 @@ void MaterialEditor::Draw(Ref material) { ImGui::BeginChild("##albedo", TexturePanelSize, true); { - uint32_t textureID = 0; + ImTextureID textureID = 0; if (material->HasAlbedo()) { - textureID = material->m_Albedo->GetID(); + auto vkTexture = GPUResources::Get().GetTexture(material->AlbedoImage); + + textureID = (ImTextureID)vkTexture->GetImGuiDescriptorSet(); } - if (ImGui::ImageButtonEx(ImGui::GetCurrentWindow()->GetID("#image1"), (void*)textureID, ImVec2(80, 80), ImVec2(0, 1), ImVec2(1, 0), ImVec4(0, 0, 0, 0), ImVec4(1, 1, 1, 1))) + if (ImGui::ImageButtonEx(ImGui::GetCurrentWindow()->GetID("#image1"), (ImTextureID)textureID, ImVec2(80, 80), ImVec2(0, 1), ImVec2(1, 0), ImVec4(0, 0, 0, 0), ImVec4(1, 1, 1, 1))) { std::string texture = FileDialog::OpenFile("*.png | *.jpg"); if (texture != "") diff --git a/Resources/Shaders/Vulkan/triangle.frag b/Resources/Shaders/Vulkan/triangle.frag index 3a8ba29e..60cece66 100644 --- a/Resources/Shaders/Vulkan/triangle.frag +++ b/Resources/Shaders/Vulkan/triangle.frag @@ -1,5 +1,5 @@ [[vk::binding(0, 3)]] -Texture2D albedo : register(t1); // Texture binding at slot t0 +Texture2D albedo : register(t1); // Texture binding at slot t0 [[vk::binding(0, 4)]] SamplerState mySampler : register(s0); // Sampler binding at slot s0 @@ -24,6 +24,7 @@ struct PSInput { float3 Color : TEXCOORD0; float2 UV : TEXCOORD1; float3 Normal : TEXCOORD2; + float3x3 TBN : TEXCOORD3; }; struct PSOutput { @@ -44,11 +45,59 @@ ModelPushConstant pushConstants; PSOutput main(PSInput input) { PSOutput output; + Material inMaterial = material[pushConstants.materialIndex]; - float3 textureColor = albedo.Sample(mySampler, input.UV); - output.oColor0 = float4(textureColor, 1.0f); - output.oNormal = float4(input.Normal, 1.0f); - output.oMaterial = float4(inMaterial.metalnessValue, inMaterial.aoValue, inMaterial.roughnessValue, 1.0f); - //output.oMaterial = float4(inMaterial.albedo.xyz, 1.0f); + // NORMAL + // TODO use TBN matrix + float3 normal = float3(0.0, 0.0f, 1.0f); + if(inMaterial.hasNormal == 1) + { + // Sample from texture. + } + + normal = mul(input.TBN, normal); + normal = normal / 2.0f + 0.5f; + output.oNormal = float4(normal, 1.0f); + + // MATERIAL + + // ALBEDO COLOR + float4 albedoColor = float4(inMaterial.albedo.xyz, 1.0f); + if(inMaterial.hasAlbedo == 1) + { + float4 albedoTextureSample = albedo.Sample(mySampler, input.UV); + + // Alpha cutout? + if(albedoTextureSample.a < 0.001f) + { + discard; + } + + albedoColor.xyz = albedoTextureSample.xyz; + } + output.oColor0 = albedoColor; + + // MATERIAL PROPERTIES + float metalnessValue = inMaterial.metalnessValue; + if(inMaterial.hasMetalness == 1) + { + // TODO: Sample from metal texture + } + + float aoValue = inMaterial.aoValue; + if(inMaterial.hasAO == 1) + { + // TODO: Sample from AO texture + } + + float roughnessValue = inMaterial.roughnessValue; + if(inMaterial.hasRoughness == 1) + { + // TODO: Sample from roughness texture + } + + float3 materialOuput = float3(inMaterial.metalnessValue, inMaterial.aoValue, inMaterial.roughnessValue); + + output.oMaterial = float4(materialOuput, 1.0f); return output; } \ No newline at end of file diff --git a/Resources/Shaders/Vulkan/triangle.vert b/Resources/Shaders/Vulkan/triangle.vert index 2ba4afbc..a01bd138 100644 --- a/Resources/Shaders/Vulkan/triangle.vert +++ b/Resources/Shaders/Vulkan/triangle.vert @@ -19,8 +19,8 @@ struct Vertex float uv_x; float3 normal; float uv_y; - float4 tangent; - float4 bitangent; + float3 tangent; + float3 bitangent; }; [[vk::binding(0, 2)]] @@ -41,6 +41,7 @@ struct VSOutput { float3 Color : TEXCOORD0; float2 UV : TEXCOORD1; float3 Normal : TEXCOORD2; + float3x3 TBN : TEXCOORD3; }; // Main vertex shader @@ -60,5 +61,10 @@ VSOutput main(uint vertexIndex : SV_VertexID) output.Color = normalize(float3(v.position.xyz)); output.UV = float2(v.uv_x, v.uv_y); output.Normal = normalize(v.normal); + + float3 T = normalize(mul((float3x3)modelData.model, normalize(v.tangent.xyz))); + float3 B = normalize(mul((float3x3)modelData.model, normalize(v.bitangent.xyz))); + float3 N = normalize(mul((float3x3)modelData.model, normalize(v.normal)).xyz); + output.TBN = transpose(float3x3(T, B, N)); return output; } \ No newline at end of file