From ce0fbc9981d6560601aa19a5a4aeef37d3ec4e87 Mon Sep 17 00:00:00 2001 From: Adrien GIVRY Date: Thu, 23 Nov 2023 14:17:54 -0500 Subject: [PATCH 01/12] Adding a premake project for Overload editor and engine resources This way we can see engine and editor resources, such as shaders, directly in visual studio --- Resources/premake5.lua | 3 +++ Sources/Overload/premake5.lua | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 Resources/premake5.lua diff --git a/Resources/premake5.lua b/Resources/premake5.lua new file mode 100644 index 000000000..347b53fc5 --- /dev/null +++ b/Resources/premake5.lua @@ -0,0 +1,3 @@ +project "Resources" + kind "None" + files { "Editor/**", "Engine/**" } diff --git a/Sources/Overload/premake5.lua b/Sources/Overload/premake5.lua index 65a20524f..6298018e6 100644 --- a/Sources/Overload/premake5.lua +++ b/Sources/Overload/premake5.lua @@ -19,4 +19,6 @@ include "OvUI" include "OvWindowing" include "OvEditor" -include "OvGame" \ No newline at end of file +include "OvGame" + +include "../../Resources/" \ No newline at end of file From 415b5f7e172cf79063d9db01975974044e6cd6dd Mon Sep 17 00:00:00 2001 From: Adrien GIVRY Date: Thu, 23 Nov 2023 15:41:24 -0500 Subject: [PATCH 02/12] Fixed shader reloading and added shader include parsing --- .../ResourceManagement/ShaderManager.cpp | 6 +- .../src/OvEditor/Panels/AssetBrowser.cpp | 2 +- .../Resources/Loaders/ShaderLoader.h | 14 +++- .../include/OvRendering/Resources/Shader.h | 2 - .../Resources/Loaders/ShaderLoader.cpp | 83 ++++++++++++++++--- 5 files changed, 88 insertions(+), 19 deletions(-) diff --git a/Sources/Overload/OvCore/src/OvCore/ResourceManagement/ShaderManager.cpp b/Sources/Overload/OvCore/src/OvCore/ResourceManagement/ShaderManager.cpp index 3e06beff0..546610a3b 100644 --- a/Sources/Overload/OvCore/src/OvCore/ResourceManagement/ShaderManager.cpp +++ b/Sources/Overload/OvCore/src/OvCore/ResourceManagement/ShaderManager.cpp @@ -9,7 +9,8 @@ OvRendering::Resources::Shader* OvCore::ResourceManagement::ShaderManager::CreateResource(const std::string & p_path) { std::string realPath = GetRealPath(p_path); - OvRendering::Resources::Shader* shader = OvRendering::Resources::Loaders::ShaderLoader::Create(realPath); + auto pathParserCallback = std::bind(&OvCore::ResourceManagement::ShaderManager::GetRealPath, this, std::placeholders::_1); + OvRendering::Resources::Shader* shader = OvRendering::Resources::Loaders::ShaderLoader::Create(realPath, pathParserCallback); if (shader) *reinterpret_cast(reinterpret_cast(shader) + offsetof(OvRendering::Resources::Shader, path)) = p_path; // Force the resource path to fit the given path @@ -23,5 +24,6 @@ void OvCore::ResourceManagement::ShaderManager::DestroyResource(OvRendering::Res void OvCore::ResourceManagement::ShaderManager::ReloadResource(OvRendering::Resources::Shader* p_resource, const std::string& p_path) { - OvRendering::Resources::Loaders::ShaderLoader::Recompile(*p_resource, p_path); + auto pathParserCallback = std::bind(&OvCore::ResourceManagement::ShaderManager::GetRealPath, this, std::placeholders::_1); + OvRendering::Resources::Loaders::ShaderLoader::Recompile(*p_resource, p_path, pathParserCallback); } diff --git a/Sources/Overload/OvEditor/src/OvEditor/Panels/AssetBrowser.cpp b/Sources/Overload/OvEditor/src/OvEditor/Panels/AssetBrowser.cpp index 02daf4b58..2fbd76cab 100644 --- a/Sources/Overload/OvEditor/src/OvEditor/Panels/AssetBrowser.cpp +++ b/Sources/Overload/OvEditor/src/OvEditor/Panels/AssetBrowser.cpp @@ -683,7 +683,7 @@ class ShaderContextualMenu : public FileContextualMenu if (shaderManager.IsResourceRegistered(resourcePath)) { /* Trying to recompile */ - OvRendering::Resources::Loaders::ShaderLoader::Recompile(*shaderManager[resourcePath], filePath); + shaderManager.ReloadResource(shaderManager[resourcePath], filePath); } else { diff --git a/Sources/Overload/OvRendering/include/OvRendering/Resources/Loaders/ShaderLoader.h b/Sources/Overload/OvRendering/include/OvRendering/Resources/Loaders/ShaderLoader.h index 5aeb9607c..f07f2a1bd 100644 --- a/Sources/Overload/OvRendering/include/OvRendering/Resources/Loaders/ShaderLoader.h +++ b/Sources/Overload/OvRendering/include/OvRendering/Resources/Loaders/ShaderLoader.h @@ -6,6 +6,8 @@ #pragma once +#include + #include "OvRendering/Resources/Shader.h" namespace OvRendering::Resources::Loaders @@ -16,6 +18,8 @@ namespace OvRendering::Resources::Loaders class ShaderLoader { public: + using FilePathParserCallback = std::function; + /** * Disabled constructor */ @@ -24,13 +28,15 @@ namespace OvRendering::Resources::Loaders /** * Create a shader * @param p_filePath + * @param p_pathParser */ - static Shader* Create(const std::string& p_filePath); + static Shader* Create(const std::string& p_filePath, FilePathParserCallback p_pathParser = nullptr); /** * Create a shader from source * @param p_vertexShader * @param p_fragmentShader + * @note Doesn't support path parsing/resolving */ static Shader* CreateFromSource(const std::string& p_vertexShader, const std::string& p_fragmentShader); @@ -39,7 +45,7 @@ namespace OvRendering::Resources::Loaders * @param p_shader * @param p_filePath */ - static void Recompile(Shader& p_shader, const std::string& p_filePath); + static void Recompile(Shader& p_shader, const std::string& p_filePath, FilePathParserCallback p_pathParser = nullptr); /** * Destroy a shader @@ -48,7 +54,9 @@ namespace OvRendering::Resources::Loaders static bool Destroy(Shader*& p_shader); private: - static std::pair ParseShader(const std::string& p_filePath); + static bool ParseIncludeDirective(const std::string& line, std::string& includeFilePath); + static std::string LoadShader(const std::string& p_filePath, FilePathParserCallback p_pathParser); + static std::pair ParseShader(const std::string& p_filePath, FilePathParserCallback p_pathParser); static uint32_t CreateProgram(const std::string& p_vertexShader, const std::string& p_fragmentShader); static uint32_t CompileShader(uint32_t p_type, const std::string& p_source); diff --git a/Sources/Overload/OvRendering/include/OvRendering/Resources/Shader.h b/Sources/Overload/OvRendering/include/OvRendering/Resources/Shader.h index 5ddd44fec..8d309083f 100644 --- a/Sources/Overload/OvRendering/include/OvRendering/Resources/Shader.h +++ b/Sources/Overload/OvRendering/include/OvRendering/Resources/Shader.h @@ -15,8 +15,6 @@ #include "OvRendering/Resources/UniformInfo.h" - - namespace OvRendering::Resources { namespace Loaders { class ShaderLoader; } diff --git a/Sources/Overload/OvRendering/src/OvRendering/Resources/Loaders/ShaderLoader.cpp b/Sources/Overload/OvRendering/src/OvRendering/Resources/Loaders/ShaderLoader.cpp index 5c25dab9a..429a51475 100644 --- a/Sources/Overload/OvRendering/src/OvRendering/Resources/Loaders/ShaderLoader.cpp +++ b/Sources/Overload/OvRendering/src/OvRendering/Resources/Loaders/ShaderLoader.cpp @@ -15,11 +15,11 @@ std::string OvRendering::Resources::Loaders::ShaderLoader::__FILE_TRACE; -OvRendering::Resources::Shader* OvRendering::Resources::Loaders::ShaderLoader::Create(const std::string& p_filePath) +OvRendering::Resources::Shader* OvRendering::Resources::Loaders::ShaderLoader::Create(const std::string& p_filePath, FilePathParserCallback p_pathParser) { __FILE_TRACE = p_filePath; - std::pair source = ParseShader(p_filePath); + std::pair source = ParseShader(p_filePath, p_pathParser); uint32_t programID = CreateProgram(source.first, source.second); @@ -39,11 +39,11 @@ OvRendering::Resources::Shader* OvRendering::Resources::Loaders::ShaderLoader::C return nullptr; } -void OvRendering::Resources::Loaders::ShaderLoader::Recompile(Shader& p_shader, const std::string& p_filePath) +void OvRendering::Resources::Loaders::ShaderLoader::Recompile(Shader& p_shader, const std::string& p_filePath, FilePathParserCallback p_pathParser) { __FILE_TRACE = p_filePath; - std::pair source = ParseShader(p_filePath); + std::pair source = ParseShader(p_filePath, p_pathParser); /* Create the new program */ uint32_t newProgram = CreateProgram(source.first, source.second); @@ -82,24 +82,85 @@ bool OvRendering::Resources::Loaders::ShaderLoader::Destroy(Shader*& p_shader) return false; } -std::pair OvRendering::Resources::Loaders::ShaderLoader::ParseShader(const std::string& p_filePath) +bool OvRendering::Resources::Loaders::ShaderLoader::ParseIncludeDirective(const std::string& line, std::string& includeFilePath) { - std::ifstream stream(p_filePath); + // Find the position of the opening and closing quotes + size_t start = line.find("\""); + size_t end = line.find("\"", start + 1); - enum class ShaderType { NONE = -1, VERTEX = 0, FRAGMENT = 1 }; + // Check if both quotes are found + if (start != std::string::npos && end != std::string::npos && end > start) + { + // Extract the included file path + includeFilePath = line.substr(start + 1, end - start - 1); + return true; + } + else + { + return false; + } +} + +std::string OvRendering::Resources::Loaders::ShaderLoader::LoadShader(const std::string& p_filePath, FilePathParserCallback p_pathParser) +{ + std::ifstream file(p_filePath); + if (!file.is_open()) + { + OVLOG_ERROR("Error: Could not open shader file - " + p_filePath); + return ""; + } + + std::stringstream buffer; std::string line; + while (std::getline(file, line)) + { + if (line.find("#include") != std::string::npos) + { + // If the line contains #include, process the included file + std::string includeFilePath; + if (ParseIncludeDirective(line, includeFilePath)) + { + // Recursively load the included file + const std::string realIncludeFilePath = p_pathParser ? p_pathParser(includeFilePath) : includeFilePath; + std::string includedShader = LoadShader(realIncludeFilePath, p_pathParser); + buffer << includedShader << std::endl; + } + else + { + OVLOG_ERROR("Error: Invalid #include directive in file - " + p_filePath); + } + } + else { + // If the line does not contain #include, just append it to the buffer + buffer << line << std::endl; + } + } + + return buffer.str(); +} + +std::pair OvRendering::Resources::Loaders::ShaderLoader::ParseShader(const std::string& p_filePath, FilePathParserCallback p_pathParser) +{ + const std::string shaderCode = LoadShader(p_filePath, p_pathParser); + + std::istringstream stream(shaderCode); // Add this line to create a stringstream from shaderCode + std::string line; std::stringstream ss[2]; + enum class ShaderType { NONE = -1, VERTEX = 0, FRAGMENT = 1 }; + ShaderType type = ShaderType::NONE; while (std::getline(stream, line)) { if (line.find("#shader") != std::string::npos) { - if (line.find("vertex") != std::string::npos) type = ShaderType::VERTEX; - else if (line.find("fragment") != std::string::npos) type = ShaderType::FRAGMENT; + if (line.find("vertex") != std::string::npos) + type = ShaderType::VERTEX; + else if (line.find("fragment") != std::string::npos) + type = ShaderType::FRAGMENT; } else if (type != ShaderType::NONE) { @@ -107,8 +168,8 @@ std::pair OvRendering::Resources::Loaders::ShaderLoade } } - return - { + return + { ss[static_cast(ShaderType::VERTEX)].str(), ss[static_cast(ShaderType::FRAGMENT)].str() }; From 4dcbc593dd73940b8ec9c3a754bfd1b0f2aa4b9b Mon Sep 17 00:00:00 2001 From: Adrien GIVRY Date: Thu, 23 Nov 2023 15:41:53 -0500 Subject: [PATCH 03/12] Updated premake files to include lua files (and therefore premake5.lua) --- Sources/Overload/OvAnalytics/premake5.lua | 2 +- Sources/Overload/OvAudio/premake5.lua | 2 +- Sources/Overload/OvCore/premake5.lua | 2 +- Sources/Overload/OvDebug/premake5.lua | 2 +- Sources/Overload/OvEditor/premake5.lua | 4 +++- Sources/Overload/OvGame/premake5.lua | 2 +- Sources/Overload/OvMaths/premake5.lua | 2 +- Sources/Overload/OvPhysics/premake5.lua | 2 +- Sources/Overload/OvRendering/premake5.lua | 2 +- Sources/Overload/OvTools/premake5.lua | 2 +- Sources/Overload/OvUI/premake5.lua | 2 +- Sources/Overload/OvWindowing/premake5.lua | 2 +- 12 files changed, 14 insertions(+), 12 deletions(-) diff --git a/Sources/Overload/OvAnalytics/premake5.lua b/Sources/Overload/OvAnalytics/premake5.lua index 3aaf84372..ed0b17f86 100644 --- a/Sources/Overload/OvAnalytics/premake5.lua +++ b/Sources/Overload/OvAnalytics/premake5.lua @@ -2,7 +2,7 @@ project "OvAnalytics" kind "StaticLib" language "C++" cppdialect "C++17" - files { "**.h", "**.inl", "**.cpp" } + files { "**.h", "**.inl", "**.cpp", "**.lua" } includedirs { "include" } targetdir (outputdir .. "%{cfg.buildcfg}/%{prj.name}") objdir (objoutdir .. "%{cfg.buildcfg}/%{prj.name}") diff --git a/Sources/Overload/OvAudio/premake5.lua b/Sources/Overload/OvAudio/premake5.lua index 8c80333c9..8c9976e49 100644 --- a/Sources/Overload/OvAudio/premake5.lua +++ b/Sources/Overload/OvAudio/premake5.lua @@ -2,7 +2,7 @@ project "OvAudio" kind "StaticLib" language "C++" cppdialect "C++17" - files { "**.h", "**.inl", "**.cpp" } + files { "**.h", "**.inl", "**.cpp", "**.lua" } includedirs { "include", dependdir .. "irrklang/include", "%{wks.location}/OvDebug/include", "%{wks.location}/OvMaths/include", "%{wks.location}/OvTools/include" } targetdir (outputdir .. "%{cfg.buildcfg}/%{prj.name}") objdir (objoutdir .. "%{cfg.buildcfg}/%{prj.name}") diff --git a/Sources/Overload/OvCore/premake5.lua b/Sources/Overload/OvCore/premake5.lua index 3c8bd69b9..8f749ff91 100644 --- a/Sources/Overload/OvCore/premake5.lua +++ b/Sources/Overload/OvCore/premake5.lua @@ -2,7 +2,7 @@ project "OvCore" kind "StaticLib" language "C++" cppdialect "C++17" - files { "**.h", "**.inl", "**.cpp" } + files { "**.h", "**.inl", "**.cpp", "**.lua" } includedirs { "include", dependdir .. "glfw/include", dependdir .. "stb_image/include", dependdir .. "lua/include", dependdir .. "bullet3/include", dependdir .. "glew/include", dependdir .. "irrklang/include", "%{wks.location}/OvAnalytics/include", "%{wks.location}/OvAudio/include", "%{wks.location}/OvDebug/include", "%{wks.location}/OvMaths/include", "%{wks.location}/OvPhysics/include", "%{wks.location}/OvRendering/include", "%{wks.location}/OvTools/include", "%{wks.location}/OvUI/include", "%{wks.location}/OvWindowing/include" } diff --git a/Sources/Overload/OvDebug/premake5.lua b/Sources/Overload/OvDebug/premake5.lua index f58419998..c599b58c5 100644 --- a/Sources/Overload/OvDebug/premake5.lua +++ b/Sources/Overload/OvDebug/premake5.lua @@ -2,7 +2,7 @@ project "OvDebug" kind "StaticLib" language "C++" cppdialect "C++17" - files { "**.h", "**.inl", "**.cpp" } + files { "**.h", "**.inl", "**.cpp", "**.lua" } includedirs { "include", "%{wks.location}/OvTools/include" } targetdir (outputdir .. "%{cfg.buildcfg}/%{prj.name}") objdir (objoutdir .. "%{cfg.buildcfg}/%{prj.name}") diff --git a/Sources/Overload/OvEditor/premake5.lua b/Sources/Overload/OvEditor/premake5.lua index 2722061c4..e19d93115 100644 --- a/Sources/Overload/OvEditor/premake5.lua +++ b/Sources/Overload/OvEditor/premake5.lua @@ -1,7 +1,7 @@ project "OvEditor" language "C++" cppdialect "C++17" - files { "**.h", "**.inl","**.cpp", "**.rc" } + files { "**.h", "**.inl","**.cpp", "**.lua", "**.rc" } includedirs { "include", dependdir .. "glfw/include", dependdir .. "stb_image/include", dependdir .. "lua/include", dependdir .. "bullet3/include", dependdir .. "glew/include", dependdir .. "irrklang/include", "%{wks.location}/OvAnalytics/include", "%{wks.location}/OvAudio/include", "%{wks.location}/OvCore/include", "%{wks.location}/OvDebug/include", "%{wks.location}/OvMaths/include", "%{wks.location}/OvPhysics/include", @@ -19,6 +19,8 @@ project "OvEditor" postbuildcommands { "for /f \"delims=|\" %%i in ('dir /B /S \"%{wks.location}..\\..\\Dependencies\\*.dll\"') do xcopy /Q /Y \"%%i\" \"%{wks.location}..\\..\\Bin\\%{cfg.buildcfg}\\%{prj.name}\"", + "rmdir /s /q \"%{wks.location}\\..\\..\\Build\\%{cfg.buildcfg}\\Data\"", + "xcopy \"%{wks.location}\\..\\..\\Resources\\Engine\\*\" \"%{wks.location}\\..\\..\\Build\\%{cfg.buildcfg}\\Data\\Engine\" /y /i /r /e /q", "xcopy \"%{wks.location}\\..\\..\\Resources\\Editor\\*\" \"%{wks.location}\\..\\..\\Build\\%{cfg.buildcfg}\\Data\\Editor\" /y /i /r /e /q", "xcopy \"%{prj.location}\\Layout.ini\" \"%{wks.location}\\..\\..\\Build\\%{cfg.buildcfg}\\Config\\\" /y /i", diff --git a/Sources/Overload/OvGame/premake5.lua b/Sources/Overload/OvGame/premake5.lua index 4ac3a9cc2..0cdd922dd 100644 --- a/Sources/Overload/OvGame/premake5.lua +++ b/Sources/Overload/OvGame/premake5.lua @@ -1,7 +1,7 @@ project "OvGame" language "C++" cppdialect "C++17" - files { "**.h", "**.inl","**.cpp", "**.rc" } + files { "**.h", "**.inl","**.cpp", "**.lua", "**.rc" } includedirs { "include", dependdir .. "glfw/include", dependdir .. "stb_image/include", dependdir .. "lua/include", dependdir .. "bullet3/include", dependdir .. "glew/include", dependdir .. "irrklang/include", "%{wks.location}/OvAnalytics/include", "%{wks.location}/OvAudio/include", "%{wks.location}/OvCore/include", "%{wks.location}/OvDebug/include", "%{wks.location}/OvMaths/include", "%{wks.location}/OvPhysics/include", diff --git a/Sources/Overload/OvMaths/premake5.lua b/Sources/Overload/OvMaths/premake5.lua index 1d646ff85..aa14650a4 100644 --- a/Sources/Overload/OvMaths/premake5.lua +++ b/Sources/Overload/OvMaths/premake5.lua @@ -2,7 +2,7 @@ project "OvMaths" kind "StaticLib" language "C++" cppdialect "C++17" - files { "**.h", "**.inl", "**.cpp" } + files { "**.h", "**.inl", "**.cpp", "**.lua" } includedirs { "include" } targetdir (outputdir .. "%{cfg.buildcfg}/%{prj.name}") objdir (objoutdir .. "%{cfg.buildcfg}/%{prj.name}") diff --git a/Sources/Overload/OvPhysics/premake5.lua b/Sources/Overload/OvPhysics/premake5.lua index f04bdc870..f538013cb 100644 --- a/Sources/Overload/OvPhysics/premake5.lua +++ b/Sources/Overload/OvPhysics/premake5.lua @@ -2,7 +2,7 @@ project "OvPhysics" kind "StaticLib" language "C++" cppdialect "C++17" - files { "**.h", "**.inl", "**.cpp" } + files { "**.h", "**.inl", "**.cpp", "**.lua" } includedirs { "include", dependdir .. "bullet3/include", "%{wks.location}/OvDebug/include", "%{wks.location}/OvMaths/include", "%{wks.location}/OvTools/include" } targetdir (outputdir .. "%{cfg.buildcfg}/%{prj.name}") objdir (objoutdir .. "%{cfg.buildcfg}/%{prj.name}") diff --git a/Sources/Overload/OvRendering/premake5.lua b/Sources/Overload/OvRendering/premake5.lua index ab2012175..cc1f0aaa7 100644 --- a/Sources/Overload/OvRendering/premake5.lua +++ b/Sources/Overload/OvRendering/premake5.lua @@ -2,7 +2,7 @@ project "OvRendering" kind "StaticLib" language "C++" cppdialect "C++17" - files { "**.h", "**.inl", "**.cpp" } + files { "**.h", "**.inl", "**.cpp", "**.lua" } includedirs { "include", dependdir .. "glew/include", dependdir .. "stb_image/include", dependdir .. "assimp/include", "%{wks.location}/OvDebug/include", "%{wks.location}/OvMaths/include", "%{wks.location}/OvTools/include" } targetdir (outputdir .. "%{cfg.buildcfg}/%{prj.name}") objdir (objoutdir .. "%{cfg.buildcfg}/%{prj.name}") diff --git a/Sources/Overload/OvTools/premake5.lua b/Sources/Overload/OvTools/premake5.lua index f31b1e9c9..f6631c31e 100644 --- a/Sources/Overload/OvTools/premake5.lua +++ b/Sources/Overload/OvTools/premake5.lua @@ -2,7 +2,7 @@ project "OvTools" kind "StaticLib" language "C++" cppdialect "C++17" - files { "**.h", "**.inl", "**.cpp" } + files { "**.h", "**.inl", "**.cpp", "**.lua" } includedirs { "include" } targetdir (outputdir .. "%{cfg.buildcfg}/%{prj.name}") objdir (objoutdir .. "%{cfg.buildcfg}/%{prj.name}") diff --git a/Sources/Overload/OvUI/premake5.lua b/Sources/Overload/OvUI/premake5.lua index d1e6c1212..f826271ec 100644 --- a/Sources/Overload/OvUI/premake5.lua +++ b/Sources/Overload/OvUI/premake5.lua @@ -2,7 +2,7 @@ project "OvUI" kind "StaticLib" language "C++" cppdialect "C++17" - files { "**.h", "**.inl", "**.cpp" } + files { "**.h", "**.inl", "**.cpp", "**.lua" } includedirs { "include", dependdir .. "glfw/include", dependdir .. "glew/include", "%{wks.location}/OvMaths/include", "%{wks.location}/OvTools/include" } targetdir (outputdir .. "%{cfg.buildcfg}/%{prj.name}") objdir (objoutdir .. "%{cfg.buildcfg}/%{prj.name}") diff --git a/Sources/Overload/OvWindowing/premake5.lua b/Sources/Overload/OvWindowing/premake5.lua index f4d1a7fbf..0fb15a7f3 100644 --- a/Sources/Overload/OvWindowing/premake5.lua +++ b/Sources/Overload/OvWindowing/premake5.lua @@ -2,7 +2,7 @@ project "OvWindowing" kind "StaticLib" language "C++" cppdialect "C++17" - files { "**.h", "**.inl", "**.cpp" } + files { "**.h", "**.inl", "**.cpp", "**.lua" } includedirs { "include", dependdir .. "glfw/include", dependdir .. "stb_image/include", "%{wks.location}/OvTools/include" } targetdir (outputdir .. "%{cfg.buildcfg}/%{prj.name}") objdir (objoutdir .. "%{cfg.buildcfg}/%{prj.name}") From 1467fd202b973ebddb927d5f9499e54a357d3468 Mon Sep 17 00:00:00 2001 From: Adrien GIVRY Date: Thu, 23 Nov 2023 16:48:58 -0500 Subject: [PATCH 04/12] Added variable for common paths to premake files Also changing the project kind for "Resources" from "None" to "SharedItems" --- Resources/premake5.lua | 4 ++-- Sources/Overload/OvEditor/premake5.lua | 22 +++++++++++----------- Sources/Overload/OvGame/premake5.lua | 10 +++++----- Sources/Overload/premake5.lua | 4 +++- 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/Resources/premake5.lua b/Resources/premake5.lua index 347b53fc5..863731465 100644 --- a/Resources/premake5.lua +++ b/Resources/premake5.lua @@ -1,3 +1,3 @@ project "Resources" - kind "None" - files { "Editor/**", "Engine/**" } + kind "SharedItems" + files { "Editor/**", "Engine/**", "**.lua" } diff --git a/Sources/Overload/OvEditor/premake5.lua b/Sources/Overload/OvEditor/premake5.lua index e19d93115..e72fe60f0 100644 --- a/Sources/Overload/OvEditor/premake5.lua +++ b/Sources/Overload/OvEditor/premake5.lua @@ -17,21 +17,21 @@ project "OvEditor" debugdir "%{wks.location}/../../Build/%{cfg.buildcfg}" postbuildcommands { - "for /f \"delims=|\" %%i in ('dir /B /S \"%{wks.location}..\\..\\Dependencies\\*.dll\"') do xcopy /Q /Y \"%%i\" \"%{wks.location}..\\..\\Bin\\%{cfg.buildcfg}\\%{prj.name}\"", + "for /f \"delims=|\" %%i in ('dir /B /S \"%{dependdir}*.dll\"') do xcopy /Q /Y \"%%i\" \"%{outputdir}%{cfg.buildcfg}\\%{prj.name}\"", - "rmdir /s /q \"%{wks.location}\\..\\..\\Build\\%{cfg.buildcfg}\\Data\"", + "rmdir /s /q \"%{builddir}%{cfg.buildcfg}\\Data\"", - "xcopy \"%{wks.location}\\..\\..\\Resources\\Engine\\*\" \"%{wks.location}\\..\\..\\Build\\%{cfg.buildcfg}\\Data\\Engine\" /y /i /r /e /q", - "xcopy \"%{wks.location}\\..\\..\\Resources\\Editor\\*\" \"%{wks.location}\\..\\..\\Build\\%{cfg.buildcfg}\\Data\\Editor\" /y /i /r /e /q", - "xcopy \"%{prj.location}\\Layout.ini\" \"%{wks.location}\\..\\..\\Build\\%{cfg.buildcfg}\\Config\\\" /y /i", + "xcopy \"%{resdir}Engine\\*\" \"%{builddir}%{cfg.buildcfg}\\Data\\Engine\" /y /i /r /e /q", + "xcopy \"%{resdir}Editor\\*\" \"%{builddir}%{cfg.buildcfg}\\Data\\Editor\" /y /i /r /e /q", + "xcopy \"%{prj.location}\\Layout.ini\" \"%{builddir}%{cfg.buildcfg}\\Config\\\" /y /i", - "xcopy /Y /I /Q /D \"%{wks.location}\\..\\..\\Bin\\Debug\\%{prj.name}\\*.exe\" \"%{wks.location}\\..\\..\\Build\\%{cfg.buildcfg}\\\"", - "xcopy /Y /I /Q /D \"%{wks.location}\\..\\..\\Bin\\Debug\\%{prj.name}\\*.dll\" \"%{wks.location}\\..\\..\\Build\\%{cfg.buildcfg}\\\"", - "xcopy /Y /I /Q /D \"%{wks.location}\\..\\..\\Bin\\Release\\%{prj.name}\\*.exe\" \"%{wks.location}\\..\\..\\Build\\%{cfg.buildcfg}\\\"", - "xcopy /Y /I /Q /D \"%{wks.location}\\..\\..\\Bin\\Release\\%{prj.name}\\*.dll\" \"%{wks.location}\\..\\..\\Build\\%{cfg.buildcfg}\\\"", + "xcopy /Y /I /Q /D \"%{outputdir}Debug\\%{prj.name}\\*.exe\" \"%{builddir}%{cfg.buildcfg}\\\"", + "xcopy /Y /I /Q /D \"%{outputdir}Debug\\%{prj.name}\\*.dll\" \"%{builddir}%{cfg.buildcfg}\\\"", + "xcopy /Y /I /Q /D \"%{outputdir}Release\\%{prj.name}\\*.exe\" \"%{builddir}%{cfg.buildcfg}\\\"", + "xcopy /Y /I /Q /D \"%{outputdir}Release\\%{prj.name}\\*.dll\" \"%{builddir}%{cfg.buildcfg}\\\"", - "xcopy \"%{wks.location}..\\..\\Bin\\Debug\\OvGame\\*.exe\" \"%{wks.location}..\\..\\Build\\%{cfg.buildcfg}\\Builder\\Development\" /y /i /c", - "xcopy \"%{wks.location}..\\..\\Bin\\Release\\OvGame\\*.exe\" \"%{wks.location}..\\..\\Build\\%{cfg.buildcfg}\\Builder\\Shipping\" /y /i /c", + "xcopy \"%{outputdir}Debug\\OvGame\\*.exe\" \"%{builddir}%{cfg.buildcfg}\\Builder\\Development\" /y /i /c", + "xcopy \"%{outputdir}Release\\OvGame\\*.exe\" \"%{builddir}%{cfg.buildcfg}\\Builder\\Shipping\" /y /i /c", "EXIT /B 0" } diff --git a/Sources/Overload/OvGame/premake5.lua b/Sources/Overload/OvGame/premake5.lua index 0cdd922dd..953fd0753 100644 --- a/Sources/Overload/OvGame/premake5.lua +++ b/Sources/Overload/OvGame/premake5.lua @@ -16,12 +16,12 @@ project "OvGame" characterset ("MBCS") postbuildcommands { - "for /f \"delims=|\" %%i in ('dir /B /S \"%{wks.location}..\\..\\Dependencies\\*.dll\"') do xcopy /Q /Y \"%%i\" \"%{wks.location}..\\..\\Bin\\%{cfg.buildcfg}\\%{prj.name}\"", + "for /f \"delims=|\" %%i in ('dir /B /S \"%{dependdir}*.dll\"') do xcopy /Q /Y \"%%i\" \"%{builddir}%{cfg.buildcfg}\\%{prj.name}\"", - "xcopy /Y /I /Q /D \"%{wks.location}\\..\\..\\Bin\\Debug\\%{prj.name}\\*.exe\" \"%{wks.location}\\..\\..\\Build\\%{cfg.buildcfg}\\Builder\\Development\"", - "xcopy /Y /I /Q /D \"%{wks.location}\\..\\..\\Bin\\Debug\\%{prj.name}\\*.dll\" \"%{wks.location}\\..\\..\\Build\\%{cfg.buildcfg}\\Builder\\Development\"", - "xcopy /Y /I /Q /D \"%{wks.location}\\..\\..\\Bin\\Release\\%{prj.name}\\*.exe\" \"%{wks.location}\\..\\..\\Build\\%{cfg.buildcfg}\\Builder\\Shipping\"", - "xcopy /Y /I /Q /D \"%{wks.location}\\..\\..\\Bin\\Release\\%{prj.name}\\*.dll\" \"%{wks.location}\\..\\..\\Build\\%{cfg.buildcfg}\\Builder\\Shipping\"", + "xcopy /Y /I /Q /D \"%{outputdir}Debug\\%{prj.name}\\*.exe\" \"%{builddir}%{cfg.buildcfg}\\Builder\\Development\"", + "xcopy /Y /I /Q /D \"%{outputdir}Debug\\%{prj.name}\\*.dll\" \"%{builddir}%{cfg.buildcfg}\\Builder\\Development\"", + "xcopy /Y /I /Q /D \"%{outputdir}Release\\%{prj.name}\\*.exe\" \"%{builddir}%{cfg.buildcfg}\\Builder\\Shipping\"", + "xcopy /Y /I /Q /D \"%{outputdir}Release\\%{prj.name}\\*.dll\" \"%{builddir}%{cfg.buildcfg}\\Builder\\Shipping\"", "EXIT /B 0" } diff --git a/Sources/Overload/premake5.lua b/Sources/Overload/premake5.lua index 6298018e6..dbac74c4b 100644 --- a/Sources/Overload/premake5.lua +++ b/Sources/Overload/premake5.lua @@ -6,6 +6,8 @@ workspace "Overload" outputdir = "%{wks.location}/../../Bin/" objoutdir = "%{wks.location}/../../Bin-Int/" dependdir = "%{wks.location}/../../Dependencies/" +builddir = "%{wks.location}/../../Build/" +resdir = "%{wks.location}/../../Resources/" include "OvAnalytics" include "OvAudio" @@ -21,4 +23,4 @@ include "OvWindowing" include "OvEditor" include "OvGame" -include "../../Resources/" \ No newline at end of file +include "../../Resources" From 689b6c4f2010c9b0c93861264a15303c9de7cf12 Mon Sep 17 00:00:00 2001 From: Adrien GIVRY Date: Thu, 23 Nov 2023 16:49:18 -0500 Subject: [PATCH 05/12] Updated gitignore to exclude vcxitems (From SharedItems) --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index d2bcfb5cf..ae0d3acdf 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ Build/ imgui.ini *.aps *.vcxproj* -*.sln \ No newline at end of file +*.sln +*.vcxitems* \ No newline at end of file From db10d4e3528c08220bcefb541a4d1d841bf7d56a Mon Sep 17 00:00:00 2001 From: Adrien GIVRY Date: Thu, 23 Nov 2023 16:50:14 -0500 Subject: [PATCH 06/12] Splitting engine shader sources into multiple files --- .../Shaders/Common/Buffers/EngineUBO.glsl | 9 + .../Shaders/Common/Buffers/LightsSSBO.glsl | 4 + .../Shaders/Common/IO/FragmentInput.glsl | 9 + .../Shaders/Common/IO/VertexOutput.glsl | 9 + .../Engine/Shaders/Fragment/BlinnPhong.glsl | 184 +++++++++++ .../Engine/Shaders/Fragment/Lambert.glsl | 26 ++ Resources/Engine/Shaders/Fragment/PBR.glsl | 220 +++++++++++++ Resources/Engine/Shaders/Fragment/Unlit.glsl | 23 ++ Resources/Engine/Shaders/Lambert.glsl | 64 +--- Resources/Engine/Shaders/Standard.glsl | 258 +-------------- Resources/Engine/Shaders/StandardPBR.glsl | 294 +----------------- Resources/Engine/Shaders/Unlit.glsl | 47 +-- Resources/Engine/Shaders/Vertex/Basic.glsl | 30 ++ 13 files changed, 522 insertions(+), 655 deletions(-) create mode 100644 Resources/Engine/Shaders/Common/Buffers/EngineUBO.glsl create mode 100644 Resources/Engine/Shaders/Common/Buffers/LightsSSBO.glsl create mode 100644 Resources/Engine/Shaders/Common/IO/FragmentInput.glsl create mode 100644 Resources/Engine/Shaders/Common/IO/VertexOutput.glsl create mode 100644 Resources/Engine/Shaders/Fragment/BlinnPhong.glsl create mode 100644 Resources/Engine/Shaders/Fragment/Lambert.glsl create mode 100644 Resources/Engine/Shaders/Fragment/PBR.glsl create mode 100644 Resources/Engine/Shaders/Fragment/Unlit.glsl create mode 100644 Resources/Engine/Shaders/Vertex/Basic.glsl diff --git a/Resources/Engine/Shaders/Common/Buffers/EngineUBO.glsl b/Resources/Engine/Shaders/Common/Buffers/EngineUBO.glsl new file mode 100644 index 000000000..fd9f776b9 --- /dev/null +++ b/Resources/Engine/Shaders/Common/Buffers/EngineUBO.glsl @@ -0,0 +1,9 @@ +layout (std140) uniform EngineUBO +{ + mat4 ubo_Model; + mat4 ubo_View; + mat4 ubo_Projection; + vec3 ubo_ViewPos; + float ubo_Time; + mat4 ubo_UserMatrix; +}; diff --git a/Resources/Engine/Shaders/Common/Buffers/LightsSSBO.glsl b/Resources/Engine/Shaders/Common/Buffers/LightsSSBO.glsl new file mode 100644 index 000000000..77e949467 --- /dev/null +++ b/Resources/Engine/Shaders/Common/Buffers/LightsSSBO.glsl @@ -0,0 +1,4 @@ +layout(std430, binding = 0) buffer LightSSBO +{ + mat4 ssbo_Lights[]; +}; diff --git a/Resources/Engine/Shaders/Common/IO/FragmentInput.glsl b/Resources/Engine/Shaders/Common/IO/FragmentInput.glsl new file mode 100644 index 000000000..a1e3a97ef --- /dev/null +++ b/Resources/Engine/Shaders/Common/IO/FragmentInput.glsl @@ -0,0 +1,9 @@ +in VS_OUT +{ + vec3 FragPos; + vec3 Normal; + vec2 TexCoords; + mat3 TBN; + flat vec3 TangentViewPos; + vec3 TangentFragPos; +} fs_in; \ No newline at end of file diff --git a/Resources/Engine/Shaders/Common/IO/VertexOutput.glsl b/Resources/Engine/Shaders/Common/IO/VertexOutput.glsl new file mode 100644 index 000000000..3834539be --- /dev/null +++ b/Resources/Engine/Shaders/Common/IO/VertexOutput.glsl @@ -0,0 +1,9 @@ +out VS_OUT +{ + vec3 FragPos; + vec3 Normal; + vec2 TexCoords; + mat3 TBN; + flat vec3 TangentViewPos; + vec3 TangentFragPos; +} vs_out; \ No newline at end of file diff --git a/Resources/Engine/Shaders/Fragment/BlinnPhong.glsl b/Resources/Engine/Shaders/Fragment/BlinnPhong.glsl new file mode 100644 index 000000000..b5e54b088 --- /dev/null +++ b/Resources/Engine/Shaders/Fragment/BlinnPhong.glsl @@ -0,0 +1,184 @@ +#version 430 core + +#include ":Shaders/Common/IO/FragmentInput.glsl" +#include ":Shaders/Common/Buffers/EngineUBO.glsl" +#include ":Shaders/Common/Buffers/LightsSSBO.glsl" + +/* Uniforms (Tweakable from the material editor) */ +uniform vec2 u_TextureTiling = vec2(1.0, 1.0); +uniform vec2 u_TextureOffset = vec2(0.0, 0.0); +uniform vec4 u_Diffuse = vec4(1.0, 1.0, 1.0, 1.0); +uniform vec3 u_Specular = vec3(1.0, 1.0, 1.0); +uniform float u_Shininess = 100.0; +uniform float u_HeightScale = 0.0; +uniform bool u_EnableNormalMapping = false; +uniform sampler2D u_DiffuseMap; +uniform sampler2D u_SpecularMap; +uniform sampler2D u_NormalMap; +uniform sampler2D u_HeightMap; +uniform sampler2D u_MaskMap; + +/* Global variables */ +vec3 g_Normal; +vec2 g_TexCoords; +vec3 g_ViewDir; +vec4 g_DiffuseTexel; +vec4 g_SpecularTexel; +vec4 g_HeightTexel; +vec4 g_NormalTexel; + +out vec4 FRAGMENT_COLOR; + +vec3 UnPack(float p_Target) +{ + return vec3 + ( + float((uint(p_Target) >> 24) & 0xff) * 0.003921568627451, + float((uint(p_Target) >> 16) & 0xff) * 0.003921568627451, + float((uint(p_Target) >> 8) & 0xff) * 0.003921568627451 + ); +} + +bool PointInAABB(vec3 p_Point, vec3 p_AabbCenter, vec3 p_AabbHalfSize) +{ + return + ( + p_Point.x > p_AabbCenter.x - p_AabbHalfSize.x && p_Point.x < p_AabbCenter.x + p_AabbHalfSize.x && + p_Point.y > p_AabbCenter.y - p_AabbHalfSize.y && p_Point.y < p_AabbCenter.y + p_AabbHalfSize.y && + p_Point.z > p_AabbCenter.z - p_AabbHalfSize.z && p_Point.z < p_AabbCenter.z + p_AabbHalfSize.z + ); +} + +vec2 ParallaxMapping(vec3 p_ViewDir) +{ + const vec2 parallax = p_ViewDir.xy * u_HeightScale * texture(u_HeightMap, g_TexCoords).r; + return g_TexCoords - vec2(parallax.x, 1.0 - parallax.y); +} + +vec3 BlinnPhong(vec3 p_LightDir, vec3 p_LightColor, float p_Luminosity) +{ + const vec3 halfwayDir = normalize(p_LightDir + g_ViewDir); + const float diffuseCoefficient = max(dot(g_Normal, p_LightDir), 0.0); + const float specularCoefficient = pow(max(dot(g_Normal, halfwayDir), 0.0), u_Shininess * 2.0); + + return p_LightColor * g_DiffuseTexel.rgb * diffuseCoefficient * p_Luminosity + ((p_Luminosity > 0.0) ? (p_LightColor * g_SpecularTexel.rgb * specularCoefficient * p_Luminosity) : vec3(0.0)); +} + +float LuminosityFromAttenuation(mat4 p_Light) +{ + const vec3 lightPosition = p_Light[0].rgb; + const float constant = p_Light[0][3]; + const float linear = p_Light[1][3]; + const float quadratic = p_Light[2][3]; + + const float distanceToLight = length(lightPosition - fs_in.FragPos); + const float attenuation = (constant + linear * distanceToLight + quadratic * (distanceToLight * distanceToLight)); + return 1.0 / attenuation; +} + +vec3 CalcPointLight(mat4 p_Light) +{ + /* Extract light information from light mat4 */ + const vec3 lightPosition = p_Light[0].rgb; + const vec3 lightColor = UnPack(p_Light[2][0]); + const float intensity = p_Light[3][3]; + + const vec3 lightDirection = normalize(lightPosition - fs_in.FragPos); + const float luminosity = LuminosityFromAttenuation(p_Light); + + return BlinnPhong(lightDirection, lightColor, intensity * luminosity); +} + +vec3 CalcDirectionalLight(mat4 light) +{ + return BlinnPhong(-light[1].rgb, UnPack(light[2][0]), light[3][3]); +} + +vec3 CalcSpotLight(mat4 p_Light) +{ + /* Extract light information from light mat4 */ + const vec3 lightPosition = p_Light[0].rgb; + const vec3 lightForward = p_Light[1].rgb; + const vec3 lightColor = UnPack(p_Light[2][0]); + const float intensity = p_Light[3][3]; + const float cutOff = cos(radians(p_Light[3][1])); + const float outerCutOff = cos(radians(p_Light[3][1] + p_Light[3][2])); + + const vec3 lightDirection = normalize(lightPosition - fs_in.FragPos); + const float luminosity = LuminosityFromAttenuation(p_Light); + + /* Calculate the spot intensity */ + const float theta = dot(lightDirection, normalize(-lightForward)); + const float epsilon = cutOff - outerCutOff; + const float spotIntensity = clamp((theta - outerCutOff) / epsilon, 0.0, 1.0); + + return BlinnPhong(lightDirection, lightColor, intensity * spotIntensity * luminosity); +} + +vec3 CalcAmbientBoxLight(mat4 p_Light) +{ + const vec3 lightPosition = p_Light[0].rgb; + const vec3 lightColor = UnPack(p_Light[2][0]); + const float intensity = p_Light[3][3]; + const vec3 size = vec3(p_Light[0][3], p_Light[1][3], p_Light[2][3]); + + return PointInAABB(fs_in.FragPos, lightPosition, size) ? g_DiffuseTexel.rgb * lightColor * intensity : vec3(0.0); +} + +vec3 CalcAmbientSphereLight(mat4 p_Light) +{ + const vec3 lightPosition = p_Light[0].rgb; + const vec3 lightColor = UnPack(p_Light[2][0]); + const float intensity = p_Light[3][3]; + const float radius = p_Light[0][3]; + + return distance(lightPosition, fs_in.FragPos) <= radius ? g_DiffuseTexel.rgb * lightColor * intensity : vec3(0.0); +} + +void main() +{ + g_TexCoords = u_TextureOffset + vec2(mod(fs_in.TexCoords.x * u_TextureTiling.x, 1), mod(fs_in.TexCoords.y * u_TextureTiling.y, 1)); + + /* Apply parallax mapping */ + if (u_HeightScale > 0) + g_TexCoords = ParallaxMapping(normalize(fs_in.TangentViewPos - fs_in.TangentFragPos)); + + /* Apply color mask */ + if (texture(u_MaskMap, g_TexCoords).r != 0.0) + { + g_ViewDir = normalize(ubo_ViewPos - fs_in.FragPos); + g_DiffuseTexel = texture(u_DiffuseMap, g_TexCoords) * u_Diffuse; + g_SpecularTexel = texture(u_SpecularMap, g_TexCoords) * vec4(u_Specular, 1.0); + + if (u_EnableNormalMapping) + { + g_Normal = texture(u_NormalMap, g_TexCoords).rgb; + g_Normal = normalize(g_Normal * 2.0 - 1.0); + g_Normal = normalize(fs_in.TBN * g_Normal); + } + else + { + g_Normal = normalize(fs_in.Normal); + } + + vec3 lightSum = vec3(0.0); + + for (int i = 0; i < ssbo_Lights.length(); ++i) + { + switch(int(ssbo_Lights[i][3][0])) + { + case 0: lightSum += CalcPointLight(ssbo_Lights[i]); break; + case 1: lightSum += CalcDirectionalLight(ssbo_Lights[i]); break; + case 2: lightSum += CalcSpotLight(ssbo_Lights[i]); break; + case 3: lightSum += CalcAmbientBoxLight(ssbo_Lights[i]); break; + case 4: lightSum += CalcAmbientSphereLight(ssbo_Lights[i]); break; + } + } + + FRAGMENT_COLOR = vec4(lightSum, g_DiffuseTexel.a); + } + else + { + FRAGMENT_COLOR = vec4(0.0); + } +} diff --git a/Resources/Engine/Shaders/Fragment/Lambert.glsl b/Resources/Engine/Shaders/Fragment/Lambert.glsl new file mode 100644 index 000000000..54f3fbea2 --- /dev/null +++ b/Resources/Engine/Shaders/Fragment/Lambert.glsl @@ -0,0 +1,26 @@ +#version 430 core + +out vec4 FRAGMENT_COLOR; + +#include ":Shaders/Common/IO/FragmentInput.glsl" + +uniform vec4 u_Diffuse = vec4(1.0, 1.0, 1.0, 1.0); +uniform sampler2D u_DiffuseMap; +uniform vec2 u_TextureTiling = vec2(1.0, 1.0); +uniform vec2 u_TextureOffset = vec2(0.0, 0.0); + +const vec3 c_lightPosition = vec3(-9000.0, 10000.0, 11000.0); +const vec3 c_lightDiffuse = vec3(1.0, 1.0, 1.0); +const vec3 c_lightAmbient = vec3(0.3, 0.3, 0.3); + +vec3 Lambert(vec3 p_fragPos, vec3 p_normal) +{ + const float diffuse = max(dot(p_normal, normalize(c_lightPosition - p_fragPos)), 0.0); + return clamp(c_lightDiffuse * diffuse + c_lightAmbient, 0.0, 1.0); +} + +void main() +{ + const vec4 diffuse = texture(u_DiffuseMap, u_TextureOffset + vec2(mod(fs_in.TexCoords.x * u_TextureTiling.x, 1), mod(fs_in.TexCoords.y * u_TextureTiling.y, 1))) * u_Diffuse; + FRAGMENT_COLOR = vec4(Lambert(fs_in.FragPos, fs_in.Normal) * diffuse.rgb, diffuse.a); +} diff --git a/Resources/Engine/Shaders/Fragment/PBR.glsl b/Resources/Engine/Shaders/Fragment/PBR.glsl new file mode 100644 index 000000000..d07f9477e --- /dev/null +++ b/Resources/Engine/Shaders/Fragment/PBR.glsl @@ -0,0 +1,220 @@ +#version 430 core + +#include ":Shaders/Common/IO/FragmentInput.glsl" +#include ":Shaders/Common/Buffers/EngineUBO.glsl" +#include ":Shaders/Common/Buffers/LightsSSBO.glsl" + +out vec4 FRAGMENT_COLOR; + +uniform sampler2D u_AlbedoMap; +uniform sampler2D u_MetallicMap; +uniform sampler2D u_RoughnessMap; +uniform sampler2D u_AmbientOcclusionMap; +uniform sampler2D u_NormalMap; +uniform vec4 u_Albedo = vec4(1.0); +uniform vec2 u_TextureTiling = vec2(1.0, 1.0); +uniform vec2 u_TextureOffset = vec2(0.0, 0.0); +uniform bool u_EnableNormalMapping = false; +uniform float u_HeightScale = 0.0; +uniform float u_Metallic = 1.0; +uniform float u_Roughness = 1.0; + +const float PI = 3.14159265359; + +float DistributionGGX(vec3 N, vec3 H, float roughness) +{ + float a = roughness*roughness; + float a2 = a*a; + float NdotH = max(dot(N, H), 0.0); + float NdotH2 = NdotH*NdotH; + + float num = a2; + float denom = (NdotH2 * (a2 - 1.0) + 1.0); + denom = PI * denom * denom; + + return num / denom; +} + +float GeometrySchlickGGX(float NdotV, float roughness) +{ + float r = (roughness + 1.0); + float k = (r*r) / 8.0; + + float num = NdotV; + float denom = NdotV * (1.0 - k) + k; + + return num / denom; +} +float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness) +{ + float NdotV = max(dot(N, V), 0.0); + float NdotL = max(dot(N, L), 0.0); + float ggx2 = GeometrySchlickGGX(NdotV, roughness); + float ggx1 = GeometrySchlickGGX(NdotL, roughness); + + return ggx1 * ggx2; +} + +vec3 fresnelSchlick(float cosTheta, vec3 F0) +{ + return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0); +} + +vec3 UnPack(float p_Target) +{ + return vec3 + ( + float((uint(p_Target) >> 24) & 0xff) * 0.003921568627451, + float((uint(p_Target) >> 16) & 0xff) * 0.003921568627451, + float((uint(p_Target) >> 8) & 0xff) * 0.003921568627451 + ); +} + +bool PointInAABB(vec3 p_Point, vec3 p_AabbCenter, vec3 p_AabbHalfSize) +{ + return + ( + p_Point.x > p_AabbCenter.x - p_AabbHalfSize.x && p_Point.x < p_AabbCenter.x + p_AabbHalfSize.x && + p_Point.y > p_AabbCenter.y - p_AabbHalfSize.y && p_Point.y < p_AabbCenter.y + p_AabbHalfSize.y && + p_Point.z > p_AabbCenter.z - p_AabbHalfSize.z && p_Point.z < p_AabbCenter.z + p_AabbHalfSize.z + ); +} + +float LuminosityFromAttenuation(mat4 p_Light) +{ + const vec3 lightPosition = p_Light[0].rgb; + const float constant = p_Light[0][3]; + const float linear = p_Light[1][3]; + const float quadratic = p_Light[2][3]; + + const float distanceToLight = length(lightPosition - fs_in.FragPos); + const float attenuation = (constant + linear * distanceToLight + quadratic * (distanceToLight * distanceToLight)); + return 1.0 / attenuation; +} + +vec3 CalcAmbientBoxLight(mat4 p_Light) +{ + const vec3 lightPosition = p_Light[0].rgb; + const vec3 lightColor = UnPack(p_Light[2][0]); + const float intensity = p_Light[3][3]; + const vec3 size = vec3(p_Light[0][3], p_Light[1][3], p_Light[2][3]); + + return PointInAABB(fs_in.FragPos, lightPosition, size) ? lightColor * intensity : vec3(0.0); +} + +vec3 CalcAmbientSphereLight(mat4 p_Light) +{ + const vec3 lightPosition = p_Light[0].rgb; + const vec3 lightColor = UnPack(p_Light[2][0]); + const float intensity = p_Light[3][3]; + const float radius = p_Light[0][3]; + + return distance(lightPosition, fs_in.FragPos) <= radius ? lightColor * intensity : vec3(0.0); +} + +void main() +{ + vec2 texCoords = u_TextureOffset + vec2(mod(fs_in.TexCoords.x * u_TextureTiling.x, 1), mod(fs_in.TexCoords.y * u_TextureTiling.y, 1)); + + vec4 albedoRGBA = texture(u_AlbedoMap, texCoords) * u_Albedo; + vec3 albedo = pow(albedoRGBA.rgb, vec3(2.2)); + float metallic = texture(u_MetallicMap, texCoords).r * u_Metallic; + float roughness = texture(u_RoughnessMap, texCoords).r * u_Roughness; + float ao = texture(u_AmbientOcclusionMap, texCoords).r; + vec3 normal; + + if (u_EnableNormalMapping) + { + normal = texture(u_NormalMap, texCoords).rgb; + normal = normalize(normal * 2.0 - 1.0); + normal = normalize(fs_in.TBN * normal); + } + else + { + normal = normalize(fs_in.Normal); + } + + vec3 N = normalize(normal); + vec3 V = normalize(ubo_ViewPos - fs_in.FragPos); + + vec3 F0 = vec3(0.04); + F0 = mix(F0, albedo, metallic); + + // reflectance equation + vec3 Lo = vec3(0.0); + vec3 ambientSum = vec3(0.0); + + for (int i = 0; i < ssbo_Lights.length(); ++i) + { + if (int(ssbo_Lights[i][3][0]) == 3) + { + ambientSum += CalcAmbientBoxLight(ssbo_Lights[i]); + } + else if (int(ssbo_Lights[i][3][0]) == 4) + { + ambientSum += CalcAmbientSphereLight(ssbo_Lights[i]); + } + else + { + // calculate per-light radiance + vec3 L = int(ssbo_Lights[i][3][0]) == 1 ? -ssbo_Lights[i][1].rgb : normalize(ssbo_Lights[i][0].rgb - fs_in.FragPos); + vec3 H = normalize(V + L); + float distance = length(ssbo_Lights[i][0].rgb - fs_in.FragPos); + float lightCoeff = 0.0; + + switch(int(ssbo_Lights[i][3][0])) + { + case 0: + lightCoeff = LuminosityFromAttenuation(ssbo_Lights[i]) * ssbo_Lights[i][3][3]; + break; + + case 1: + lightCoeff = ssbo_Lights[i][3][3]; + break; + + case 2: + const vec3 lightForward = ssbo_Lights[i][1].rgb; + const float cutOff = cos(radians(ssbo_Lights[i][3][1])); + const float outerCutOff = cos(radians(ssbo_Lights[i][3][1] + ssbo_Lights[i][3][2])); + + const vec3 lightDirection = normalize(ssbo_Lights[i][0].rgb - fs_in.FragPos); + const float luminosity = LuminosityFromAttenuation(ssbo_Lights[i]); + + /* Calculate the spot intensity */ + const float theta = dot(lightDirection, normalize(-lightForward)); + const float epsilon = cutOff - outerCutOff; + const float spotIntensity = clamp((theta - outerCutOff) / epsilon, 0.0, 1.0); + + lightCoeff = luminosity * spotIntensity * ssbo_Lights[i][3][3]; + break; + } + + vec3 radiance = UnPack(ssbo_Lights[i][2][0]) * lightCoeff; + + // cook-torrance brdf + float NDF = DistributionGGX(N, H, roughness); + float G = GeometrySmith(N, V, L, roughness); + vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0); + + vec3 kS = F; + vec3 kD = vec3(1.0) - kS; + kD *= 1.0 - metallic; + + vec3 numerator = NDF * G * F; + float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0); + vec3 specular = numerator / max(denominator, 0.001); + + // add to outgoing radiance Lo + float NdotL = max(dot(N, L), 0.0); + Lo += (kD * albedo / PI + specular) * radiance * NdotL; + } + } + + vec3 ambient = ambientSum * albedo * ao; + vec3 color = ambient + Lo; + + color = color / (color + vec3(1.0)); + color = pow(color, vec3(1.0/2.2)); + + FRAGMENT_COLOR = vec4(color, albedoRGBA.a); +} diff --git a/Resources/Engine/Shaders/Fragment/Unlit.glsl b/Resources/Engine/Shaders/Fragment/Unlit.glsl new file mode 100644 index 000000000..8d5872687 --- /dev/null +++ b/Resources/Engine/Shaders/Fragment/Unlit.glsl @@ -0,0 +1,23 @@ +#version 430 core + +out vec4 FRAGMENT_COLOR; + +in VS_OUT +{ + vec3 FragPos; + vec3 Normal; + vec2 TexCoords; + mat3 TBN; + flat vec3 TangentViewPos; + vec3 TangentFragPos; +} fs_in; + +uniform vec4 u_Diffuse = vec4(1.0, 1.0, 1.0, 1.0); +uniform sampler2D u_DiffuseMap; +uniform vec2 u_TextureTiling = vec2(1.0, 1.0); +uniform vec2 u_TextureOffset = vec2(0.0, 0.0); + +void main() +{ + FRAGMENT_COLOR = texture(u_DiffuseMap, u_TextureOffset + vec2(mod(fs_in.TexCoords.x * u_TextureTiling.x, 1), mod(fs_in.TexCoords.y * u_TextureTiling.y, 1))) * u_Diffuse; +} diff --git a/Resources/Engine/Shaders/Lambert.glsl b/Resources/Engine/Shaders/Lambert.glsl index debb5ba4b..51be4df1a 100644 --- a/Resources/Engine/Shaders/Lambert.glsl +++ b/Resources/Engine/Shaders/Lambert.glsl @@ -1,64 +1,4 @@ #shader vertex -#version 430 core - -layout (location = 0) in vec3 geo_Pos; -layout (location = 1) in vec2 geo_TexCoords; -layout (location = 2) in vec3 geo_Normal; - -layout (std140) uniform EngineUBO -{ - mat4 ubo_Model; - mat4 ubo_View; - mat4 ubo_Projection; - vec3 ubo_ViewPos; - float ubo_Time; -}; - -out VS_OUT -{ - vec3 FragPos; - vec3 Normal; - vec2 TexCoords; -} vs_out; - -void main() -{ - vs_out.FragPos = vec3(ubo_Model * vec4(geo_Pos, 1.0)); - vs_out.Normal = normalize(mat3(transpose(inverse(ubo_Model))) * geo_Normal); - vs_out.TexCoords = geo_TexCoords; - - gl_Position = ubo_Projection * ubo_View * vec4(vs_out.FragPos, 1.0); -} - +#include ":Shaders/Vertex/Basic.glsl" #shader fragment -#version 430 core - -out vec4 FRAGMENT_COLOR; - -in VS_OUT -{ - vec3 FragPos; - vec3 Normal; - vec2 TexCoords; -} fs_in; - -uniform vec4 u_Diffuse = vec4(1.0, 1.0, 1.0, 1.0); -uniform sampler2D u_DiffuseMap; -uniform vec2 u_TextureTiling = vec2(1.0, 1.0); -uniform vec2 u_TextureOffset = vec2(0.0, 0.0); - -const vec3 c_lightPosition = vec3(-9000.0, 10000.0, 11000.0); -const vec3 c_lightDiffuse = vec3(1.0, 1.0, 1.0); -const vec3 c_lightAmbient = vec3(0.3, 0.3, 0.3); - -vec3 Lambert(vec3 p_fragPos, vec3 p_normal) -{ - const float diffuse = max(dot(p_normal, normalize(c_lightPosition - p_fragPos)), 0.0); - return clamp(c_lightDiffuse * diffuse + c_lightAmbient, 0.0, 1.0); -} - -void main() -{ - const vec4 diffuse = texture(u_DiffuseMap, u_TextureOffset + vec2(mod(fs_in.TexCoords.x * u_TextureTiling.x, 1), mod(fs_in.TexCoords.y * u_TextureTiling.y, 1))) * u_Diffuse; - FRAGMENT_COLOR = vec4(Lambert(fs_in.FragPos, fs_in.Normal) * diffuse.rgb, diffuse.a); -} \ No newline at end of file +#include ":Shaders/Fragment/Lambert.glsl" diff --git a/Resources/Engine/Shaders/Standard.glsl b/Resources/Engine/Shaders/Standard.glsl index 36ba7843f..7802143ef 100644 --- a/Resources/Engine/Shaders/Standard.glsl +++ b/Resources/Engine/Shaders/Standard.glsl @@ -1,258 +1,4 @@ #shader vertex -#version 430 core - -layout (location = 0) in vec3 geo_Pos; -layout (location = 1) in vec2 geo_TexCoords; -layout (location = 2) in vec3 geo_Normal; -layout (location = 3) in vec3 geo_Tangent; -layout (location = 4) in vec3 geo_Bitangent; - -/* Global information sent by the engine */ -layout (std140) uniform EngineUBO -{ - mat4 ubo_Model; - mat4 ubo_View; - mat4 ubo_Projection; - vec3 ubo_ViewPos; - float ubo_Time; -}; - -/* Information passed to the fragment shader */ -out VS_OUT -{ - vec3 FragPos; - vec3 Normal; - vec2 TexCoords; - mat3 TBN; - flat vec3 TangentViewPos; - vec3 TangentFragPos; -} vs_out; - -void main() -{ - vs_out.TBN = mat3 - ( - normalize(vec3(ubo_Model * vec4(geo_Tangent, 0.0))), - normalize(vec3(ubo_Model * vec4(geo_Bitangent, 0.0))), - normalize(vec3(ubo_Model * vec4(geo_Normal, 0.0))) - ); - - mat3 TBNi = transpose(vs_out.TBN); - - vs_out.FragPos = vec3(ubo_Model * vec4(geo_Pos, 1.0)); - vs_out.Normal = normalize(mat3(transpose(inverse(ubo_Model))) * geo_Normal); - vs_out.TexCoords = geo_TexCoords; - vs_out.TangentViewPos = TBNi * ubo_ViewPos; - vs_out.TangentFragPos = TBNi * vs_out.FragPos; - - gl_Position = ubo_Projection * ubo_View * vec4(vs_out.FragPos, 1.0); -} - +#include ":Shaders/Vertex/Basic.glsl" #shader fragment -#version 430 core - -/* Global information sent by the engine */ -layout (std140) uniform EngineUBO -{ - mat4 ubo_Model; - mat4 ubo_View; - mat4 ubo_Projection; - vec3 ubo_ViewPos; - float ubo_Time; -}; - -/* Information passed from the fragment shader */ -in VS_OUT -{ - vec3 FragPos; - vec3 Normal; - vec2 TexCoords; - mat3 TBN; - flat vec3 TangentViewPos; - vec3 TangentFragPos; -} fs_in; - -/* Light information sent by the engine */ -layout(std430, binding = 0) buffer LightSSBO -{ - mat4 ssbo_Lights[]; -}; - -/* Uniforms (Tweakable from the material editor) */ -uniform vec2 u_TextureTiling = vec2(1.0, 1.0); -uniform vec2 u_TextureOffset = vec2(0.0, 0.0); -uniform vec4 u_Diffuse = vec4(1.0, 1.0, 1.0, 1.0); -uniform vec3 u_Specular = vec3(1.0, 1.0, 1.0); -uniform float u_Shininess = 100.0; -uniform float u_HeightScale = 0.0; -uniform bool u_EnableNormalMapping = false; -uniform sampler2D u_DiffuseMap; -uniform sampler2D u_SpecularMap; -uniform sampler2D u_NormalMap; -uniform sampler2D u_HeightMap; -uniform sampler2D u_MaskMap; - -/* Global variables */ -vec3 g_Normal; -vec2 g_TexCoords; -vec3 g_ViewDir; -vec4 g_DiffuseTexel; -vec4 g_SpecularTexel; -vec4 g_HeightTexel; -vec4 g_NormalTexel; - -out vec4 FRAGMENT_COLOR; - -vec3 UnPack(float p_Target) -{ - return vec3 - ( - float((uint(p_Target) >> 24) & 0xff) * 0.003921568627451, - float((uint(p_Target) >> 16) & 0xff) * 0.003921568627451, - float((uint(p_Target) >> 8) & 0xff) * 0.003921568627451 - ); -} - -bool PointInAABB(vec3 p_Point, vec3 p_AabbCenter, vec3 p_AabbHalfSize) -{ - return - ( - p_Point.x > p_AabbCenter.x - p_AabbHalfSize.x && p_Point.x < p_AabbCenter.x + p_AabbHalfSize.x && - p_Point.y > p_AabbCenter.y - p_AabbHalfSize.y && p_Point.y < p_AabbCenter.y + p_AabbHalfSize.y && - p_Point.z > p_AabbCenter.z - p_AabbHalfSize.z && p_Point.z < p_AabbCenter.z + p_AabbHalfSize.z - ); -} - -vec2 ParallaxMapping(vec3 p_ViewDir) -{ - const vec2 parallax = p_ViewDir.xy * u_HeightScale * texture(u_HeightMap, g_TexCoords).r; - return g_TexCoords - vec2(parallax.x, 1.0 - parallax.y); -} - -vec3 BlinnPhong(vec3 p_LightDir, vec3 p_LightColor, float p_Luminosity) -{ - const vec3 halfwayDir = normalize(p_LightDir + g_ViewDir); - const float diffuseCoefficient = max(dot(g_Normal, p_LightDir), 0.0); - const float specularCoefficient = pow(max(dot(g_Normal, halfwayDir), 0.0), u_Shininess * 2.0); - - return p_LightColor * g_DiffuseTexel.rgb * diffuseCoefficient * p_Luminosity + ((p_Luminosity > 0.0) ? (p_LightColor * g_SpecularTexel.rgb * specularCoefficient * p_Luminosity) : vec3(0.0)); -} - -float LuminosityFromAttenuation(mat4 p_Light) -{ - const vec3 lightPosition = p_Light[0].rgb; - const float constant = p_Light[0][3]; - const float linear = p_Light[1][3]; - const float quadratic = p_Light[2][3]; - - const float distanceToLight = length(lightPosition - fs_in.FragPos); - const float attenuation = (constant + linear * distanceToLight + quadratic * (distanceToLight * distanceToLight)); - return 1.0 / attenuation; -} - -vec3 CalcPointLight(mat4 p_Light) -{ - /* Extract light information from light mat4 */ - const vec3 lightPosition = p_Light[0].rgb; - const vec3 lightColor = UnPack(p_Light[2][0]); - const float intensity = p_Light[3][3]; - - const vec3 lightDirection = normalize(lightPosition - fs_in.FragPos); - const float luminosity = LuminosityFromAttenuation(p_Light); - - return BlinnPhong(lightDirection, lightColor, intensity * luminosity); -} - -vec3 CalcDirectionalLight(mat4 light) -{ - return BlinnPhong(-light[1].rgb, UnPack(light[2][0]), light[3][3]); -} - -vec3 CalcSpotLight(mat4 p_Light) -{ - /* Extract light information from light mat4 */ - const vec3 lightPosition = p_Light[0].rgb; - const vec3 lightForward = p_Light[1].rgb; - const vec3 lightColor = UnPack(p_Light[2][0]); - const float intensity = p_Light[3][3]; - const float cutOff = cos(radians(p_Light[3][1])); - const float outerCutOff = cos(radians(p_Light[3][1] + p_Light[3][2])); - - const vec3 lightDirection = normalize(lightPosition - fs_in.FragPos); - const float luminosity = LuminosityFromAttenuation(p_Light); - - /* Calculate the spot intensity */ - const float theta = dot(lightDirection, normalize(-lightForward)); - const float epsilon = cutOff - outerCutOff; - const float spotIntensity = clamp((theta - outerCutOff) / epsilon, 0.0, 1.0); - - return BlinnPhong(lightDirection, lightColor, intensity * spotIntensity * luminosity); -} - -vec3 CalcAmbientBoxLight(mat4 p_Light) -{ - const vec3 lightPosition = p_Light[0].rgb; - const vec3 lightColor = UnPack(p_Light[2][0]); - const float intensity = p_Light[3][3]; - const vec3 size = vec3(p_Light[0][3], p_Light[1][3], p_Light[2][3]); - - return PointInAABB(fs_in.FragPos, lightPosition, size) ? g_DiffuseTexel.rgb * lightColor * intensity : vec3(0.0); -} - -vec3 CalcAmbientSphereLight(mat4 p_Light) -{ - const vec3 lightPosition = p_Light[0].rgb; - const vec3 lightColor = UnPack(p_Light[2][0]); - const float intensity = p_Light[3][3]; - const float radius = p_Light[0][3]; - - return distance(lightPosition, fs_in.FragPos) <= radius ? g_DiffuseTexel.rgb * lightColor * intensity : vec3(0.0); -} - -void main() -{ - g_TexCoords = u_TextureOffset + vec2(mod(fs_in.TexCoords.x * u_TextureTiling.x, 1), mod(fs_in.TexCoords.y * u_TextureTiling.y, 1)); - - /* Apply parallax mapping */ - if (u_HeightScale > 0) - g_TexCoords = ParallaxMapping(normalize(fs_in.TangentViewPos - fs_in.TangentFragPos)); - - /* Apply color mask */ - if (texture(u_MaskMap, g_TexCoords).r != 0.0) - { - g_ViewDir = normalize(ubo_ViewPos - fs_in.FragPos); - g_DiffuseTexel = texture(u_DiffuseMap, g_TexCoords) * u_Diffuse; - g_SpecularTexel = texture(u_SpecularMap, g_TexCoords) * vec4(u_Specular, 1.0); - - if (u_EnableNormalMapping) - { - g_Normal = texture(u_NormalMap, g_TexCoords).rgb; - g_Normal = normalize(g_Normal * 2.0 - 1.0); - g_Normal = normalize(fs_in.TBN * g_Normal); - } - else - { - g_Normal = normalize(fs_in.Normal); - } - - vec3 lightSum = vec3(0.0); - - for (int i = 0; i < ssbo_Lights.length(); ++i) - { - switch(int(ssbo_Lights[i][3][0])) - { - case 0: lightSum += CalcPointLight(ssbo_Lights[i]); break; - case 1: lightSum += CalcDirectionalLight(ssbo_Lights[i]); break; - case 2: lightSum += CalcSpotLight(ssbo_Lights[i]); break; - case 3: lightSum += CalcAmbientBoxLight(ssbo_Lights[i]); break; - case 4: lightSum += CalcAmbientSphereLight(ssbo_Lights[i]); break; - } - } - - FRAGMENT_COLOR = vec4(lightSum, g_DiffuseTexel.a); - } - else - { - FRAGMENT_COLOR = vec4(0.0); - } -} \ No newline at end of file +#include ":Shaders/Fragment/BlinnPhong.glsl" diff --git a/Resources/Engine/Shaders/StandardPBR.glsl b/Resources/Engine/Shaders/StandardPBR.glsl index c0bd2e7d0..6e5922ac2 100644 --- a/Resources/Engine/Shaders/StandardPBR.glsl +++ b/Resources/Engine/Shaders/StandardPBR.glsl @@ -1,294 +1,4 @@ #shader vertex -#version 430 core - -layout (location = 0) in vec3 geo_Pos; -layout (location = 1) in vec2 geo_TexCoords; -layout (location = 2) in vec3 geo_Normal; -layout (location = 3) in vec3 geo_Tangent; -layout (location = 4) in vec3 geo_Bitangent; - -/* Global information sent by the engine */ -layout (std140) uniform EngineUBO -{ - mat4 ubo_Model; - mat4 ubo_View; - mat4 ubo_Projection; - vec3 ubo_ViewPos; - float ubo_Time; -}; - -/* Information passed to the fragment shader */ -out VS_OUT -{ - vec3 FragPos; - vec3 Normal; - vec2 TexCoords; - mat3 TBN; - flat vec3 TangentViewPos; - vec3 TangentFragPos; -} vs_out; - -void main() -{ - vs_out.TBN = mat3 - ( - normalize(vec3(ubo_Model * vec4(geo_Tangent, 0.0))), - normalize(vec3(ubo_Model * vec4(geo_Bitangent, 0.0))), - normalize(vec3(ubo_Model * vec4(geo_Normal, 0.0))) - ); - - mat3 TBNi = transpose(vs_out.TBN); - - vs_out.FragPos = vec3(ubo_Model * vec4(geo_Pos, 1.0)); - vs_out.Normal = normalize(mat3(transpose(inverse(ubo_Model))) * geo_Normal); - vs_out.TexCoords = geo_TexCoords; - vs_out.TangentViewPos = TBNi * ubo_ViewPos; - vs_out.TangentFragPos = TBNi * vs_out.FragPos; - - gl_Position = ubo_Projection * ubo_View * vec4(vs_out.FragPos, 1.0); -} - +#include ":Shaders/Vertex/Basic.glsl" #shader fragment -#version 430 core - -/* Global information sent by the engine */ -layout (std140) uniform EngineUBO -{ - mat4 ubo_Model; - mat4 ubo_View; - mat4 ubo_Projection; - vec3 ubo_ViewPos; - float ubo_Time; -}; - -/* Information passed from the fragment shader */ -in VS_OUT -{ - vec3 FragPos; - vec3 Normal; - vec2 TexCoords; - mat3 TBN; - flat vec3 TangentViewPos; - vec3 TangentFragPos; -} fs_in; - -/* Light information sent by the engine */ -layout(std430, binding = 0) buffer LightSSBO -{ - mat4 ssbo_Lights[]; -}; - -out vec4 FRAGMENT_COLOR; - -uniform sampler2D u_AlbedoMap; -uniform sampler2D u_MetallicMap; -uniform sampler2D u_RoughnessMap; -uniform sampler2D u_AmbientOcclusionMap; -uniform sampler2D u_NormalMap; -uniform vec4 u_Albedo = vec4(1.0); -uniform vec2 u_TextureTiling = vec2(1.0, 1.0); -uniform vec2 u_TextureOffset = vec2(0.0, 0.0); -uniform bool u_EnableNormalMapping = false; -uniform float u_HeightScale = 0.0; -uniform float u_Metallic = 1.0; -uniform float u_Roughness = 1.0; - -const float PI = 3.14159265359; - -float DistributionGGX(vec3 N, vec3 H, float roughness) -{ - float a = roughness*roughness; - float a2 = a*a; - float NdotH = max(dot(N, H), 0.0); - float NdotH2 = NdotH*NdotH; - - float num = a2; - float denom = (NdotH2 * (a2 - 1.0) + 1.0); - denom = PI * denom * denom; - - return num / denom; -} - -float GeometrySchlickGGX(float NdotV, float roughness) -{ - float r = (roughness + 1.0); - float k = (r*r) / 8.0; - - float num = NdotV; - float denom = NdotV * (1.0 - k) + k; - - return num / denom; -} -float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness) -{ - float NdotV = max(dot(N, V), 0.0); - float NdotL = max(dot(N, L), 0.0); - float ggx2 = GeometrySchlickGGX(NdotV, roughness); - float ggx1 = GeometrySchlickGGX(NdotL, roughness); - - return ggx1 * ggx2; -} - -vec3 fresnelSchlick(float cosTheta, vec3 F0) -{ - return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0); -} - -vec3 UnPack(float p_Target) -{ - return vec3 - ( - float((uint(p_Target) >> 24) & 0xff) * 0.003921568627451, - float((uint(p_Target) >> 16) & 0xff) * 0.003921568627451, - float((uint(p_Target) >> 8) & 0xff) * 0.003921568627451 - ); -} - -bool PointInAABB(vec3 p_Point, vec3 p_AabbCenter, vec3 p_AabbHalfSize) -{ - return - ( - p_Point.x > p_AabbCenter.x - p_AabbHalfSize.x && p_Point.x < p_AabbCenter.x + p_AabbHalfSize.x && - p_Point.y > p_AabbCenter.y - p_AabbHalfSize.y && p_Point.y < p_AabbCenter.y + p_AabbHalfSize.y && - p_Point.z > p_AabbCenter.z - p_AabbHalfSize.z && p_Point.z < p_AabbCenter.z + p_AabbHalfSize.z - ); -} - -float LuminosityFromAttenuation(mat4 p_Light) -{ - const vec3 lightPosition = p_Light[0].rgb; - const float constant = p_Light[0][3]; - const float linear = p_Light[1][3]; - const float quadratic = p_Light[2][3]; - - const float distanceToLight = length(lightPosition - fs_in.FragPos); - const float attenuation = (constant + linear * distanceToLight + quadratic * (distanceToLight * distanceToLight)); - return 1.0 / attenuation; -} - -vec3 CalcAmbientBoxLight(mat4 p_Light) -{ - const vec3 lightPosition = p_Light[0].rgb; - const vec3 lightColor = UnPack(p_Light[2][0]); - const float intensity = p_Light[3][3]; - const vec3 size = vec3(p_Light[0][3], p_Light[1][3], p_Light[2][3]); - - return PointInAABB(fs_in.FragPos, lightPosition, size) ? lightColor * intensity : vec3(0.0); -} - -vec3 CalcAmbientSphereLight(mat4 p_Light) -{ - const vec3 lightPosition = p_Light[0].rgb; - const vec3 lightColor = UnPack(p_Light[2][0]); - const float intensity = p_Light[3][3]; - const float radius = p_Light[0][3]; - - return distance(lightPosition, fs_in.FragPos) <= radius ? lightColor * intensity : vec3(0.0); -} - -void main() -{ - vec2 texCoords = u_TextureOffset + vec2(mod(fs_in.TexCoords.x * u_TextureTiling.x, 1), mod(fs_in.TexCoords.y * u_TextureTiling.y, 1)); - - vec4 albedoRGBA = texture(u_AlbedoMap, texCoords) * u_Albedo; - vec3 albedo = pow(albedoRGBA.rgb, vec3(2.2)); - float metallic = texture(u_MetallicMap, texCoords).r * u_Metallic; - float roughness = texture(u_RoughnessMap, texCoords).r * u_Roughness; - float ao = texture(u_AmbientOcclusionMap, texCoords).r; - vec3 normal; - - if (u_EnableNormalMapping) - { - normal = texture(u_NormalMap, texCoords).rgb; - normal = normalize(normal * 2.0 - 1.0); - normal = normalize(fs_in.TBN * normal); - } - else - { - normal = normalize(fs_in.Normal); - } - - vec3 N = normalize(normal); - vec3 V = normalize(ubo_ViewPos - fs_in.FragPos); - - vec3 F0 = vec3(0.04); - F0 = mix(F0, albedo, metallic); - - // reflectance equation - vec3 Lo = vec3(0.0); - vec3 ambientSum = vec3(0.0); - - for (int i = 0; i < ssbo_Lights.length(); ++i) - { - if (int(ssbo_Lights[i][3][0]) == 3) - { - ambientSum += CalcAmbientBoxLight(ssbo_Lights[i]); - } - else if (int(ssbo_Lights[i][3][0]) == 4) - { - ambientSum += CalcAmbientSphereLight(ssbo_Lights[i]); - } - else - { - // calculate per-light radiance - vec3 L = int(ssbo_Lights[i][3][0]) == 1 ? -ssbo_Lights[i][1].rgb : normalize(ssbo_Lights[i][0].rgb - fs_in.FragPos); - vec3 H = normalize(V + L); - float distance = length(ssbo_Lights[i][0].rgb - fs_in.FragPos); - float lightCoeff = 0.0; - - switch(int(ssbo_Lights[i][3][0])) - { - case 0: - lightCoeff = LuminosityFromAttenuation(ssbo_Lights[i]) * ssbo_Lights[i][3][3]; - break; - - case 1: - lightCoeff = ssbo_Lights[i][3][3]; - break; - - case 2: - const vec3 lightForward = ssbo_Lights[i][1].rgb; - const float cutOff = cos(radians(ssbo_Lights[i][3][1])); - const float outerCutOff = cos(radians(ssbo_Lights[i][3][1] + ssbo_Lights[i][3][2])); - - const vec3 lightDirection = normalize(ssbo_Lights[i][0].rgb - fs_in.FragPos); - const float luminosity = LuminosityFromAttenuation(ssbo_Lights[i]); - - /* Calculate the spot intensity */ - const float theta = dot(lightDirection, normalize(-lightForward)); - const float epsilon = cutOff - outerCutOff; - const float spotIntensity = clamp((theta - outerCutOff) / epsilon, 0.0, 1.0); - - lightCoeff = luminosity * spotIntensity * ssbo_Lights[i][3][3]; - break; - } - - vec3 radiance = UnPack(ssbo_Lights[i][2][0]) * lightCoeff; - - // cook-torrance brdf - float NDF = DistributionGGX(N, H, roughness); - float G = GeometrySmith(N, V, L, roughness); - vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0); - - vec3 kS = F; - vec3 kD = vec3(1.0) - kS; - kD *= 1.0 - metallic; - - vec3 numerator = NDF * G * F; - float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0); - vec3 specular = numerator / max(denominator, 0.001); - - // add to outgoing radiance Lo - float NdotL = max(dot(N, L), 0.0); - Lo += (kD * albedo / PI + specular) * radiance * NdotL; - } - } - - vec3 ambient = ambientSum * albedo * ao; - vec3 color = ambient + Lo; - - color = color / (color + vec3(1.0)); - color = pow(color, vec3(1.0/2.2)); - - FRAGMENT_COLOR = vec4(color, albedoRGBA.a); -} \ No newline at end of file +#include ":Shaders/Fragment/PBR.glsl" diff --git a/Resources/Engine/Shaders/Unlit.glsl b/Resources/Engine/Shaders/Unlit.glsl index e734b6493..d1dc7bfec 100644 --- a/Resources/Engine/Shaders/Unlit.glsl +++ b/Resources/Engine/Shaders/Unlit.glsl @@ -1,47 +1,4 @@ #shader vertex -#version 430 core - -layout (location = 0) in vec3 geo_Pos; -layout (location = 1) in vec2 geo_TexCoords; -layout (location = 2) in vec3 geo_Normal; - -layout (std140) uniform EngineUBO -{ - mat4 ubo_Model; - mat4 ubo_View; - mat4 ubo_Projection; - vec3 ubo_ViewPos; - float ubo_Time; -}; - -out VS_OUT -{ - vec2 TexCoords; -} vs_out; - -void main() -{ - vs_out.TexCoords = geo_TexCoords; - - gl_Position = ubo_Projection * ubo_View * ubo_Model * vec4(geo_Pos, 1.0); -} - +#include ":Shaders/Vertex/Basic.glsl" #shader fragment -#version 430 core - -out vec4 FRAGMENT_COLOR; - -in VS_OUT -{ - vec2 TexCoords; -} fs_in; - -uniform vec4 u_Diffuse = vec4(1.0, 1.0, 1.0, 1.0); -uniform sampler2D u_DiffuseMap; -uniform vec2 u_TextureTiling = vec2(1.0, 1.0); -uniform vec2 u_TextureOffset = vec2(0.0, 0.0); - -void main() -{ - FRAGMENT_COLOR = texture(u_DiffuseMap, u_TextureOffset + vec2(mod(fs_in.TexCoords.x * u_TextureTiling.x, 1), mod(fs_in.TexCoords.y * u_TextureTiling.y, 1))) * u_Diffuse; -} \ No newline at end of file +#include ":Shaders/Fragment/Unlit.glsl" diff --git a/Resources/Engine/Shaders/Vertex/Basic.glsl b/Resources/Engine/Shaders/Vertex/Basic.glsl new file mode 100644 index 000000000..698f7f027 --- /dev/null +++ b/Resources/Engine/Shaders/Vertex/Basic.glsl @@ -0,0 +1,30 @@ +#version 430 core + +layout (location = 0) in vec3 geo_Pos; +layout (location = 1) in vec2 geo_TexCoords; +layout (location = 2) in vec3 geo_Normal; +layout (location = 3) in vec3 geo_Tangent; +layout (location = 4) in vec3 geo_Bitangent; + +#include ":Shaders/Common/Buffers/EngineUBO.glsl" +#include ":Shaders/Common/IO/VertexOutput.glsl" + +void main() +{ + vs_out.TBN = mat3 + ( + normalize(vec3(ubo_Model * vec4(geo_Tangent, 0.0))), + normalize(vec3(ubo_Model * vec4(geo_Bitangent, 0.0))), + normalize(vec3(ubo_Model * vec4(geo_Normal, 0.0))) + ); + + mat3 TBNi = transpose(vs_out.TBN); + + vs_out.FragPos = vec3(ubo_Model * vec4(geo_Pos, 1.0)); + vs_out.Normal = normalize(mat3(transpose(inverse(ubo_Model))) * geo_Normal); + vs_out.TexCoords = geo_TexCoords; + vs_out.TangentViewPos = TBNi * ubo_ViewPos; + vs_out.TangentFragPos = TBNi * vs_out.FragPos; + + gl_Position = ubo_Projection * ubo_View * vec4(vs_out.FragPos, 1.0); +} From a1f2b9ec28bebc553a7ab0d5340bc7dc11f0882a Mon Sep 17 00:00:00 2001 From: Adrien GIVRY Date: Thu, 23 Nov 2023 16:58:16 -0500 Subject: [PATCH 07/12] Updating shader files --- .../Buffers/{EngineUBO.glsl => EngineUBO.part.glsl} | 0 .../Buffers/{LightsSSBO.glsl => LightsSSBO.part.glsl} | 0 .../IO/{FragmentInput.glsl => FragmentInput.part.glsl} | 0 .../IO/{VertexOutput.glsl => VertexOutput.part.glsl} | 0 .../Fragment/{BlinnPhong.glsl => BlinnPhong.frag.glsl} | 6 +++--- .../Fragment/{Lambert.glsl => Lambert.frag.glsl} | 2 +- .../Shaders/Fragment/{PBR.glsl => PBR.frag.glsl} | 6 +++--- .../Shaders/Fragment/{Unlit.glsl => Unlit.frag.glsl} | 10 +--------- Resources/Engine/Shaders/Lambert.glsl | 4 ++-- Resources/Engine/Shaders/Standard.glsl | 4 ++-- Resources/Engine/Shaders/StandardPBR.glsl | 4 ++-- Resources/Engine/Shaders/Unlit.glsl | 4 ++-- .../Shaders/Vertex/{Basic.glsl => Basic.vert.glsl} | 4 ++-- 13 files changed, 18 insertions(+), 26 deletions(-) rename Resources/Engine/Shaders/Common/Buffers/{EngineUBO.glsl => EngineUBO.part.glsl} (100%) rename Resources/Engine/Shaders/Common/Buffers/{LightsSSBO.glsl => LightsSSBO.part.glsl} (100%) rename Resources/Engine/Shaders/Common/IO/{FragmentInput.glsl => FragmentInput.part.glsl} (100%) rename Resources/Engine/Shaders/Common/IO/{VertexOutput.glsl => VertexOutput.part.glsl} (100%) rename Resources/Engine/Shaders/Fragment/{BlinnPhong.glsl => BlinnPhong.frag.glsl} (97%) rename Resources/Engine/Shaders/Fragment/{Lambert.glsl => Lambert.frag.glsl} (94%) rename Resources/Engine/Shaders/Fragment/{PBR.glsl => PBR.frag.glsl} (97%) rename Resources/Engine/Shaders/Fragment/{Unlit.glsl => Unlit.frag.glsl} (70%) rename Resources/Engine/Shaders/Vertex/{Basic.glsl => Basic.vert.glsl} (89%) diff --git a/Resources/Engine/Shaders/Common/Buffers/EngineUBO.glsl b/Resources/Engine/Shaders/Common/Buffers/EngineUBO.part.glsl similarity index 100% rename from Resources/Engine/Shaders/Common/Buffers/EngineUBO.glsl rename to Resources/Engine/Shaders/Common/Buffers/EngineUBO.part.glsl diff --git a/Resources/Engine/Shaders/Common/Buffers/LightsSSBO.glsl b/Resources/Engine/Shaders/Common/Buffers/LightsSSBO.part.glsl similarity index 100% rename from Resources/Engine/Shaders/Common/Buffers/LightsSSBO.glsl rename to Resources/Engine/Shaders/Common/Buffers/LightsSSBO.part.glsl diff --git a/Resources/Engine/Shaders/Common/IO/FragmentInput.glsl b/Resources/Engine/Shaders/Common/IO/FragmentInput.part.glsl similarity index 100% rename from Resources/Engine/Shaders/Common/IO/FragmentInput.glsl rename to Resources/Engine/Shaders/Common/IO/FragmentInput.part.glsl diff --git a/Resources/Engine/Shaders/Common/IO/VertexOutput.glsl b/Resources/Engine/Shaders/Common/IO/VertexOutput.part.glsl similarity index 100% rename from Resources/Engine/Shaders/Common/IO/VertexOutput.glsl rename to Resources/Engine/Shaders/Common/IO/VertexOutput.part.glsl diff --git a/Resources/Engine/Shaders/Fragment/BlinnPhong.glsl b/Resources/Engine/Shaders/Fragment/BlinnPhong.frag.glsl similarity index 97% rename from Resources/Engine/Shaders/Fragment/BlinnPhong.glsl rename to Resources/Engine/Shaders/Fragment/BlinnPhong.frag.glsl index b5e54b088..a23f348b3 100644 --- a/Resources/Engine/Shaders/Fragment/BlinnPhong.glsl +++ b/Resources/Engine/Shaders/Fragment/BlinnPhong.frag.glsl @@ -1,8 +1,8 @@ #version 430 core -#include ":Shaders/Common/IO/FragmentInput.glsl" -#include ":Shaders/Common/Buffers/EngineUBO.glsl" -#include ":Shaders/Common/Buffers/LightsSSBO.glsl" +#include ":Shaders/Common/IO/FragmentInput.part.glsl" +#include ":Shaders/Common/Buffers/EngineUBO.part.glsl" +#include ":Shaders/Common/Buffers/LightsSSBO.part.glsl" /* Uniforms (Tweakable from the material editor) */ uniform vec2 u_TextureTiling = vec2(1.0, 1.0); diff --git a/Resources/Engine/Shaders/Fragment/Lambert.glsl b/Resources/Engine/Shaders/Fragment/Lambert.frag.glsl similarity index 94% rename from Resources/Engine/Shaders/Fragment/Lambert.glsl rename to Resources/Engine/Shaders/Fragment/Lambert.frag.glsl index 54f3fbea2..3c44ec7dd 100644 --- a/Resources/Engine/Shaders/Fragment/Lambert.glsl +++ b/Resources/Engine/Shaders/Fragment/Lambert.frag.glsl @@ -2,7 +2,7 @@ out vec4 FRAGMENT_COLOR; -#include ":Shaders/Common/IO/FragmentInput.glsl" +#include ":Shaders/Common/IO/FragmentInput.part.glsl" uniform vec4 u_Diffuse = vec4(1.0, 1.0, 1.0, 1.0); uniform sampler2D u_DiffuseMap; diff --git a/Resources/Engine/Shaders/Fragment/PBR.glsl b/Resources/Engine/Shaders/Fragment/PBR.frag.glsl similarity index 97% rename from Resources/Engine/Shaders/Fragment/PBR.glsl rename to Resources/Engine/Shaders/Fragment/PBR.frag.glsl index d07f9477e..7c37a9245 100644 --- a/Resources/Engine/Shaders/Fragment/PBR.glsl +++ b/Resources/Engine/Shaders/Fragment/PBR.frag.glsl @@ -1,8 +1,8 @@ #version 430 core -#include ":Shaders/Common/IO/FragmentInput.glsl" -#include ":Shaders/Common/Buffers/EngineUBO.glsl" -#include ":Shaders/Common/Buffers/LightsSSBO.glsl" +#include ":Shaders/Common/IO/FragmentInput.part.glsl" +#include ":Shaders/Common/Buffers/EngineUBO.part.glsl" +#include ":Shaders/Common/Buffers/LightsSSBO.part.glsl" out vec4 FRAGMENT_COLOR; diff --git a/Resources/Engine/Shaders/Fragment/Unlit.glsl b/Resources/Engine/Shaders/Fragment/Unlit.frag.glsl similarity index 70% rename from Resources/Engine/Shaders/Fragment/Unlit.glsl rename to Resources/Engine/Shaders/Fragment/Unlit.frag.glsl index 8d5872687..524aed930 100644 --- a/Resources/Engine/Shaders/Fragment/Unlit.glsl +++ b/Resources/Engine/Shaders/Fragment/Unlit.frag.glsl @@ -2,15 +2,7 @@ out vec4 FRAGMENT_COLOR; -in VS_OUT -{ - vec3 FragPos; - vec3 Normal; - vec2 TexCoords; - mat3 TBN; - flat vec3 TangentViewPos; - vec3 TangentFragPos; -} fs_in; +#include ":Shaders/Common/IO/FragmentInput.part.glsl" uniform vec4 u_Diffuse = vec4(1.0, 1.0, 1.0, 1.0); uniform sampler2D u_DiffuseMap; diff --git a/Resources/Engine/Shaders/Lambert.glsl b/Resources/Engine/Shaders/Lambert.glsl index 51be4df1a..6fcca1688 100644 --- a/Resources/Engine/Shaders/Lambert.glsl +++ b/Resources/Engine/Shaders/Lambert.glsl @@ -1,4 +1,4 @@ #shader vertex -#include ":Shaders/Vertex/Basic.glsl" +#include ":Shaders/Vertex/Basic.vert.glsl" #shader fragment -#include ":Shaders/Fragment/Lambert.glsl" +#include ":Shaders/Fragment/Lambert.frag.glsl" diff --git a/Resources/Engine/Shaders/Standard.glsl b/Resources/Engine/Shaders/Standard.glsl index 7802143ef..2a739db89 100644 --- a/Resources/Engine/Shaders/Standard.glsl +++ b/Resources/Engine/Shaders/Standard.glsl @@ -1,4 +1,4 @@ #shader vertex -#include ":Shaders/Vertex/Basic.glsl" +#include ":Shaders/Vertex/Basic.vert.glsl" #shader fragment -#include ":Shaders/Fragment/BlinnPhong.glsl" +#include ":Shaders/Fragment/BlinnPhong.frag.glsl" diff --git a/Resources/Engine/Shaders/StandardPBR.glsl b/Resources/Engine/Shaders/StandardPBR.glsl index 6e5922ac2..f43c4c621 100644 --- a/Resources/Engine/Shaders/StandardPBR.glsl +++ b/Resources/Engine/Shaders/StandardPBR.glsl @@ -1,4 +1,4 @@ #shader vertex -#include ":Shaders/Vertex/Basic.glsl" +#include ":Shaders/Vertex/Basic.vert.glsl" #shader fragment -#include ":Shaders/Fragment/PBR.glsl" +#include ":Shaders/Fragment/PBR.frag.glsl" diff --git a/Resources/Engine/Shaders/Unlit.glsl b/Resources/Engine/Shaders/Unlit.glsl index d1dc7bfec..f48affb3e 100644 --- a/Resources/Engine/Shaders/Unlit.glsl +++ b/Resources/Engine/Shaders/Unlit.glsl @@ -1,4 +1,4 @@ #shader vertex -#include ":Shaders/Vertex/Basic.glsl" +#include ":Shaders/Vertex/Basic.vert.glsl" #shader fragment -#include ":Shaders/Fragment/Unlit.glsl" +#include ":Shaders/Fragment/Unlit.frag.glsl" diff --git a/Resources/Engine/Shaders/Vertex/Basic.glsl b/Resources/Engine/Shaders/Vertex/Basic.vert.glsl similarity index 89% rename from Resources/Engine/Shaders/Vertex/Basic.glsl rename to Resources/Engine/Shaders/Vertex/Basic.vert.glsl index 698f7f027..266381556 100644 --- a/Resources/Engine/Shaders/Vertex/Basic.glsl +++ b/Resources/Engine/Shaders/Vertex/Basic.vert.glsl @@ -6,8 +6,8 @@ layout (location = 2) in vec3 geo_Normal; layout (location = 3) in vec3 geo_Tangent; layout (location = 4) in vec3 geo_Bitangent; -#include ":Shaders/Common/Buffers/EngineUBO.glsl" -#include ":Shaders/Common/IO/VertexOutput.glsl" +#include ":Shaders/Common/Buffers/EngineUBO.part.glsl" +#include ":Shaders/Common/IO/VertexOutput.part.glsl" void main() { From 2d260a9d66328016889aa7dd97412771c9c41d16 Mon Sep 17 00:00:00 2001 From: Adrien GIVRY Date: Thu, 23 Nov 2023 17:22:18 -0500 Subject: [PATCH 08/12] Fixed dependir usage in postbuildcommands --- Sources/Overload/OvEditor/premake5.lua | 2 +- Sources/Overload/OvGame/premake5.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/Overload/OvEditor/premake5.lua b/Sources/Overload/OvEditor/premake5.lua index e72fe60f0..37a2a20a0 100644 --- a/Sources/Overload/OvEditor/premake5.lua +++ b/Sources/Overload/OvEditor/premake5.lua @@ -17,7 +17,7 @@ project "OvEditor" debugdir "%{wks.location}/../../Build/%{cfg.buildcfg}" postbuildcommands { - "for /f \"delims=|\" %%i in ('dir /B /S \"%{dependdir}*.dll\"') do xcopy /Q /Y \"%%i\" \"%{outputdir}%{cfg.buildcfg}\\%{prj.name}\"", + "for /f \"delims=|\" %%i in ('dir /B /S \"%{dependdir}\\*.dll\"') do xcopy /Q /Y \"%%i\" \"%{outputdir}%{cfg.buildcfg}\\%{prj.name}\"", "rmdir /s /q \"%{builddir}%{cfg.buildcfg}\\Data\"", diff --git a/Sources/Overload/OvGame/premake5.lua b/Sources/Overload/OvGame/premake5.lua index 953fd0753..5dd43e6dc 100644 --- a/Sources/Overload/OvGame/premake5.lua +++ b/Sources/Overload/OvGame/premake5.lua @@ -16,7 +16,7 @@ project "OvGame" characterset ("MBCS") postbuildcommands { - "for /f \"delims=|\" %%i in ('dir /B /S \"%{dependdir}*.dll\"') do xcopy /Q /Y \"%%i\" \"%{builddir}%{cfg.buildcfg}\\%{prj.name}\"", + "for /f \"delims=|\" %%i in ('dir /B /S \"%{dependdir}\\*.dll\"') do xcopy /Q /Y \"%%i\" \"%{builddir}%{cfg.buildcfg}\\%{prj.name}\"", "xcopy /Y /I /Q /D \"%{outputdir}Debug\\%{prj.name}\\*.exe\" \"%{builddir}%{cfg.buildcfg}\\Builder\\Development\"", "xcopy /Y /I /Q /D \"%{outputdir}Debug\\%{prj.name}\\*.dll\" \"%{builddir}%{cfg.buildcfg}\\Builder\\Development\"", From 8b39ca16228b10b81e08d1932eeee40d91e7ab8b Mon Sep 17 00:00:00 2001 From: Adrien GIVRY Date: Tue, 19 Mar 2024 14:33:17 -0400 Subject: [PATCH 09/12] Updated extension for shaders and added icon for shader parts --- Resources/Editor/Icons/puzzle.png | Bin 0 -> 456 bytes .../{Billboard.glsl => Billboard.ovfx} | 0 .../Editor/Shaders/{Gizmo.glsl => Gizmo.ovfx} | 0 .../Editor/Shaders/{Grid.glsl => Grid.ovfx} | 0 Resources/Engine/Materials/Default.ovmat | 2 +- .../{EngineUBO.part.glsl => EngineUBO.ovfxh} | 0 ...{LightsSSBO.part.glsl => LightsSSBO.ovfxh} | 0 ...entInput.part.glsl => FragmentInput.ovfxh} | 0 .../Shaders/Common/IO/VertexInput.ovfxh | 5 ++ ...texOutput.part.glsl => VertexOutput.ovfxh} | 0 ...{BlinnPhong.frag.glsl => BlinnPhong.ovfxh} | 6 +-- .../{Lambert.frag.glsl => Lambert.ovfxh} | 2 +- .../Fragment/{PBR.frag.glsl => PBR.ovfxh} | 6 +-- .../Fragment/{Unlit.frag.glsl => Unlit.ovfxh} | 2 +- Resources/Engine/Shaders/Lambert.glsl | 4 -- Resources/Engine/Shaders/Lambert.ovfx | 4 ++ Resources/Engine/Shaders/Standard.glsl | 4 -- Resources/Engine/Shaders/Standard.ovfx | 4 ++ Resources/Engine/Shaders/StandardPBR.glsl | 4 -- Resources/Engine/Shaders/StandardPBR.ovfx | 4 ++ Resources/Engine/Shaders/Unlit.glsl | 4 -- Resources/Engine/Shaders/Unlit.ovfx | 4 ++ .../Vertex/{Basic.vert.glsl => Basic.ovfxh} | 11 ++--- .../src/OvEditor/Core/EditorActions.cpp | 12 +++-- .../src/OvEditor/Core/EditorResources.cpp | 11 +++-- .../src/OvEditor/Panels/AssetBrowser.cpp | 44 +++++++++++------- .../src/OvEditor/Panels/AssetView.cpp | 4 +- .../src/OvEditor/Panels/SceneView.cpp | 2 +- .../OvEditor/Rendering/DebugSceneRenderer.cpp | 2 +- .../Rendering/OutlineRenderFeature.cpp | 4 +- .../OvEditor/Rendering/PickingRenderPass.cpp | 2 +- .../Overload/OvGame/src/OvGame/Core/Game.cpp | 5 -- .../include/OvTools/Utils/PathParser.h | 1 + .../OvTools/src/OvTools/Utils/PathParser.cpp | 4 +- 34 files changed, 86 insertions(+), 71 deletions(-) create mode 100644 Resources/Editor/Icons/puzzle.png rename Resources/Editor/Shaders/{Billboard.glsl => Billboard.ovfx} (100%) rename Resources/Editor/Shaders/{Gizmo.glsl => Gizmo.ovfx} (100%) rename Resources/Editor/Shaders/{Grid.glsl => Grid.ovfx} (100%) rename Resources/Engine/Shaders/Common/Buffers/{EngineUBO.part.glsl => EngineUBO.ovfxh} (100%) rename Resources/Engine/Shaders/Common/Buffers/{LightsSSBO.part.glsl => LightsSSBO.ovfxh} (100%) rename Resources/Engine/Shaders/Common/IO/{FragmentInput.part.glsl => FragmentInput.ovfxh} (100%) create mode 100644 Resources/Engine/Shaders/Common/IO/VertexInput.ovfxh rename Resources/Engine/Shaders/Common/IO/{VertexOutput.part.glsl => VertexOutput.ovfxh} (100%) rename Resources/Engine/Shaders/Fragment/{BlinnPhong.frag.glsl => BlinnPhong.ovfxh} (97%) rename Resources/Engine/Shaders/Fragment/{Lambert.frag.glsl => Lambert.ovfxh} (94%) rename Resources/Engine/Shaders/Fragment/{PBR.frag.glsl => PBR.ovfxh} (97%) rename Resources/Engine/Shaders/Fragment/{Unlit.frag.glsl => Unlit.ovfxh} (89%) delete mode 100644 Resources/Engine/Shaders/Lambert.glsl create mode 100644 Resources/Engine/Shaders/Lambert.ovfx delete mode 100644 Resources/Engine/Shaders/Standard.glsl create mode 100644 Resources/Engine/Shaders/Standard.ovfx delete mode 100644 Resources/Engine/Shaders/StandardPBR.glsl create mode 100644 Resources/Engine/Shaders/StandardPBR.ovfx delete mode 100644 Resources/Engine/Shaders/Unlit.glsl create mode 100644 Resources/Engine/Shaders/Unlit.ovfx rename Resources/Engine/Shaders/Vertex/{Basic.vert.glsl => Basic.ovfxh} (67%) diff --git a/Resources/Editor/Icons/puzzle.png b/Resources/Editor/Icons/puzzle.png new file mode 100644 index 0000000000000000000000000000000000000000..8b014d0c6c2a060b3f88738ef2b7cd5744689c8e GIT binary patch literal 456 zcmV;(0XP1MP)EJHIsoE8q8_a$d*xsN+;yE0?(Gl0uZ3LBsg{dNCTHmT z(x!ndPy=oIUU%FFAE1(1T@bn&psKEY_*!sKX4*$KE;s}{OGYCcRI+f_VrS>q0F_K~Qn2II1IK9Q yv^?QNukHu9RtB2>60`^KxS1XiB!GtJl)eBGv1-qmh}Mw+0000 - :Shaders\Standard.glsl + :Shaders\Standard.ovfx false true diff --git a/Resources/Engine/Shaders/Common/Buffers/EngineUBO.part.glsl b/Resources/Engine/Shaders/Common/Buffers/EngineUBO.ovfxh similarity index 100% rename from Resources/Engine/Shaders/Common/Buffers/EngineUBO.part.glsl rename to Resources/Engine/Shaders/Common/Buffers/EngineUBO.ovfxh diff --git a/Resources/Engine/Shaders/Common/Buffers/LightsSSBO.part.glsl b/Resources/Engine/Shaders/Common/Buffers/LightsSSBO.ovfxh similarity index 100% rename from Resources/Engine/Shaders/Common/Buffers/LightsSSBO.part.glsl rename to Resources/Engine/Shaders/Common/Buffers/LightsSSBO.ovfxh diff --git a/Resources/Engine/Shaders/Common/IO/FragmentInput.part.glsl b/Resources/Engine/Shaders/Common/IO/FragmentInput.ovfxh similarity index 100% rename from Resources/Engine/Shaders/Common/IO/FragmentInput.part.glsl rename to Resources/Engine/Shaders/Common/IO/FragmentInput.ovfxh diff --git a/Resources/Engine/Shaders/Common/IO/VertexInput.ovfxh b/Resources/Engine/Shaders/Common/IO/VertexInput.ovfxh new file mode 100644 index 000000000..816cbcc58 --- /dev/null +++ b/Resources/Engine/Shaders/Common/IO/VertexInput.ovfxh @@ -0,0 +1,5 @@ +layout (location = 0) in vec3 geo_Pos; +layout (location = 1) in vec2 geo_TexCoords; +layout (location = 2) in vec3 geo_Normal; +layout (location = 3) in vec3 geo_Tangent; +layout (location = 4) in vec3 geo_Bitangent; \ No newline at end of file diff --git a/Resources/Engine/Shaders/Common/IO/VertexOutput.part.glsl b/Resources/Engine/Shaders/Common/IO/VertexOutput.ovfxh similarity index 100% rename from Resources/Engine/Shaders/Common/IO/VertexOutput.part.glsl rename to Resources/Engine/Shaders/Common/IO/VertexOutput.ovfxh diff --git a/Resources/Engine/Shaders/Fragment/BlinnPhong.frag.glsl b/Resources/Engine/Shaders/Fragment/BlinnPhong.ovfxh similarity index 97% rename from Resources/Engine/Shaders/Fragment/BlinnPhong.frag.glsl rename to Resources/Engine/Shaders/Fragment/BlinnPhong.ovfxh index a23f348b3..9606043a3 100644 --- a/Resources/Engine/Shaders/Fragment/BlinnPhong.frag.glsl +++ b/Resources/Engine/Shaders/Fragment/BlinnPhong.ovfxh @@ -1,8 +1,8 @@ #version 430 core -#include ":Shaders/Common/IO/FragmentInput.part.glsl" -#include ":Shaders/Common/Buffers/EngineUBO.part.glsl" -#include ":Shaders/Common/Buffers/LightsSSBO.part.glsl" +#include ":Shaders/Common/IO/FragmentInput.ovfxh" +#include ":Shaders/Common/Buffers/EngineUBO.ovfxh" +#include ":Shaders/Common/Buffers/LightsSSBO.ovfxh" /* Uniforms (Tweakable from the material editor) */ uniform vec2 u_TextureTiling = vec2(1.0, 1.0); diff --git a/Resources/Engine/Shaders/Fragment/Lambert.frag.glsl b/Resources/Engine/Shaders/Fragment/Lambert.ovfxh similarity index 94% rename from Resources/Engine/Shaders/Fragment/Lambert.frag.glsl rename to Resources/Engine/Shaders/Fragment/Lambert.ovfxh index 3c44ec7dd..ee841fd41 100644 --- a/Resources/Engine/Shaders/Fragment/Lambert.frag.glsl +++ b/Resources/Engine/Shaders/Fragment/Lambert.ovfxh @@ -2,7 +2,7 @@ out vec4 FRAGMENT_COLOR; -#include ":Shaders/Common/IO/FragmentInput.part.glsl" +#include ":Shaders/Common/IO/FragmentInput.ovfxh" uniform vec4 u_Diffuse = vec4(1.0, 1.0, 1.0, 1.0); uniform sampler2D u_DiffuseMap; diff --git a/Resources/Engine/Shaders/Fragment/PBR.frag.glsl b/Resources/Engine/Shaders/Fragment/PBR.ovfxh similarity index 97% rename from Resources/Engine/Shaders/Fragment/PBR.frag.glsl rename to Resources/Engine/Shaders/Fragment/PBR.ovfxh index 7c37a9245..0fb3bd6e5 100644 --- a/Resources/Engine/Shaders/Fragment/PBR.frag.glsl +++ b/Resources/Engine/Shaders/Fragment/PBR.ovfxh @@ -1,8 +1,8 @@ #version 430 core -#include ":Shaders/Common/IO/FragmentInput.part.glsl" -#include ":Shaders/Common/Buffers/EngineUBO.part.glsl" -#include ":Shaders/Common/Buffers/LightsSSBO.part.glsl" +#include ":Shaders/Common/IO/FragmentInput.ovfxh" +#include ":Shaders/Common/Buffers/EngineUBO.ovfxh" +#include ":Shaders/Common/Buffers/LightsSSBO.ovfxh" out vec4 FRAGMENT_COLOR; diff --git a/Resources/Engine/Shaders/Fragment/Unlit.frag.glsl b/Resources/Engine/Shaders/Fragment/Unlit.ovfxh similarity index 89% rename from Resources/Engine/Shaders/Fragment/Unlit.frag.glsl rename to Resources/Engine/Shaders/Fragment/Unlit.ovfxh index 524aed930..56df673b2 100644 --- a/Resources/Engine/Shaders/Fragment/Unlit.frag.glsl +++ b/Resources/Engine/Shaders/Fragment/Unlit.ovfxh @@ -2,7 +2,7 @@ out vec4 FRAGMENT_COLOR; -#include ":Shaders/Common/IO/FragmentInput.part.glsl" +#include ":Shaders/Common/IO/FragmentInput.ovfxh" uniform vec4 u_Diffuse = vec4(1.0, 1.0, 1.0, 1.0); uniform sampler2D u_DiffuseMap; diff --git a/Resources/Engine/Shaders/Lambert.glsl b/Resources/Engine/Shaders/Lambert.glsl deleted file mode 100644 index 6fcca1688..000000000 --- a/Resources/Engine/Shaders/Lambert.glsl +++ /dev/null @@ -1,4 +0,0 @@ -#shader vertex -#include ":Shaders/Vertex/Basic.vert.glsl" -#shader fragment -#include ":Shaders/Fragment/Lambert.frag.glsl" diff --git a/Resources/Engine/Shaders/Lambert.ovfx b/Resources/Engine/Shaders/Lambert.ovfx new file mode 100644 index 000000000..84ada91d9 --- /dev/null +++ b/Resources/Engine/Shaders/Lambert.ovfx @@ -0,0 +1,4 @@ +#shader vertex +#include ":Shaders/Vertex/Basic.ovfxh" +#shader fragment +#include ":Shaders/Fragment/Lambert.ovfxh" diff --git a/Resources/Engine/Shaders/Standard.glsl b/Resources/Engine/Shaders/Standard.glsl deleted file mode 100644 index 2a739db89..000000000 --- a/Resources/Engine/Shaders/Standard.glsl +++ /dev/null @@ -1,4 +0,0 @@ -#shader vertex -#include ":Shaders/Vertex/Basic.vert.glsl" -#shader fragment -#include ":Shaders/Fragment/BlinnPhong.frag.glsl" diff --git a/Resources/Engine/Shaders/Standard.ovfx b/Resources/Engine/Shaders/Standard.ovfx new file mode 100644 index 000000000..ea8d9ba6d --- /dev/null +++ b/Resources/Engine/Shaders/Standard.ovfx @@ -0,0 +1,4 @@ +#shader vertex +#include ":Shaders/Vertex/Basic.ovfxh" +#shader fragment +#include ":Shaders/Fragment/BlinnPhong.ovfxh" diff --git a/Resources/Engine/Shaders/StandardPBR.glsl b/Resources/Engine/Shaders/StandardPBR.glsl deleted file mode 100644 index f43c4c621..000000000 --- a/Resources/Engine/Shaders/StandardPBR.glsl +++ /dev/null @@ -1,4 +0,0 @@ -#shader vertex -#include ":Shaders/Vertex/Basic.vert.glsl" -#shader fragment -#include ":Shaders/Fragment/PBR.frag.glsl" diff --git a/Resources/Engine/Shaders/StandardPBR.ovfx b/Resources/Engine/Shaders/StandardPBR.ovfx new file mode 100644 index 000000000..738951933 --- /dev/null +++ b/Resources/Engine/Shaders/StandardPBR.ovfx @@ -0,0 +1,4 @@ +#shader vertex +#include ":Shaders/Vertex/Basic.ovfxh" +#shader fragment +#include ":Shaders/Fragment/PBR.ovfxh" diff --git a/Resources/Engine/Shaders/Unlit.glsl b/Resources/Engine/Shaders/Unlit.glsl deleted file mode 100644 index f48affb3e..000000000 --- a/Resources/Engine/Shaders/Unlit.glsl +++ /dev/null @@ -1,4 +0,0 @@ -#shader vertex -#include ":Shaders/Vertex/Basic.vert.glsl" -#shader fragment -#include ":Shaders/Fragment/Unlit.frag.glsl" diff --git a/Resources/Engine/Shaders/Unlit.ovfx b/Resources/Engine/Shaders/Unlit.ovfx new file mode 100644 index 000000000..08569d779 --- /dev/null +++ b/Resources/Engine/Shaders/Unlit.ovfx @@ -0,0 +1,4 @@ +#shader vertex +#include ":Shaders/Vertex/Basic.ovfxh" +#shader fragment +#include ":Shaders/Fragment/Unlit.ovfxh" diff --git a/Resources/Engine/Shaders/Vertex/Basic.vert.glsl b/Resources/Engine/Shaders/Vertex/Basic.ovfxh similarity index 67% rename from Resources/Engine/Shaders/Vertex/Basic.vert.glsl rename to Resources/Engine/Shaders/Vertex/Basic.ovfxh index 266381556..752aa3593 100644 --- a/Resources/Engine/Shaders/Vertex/Basic.vert.glsl +++ b/Resources/Engine/Shaders/Vertex/Basic.ovfxh @@ -1,13 +1,8 @@ #version 430 core -layout (location = 0) in vec3 geo_Pos; -layout (location = 1) in vec2 geo_TexCoords; -layout (location = 2) in vec3 geo_Normal; -layout (location = 3) in vec3 geo_Tangent; -layout (location = 4) in vec3 geo_Bitangent; - -#include ":Shaders/Common/Buffers/EngineUBO.part.glsl" -#include ":Shaders/Common/IO/VertexOutput.part.glsl" +#include ":Shaders/Common/IO/VertexInput.ovfxh" +#include ":Shaders/Common/Buffers/EngineUBO.ovfxh" +#include ":Shaders/Common/IO/VertexOutput.ovfxh" void main() { diff --git a/Sources/Overload/OvEditor/src/OvEditor/Core/EditorActions.cpp b/Sources/Overload/OvEditor/src/OvEditor/Core/EditorActions.cpp index 1afcbde39..d1441e34d 100644 --- a/Sources/Overload/OvEditor/src/OvEditor/Core/EditorActions.cpp +++ b/Sources/Overload/OvEditor/src/OvEditor/Core/EditorActions.cpp @@ -625,14 +625,16 @@ bool OvEditor::Core::EditorActions::ImportAsset(const std::string& p_initialDest std::string modelFormats = "*.fbx;*.obj;"; std::string textureFormats = "*.png;*.jpeg;*.jpg;*.tga"; - std::string shaderFormats = "*.glsl;"; + std::string shaderFormats = "*.ovfx"; + std::string shaderPartFormats = "*.ovfxh"; std::string soundFormats = "*.mp3;*.ogg;*.wav;"; OpenFileDialog selectAssetDialog("Select an asset to import"); selectAssetDialog.AddFileType("Any supported format", modelFormats + textureFormats + shaderFormats + soundFormats); selectAssetDialog.AddFileType("Model (.fbx, .obj)", modelFormats); selectAssetDialog.AddFileType("Texture (.png, .jpeg, .jpg, .tga)", textureFormats); - selectAssetDialog.AddFileType("Shader (.glsl)", shaderFormats); + selectAssetDialog.AddFileType("Shader (.ovfx)", shaderFormats); + selectAssetDialog.AddFileType("Shader Parts (.ovfxh)", shaderPartFormats); selectAssetDialog.AddFileType("Sound (.mp3, .ogg, .wav)", soundFormats); selectAssetDialog.Show(); @@ -669,14 +671,16 @@ bool OvEditor::Core::EditorActions::ImportAssetAtLocation(const std::string& p_d std::string modelFormats = "*.fbx;*.obj;"; std::string textureFormats = "*.png;*.jpeg;*.jpg;*.tga;"; - std::string shaderFormats = "*.glsl;"; + std::string shaderFormats = "*.ovfx"; + std::string shaderPartFormats = "*.ovfxh"; std::string soundFormats = "*.mp3;*.ogg;*.wav;"; OpenFileDialog selectAssetDialog("Select an asset to import"); selectAssetDialog.AddFileType("Any supported format", modelFormats + textureFormats + shaderFormats + soundFormats); selectAssetDialog.AddFileType("Model (.fbx, .obj)", modelFormats); selectAssetDialog.AddFileType("Texture (.png, .jpeg, .jpg, .tga)", textureFormats); - selectAssetDialog.AddFileType("Shader (.glsl)", shaderFormats); + selectAssetDialog.AddFileType("Shader (.ovfx)", shaderFormats); + selectAssetDialog.AddFileType("Shader Parts (.ovfxh)", shaderPartFormats); selectAssetDialog.AddFileType("Sound (.mp3, .ogg, .wav)", soundFormats); selectAssetDialog.Show(); diff --git a/Sources/Overload/OvEditor/src/OvEditor/Core/EditorResources.cpp b/Sources/Overload/OvEditor/src/OvEditor/Core/EditorResources.cpp index b07161add..ff784c5e0 100644 --- a/Sources/Overload/OvEditor/src/OvEditor/Core/EditorResources.cpp +++ b/Sources/Overload/OvEditor/src/OvEditor/Core/EditorResources.cpp @@ -17,8 +17,7 @@ OvEditor::Core::EditorResources::EditorResources(const std::string& p_editorAsse { using namespace OvRendering::Resources::Loaders; - std::string buttonsFolder = p_editorAssetsPath + "Textures\\Buttons\\"; - std::string iconsFolder = p_editorAssetsPath + "Textures\\Icons\\"; + std::string iconsFolder = p_editorAssetsPath + "Icons\\"; std::string modelsFolder = p_editorAssetsPath + "Models\\"; std::string shadersFolder = p_editorAssetsPath + "Shaders\\"; @@ -147,6 +146,8 @@ OvEditor::Core::EditorResources::EditorResources(const std::string& p_editorAsse m_textures["Bill_Ambient_Sphere_Light"] = TextureLoader::CreateFromMemory(reinterpret_cast(raw.data()), 128, 128, firstFilterBillboard, secondFilterBillboard, false); } + m_textures["Icon_Shader_Part"] = TextureLoader::Create(iconsFolder + "puzzle.png", firstFilterEditor, secondFilterEditor, false); + /* Models */ m_models["Cube"] = ModelLoader::Create(modelsFolder + "Cube.fbx", modelParserFlags); m_models["Cylinder"] = ModelLoader::Create(modelsFolder + "Cylinder.fbx", modelParserFlags); @@ -161,9 +162,9 @@ OvEditor::Core::EditorResources::EditorResources(const std::string& p_editorAsse m_models["Camera"] = ModelLoader::Create(modelsFolder + "Camera.fbx", modelParserFlags); /* Shaders */ - m_shaders["Grid"] = ShaderLoader::Create(shadersFolder + "Grid.glsl"); - m_shaders["Gizmo"] = ShaderLoader::Create(shadersFolder + "Gizmo.glsl"); - m_shaders["Billboard"] = ShaderLoader::Create(shadersFolder + "Billboard.glsl"); + m_shaders["Grid"] = ShaderLoader::Create(shadersFolder + "Grid.ovfx"); + m_shaders["Gizmo"] = ShaderLoader::Create(shadersFolder + "Gizmo.ovfx"); + m_shaders["Billboard"] = ShaderLoader::Create(shadersFolder + "Billboard.ovfx"); /* From memory */ { diff --git a/Sources/Overload/OvEditor/src/OvEditor/Panels/AssetBrowser.cpp b/Sources/Overload/OvEditor/src/OvEditor/Panels/AssetBrowser.cpp index 2fbd76cab..1a7889378 100644 --- a/Sources/Overload/OvEditor/src/OvEditor/Panels/AssetBrowser.cpp +++ b/Sources/Overload/OvEditor/src/OvEditor/Panels/AssetBrowser.cpp @@ -287,12 +287,12 @@ class FolderContextualMenu : public BrowserItemContextualMenu do { - finalPath = filePath + '\\' + (!fails ? newShaderName : newShaderName + " (" + std::to_string(fails) + ')') + ".glsl"; + finalPath = filePath + '\\' + (!fails ? newShaderName : newShaderName + " (" + std::to_string(fails) + ')') + ".ovfx"; ++fails; } while (std::filesystem::exists(finalPath)); - std::filesystem::copy_file(EDITOR_CONTEXT(engineAssetsPath) + "Shaders\\Standard.glsl", finalPath); + std::filesystem::copy_file(EDITOR_CONTEXT(engineAssetsPath) + "Shaders\\Standard.ovfx", finalPath); ItemAddedEvent.Invoke(finalPath); Close(); }; @@ -304,12 +304,12 @@ class FolderContextualMenu : public BrowserItemContextualMenu do { - finalPath = filePath + '\\' + (!fails ? newShaderName : newShaderName + " (" + std::to_string(fails) + ')') + ".glsl"; + finalPath = filePath + '\\' + (!fails ? newShaderName : newShaderName + " (" + std::to_string(fails) + ')') + ".ovfx"; ++fails; } while (std::filesystem::exists(finalPath)); - std::filesystem::copy_file(EDITOR_CONTEXT(engineAssetsPath) + "Shaders\\StandardPBR.glsl", finalPath); + std::filesystem::copy_file(EDITOR_CONTEXT(engineAssetsPath) + "Shaders\\StandardPBR.ovfx", finalPath); ItemAddedEvent.Invoke(finalPath); Close(); }; @@ -321,12 +321,12 @@ class FolderContextualMenu : public BrowserItemContextualMenu do { - finalPath = filePath + '\\' + (!fails ? newShaderName : newShaderName + " (" + std::to_string(fails) + ')') + ".glsl"; + finalPath = filePath + '\\' + (!fails ? newShaderName : newShaderName + " (" + std::to_string(fails) + ')') + ".ovfx"; ++fails; } while (std::filesystem::exists(finalPath)); - std::filesystem::copy_file(EDITOR_CONTEXT(engineAssetsPath) + "Shaders\\Unlit.glsl", finalPath); + std::filesystem::copy_file(EDITOR_CONTEXT(engineAssetsPath) + "Shaders\\Unlit.ovfx", finalPath); ItemAddedEvent.Invoke(finalPath); Close(); }; @@ -338,12 +338,12 @@ class FolderContextualMenu : public BrowserItemContextualMenu do { - finalPath = filePath + '\\' + (!fails ? newShaderName : newShaderName + " (" + std::to_string(fails) + ')') + ".glsl"; + finalPath = filePath + '\\' + (!fails ? newShaderName : newShaderName + " (" + std::to_string(fails) + ')') + ".ovfx"; ++fails; } while (std::filesystem::exists(finalPath)); - std::filesystem::copy_file(EDITOR_CONTEXT(engineAssetsPath) + "Shaders\\Lambert.glsl", finalPath); + std::filesystem::copy_file(EDITOR_CONTEXT(engineAssetsPath) + "Shaders\\Lambert.ovfx", finalPath); ItemAddedEvent.Invoke(finalPath); Close(); }; @@ -392,7 +392,7 @@ class FolderContextualMenu : public BrowserItemContextualMenu { std::ofstream outfile(finalPath); - outfile << ":Shaders\\Standard.glsl" << std::endl; // Empty standard material content + outfile << ":Shaders\\Standard.ovfx" << std::endl; // Empty standard material content } ItemAddedEvent.Invoke(finalPath); @@ -422,7 +422,7 @@ class FolderContextualMenu : public BrowserItemContextualMenu { std::ofstream outfile(finalPath); - outfile << ":Shaders\\StandardPBR.glsl" << std::endl; // Empty standard material content + outfile << ":Shaders\\StandardPBR.ovfx" << std::endl; // Empty standard material content } ItemAddedEvent.Invoke(finalPath); @@ -453,7 +453,7 @@ class FolderContextualMenu : public BrowserItemContextualMenu { std::ofstream outfile(finalPath); - outfile << ":Shaders\\Unlit.glsl" << std::endl; // Empty unlit material content + outfile << ":Shaders\\Unlit.ovfx" << std::endl; // Empty unlit material content } ItemAddedEvent.Invoke(finalPath); @@ -483,7 +483,7 @@ class FolderContextualMenu : public BrowserItemContextualMenu { std::ofstream outfile(finalPath); - outfile << ":Shaders\\Lambert.glsl" << std::endl; // Empty unlit material content + outfile << ":Shaders\\Lambert.ovfx" << std::endl; // Empty unlit material content } ItemAddedEvent.Invoke(finalPath); @@ -697,6 +697,17 @@ class ShaderContextualMenu : public FileContextualMenu } }; +class ShaderPartContextualMenu : public FileContextualMenu +{ +public: + ShaderPartContextualMenu(const std::string& p_filePath, bool p_protected = false) : FileContextualMenu(p_filePath, p_protected) {} + + virtual void CreateList() override + { + FileContextualMenu::CreateList(); + } +}; + class ModelContextualMenu : public PreviewableContextualMenu { public: @@ -740,7 +751,7 @@ class ModelContextualMenu : public PreviewableContextualMenu:Shaders\\Standard.glsl" << std::endl; // Empty standard material content + outfile << ":Shaders\\Standard.ovfx" << std::endl; // Empty standard material content } DuplicateEvent.Invoke(finalPath); @@ -768,7 +779,7 @@ class ModelContextualMenu : public PreviewableContextualMenu:Shaders\\StandardPBR.glsl" << std::endl; // Empty standard material content + outfile << ":Shaders\\StandardPBR.ovfx" << std::endl; // Empty standard material content } DuplicateEvent.Invoke(finalPath); @@ -796,7 +807,7 @@ class ModelContextualMenu : public PreviewableContextualMenu:Shaders\\Unlit.glsl" << std::endl; // Empty standard material content + outfile << ":Shaders\\Unlit.ovfx" << std::endl; // Empty standard material content } DuplicateEvent.Invoke(finalPath); @@ -824,7 +835,7 @@ class ModelContextualMenu : public PreviewableContextualMenu:Shaders\\Lambert.glsl" << std::endl; // Empty standard material content + outfile << ":Shaders\\Lambert.ovfx" << std::endl; // Empty standard material content } DuplicateEvent.Invoke(finalPath); @@ -1222,6 +1233,7 @@ void OvEditor::Panels::AssetBrowser::ConsiderItem(OvUI::Widgets::Layout::TreeNod case OvTools::Utils::PathParser::EFileType::MODEL: contextMenu = &clickableText.AddPlugin(path, protectedItem); break; case OvTools::Utils::PathParser::EFileType::TEXTURE: contextMenu = &clickableText.AddPlugin(path, protectedItem); break; case OvTools::Utils::PathParser::EFileType::SHADER: contextMenu = &clickableText.AddPlugin(path, protectedItem); break; + case OvTools::Utils::PathParser::EFileType::SHADER_PART:contextMenu = &clickableText.AddPlugin(path, protectedItem); break; case OvTools::Utils::PathParser::EFileType::MATERIAL: contextMenu = &clickableText.AddPlugin(path, protectedItem); break; case OvTools::Utils::PathParser::EFileType::SCENE: contextMenu = &clickableText.AddPlugin(path, protectedItem); break; default: contextMenu = &clickableText.AddPlugin(path, protectedItem); break; diff --git a/Sources/Overload/OvEditor/src/OvEditor/Panels/AssetView.cpp b/Sources/Overload/OvEditor/src/OvEditor/Panels/AssetView.cpp index 919d86c84..63c1583ed 100644 --- a/Sources/Overload/OvEditor/src/OvEditor/Panels/AssetView.cpp +++ b/Sources/Overload/OvEditor/src/OvEditor/Panels/AssetView.cpp @@ -50,13 +50,13 @@ OvEditor::Panels::AssetView::AssetView m_cameraController.LockTargetActor(*m_assetActor); /* Default Material */ - m_defaultMaterial.SetShader(EDITOR_CONTEXT(shaderManager)[":Shaders\\Standard.glsl"]); + m_defaultMaterial.SetShader(EDITOR_CONTEXT(shaderManager)[":Shaders\\Standard.ovfx"]); m_defaultMaterial.Set("u_Diffuse", OvMaths::FVector4(1.f, 1.f, 1.f, 1.f)); m_defaultMaterial.Set("u_Shininess", 100.0f); m_defaultMaterial.Set("u_DiffuseMap", nullptr); /* Texture Material */ - m_textureMaterial.SetShader(EDITOR_CONTEXT(shaderManager)[":Shaders\\Unlit.glsl"]); + m_textureMaterial.SetShader(EDITOR_CONTEXT(shaderManager)[":Shaders\\Unlit.ovfx"]); m_textureMaterial.Set("u_Diffuse", OvMaths::FVector4(1.f, 1.f, 1.f, 1.f)); m_textureMaterial.SetBackfaceCulling(false); m_textureMaterial.SetBlendable(true); diff --git a/Sources/Overload/OvEditor/src/OvEditor/Panels/SceneView.cpp b/Sources/Overload/OvEditor/src/OvEditor/Panels/SceneView.cpp index c4033ee99..81403e78a 100644 --- a/Sources/Overload/OvEditor/src/OvEditor/Panels/SceneView.cpp +++ b/Sources/Overload/OvEditor/src/OvEditor/Panels/SceneView.cpp @@ -25,7 +25,7 @@ OvEditor::Panels::SceneView::SceneView m_camera.SetFar(5000.0f); - m_fallbackMaterial.SetShader(EDITOR_CONTEXT(shaderManager)[":Shaders\\Unlit.glsl"]); + m_fallbackMaterial.SetShader(EDITOR_CONTEXT(shaderManager)[":Shaders\\Unlit.ovfx"]); m_fallbackMaterial.Set("u_Diffuse", { 1.f, 0.f, 1.f, 1.0f }); m_fallbackMaterial.Set("u_DiffuseMap", nullptr); diff --git a/Sources/Overload/OvEditor/src/OvEditor/Rendering/DebugSceneRenderer.cpp b/Sources/Overload/OvEditor/src/OvEditor/Rendering/DebugSceneRenderer.cpp index f21778fde..73c8f7004 100644 --- a/Sources/Overload/OvEditor/src/OvEditor/Rendering/DebugSceneRenderer.cpp +++ b/Sources/Overload/OvEditor/src/OvEditor/Rendering/DebugSceneRenderer.cpp @@ -77,7 +77,7 @@ class DebugCamerasRenderPass : public OvRendering::Core::ARenderPass public: DebugCamerasRenderPass(OvRendering::Core::CompositeRenderer& p_renderer) : OvRendering::Core::ARenderPass(p_renderer) { - m_cameraMaterial.SetShader(EDITOR_CONTEXT(shaderManager)[":Shaders\\Lambert.glsl"]); + m_cameraMaterial.SetShader(EDITOR_CONTEXT(shaderManager)[":Shaders\\Lambert.ovfx"]); m_cameraMaterial.Set("u_Diffuse", FVector4(0.0f, 0.3f, 0.7f, 1.0f)); m_cameraMaterial.Set("u_DiffuseMap", nullptr); } diff --git a/Sources/Overload/OvEditor/src/OvEditor/Rendering/OutlineRenderFeature.cpp b/Sources/Overload/OvEditor/src/OvEditor/Rendering/OutlineRenderFeature.cpp index 13d56fa24..c115c8f2d 100644 --- a/Sources/Overload/OvEditor/src/OvEditor/Rendering/OutlineRenderFeature.cpp +++ b/Sources/Overload/OvEditor/src/OvEditor/Rendering/OutlineRenderFeature.cpp @@ -20,14 +20,14 @@ OvEditor::Rendering::OutlineRenderFeature::OutlineRenderFeature(OvRendering::Cor OvRendering::Features::ARenderFeature(p_renderer) { /* Stencil Fill Material */ - m_stencilFillMaterial.SetShader(EDITOR_CONTEXT(shaderManager)[":Shaders\\Unlit.glsl"]); + m_stencilFillMaterial.SetShader(EDITOR_CONTEXT(shaderManager)[":Shaders\\Unlit.ovfx"]); m_stencilFillMaterial.SetBackfaceCulling(true); m_stencilFillMaterial.SetDepthTest(false); m_stencilFillMaterial.SetColorWriting(false); m_stencilFillMaterial.Set("u_DiffuseMap", nullptr); /* Outline Material */ - m_outlineMaterial.SetShader(EDITOR_CONTEXT(shaderManager)[":Shaders\\Unlit.glsl"]); + m_outlineMaterial.SetShader(EDITOR_CONTEXT(shaderManager)[":Shaders\\Unlit.ovfx"]); m_outlineMaterial.Set("u_DiffuseMap", nullptr); m_outlineMaterial.SetDepthTest(false); } diff --git a/Sources/Overload/OvEditor/src/OvEditor/Rendering/PickingRenderPass.cpp b/Sources/Overload/OvEditor/src/OvEditor/Rendering/PickingRenderPass.cpp index c1ba0ba83..d04363f63 100644 --- a/Sources/Overload/OvEditor/src/OvEditor/Rendering/PickingRenderPass.cpp +++ b/Sources/Overload/OvEditor/src/OvEditor/Rendering/PickingRenderPass.cpp @@ -27,7 +27,7 @@ OvEditor::Rendering::PickingRenderPass::PickingRenderPass(OvRendering::Core::Com m_gizmoPickingMaterial.Set("u_IsPickable", true); /* Picking Material */ - m_actorPickingMaterial.SetShader(EDITOR_CONTEXT(shaderManager)[":Shaders\\Unlit.glsl"]); + m_actorPickingMaterial.SetShader(EDITOR_CONTEXT(shaderManager)[":Shaders\\Unlit.ovfx"]); m_actorPickingMaterial.Set("u_Diffuse", OvMaths::FVector4(1.f, 1.f, 1.f, 1.0f)); m_actorPickingMaterial.Set("u_DiffuseMap", nullptr); m_actorPickingMaterial.SetFrontfaceCulling(false); diff --git a/Sources/Overload/OvGame/src/OvGame/Core/Game.cpp b/Sources/Overload/OvGame/src/OvGame/Core/Game.cpp index 7ed2f6c64..694ba9758 100644 --- a/Sources/Overload/OvGame/src/OvGame/Core/Game.cpp +++ b/Sources/Overload/OvGame/src/OvGame/Core/Game.cpp @@ -119,11 +119,6 @@ void OvGame::Core::Game::Update(float p_deltaTime) if (m_context.inputManager->IsKeyPressed(OvWindowing::Inputs::EKey::KEY_F12)) m_showDebugInformation = !m_showDebugInformation; - #ifdef _DEBUG - if (m_context.inputManager->IsKeyPressed(OvWindowing::Inputs::EKey::KEY_R)) - OvRendering::Resources::Loaders::ShaderLoader::Recompile(*m_context.shaderManager[":Shaders\\Standard.glsl"], "Data\\Engine\\Shaders\\Standard.glsl"); - #endif - if (m_showDebugInformation) { m_fpsCounter.Update(p_deltaTime); diff --git a/Sources/Overload/OvTools/include/OvTools/Utils/PathParser.h b/Sources/Overload/OvTools/include/OvTools/Utils/PathParser.h index 6fc60c992..758d21fbf 100644 --- a/Sources/Overload/OvTools/include/OvTools/Utils/PathParser.h +++ b/Sources/Overload/OvTools/include/OvTools/Utils/PathParser.h @@ -23,6 +23,7 @@ namespace OvTools::Utils MODEL, TEXTURE, SHADER, + SHADER_PART, MATERIAL, SOUND, SCENE, diff --git a/Sources/Overload/OvTools/src/OvTools/Utils/PathParser.cpp b/Sources/Overload/OvTools/src/OvTools/Utils/PathParser.cpp index 5969e235e..4a0da637d 100644 --- a/Sources/Overload/OvTools/src/OvTools/Utils/PathParser.cpp +++ b/Sources/Overload/OvTools/src/OvTools/Utils/PathParser.cpp @@ -88,6 +88,7 @@ std::string OvTools::Utils::PathParser::FileTypeToString(EFileType p_fileType) case OvTools::Utils::PathParser::EFileType::MODEL: return "Model"; case OvTools::Utils::PathParser::EFileType::TEXTURE: return "Texture"; case OvTools::Utils::PathParser::EFileType::SHADER: return "Shader"; + case OvTools::Utils::PathParser::EFileType::SHADER_PART:return "Shader_Part"; case OvTools::Utils::PathParser::EFileType::MATERIAL: return "Material"; case OvTools::Utils::PathParser::EFileType::SOUND: return "Sound"; case OvTools::Utils::PathParser::EFileType::SCENE: return "Scene"; @@ -105,7 +106,8 @@ OvTools::Utils::PathParser::EFileType OvTools::Utils::PathParser::GetFileType(co if (ext == "fbx" || ext == "obj") return EFileType::MODEL; else if (ext == "png" || ext == "jpeg" || ext == "jpg" || ext == "tga") return EFileType::TEXTURE; - else if (ext == "glsl") return EFileType::SHADER; + else if (ext == "ovfx") return EFileType::SHADER; + else if (ext == "ovfxh") return EFileType::SHADER_PART; else if (ext == "ovmat") return EFileType::MATERIAL; else if (ext == "wav" || ext == "mp3" || ext == "ogg") return EFileType::SOUND; else if (ext == "ovscene") return EFileType::SCENE; From 79e51d764eb3a62b51afd20138434263146b6819 Mon Sep 17 00:00:00 2001 From: Adrien GIVRY Date: Tue, 19 Mar 2024 21:30:24 -0400 Subject: [PATCH 10/12] Reworked built-in shaders --- .../Engine/Shaders/Common/Constants.ovfxh | 1 + .../Shaders/Common/IO/FragmentInput.ovfxh | 9 - .../Shaders/Common/IO/VertexInput.ovfxh | 5 - .../Shaders/Common/IO/VertexOutput.ovfxh | 9 - Resources/Engine/Shaders/Common/Physics.ovfxh | 14 ++ Resources/Engine/Shaders/Common/Utils.ovfxh | 55 ++++++ .../Engine/Shaders/Fragment/BlinnPhong.ovfxh | 184 ------------------ .../Engine/Shaders/Fragment/Lambert.ovfxh | 26 --- Resources/Engine/Shaders/Fragment/Unlit.ovfxh | 15 -- Resources/Engine/Shaders/Lambert.ovfx | 58 +++++- .../Engine/Shaders/Lighting/BlinnPhong.ovfxh | 106 ++++++++++ .../Engine/Shaders/Lighting/Lambert.ovfxh | 5 + .../Shaders/{Fragment => Lighting}/PBR.ovfxh | 102 +++------- Resources/Engine/Shaders/Standard.ovfx | 80 +++++++- Resources/Engine/Shaders/StandardPBR.ovfx | 82 +++++++- Resources/Engine/Shaders/Unlit.ovfx | 47 ++++- Resources/Engine/Shaders/Vertex/Basic.ovfxh | 25 --- 17 files changed, 465 insertions(+), 358 deletions(-) create mode 100644 Resources/Engine/Shaders/Common/Constants.ovfxh delete mode 100644 Resources/Engine/Shaders/Common/IO/FragmentInput.ovfxh delete mode 100644 Resources/Engine/Shaders/Common/IO/VertexInput.ovfxh delete mode 100644 Resources/Engine/Shaders/Common/IO/VertexOutput.ovfxh create mode 100644 Resources/Engine/Shaders/Common/Physics.ovfxh create mode 100644 Resources/Engine/Shaders/Common/Utils.ovfxh delete mode 100644 Resources/Engine/Shaders/Fragment/BlinnPhong.ovfxh delete mode 100644 Resources/Engine/Shaders/Fragment/Lambert.ovfxh delete mode 100644 Resources/Engine/Shaders/Fragment/Unlit.ovfxh create mode 100644 Resources/Engine/Shaders/Lighting/BlinnPhong.ovfxh create mode 100644 Resources/Engine/Shaders/Lighting/Lambert.ovfxh rename Resources/Engine/Shaders/{Fragment => Lighting}/PBR.ovfxh (61%) delete mode 100644 Resources/Engine/Shaders/Vertex/Basic.ovfxh diff --git a/Resources/Engine/Shaders/Common/Constants.ovfxh b/Resources/Engine/Shaders/Common/Constants.ovfxh new file mode 100644 index 000000000..a3a5308e0 --- /dev/null +++ b/Resources/Engine/Shaders/Common/Constants.ovfxh @@ -0,0 +1 @@ +const float PI = 3.14159265359; diff --git a/Resources/Engine/Shaders/Common/IO/FragmentInput.ovfxh b/Resources/Engine/Shaders/Common/IO/FragmentInput.ovfxh deleted file mode 100644 index a1e3a97ef..000000000 --- a/Resources/Engine/Shaders/Common/IO/FragmentInput.ovfxh +++ /dev/null @@ -1,9 +0,0 @@ -in VS_OUT -{ - vec3 FragPos; - vec3 Normal; - vec2 TexCoords; - mat3 TBN; - flat vec3 TangentViewPos; - vec3 TangentFragPos; -} fs_in; \ No newline at end of file diff --git a/Resources/Engine/Shaders/Common/IO/VertexInput.ovfxh b/Resources/Engine/Shaders/Common/IO/VertexInput.ovfxh deleted file mode 100644 index 816cbcc58..000000000 --- a/Resources/Engine/Shaders/Common/IO/VertexInput.ovfxh +++ /dev/null @@ -1,5 +0,0 @@ -layout (location = 0) in vec3 geo_Pos; -layout (location = 1) in vec2 geo_TexCoords; -layout (location = 2) in vec3 geo_Normal; -layout (location = 3) in vec3 geo_Tangent; -layout (location = 4) in vec3 geo_Bitangent; \ No newline at end of file diff --git a/Resources/Engine/Shaders/Common/IO/VertexOutput.ovfxh b/Resources/Engine/Shaders/Common/IO/VertexOutput.ovfxh deleted file mode 100644 index 3834539be..000000000 --- a/Resources/Engine/Shaders/Common/IO/VertexOutput.ovfxh +++ /dev/null @@ -1,9 +0,0 @@ -out VS_OUT -{ - vec3 FragPos; - vec3 Normal; - vec2 TexCoords; - mat3 TBN; - flat vec3 TangentViewPos; - vec3 TangentFragPos; -} vs_out; \ No newline at end of file diff --git a/Resources/Engine/Shaders/Common/Physics.ovfxh b/Resources/Engine/Shaders/Common/Physics.ovfxh new file mode 100644 index 000000000..f299c4d0c --- /dev/null +++ b/Resources/Engine/Shaders/Common/Physics.ovfxh @@ -0,0 +1,14 @@ +bool PointInAABB(vec3 p_Point, vec3 p_AabbCenter, vec3 p_AabbHalfSize) +{ + return + ( + p_Point.x > p_AabbCenter.x - p_AabbHalfSize.x && p_Point.x < p_AabbCenter.x + p_AabbHalfSize.x && + p_Point.y > p_AabbCenter.y - p_AabbHalfSize.y && p_Point.y < p_AabbCenter.y + p_AabbHalfSize.y && + p_Point.z > p_AabbCenter.z - p_AabbHalfSize.z && p_Point.z < p_AabbCenter.z + p_AabbHalfSize.z + ); +} + +bool PointInSphere(vec3 p_Point, vec3 p_SphereCenter, float p_SphereRadius) +{ + return distance(p_Point, p_SphereCenter) <= p_SphereRadius; +} \ No newline at end of file diff --git a/Resources/Engine/Shaders/Common/Utils.ovfxh b/Resources/Engine/Shaders/Common/Utils.ovfxh new file mode 100644 index 000000000..ba948287e --- /dev/null +++ b/Resources/Engine/Shaders/Common/Utils.ovfxh @@ -0,0 +1,55 @@ +vec2 TRANSFORM_TEX_COORDS(vec2 texCoords, vec2 tiling, vec2 offset) +{ + return vec2(mod(texCoords.x * tiling.x, 1), mod(texCoords.y * tiling.y, 1)) + offset; +} + +vec2 APPLY_PARALLAX_MAPPING(vec2 texCoords, sampler2D heightMap, vec3 tangentViewPos, vec3 tangentFragPos, float heightScale) +{ + if (heightScale > 0) + { + const vec3 viewDir = normalize(tangentViewPos - tangentFragPos); + const vec2 parallax = viewDir.xy * heightScale * texture(heightMap, texCoords).r; + return texCoords - vec2(parallax.x, 1.0 - parallax.y); + } + + return texCoords; +} + +bool IS_MASKED(sampler2D maskMap, vec2 texCoords) +{ + return texture(maskMap, texCoords).r == 0.0; +} + +mat3 COMPUTE_TBN(mat4 model, vec3 normal, vec3 tangent, vec3 bitangent) +{ + return mat3( + normalize(vec3(model * vec4(tangent, 0.0))), + normalize(vec3(model * vec4(bitangent, 0.0))), + normalize(vec3(model * vec4(normal, 0.0))) + ); +} + +vec3 UnPack(float p_Target) +{ + return vec3 + ( + float((uint(p_Target) >> 24) & 0xff) * 0.003921568627451, + float((uint(p_Target) >> 16) & 0xff) * 0.003921568627451, + float((uint(p_Target) >> 8) & 0xff) * 0.003921568627451 + ); +} + +vec3 COMPUTE_NORMAL(bool enableNormalMapping, vec2 texCoords, vec3 normal, sampler2D normalMap, mat3 TBN) +{ + if (enableNormalMapping) + { + normal = texture(normalMap, texCoords).rgb; + normal = normalize(normal * 2.0 - 1.0); + normal = normalize(TBN * normal); + return normal; + } + else + { + return normalize(normal); + } +} diff --git a/Resources/Engine/Shaders/Fragment/BlinnPhong.ovfxh b/Resources/Engine/Shaders/Fragment/BlinnPhong.ovfxh deleted file mode 100644 index 9606043a3..000000000 --- a/Resources/Engine/Shaders/Fragment/BlinnPhong.ovfxh +++ /dev/null @@ -1,184 +0,0 @@ -#version 430 core - -#include ":Shaders/Common/IO/FragmentInput.ovfxh" -#include ":Shaders/Common/Buffers/EngineUBO.ovfxh" -#include ":Shaders/Common/Buffers/LightsSSBO.ovfxh" - -/* Uniforms (Tweakable from the material editor) */ -uniform vec2 u_TextureTiling = vec2(1.0, 1.0); -uniform vec2 u_TextureOffset = vec2(0.0, 0.0); -uniform vec4 u_Diffuse = vec4(1.0, 1.0, 1.0, 1.0); -uniform vec3 u_Specular = vec3(1.0, 1.0, 1.0); -uniform float u_Shininess = 100.0; -uniform float u_HeightScale = 0.0; -uniform bool u_EnableNormalMapping = false; -uniform sampler2D u_DiffuseMap; -uniform sampler2D u_SpecularMap; -uniform sampler2D u_NormalMap; -uniform sampler2D u_HeightMap; -uniform sampler2D u_MaskMap; - -/* Global variables */ -vec3 g_Normal; -vec2 g_TexCoords; -vec3 g_ViewDir; -vec4 g_DiffuseTexel; -vec4 g_SpecularTexel; -vec4 g_HeightTexel; -vec4 g_NormalTexel; - -out vec4 FRAGMENT_COLOR; - -vec3 UnPack(float p_Target) -{ - return vec3 - ( - float((uint(p_Target) >> 24) & 0xff) * 0.003921568627451, - float((uint(p_Target) >> 16) & 0xff) * 0.003921568627451, - float((uint(p_Target) >> 8) & 0xff) * 0.003921568627451 - ); -} - -bool PointInAABB(vec3 p_Point, vec3 p_AabbCenter, vec3 p_AabbHalfSize) -{ - return - ( - p_Point.x > p_AabbCenter.x - p_AabbHalfSize.x && p_Point.x < p_AabbCenter.x + p_AabbHalfSize.x && - p_Point.y > p_AabbCenter.y - p_AabbHalfSize.y && p_Point.y < p_AabbCenter.y + p_AabbHalfSize.y && - p_Point.z > p_AabbCenter.z - p_AabbHalfSize.z && p_Point.z < p_AabbCenter.z + p_AabbHalfSize.z - ); -} - -vec2 ParallaxMapping(vec3 p_ViewDir) -{ - const vec2 parallax = p_ViewDir.xy * u_HeightScale * texture(u_HeightMap, g_TexCoords).r; - return g_TexCoords - vec2(parallax.x, 1.0 - parallax.y); -} - -vec3 BlinnPhong(vec3 p_LightDir, vec3 p_LightColor, float p_Luminosity) -{ - const vec3 halfwayDir = normalize(p_LightDir + g_ViewDir); - const float diffuseCoefficient = max(dot(g_Normal, p_LightDir), 0.0); - const float specularCoefficient = pow(max(dot(g_Normal, halfwayDir), 0.0), u_Shininess * 2.0); - - return p_LightColor * g_DiffuseTexel.rgb * diffuseCoefficient * p_Luminosity + ((p_Luminosity > 0.0) ? (p_LightColor * g_SpecularTexel.rgb * specularCoefficient * p_Luminosity) : vec3(0.0)); -} - -float LuminosityFromAttenuation(mat4 p_Light) -{ - const vec3 lightPosition = p_Light[0].rgb; - const float constant = p_Light[0][3]; - const float linear = p_Light[1][3]; - const float quadratic = p_Light[2][3]; - - const float distanceToLight = length(lightPosition - fs_in.FragPos); - const float attenuation = (constant + linear * distanceToLight + quadratic * (distanceToLight * distanceToLight)); - return 1.0 / attenuation; -} - -vec3 CalcPointLight(mat4 p_Light) -{ - /* Extract light information from light mat4 */ - const vec3 lightPosition = p_Light[0].rgb; - const vec3 lightColor = UnPack(p_Light[2][0]); - const float intensity = p_Light[3][3]; - - const vec3 lightDirection = normalize(lightPosition - fs_in.FragPos); - const float luminosity = LuminosityFromAttenuation(p_Light); - - return BlinnPhong(lightDirection, lightColor, intensity * luminosity); -} - -vec3 CalcDirectionalLight(mat4 light) -{ - return BlinnPhong(-light[1].rgb, UnPack(light[2][0]), light[3][3]); -} - -vec3 CalcSpotLight(mat4 p_Light) -{ - /* Extract light information from light mat4 */ - const vec3 lightPosition = p_Light[0].rgb; - const vec3 lightForward = p_Light[1].rgb; - const vec3 lightColor = UnPack(p_Light[2][0]); - const float intensity = p_Light[3][3]; - const float cutOff = cos(radians(p_Light[3][1])); - const float outerCutOff = cos(radians(p_Light[3][1] + p_Light[3][2])); - - const vec3 lightDirection = normalize(lightPosition - fs_in.FragPos); - const float luminosity = LuminosityFromAttenuation(p_Light); - - /* Calculate the spot intensity */ - const float theta = dot(lightDirection, normalize(-lightForward)); - const float epsilon = cutOff - outerCutOff; - const float spotIntensity = clamp((theta - outerCutOff) / epsilon, 0.0, 1.0); - - return BlinnPhong(lightDirection, lightColor, intensity * spotIntensity * luminosity); -} - -vec3 CalcAmbientBoxLight(mat4 p_Light) -{ - const vec3 lightPosition = p_Light[0].rgb; - const vec3 lightColor = UnPack(p_Light[2][0]); - const float intensity = p_Light[3][3]; - const vec3 size = vec3(p_Light[0][3], p_Light[1][3], p_Light[2][3]); - - return PointInAABB(fs_in.FragPos, lightPosition, size) ? g_DiffuseTexel.rgb * lightColor * intensity : vec3(0.0); -} - -vec3 CalcAmbientSphereLight(mat4 p_Light) -{ - const vec3 lightPosition = p_Light[0].rgb; - const vec3 lightColor = UnPack(p_Light[2][0]); - const float intensity = p_Light[3][3]; - const float radius = p_Light[0][3]; - - return distance(lightPosition, fs_in.FragPos) <= radius ? g_DiffuseTexel.rgb * lightColor * intensity : vec3(0.0); -} - -void main() -{ - g_TexCoords = u_TextureOffset + vec2(mod(fs_in.TexCoords.x * u_TextureTiling.x, 1), mod(fs_in.TexCoords.y * u_TextureTiling.y, 1)); - - /* Apply parallax mapping */ - if (u_HeightScale > 0) - g_TexCoords = ParallaxMapping(normalize(fs_in.TangentViewPos - fs_in.TangentFragPos)); - - /* Apply color mask */ - if (texture(u_MaskMap, g_TexCoords).r != 0.0) - { - g_ViewDir = normalize(ubo_ViewPos - fs_in.FragPos); - g_DiffuseTexel = texture(u_DiffuseMap, g_TexCoords) * u_Diffuse; - g_SpecularTexel = texture(u_SpecularMap, g_TexCoords) * vec4(u_Specular, 1.0); - - if (u_EnableNormalMapping) - { - g_Normal = texture(u_NormalMap, g_TexCoords).rgb; - g_Normal = normalize(g_Normal * 2.0 - 1.0); - g_Normal = normalize(fs_in.TBN * g_Normal); - } - else - { - g_Normal = normalize(fs_in.Normal); - } - - vec3 lightSum = vec3(0.0); - - for (int i = 0; i < ssbo_Lights.length(); ++i) - { - switch(int(ssbo_Lights[i][3][0])) - { - case 0: lightSum += CalcPointLight(ssbo_Lights[i]); break; - case 1: lightSum += CalcDirectionalLight(ssbo_Lights[i]); break; - case 2: lightSum += CalcSpotLight(ssbo_Lights[i]); break; - case 3: lightSum += CalcAmbientBoxLight(ssbo_Lights[i]); break; - case 4: lightSum += CalcAmbientSphereLight(ssbo_Lights[i]); break; - } - } - - FRAGMENT_COLOR = vec4(lightSum, g_DiffuseTexel.a); - } - else - { - FRAGMENT_COLOR = vec4(0.0); - } -} diff --git a/Resources/Engine/Shaders/Fragment/Lambert.ovfxh b/Resources/Engine/Shaders/Fragment/Lambert.ovfxh deleted file mode 100644 index ee841fd41..000000000 --- a/Resources/Engine/Shaders/Fragment/Lambert.ovfxh +++ /dev/null @@ -1,26 +0,0 @@ -#version 430 core - -out vec4 FRAGMENT_COLOR; - -#include ":Shaders/Common/IO/FragmentInput.ovfxh" - -uniform vec4 u_Diffuse = vec4(1.0, 1.0, 1.0, 1.0); -uniform sampler2D u_DiffuseMap; -uniform vec2 u_TextureTiling = vec2(1.0, 1.0); -uniform vec2 u_TextureOffset = vec2(0.0, 0.0); - -const vec3 c_lightPosition = vec3(-9000.0, 10000.0, 11000.0); -const vec3 c_lightDiffuse = vec3(1.0, 1.0, 1.0); -const vec3 c_lightAmbient = vec3(0.3, 0.3, 0.3); - -vec3 Lambert(vec3 p_fragPos, vec3 p_normal) -{ - const float diffuse = max(dot(p_normal, normalize(c_lightPosition - p_fragPos)), 0.0); - return clamp(c_lightDiffuse * diffuse + c_lightAmbient, 0.0, 1.0); -} - -void main() -{ - const vec4 diffuse = texture(u_DiffuseMap, u_TextureOffset + vec2(mod(fs_in.TexCoords.x * u_TextureTiling.x, 1), mod(fs_in.TexCoords.y * u_TextureTiling.y, 1))) * u_Diffuse; - FRAGMENT_COLOR = vec4(Lambert(fs_in.FragPos, fs_in.Normal) * diffuse.rgb, diffuse.a); -} diff --git a/Resources/Engine/Shaders/Fragment/Unlit.ovfxh b/Resources/Engine/Shaders/Fragment/Unlit.ovfxh deleted file mode 100644 index 56df673b2..000000000 --- a/Resources/Engine/Shaders/Fragment/Unlit.ovfxh +++ /dev/null @@ -1,15 +0,0 @@ -#version 430 core - -out vec4 FRAGMENT_COLOR; - -#include ":Shaders/Common/IO/FragmentInput.ovfxh" - -uniform vec4 u_Diffuse = vec4(1.0, 1.0, 1.0, 1.0); -uniform sampler2D u_DiffuseMap; -uniform vec2 u_TextureTiling = vec2(1.0, 1.0); -uniform vec2 u_TextureOffset = vec2(0.0, 0.0); - -void main() -{ - FRAGMENT_COLOR = texture(u_DiffuseMap, u_TextureOffset + vec2(mod(fs_in.TexCoords.x * u_TextureTiling.x, 1), mod(fs_in.TexCoords.y * u_TextureTiling.y, 1))) * u_Diffuse; -} diff --git a/Resources/Engine/Shaders/Lambert.ovfx b/Resources/Engine/Shaders/Lambert.ovfx index 84ada91d9..5fae48f14 100644 --- a/Resources/Engine/Shaders/Lambert.ovfx +++ b/Resources/Engine/Shaders/Lambert.ovfx @@ -1,4 +1,58 @@ #shader vertex -#include ":Shaders/Vertex/Basic.ovfxh" +#version 430 core + +#include ":Shaders/Common/Buffers/EngineUBO.ovfxh" +#include ":Shaders/Common/Utils.ovfxh" + +layout (location = 0) in vec3 geo_Pos; +layout (location = 1) in vec2 geo_TexCoords; +layout (location = 2) in vec3 geo_Normal; + +out VS_OUT +{ + vec3 FragPos; + vec2 TexCoords; + vec3 Normal; +} vs_out; + +void main() +{ + vs_out.FragPos = vec3(ubo_Model * vec4(geo_Pos, 1.0)); + vs_out.TexCoords = geo_TexCoords; + vs_out.Normal = normalize(mat3(transpose(inverse(ubo_Model))) * geo_Normal); + + gl_Position = ubo_Projection * ubo_View * vec4(vs_out.FragPos, 1.0); +} + #shader fragment -#include ":Shaders/Fragment/Lambert.ovfxh" +#version 430 core + +#include ":Shaders/Common/Buffers/EngineUBO.ovfxh" +#include ":Shaders/Common/Utils.ovfxh" +#include ":Shaders/Lighting/Lambert.ovfxh" + +in VS_OUT +{ + vec3 FragPos; + vec2 TexCoords; + vec3 Normal; +} fs_in; + +uniform vec4 u_Diffuse = vec4(1.0, 1.0, 1.0, 1.0); +uniform sampler2D u_DiffuseMap; +uniform vec2 u_TextureTiling = vec2(1.0, 1.0); +uniform vec2 u_TextureOffset = vec2(0.0, 0.0); + +out vec4 FRAGMENT_COLOR; + +void main() +{ + vec2 texCoords = TRANSFORM_TEX_COORDS(fs_in.TexCoords, u_TextureTiling, u_TextureOffset); + + const vec3 kLightPosition = vec3(-9000.0, 10000.0, 11000.0); + const vec3 kLightDiffuse = vec3(1.0); + const vec3 kLightAmbient = vec3(0.3); + const vec3 lambert = Lambert(fs_in.FragPos, fs_in.Normal, kLightPosition, kLightDiffuse, kLightAmbient); + + FRAGMENT_COLOR = texture(u_DiffuseMap, texCoords) * u_Diffuse * vec4(lambert, 1.0); +} diff --git a/Resources/Engine/Shaders/Lighting/BlinnPhong.ovfxh b/Resources/Engine/Shaders/Lighting/BlinnPhong.ovfxh new file mode 100644 index 000000000..66241ab7c --- /dev/null +++ b/Resources/Engine/Shaders/Lighting/BlinnPhong.ovfxh @@ -0,0 +1,106 @@ +#include ":Shaders/Common/Physics.ovfxh" +#include ":Shaders/Common/Utils.ovfxh" +#include ":Shaders/Common/Buffers/LightsSSBO.ovfxh" + +vec3 BlinnPhong(vec3 p_LightDir, vec3 p_LightColor, float p_Luminosity, vec4 diffuseTexel, vec4 specularTexel, vec3 normal, vec3 viewDir, float shininess) +{ + const vec3 halfwayDir = normalize(p_LightDir + viewDir); + const float diffuseCoefficient = max(dot(normal, p_LightDir), 0.0); + const float specularCoefficient = pow(max(dot(normal, halfwayDir), 0.0), shininess * 2.0); + + return p_LightColor * diffuseTexel.rgb * diffuseCoefficient * p_Luminosity + ((p_Luminosity > 0.0) ? (p_LightColor * specularTexel.rgb * specularCoefficient * p_Luminosity) : vec3(0.0)); +} + +float LuminosityFromAttenuation(mat4 p_Light, vec3 fragPos) +{ + const vec3 lightPosition = p_Light[0].rgb; + const float constant = p_Light[0][3]; + const float linear = p_Light[1][3]; + const float quadratic = p_Light[2][3]; + + const float distanceToLight = length(lightPosition - fragPos); + const float attenuation = (constant + linear * distanceToLight + quadratic * (distanceToLight * distanceToLight)); + return 1.0 / attenuation; +} + +vec3 CalcPointLight(mat4 p_Light, vec3 fragPos, vec4 diffuseTexel, vec4 specularTexel, vec3 normal, vec3 viewDir, float shininess) +{ + /* Extract light information from light mat4 */ + const vec3 lightPosition = p_Light[0].rgb; + const vec3 lightColor = UnPack(p_Light[2][0]); + const float intensity = p_Light[3][3]; + + const vec3 lightDirection = normalize(lightPosition - fragPos); + const float luminosity = LuminosityFromAttenuation(p_Light, fragPos); + + return BlinnPhong(lightDirection, lightColor, intensity * luminosity, diffuseTexel, specularTexel, normal, viewDir, shininess); +} + +vec3 CalcDirectionalLight(mat4 light, vec4 diffuseTexel, vec4 specularTexel, vec3 normal, vec3 viewDir, float shininess) +{ + return BlinnPhong(-light[1].rgb, UnPack(light[2][0]), light[3][3], diffuseTexel, specularTexel, normal, viewDir, shininess); +} + +vec3 CalcSpotLight(mat4 p_Light, vec3 fragPos, vec4 diffuseTexel, vec4 specularTexel, vec3 normal, vec3 viewDir, float shininess) +{ + /* Extract light information from light mat4 */ + const vec3 lightPosition = p_Light[0].rgb; + const vec3 lightForward = p_Light[1].rgb; + const vec3 lightColor = UnPack(p_Light[2][0]); + const float intensity = p_Light[3][3]; + const float cutOff = cos(radians(p_Light[3][1])); + const float outerCutOff = cos(radians(p_Light[3][1] + p_Light[3][2])); + + const vec3 lightDirection = normalize(lightPosition - fragPos); + const float luminosity = LuminosityFromAttenuation(p_Light, fragPos); + + /* Calculate the spot intensity */ + const float theta = dot(lightDirection, normalize(-lightForward)); + const float epsilon = cutOff - outerCutOff; + const float spotIntensity = clamp((theta - outerCutOff) / epsilon, 0.0, 1.0); + + return BlinnPhong(lightDirection, lightColor, intensity * spotIntensity * luminosity, diffuseTexel, specularTexel, normal, viewDir, shininess); +} + +vec3 CalcAmbientBoxLight(mat4 p_Light, vec3 fragPos, vec4 diffuseTexel) +{ + const vec3 lightPosition = p_Light[0].rgb; + const vec3 lightColor = UnPack(p_Light[2][0]); + const float intensity = p_Light[3][3]; + const vec3 size = vec3(p_Light[0][3], p_Light[1][3], p_Light[2][3]); + + return PointInAABB(fragPos, lightPosition, size) ? diffuseTexel.rgb * lightColor * intensity : vec3(0.0); +} + +vec3 CalcAmbientSphereLight(mat4 p_Light, vec3 fragPos, vec4 diffuseTexel) +{ + const vec3 lightPosition = p_Light[0].rgb; + const vec3 lightColor = UnPack(p_Light[2][0]); + const float intensity = p_Light[3][3]; + const float radius = p_Light[0][3]; + + return PointInSphere(fragPos, lightPosition, radius) ? diffuseTexel.rgb * lightColor * intensity : vec3(0.0); +} + +vec4 BLINN_PHONG(vec2 texCoords, vec3 normal, vec3 viewPos, vec3 fragPos, vec4 diffuse, vec3 specular, sampler2D diffuseMap, sampler2D specularMap, float shininess) +{ + vec3 viewDir = normalize(viewPos - fragPos); + vec4 diffuseTexel = texture(diffuseMap, texCoords) * diffuse; + vec4 specularTexel = texture(specularMap, texCoords) * vec4(specular, 1.0); + + vec3 lightSum = vec3(0.0); + + for (int i = 0; i < ssbo_Lights.length(); ++i) + { + switch(int(ssbo_Lights[i][3][0])) + { + case 0: lightSum += CalcPointLight(ssbo_Lights[i], fragPos, diffuseTexel, specularTexel, normal, viewDir, shininess); break; + case 1: lightSum += CalcDirectionalLight(ssbo_Lights[i], diffuseTexel, specularTexel, normal, viewDir, shininess); break; + case 2: lightSum += CalcSpotLight(ssbo_Lights[i], fragPos, diffuseTexel, specularTexel, normal, viewDir, shininess); break; + case 3: lightSum += CalcAmbientBoxLight(ssbo_Lights[i], fragPos, diffuseTexel); break; + case 4: lightSum += CalcAmbientSphereLight(ssbo_Lights[i], fragPos, diffuseTexel); break; + } + } + + return vec4(lightSum, diffuseTexel.a); +} diff --git a/Resources/Engine/Shaders/Lighting/Lambert.ovfxh b/Resources/Engine/Shaders/Lighting/Lambert.ovfxh new file mode 100644 index 000000000..e4a8d6885 --- /dev/null +++ b/Resources/Engine/Shaders/Lighting/Lambert.ovfxh @@ -0,0 +1,5 @@ +vec3 Lambert(vec3 p_fragPos, vec3 p_normal, vec3 lightPos, vec3 lightDiffuse, vec3 lightAmbient) +{ + const float diffuse = max(dot(p_normal, normalize(lightPos - p_fragPos)), 0.0); + return clamp(lightDiffuse * diffuse + lightAmbient, 0.0, 1.0); +} diff --git a/Resources/Engine/Shaders/Fragment/PBR.ovfxh b/Resources/Engine/Shaders/Lighting/PBR.ovfxh similarity index 61% rename from Resources/Engine/Shaders/Fragment/PBR.ovfxh rename to Resources/Engine/Shaders/Lighting/PBR.ovfxh index 0fb3bd6e5..dba06e6ee 100644 --- a/Resources/Engine/Shaders/Fragment/PBR.ovfxh +++ b/Resources/Engine/Shaders/Lighting/PBR.ovfxh @@ -1,26 +1,8 @@ -#version 430 core - -#include ":Shaders/Common/IO/FragmentInput.ovfxh" -#include ":Shaders/Common/Buffers/EngineUBO.ovfxh" +#include ":Shaders/Common/Physics.ovfxh" +#include ":Shaders/Common/Utils.ovfxh" +#include ":Shaders/Common/Constants.ovfxh" #include ":Shaders/Common/Buffers/LightsSSBO.ovfxh" -out vec4 FRAGMENT_COLOR; - -uniform sampler2D u_AlbedoMap; -uniform sampler2D u_MetallicMap; -uniform sampler2D u_RoughnessMap; -uniform sampler2D u_AmbientOcclusionMap; -uniform sampler2D u_NormalMap; -uniform vec4 u_Albedo = vec4(1.0); -uniform vec2 u_TextureTiling = vec2(1.0, 1.0); -uniform vec2 u_TextureOffset = vec2(0.0, 0.0); -uniform bool u_EnableNormalMapping = false; -uniform float u_HeightScale = 0.0; -uniform float u_Metallic = 1.0; -uniform float u_Roughness = 1.0; - -const float PI = 3.14159265359; - float DistributionGGX(vec3 N, vec3 H, float roughness) { float a = roughness*roughness; @@ -60,82 +42,48 @@ vec3 fresnelSchlick(float cosTheta, vec3 F0) return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0); } -vec3 UnPack(float p_Target) -{ - return vec3 - ( - float((uint(p_Target) >> 24) & 0xff) * 0.003921568627451, - float((uint(p_Target) >> 16) & 0xff) * 0.003921568627451, - float((uint(p_Target) >> 8) & 0xff) * 0.003921568627451 - ); -} - -bool PointInAABB(vec3 p_Point, vec3 p_AabbCenter, vec3 p_AabbHalfSize) -{ - return - ( - p_Point.x > p_AabbCenter.x - p_AabbHalfSize.x && p_Point.x < p_AabbCenter.x + p_AabbHalfSize.x && - p_Point.y > p_AabbCenter.y - p_AabbHalfSize.y && p_Point.y < p_AabbCenter.y + p_AabbHalfSize.y && - p_Point.z > p_AabbCenter.z - p_AabbHalfSize.z && p_Point.z < p_AabbCenter.z + p_AabbHalfSize.z - ); -} - -float LuminosityFromAttenuation(mat4 p_Light) +float LuminosityFromAttenuation(mat4 p_Light, vec3 fragPos) { const vec3 lightPosition = p_Light[0].rgb; const float constant = p_Light[0][3]; const float linear = p_Light[1][3]; const float quadratic = p_Light[2][3]; - const float distanceToLight = length(lightPosition - fs_in.FragPos); + const float distanceToLight = length(lightPosition - fragPos); const float attenuation = (constant + linear * distanceToLight + quadratic * (distanceToLight * distanceToLight)); return 1.0 / attenuation; } -vec3 CalcAmbientBoxLight(mat4 p_Light) +vec3 CalcAmbientBoxLight(mat4 p_Light, vec3 fragPos) { const vec3 lightPosition = p_Light[0].rgb; const vec3 lightColor = UnPack(p_Light[2][0]); const float intensity = p_Light[3][3]; const vec3 size = vec3(p_Light[0][3], p_Light[1][3], p_Light[2][3]); - return PointInAABB(fs_in.FragPos, lightPosition, size) ? lightColor * intensity : vec3(0.0); + return PointInAABB(fragPos, lightPosition, size) ? lightColor * intensity : vec3(0.0); } -vec3 CalcAmbientSphereLight(mat4 p_Light) +vec3 CalcAmbientSphereLight(mat4 p_Light, vec3 fragPos) { const vec3 lightPosition = p_Light[0].rgb; const vec3 lightColor = UnPack(p_Light[2][0]); const float intensity = p_Light[3][3]; const float radius = p_Light[0][3]; - return distance(lightPosition, fs_in.FragPos) <= radius ? lightColor * intensity : vec3(0.0); + return PointInSphere(fragPos, lightPosition, radius) ? lightColor * intensity : vec3(0.0); } -void main() +vec4 PBR(vec2 texCoords, vec3 normal, vec3 viewPos, vec3 fragPos, vec4 inAlbedo, float inMetallic, float inRoughness, sampler2D albedoMap, sampler2D metallicMap, sampler2D roughnessMap, sampler2D aoMap) { - vec2 texCoords = u_TextureOffset + vec2(mod(fs_in.TexCoords.x * u_TextureTiling.x, 1), mod(fs_in.TexCoords.y * u_TextureTiling.y, 1)); - - vec4 albedoRGBA = texture(u_AlbedoMap, texCoords) * u_Albedo; + vec4 albedoRGBA = texture(albedoMap, texCoords) * inAlbedo; vec3 albedo = pow(albedoRGBA.rgb, vec3(2.2)); - float metallic = texture(u_MetallicMap, texCoords).r * u_Metallic; - float roughness = texture(u_RoughnessMap, texCoords).r * u_Roughness; - float ao = texture(u_AmbientOcclusionMap, texCoords).r; - vec3 normal; - - if (u_EnableNormalMapping) - { - normal = texture(u_NormalMap, texCoords).rgb; - normal = normalize(normal * 2.0 - 1.0); - normal = normalize(fs_in.TBN * normal); - } - else - { - normal = normalize(fs_in.Normal); - } - + float metallic = texture(metallicMap, texCoords).r * inMetallic; + float roughness = texture(roughnessMap, texCoords).r * inRoughness; + float ao = texture(aoMap, texCoords).r; + vec3 N = normalize(normal); - vec3 V = normalize(ubo_ViewPos - fs_in.FragPos); + vec3 V = normalize(viewPos - fragPos); vec3 F0 = vec3(0.04); F0 = mix(F0, albedo, metallic); @@ -148,24 +96,24 @@ void main() { if (int(ssbo_Lights[i][3][0]) == 3) { - ambientSum += CalcAmbientBoxLight(ssbo_Lights[i]); + ambientSum += CalcAmbientBoxLight(ssbo_Lights[i], fragPos); } else if (int(ssbo_Lights[i][3][0]) == 4) { - ambientSum += CalcAmbientSphereLight(ssbo_Lights[i]); + ambientSum += CalcAmbientSphereLight(ssbo_Lights[i], fragPos); } else { // calculate per-light radiance - vec3 L = int(ssbo_Lights[i][3][0]) == 1 ? -ssbo_Lights[i][1].rgb : normalize(ssbo_Lights[i][0].rgb - fs_in.FragPos); + vec3 L = int(ssbo_Lights[i][3][0]) == 1 ? -ssbo_Lights[i][1].rgb : normalize(ssbo_Lights[i][0].rgb - fragPos); vec3 H = normalize(V + L); - float distance = length(ssbo_Lights[i][0].rgb - fs_in.FragPos); + float distance = length(ssbo_Lights[i][0].rgb - fragPos); float lightCoeff = 0.0; switch(int(ssbo_Lights[i][3][0])) { case 0: - lightCoeff = LuminosityFromAttenuation(ssbo_Lights[i]) * ssbo_Lights[i][3][3]; + lightCoeff = LuminosityFromAttenuation(ssbo_Lights[i], fragPos) * ssbo_Lights[i][3][3]; break; case 1: @@ -177,8 +125,8 @@ void main() const float cutOff = cos(radians(ssbo_Lights[i][3][1])); const float outerCutOff = cos(radians(ssbo_Lights[i][3][1] + ssbo_Lights[i][3][2])); - const vec3 lightDirection = normalize(ssbo_Lights[i][0].rgb - fs_in.FragPos); - const float luminosity = LuminosityFromAttenuation(ssbo_Lights[i]); + const vec3 lightDirection = normalize(ssbo_Lights[i][0].rgb - fragPos); + const float luminosity = LuminosityFromAttenuation(ssbo_Lights[i], fragPos); /* Calculate the spot intensity */ const float theta = dot(lightDirection, normalize(-lightForward)); @@ -216,5 +164,5 @@ void main() color = color / (color + vec3(1.0)); color = pow(color, vec3(1.0/2.2)); - FRAGMENT_COLOR = vec4(color, albedoRGBA.a); -} + return vec4(color, albedoRGBA.a); +} \ No newline at end of file diff --git a/Resources/Engine/Shaders/Standard.ovfx b/Resources/Engine/Shaders/Standard.ovfx index ea8d9ba6d..aded59e4a 100644 --- a/Resources/Engine/Shaders/Standard.ovfx +++ b/Resources/Engine/Shaders/Standard.ovfx @@ -1,4 +1,80 @@ #shader vertex -#include ":Shaders/Vertex/Basic.ovfxh" +#version 430 core + +#include ":Shaders/Common/Buffers/EngineUBO.ovfxh" +#include ":Shaders/Common/Utils.ovfxh" + +layout (location = 0) in vec3 geo_Pos; +layout (location = 1) in vec2 geo_TexCoords; +layout (location = 2) in vec3 geo_Normal; +layout (location = 3) in vec3 geo_Tangent; +layout (location = 4) in vec3 geo_Bitangent; + +out VS_OUT +{ + vec3 FragPos; + vec2 TexCoords; + vec3 Normal; + mat3 TBN; + flat vec3 TangentViewPos; + vec3 TangentFragPos; +} vs_out; + +void main() +{ + vs_out.FragPos = vec3(ubo_Model * vec4(geo_Pos, 1.0)); + vs_out.TexCoords = geo_TexCoords; + vs_out.Normal = normalize(mat3(transpose(inverse(ubo_Model))) * geo_Normal); + vs_out.TBN = COMPUTE_TBN(ubo_Model, geo_Normal, geo_Tangent, geo_Bitangent); + vs_out.TangentViewPos = transpose(vs_out.TBN) * ubo_ViewPos; + vs_out.TangentFragPos = transpose(vs_out.TBN) * vs_out.FragPos; + + gl_Position = ubo_Projection * ubo_View * vec4(vs_out.FragPos, 1.0); +} + #shader fragment -#include ":Shaders/Fragment/BlinnPhong.ovfxh" +#version 430 core + +#include ":Shaders/Common/Buffers/EngineUBO.ovfxh" +#include ":Shaders/Lighting/BlinnPhong.ovfxh" + +in VS_OUT +{ + vec3 FragPos; + vec2 TexCoords; + vec3 Normal; + mat3 TBN; + flat vec3 TangentViewPos; + vec3 TangentFragPos; +} fs_in; + +uniform vec2 u_TextureTiling = vec2(1.0, 1.0); +uniform vec2 u_TextureOffset = vec2(0.0, 0.0); +uniform vec4 u_Diffuse = vec4(1.0, 1.0, 1.0, 1.0); +uniform vec3 u_Specular = vec3(1.0, 1.0, 1.0); +uniform float u_Shininess = 100.0; +uniform float u_HeightScale = 0.0; +uniform bool u_EnableNormalMapping = false; +uniform sampler2D u_DiffuseMap; +uniform sampler2D u_SpecularMap; +uniform sampler2D u_NormalMap; +uniform sampler2D u_HeightMap; +uniform sampler2D u_MaskMap; + +out vec4 FRAGMENT_COLOR; + +void main() +{ + vec2 texCoords = TRANSFORM_TEX_COORDS(fs_in.TexCoords, u_TextureTiling, u_TextureOffset); + texCoords = APPLY_PARALLAX_MAPPING(texCoords, u_HeightMap, fs_in.TangentViewPos, fs_in.TangentFragPos, u_HeightScale); + + if (!IS_MASKED(u_MaskMap, texCoords)) + { + vec3 normal = COMPUTE_NORMAL(u_EnableNormalMapping, texCoords, fs_in.Normal, u_NormalMap, fs_in.TBN); + FRAGMENT_COLOR = BLINN_PHONG(texCoords, normal, ubo_ViewPos, fs_in.FragPos, u_Diffuse, u_Specular, u_DiffuseMap, u_SpecularMap, u_Shininess); + } + else + { + FRAGMENT_COLOR = vec4(0.0); + } +} diff --git a/Resources/Engine/Shaders/StandardPBR.ovfx b/Resources/Engine/Shaders/StandardPBR.ovfx index 738951933..515d97a0d 100644 --- a/Resources/Engine/Shaders/StandardPBR.ovfx +++ b/Resources/Engine/Shaders/StandardPBR.ovfx @@ -1,4 +1,82 @@ #shader vertex -#include ":Shaders/Vertex/Basic.ovfxh" +#version 430 core + +#include ":Shaders/Common/Buffers/EngineUBO.ovfxh" +#include ":Shaders/Common/Utils.ovfxh" + +layout (location = 0) in vec3 geo_Pos; +layout (location = 1) in vec2 geo_TexCoords; +layout (location = 2) in vec3 geo_Normal; +layout (location = 3) in vec3 geo_Tangent; +layout (location = 4) in vec3 geo_Bitangent; + +out VS_OUT +{ + vec3 FragPos; + vec2 TexCoords; + vec3 Normal; + mat3 TBN; + flat vec3 TangentViewPos; + vec3 TangentFragPos; +} vs_out; + +void main() +{ + vs_out.FragPos = vec3(ubo_Model * vec4(geo_Pos, 1.0)); + vs_out.TexCoords = geo_TexCoords; + vs_out.Normal = normalize(mat3(transpose(inverse(ubo_Model))) * geo_Normal); + vs_out.TBN = COMPUTE_TBN(ubo_Model, geo_Normal, geo_Tangent, geo_Bitangent); + vs_out.TangentViewPos = transpose(vs_out.TBN) * ubo_ViewPos; + vs_out.TangentFragPos = transpose(vs_out.TBN) * vs_out.FragPos; + + gl_Position = ubo_Projection * ubo_View * vec4(vs_out.FragPos, 1.0); +} + #shader fragment -#include ":Shaders/Fragment/PBR.ovfxh" +#version 430 core + +#include ":Shaders/Common/Buffers/EngineUBO.ovfxh" +#include ":Shaders/Lighting/PBR.ovfxh" + +in VS_OUT +{ + vec3 FragPos; + vec2 TexCoords; + vec3 Normal; + mat3 TBN; + flat vec3 TangentViewPos; + vec3 TangentFragPos; +} fs_in; + +uniform sampler2D u_AlbedoMap; +uniform sampler2D u_MetallicMap; +uniform sampler2D u_RoughnessMap; +uniform sampler2D u_AmbientOcclusionMap; +uniform sampler2D u_NormalMap; +uniform vec4 u_Albedo = vec4(1.0); +uniform vec2 u_TextureTiling = vec2(1.0, 1.0); +uniform vec2 u_TextureOffset = vec2(0.0, 0.0); +uniform bool u_EnableNormalMapping = false; +uniform float u_HeightScale = 0.0; +uniform float u_Metallic = 1.0; +uniform float u_Roughness = 1.0; +uniform sampler2D u_HeightMap; +uniform sampler2D u_MaskMap; + +out vec4 FRAGMENT_COLOR; + +void main() +{ + vec2 texCoords = TRANSFORM_TEX_COORDS(fs_in.TexCoords, u_TextureTiling, u_TextureOffset); + texCoords = APPLY_PARALLAX_MAPPING(texCoords, u_HeightMap, fs_in.TangentViewPos, fs_in.TangentFragPos, u_HeightScale); + + if (!IS_MASKED(u_MaskMap, texCoords)) + { + vec3 normal = COMPUTE_NORMAL(u_EnableNormalMapping, texCoords, fs_in.Normal, u_NormalMap, fs_in.TBN); + FRAGMENT_COLOR = PBR(texCoords, normal, ubo_ViewPos, fs_in.FragPos, u_Albedo, u_Metallic, u_Roughness, u_AlbedoMap, u_MetallicMap, u_RoughnessMap, u_AmbientOcclusionMap); + } + else + { + FRAGMENT_COLOR = vec4(0.0); + } +} diff --git a/Resources/Engine/Shaders/Unlit.ovfx b/Resources/Engine/Shaders/Unlit.ovfx index 08569d779..5bcbb8984 100644 --- a/Resources/Engine/Shaders/Unlit.ovfx +++ b/Resources/Engine/Shaders/Unlit.ovfx @@ -1,4 +1,47 @@ #shader vertex -#include ":Shaders/Vertex/Basic.ovfxh" +#version 430 core + +#include ":Shaders/Common/Buffers/EngineUBO.ovfxh" +#include ":Shaders/Common/Utils.ovfxh" + +layout (location = 0) in vec3 geo_Pos; +layout (location = 1) in vec2 geo_TexCoords; + +out VS_OUT +{ + vec3 FragPos; + vec2 TexCoords; +} vs_out; + +void main() +{ + vs_out.FragPos = vec3(ubo_Model * vec4(geo_Pos, 1.0)); + vs_out.TexCoords = geo_TexCoords; + + gl_Position = ubo_Projection * ubo_View * vec4(vs_out.FragPos, 1.0); +} + #shader fragment -#include ":Shaders/Fragment/Unlit.ovfxh" +#version 430 core + +#include ":Shaders/Common/Buffers/EngineUBO.ovfxh" +#include ":Shaders/Common/Utils.ovfxh" + +in VS_OUT +{ + vec3 FragPos; + vec2 TexCoords; +} fs_in; + +uniform vec4 u_Diffuse = vec4(1.0); +uniform sampler2D u_DiffuseMap; +uniform vec2 u_TextureTiling = vec2(1.0); +uniform vec2 u_TextureOffset = vec2(0.0); + +out vec4 FRAGMENT_COLOR; + +void main() +{ + vec2 texCoords = TRANSFORM_TEX_COORDS(fs_in.TexCoords, u_TextureTiling, u_TextureOffset); + FRAGMENT_COLOR = texture(u_DiffuseMap, texCoords) * u_Diffuse; +} diff --git a/Resources/Engine/Shaders/Vertex/Basic.ovfxh b/Resources/Engine/Shaders/Vertex/Basic.ovfxh deleted file mode 100644 index 752aa3593..000000000 --- a/Resources/Engine/Shaders/Vertex/Basic.ovfxh +++ /dev/null @@ -1,25 +0,0 @@ -#version 430 core - -#include ":Shaders/Common/IO/VertexInput.ovfxh" -#include ":Shaders/Common/Buffers/EngineUBO.ovfxh" -#include ":Shaders/Common/IO/VertexOutput.ovfxh" - -void main() -{ - vs_out.TBN = mat3 - ( - normalize(vec3(ubo_Model * vec4(geo_Tangent, 0.0))), - normalize(vec3(ubo_Model * vec4(geo_Bitangent, 0.0))), - normalize(vec3(ubo_Model * vec4(geo_Normal, 0.0))) - ); - - mat3 TBNi = transpose(vs_out.TBN); - - vs_out.FragPos = vec3(ubo_Model * vec4(geo_Pos, 1.0)); - vs_out.Normal = normalize(mat3(transpose(inverse(ubo_Model))) * geo_Normal); - vs_out.TexCoords = geo_TexCoords; - vs_out.TangentViewPos = TBNi * ubo_ViewPos; - vs_out.TangentFragPos = TBNi * vs_out.FragPos; - - gl_Position = ubo_Projection * ubo_View * vec4(vs_out.FragPos, 1.0); -} From 393751a2b3e8ff33e3f42c323440e7392c46412f Mon Sep 17 00:00:00 2001 From: Adrien GIVRY Date: Tue, 19 Mar 2024 22:11:31 -0400 Subject: [PATCH 11/12] Cleaning up shader code and improving code consistency --- Resources/Engine/Shaders/Common/Physics.ovfxh | 14 +-- Resources/Engine/Shaders/Common/Utils.ovfxh | 29 +++-- Resources/Engine/Shaders/Lambert.ovfx | 4 +- .../Engine/Shaders/Lighting/BlinnPhong.ovfxh | 104 ++++++++-------- .../Engine/Shaders/Lighting/Lambert.ovfxh | 4 +- Resources/Engine/Shaders/Lighting/PBR.ovfxh | 116 ++++++++---------- .../Engine/Shaders/Lighting/Shared.ovfxh | 12 ++ Resources/Engine/Shaders/Standard.ovfx | 12 +- Resources/Engine/Shaders/StandardPBR.ovfx | 12 +- Resources/Engine/Shaders/Unlit.ovfx | 2 +- 10 files changed, 151 insertions(+), 158 deletions(-) create mode 100644 Resources/Engine/Shaders/Lighting/Shared.ovfxh diff --git a/Resources/Engine/Shaders/Common/Physics.ovfxh b/Resources/Engine/Shaders/Common/Physics.ovfxh index f299c4d0c..66d559960 100644 --- a/Resources/Engine/Shaders/Common/Physics.ovfxh +++ b/Resources/Engine/Shaders/Common/Physics.ovfxh @@ -1,14 +1,12 @@ -bool PointInAABB(vec3 p_Point, vec3 p_AabbCenter, vec3 p_AabbHalfSize) +bool IsPointInAABB(vec3 point, vec3 aabbCenter, vec3 aabbHalfSize) { return - ( - p_Point.x > p_AabbCenter.x - p_AabbHalfSize.x && p_Point.x < p_AabbCenter.x + p_AabbHalfSize.x && - p_Point.y > p_AabbCenter.y - p_AabbHalfSize.y && p_Point.y < p_AabbCenter.y + p_AabbHalfSize.y && - p_Point.z > p_AabbCenter.z - p_AabbHalfSize.z && p_Point.z < p_AabbCenter.z + p_AabbHalfSize.z - ); + point.x > aabbCenter.x - aabbHalfSize.x && point.x < aabbCenter.x + aabbHalfSize.x && + point.y > aabbCenter.y - aabbHalfSize.y && point.y < aabbCenter.y + aabbHalfSize.y && + point.z > aabbCenter.z - aabbHalfSize.z && point.z < aabbCenter.z + aabbHalfSize.z; } -bool PointInSphere(vec3 p_Point, vec3 p_SphereCenter, float p_SphereRadius) +bool IsPointInSphere(vec3 point, vec3 sphereCenter, float sphereRadius) { - return distance(p_Point, p_SphereCenter) <= p_SphereRadius; + return distance(point, sphereCenter) <= sphereRadius; } \ No newline at end of file diff --git a/Resources/Engine/Shaders/Common/Utils.ovfxh b/Resources/Engine/Shaders/Common/Utils.ovfxh index ba948287e..1ed35a101 100644 --- a/Resources/Engine/Shaders/Common/Utils.ovfxh +++ b/Resources/Engine/Shaders/Common/Utils.ovfxh @@ -1,9 +1,18 @@ -vec2 TRANSFORM_TEX_COORDS(vec2 texCoords, vec2 tiling, vec2 offset) +vec3 UnPack(float target) +{ + return vec3 ( + float((uint(target) >> 24) & 0xff) * 0.003921568627451, + float((uint(target) >> 16) & 0xff) * 0.003921568627451, + float((uint(target) >> 8) & 0xff) * 0.003921568627451 + ); +} + +vec2 TileAndOffsetTexCoords(vec2 texCoords, vec2 tiling, vec2 offset) { return vec2(mod(texCoords.x * tiling.x, 1), mod(texCoords.y * tiling.y, 1)) + offset; } -vec2 APPLY_PARALLAX_MAPPING(vec2 texCoords, sampler2D heightMap, vec3 tangentViewPos, vec3 tangentFragPos, float heightScale) +vec2 ApplyParallaxMapping(vec2 texCoords, sampler2D heightMap, vec3 tangentViewPos, vec3 tangentFragPos, float heightScale) { if (heightScale > 0) { @@ -15,12 +24,12 @@ vec2 APPLY_PARALLAX_MAPPING(vec2 texCoords, sampler2D heightMap, vec3 tangentVie return texCoords; } -bool IS_MASKED(sampler2D maskMap, vec2 texCoords) +bool IsMasked(sampler2D maskMap, vec2 texCoords) { return texture(maskMap, texCoords).r == 0.0; } -mat3 COMPUTE_TBN(mat4 model, vec3 normal, vec3 tangent, vec3 bitangent) +mat3 ConstructTBN(mat4 model, vec3 normal, vec3 tangent, vec3 bitangent) { return mat3( normalize(vec3(model * vec4(tangent, 0.0))), @@ -29,17 +38,7 @@ mat3 COMPUTE_TBN(mat4 model, vec3 normal, vec3 tangent, vec3 bitangent) ); } -vec3 UnPack(float p_Target) -{ - return vec3 - ( - float((uint(p_Target) >> 24) & 0xff) * 0.003921568627451, - float((uint(p_Target) >> 16) & 0xff) * 0.003921568627451, - float((uint(p_Target) >> 8) & 0xff) * 0.003921568627451 - ); -} - -vec3 COMPUTE_NORMAL(bool enableNormalMapping, vec2 texCoords, vec3 normal, sampler2D normalMap, mat3 TBN) +vec3 ComputeNormal(bool enableNormalMapping, vec2 texCoords, vec3 normal, sampler2D normalMap, mat3 TBN) { if (enableNormalMapping) { diff --git a/Resources/Engine/Shaders/Lambert.ovfx b/Resources/Engine/Shaders/Lambert.ovfx index 5fae48f14..4a65a625d 100644 --- a/Resources/Engine/Shaders/Lambert.ovfx +++ b/Resources/Engine/Shaders/Lambert.ovfx @@ -47,12 +47,12 @@ out vec4 FRAGMENT_COLOR; void main() { - vec2 texCoords = TRANSFORM_TEX_COORDS(fs_in.TexCoords, u_TextureTiling, u_TextureOffset); + vec2 texCoords = TileAndOffsetTexCoords(fs_in.TexCoords, u_TextureTiling, u_TextureOffset); const vec3 kLightPosition = vec3(-9000.0, 10000.0, 11000.0); const vec3 kLightDiffuse = vec3(1.0); const vec3 kLightAmbient = vec3(0.3); - const vec3 lambert = Lambert(fs_in.FragPos, fs_in.Normal, kLightPosition, kLightDiffuse, kLightAmbient); + const vec3 lambert = ComputeLambertLighting(fs_in.FragPos, fs_in.Normal, kLightPosition, kLightDiffuse, kLightAmbient); FRAGMENT_COLOR = texture(u_DiffuseMap, texCoords) * u_Diffuse * vec4(lambert, 1.0); } diff --git a/Resources/Engine/Shaders/Lighting/BlinnPhong.ovfxh b/Resources/Engine/Shaders/Lighting/BlinnPhong.ovfxh index 66241ab7c..f62ccdb41 100644 --- a/Resources/Engine/Shaders/Lighting/BlinnPhong.ovfxh +++ b/Resources/Engine/Shaders/Lighting/BlinnPhong.ovfxh @@ -1,106 +1,98 @@ #include ":Shaders/Common/Physics.ovfxh" #include ":Shaders/Common/Utils.ovfxh" +#include ":Shaders/Lighting/Shared.ovfxh" #include ":Shaders/Common/Buffers/LightsSSBO.ovfxh" -vec3 BlinnPhong(vec3 p_LightDir, vec3 p_LightColor, float p_Luminosity, vec4 diffuseTexel, vec4 specularTexel, vec3 normal, vec3 viewDir, float shininess) +vec3 BlinnPhong(vec3 lightDir, vec3 lightColor, float luminosity, vec4 diffuseTexel, vec4 specularTexel, vec3 normal, vec3 viewDir, float shininess) { - const vec3 halfwayDir = normalize(p_LightDir + viewDir); - const float diffuseCoefficient = max(dot(normal, p_LightDir), 0.0); + const vec3 halfwayDir = normalize(lightDir + viewDir); + const float diffuseCoefficient = max(dot(normal, lightDir), 0.0); const float specularCoefficient = pow(max(dot(normal, halfwayDir), 0.0), shininess * 2.0); - return p_LightColor * diffuseTexel.rgb * diffuseCoefficient * p_Luminosity + ((p_Luminosity > 0.0) ? (p_LightColor * specularTexel.rgb * specularCoefficient * p_Luminosity) : vec3(0.0)); + return lightColor * diffuseTexel.rgb * diffuseCoefficient * luminosity + ((luminosity > 0.0) ? (lightColor * specularTexel.rgb * specularCoefficient * luminosity) : vec3(0.0)); } -float LuminosityFromAttenuation(mat4 p_Light, vec3 fragPos) -{ - const vec3 lightPosition = p_Light[0].rgb; - const float constant = p_Light[0][3]; - const float linear = p_Light[1][3]; - const float quadratic = p_Light[2][3]; - - const float distanceToLight = length(lightPosition - fragPos); - const float attenuation = (constant + linear * distanceToLight + quadratic * (distanceToLight * distanceToLight)); - return 1.0 / attenuation; -} - -vec3 CalcPointLight(mat4 p_Light, vec3 fragPos, vec4 diffuseTexel, vec4 specularTexel, vec3 normal, vec3 viewDir, float shininess) +vec3 ComputePointLight(mat4 light, vec3 fragPos, vec4 diffuseTexel, vec4 specularTexel, vec3 normal, vec3 viewDir, float shininess) { /* Extract light information from light mat4 */ - const vec3 lightPosition = p_Light[0].rgb; - const vec3 lightColor = UnPack(p_Light[2][0]); - const float intensity = p_Light[3][3]; + const vec3 lightPosition = light[0].rgb; + const vec3 lightColor = UnPack(light[2][0]); + const float intensity = light[3][3]; - const vec3 lightDirection = normalize(lightPosition - fragPos); - const float luminosity = LuminosityFromAttenuation(p_Light, fragPos); + const vec3 lightDirection = normalize(lightPosition - fragPos); + const float luminosity = LuminosityFromAttenuation(light, fragPos); return BlinnPhong(lightDirection, lightColor, intensity * luminosity, diffuseTexel, specularTexel, normal, viewDir, shininess); } -vec3 CalcDirectionalLight(mat4 light, vec4 diffuseTexel, vec4 specularTexel, vec3 normal, vec3 viewDir, float shininess) +vec3 ComputeDirectionalLight(mat4 light, vec4 diffuseTexel, vec4 specularTexel, vec3 normal, vec3 viewDir, float shininess) { return BlinnPhong(-light[1].rgb, UnPack(light[2][0]), light[3][3], diffuseTexel, specularTexel, normal, viewDir, shininess); } -vec3 CalcSpotLight(mat4 p_Light, vec3 fragPos, vec4 diffuseTexel, vec4 specularTexel, vec3 normal, vec3 viewDir, float shininess) +vec3 ComputeSpotLight(mat4 light, vec3 fragPos, vec4 diffuseTexel, vec4 specularTexel, vec3 normal, vec3 viewDir, float shininess) { /* Extract light information from light mat4 */ - const vec3 lightPosition = p_Light[0].rgb; - const vec3 lightForward = p_Light[1].rgb; - const vec3 lightColor = UnPack(p_Light[2][0]); - const float intensity = p_Light[3][3]; - const float cutOff = cos(radians(p_Light[3][1])); - const float outerCutOff = cos(radians(p_Light[3][1] + p_Light[3][2])); + const vec3 lightPosition = light[0].rgb; + const vec3 lightForward = light[1].rgb; + const vec3 lightColor = UnPack(light[2][0]); + const float intensity = light[3][3]; + const float cutOff = cos(radians(light[3][1])); + const float outerCutOff = cos(radians(light[3][1] + light[3][2])); - const vec3 lightDirection = normalize(lightPosition - fragPos); - const float luminosity = LuminosityFromAttenuation(p_Light, fragPos); + const vec3 lightDirection = normalize(lightPosition - fragPos); + const float luminosity = LuminosityFromAttenuation(light, fragPos); /* Calculate the spot intensity */ - const float theta = dot(lightDirection, normalize(-lightForward)); - const float epsilon = cutOff - outerCutOff; - const float spotIntensity = clamp((theta - outerCutOff) / epsilon, 0.0, 1.0); + const float theta = dot(lightDirection, normalize(-lightForward)); + const float epsilon = cutOff - outerCutOff; + const float spotIntensity = clamp((theta - outerCutOff) / epsilon, 0.0, 1.0); return BlinnPhong(lightDirection, lightColor, intensity * spotIntensity * luminosity, diffuseTexel, specularTexel, normal, viewDir, shininess); } -vec3 CalcAmbientBoxLight(mat4 p_Light, vec3 fragPos, vec4 diffuseTexel) +vec3 ComputeAmbientBoxLight(mat4 light, vec3 fragPos, vec4 diffuseTexel) { - const vec3 lightPosition = p_Light[0].rgb; - const vec3 lightColor = UnPack(p_Light[2][0]); - const float intensity = p_Light[3][3]; - const vec3 size = vec3(p_Light[0][3], p_Light[1][3], p_Light[2][3]); + const vec3 lightPosition = light[0].rgb; + const vec3 lightColor = UnPack(light[2][0]); + const float intensity = light[3][3]; + const vec3 size = vec3(light[0][3], light[1][3], light[2][3]); - return PointInAABB(fragPos, lightPosition, size) ? diffuseTexel.rgb * lightColor * intensity : vec3(0.0); + return IsPointInAABB(fragPos, lightPosition, size) ? diffuseTexel.rgb * lightColor * intensity : vec3(0.0); } -vec3 CalcAmbientSphereLight(mat4 p_Light, vec3 fragPos, vec4 diffuseTexel) +vec3 ComputeAmbientSphereLight(mat4 light, vec3 fragPos, vec4 diffuseTexel) { - const vec3 lightPosition = p_Light[0].rgb; - const vec3 lightColor = UnPack(p_Light[2][0]); - const float intensity = p_Light[3][3]; - const float radius = p_Light[0][3]; + const vec3 lightPosition = light[0].rgb; + const vec3 lightColor = UnPack(light[2][0]); + const float intensity = light[3][3]; + const float radius = light[0][3]; - return PointInSphere(fragPos, lightPosition, radius) ? diffuseTexel.rgb * lightColor * intensity : vec3(0.0); + return IsPointInSphere(fragPos, lightPosition, radius) ? diffuseTexel.rgb * lightColor * intensity : vec3(0.0); } -vec4 BLINN_PHONG(vec2 texCoords, vec3 normal, vec3 viewPos, vec3 fragPos, vec4 diffuse, vec3 specular, sampler2D diffuseMap, sampler2D specularMap, float shininess) +vec4 ComputeBlinnPhongLighting(vec2 texCoords, vec3 normal, vec3 viewPos, vec3 fragPos, vec4 diffuse, vec3 specular, sampler2D diffuseMap, sampler2D specularMap, float shininess) { vec3 viewDir = normalize(viewPos - fragPos); vec4 diffuseTexel = texture(diffuseMap, texCoords) * diffuse; vec4 specularTexel = texture(specularMap, texCoords) * vec4(specular, 1.0); - vec3 lightSum = vec3(0.0); + vec3 lightAccumulation = vec3(0.0); for (int i = 0; i < ssbo_Lights.length(); ++i) { - switch(int(ssbo_Lights[i][3][0])) + const mat4 light = ssbo_Lights[i]; + const int lightType = int(light[3][0]); + + switch(lightType) { - case 0: lightSum += CalcPointLight(ssbo_Lights[i], fragPos, diffuseTexel, specularTexel, normal, viewDir, shininess); break; - case 1: lightSum += CalcDirectionalLight(ssbo_Lights[i], diffuseTexel, specularTexel, normal, viewDir, shininess); break; - case 2: lightSum += CalcSpotLight(ssbo_Lights[i], fragPos, diffuseTexel, specularTexel, normal, viewDir, shininess); break; - case 3: lightSum += CalcAmbientBoxLight(ssbo_Lights[i], fragPos, diffuseTexel); break; - case 4: lightSum += CalcAmbientSphereLight(ssbo_Lights[i], fragPos, diffuseTexel); break; + case 0: lightAccumulation += ComputePointLight(light, fragPos, diffuseTexel, specularTexel, normal, viewDir, shininess); break; + case 1: lightAccumulation += ComputeDirectionalLight(light, diffuseTexel, specularTexel, normal, viewDir, shininess); break; + case 2: lightAccumulation += ComputeSpotLight(light, fragPos, diffuseTexel, specularTexel, normal, viewDir, shininess); break; + case 3: lightAccumulation += ComputeAmbientBoxLight(light, fragPos, diffuseTexel); break; + case 4: lightAccumulation += ComputeAmbientSphereLight(light, fragPos, diffuseTexel); break; } } - return vec4(lightSum, diffuseTexel.a); + return vec4(lightAccumulation, diffuseTexel.a); } diff --git a/Resources/Engine/Shaders/Lighting/Lambert.ovfxh b/Resources/Engine/Shaders/Lighting/Lambert.ovfxh index e4a8d6885..223c5280f 100644 --- a/Resources/Engine/Shaders/Lighting/Lambert.ovfxh +++ b/Resources/Engine/Shaders/Lighting/Lambert.ovfxh @@ -1,5 +1,5 @@ -vec3 Lambert(vec3 p_fragPos, vec3 p_normal, vec3 lightPos, vec3 lightDiffuse, vec3 lightAmbient) +vec3 ComputeLambertLighting(vec3 fragPos, vec3 normal, vec3 lightPos, vec3 lightDiffuse, vec3 lightAmbient) { - const float diffuse = max(dot(p_normal, normalize(lightPos - p_fragPos)), 0.0); + const float diffuse = max(dot(normal, normalize(lightPos - fragPos)), 0.0); return clamp(lightDiffuse * diffuse + lightAmbient, 0.0, 1.0); } diff --git a/Resources/Engine/Shaders/Lighting/PBR.ovfxh b/Resources/Engine/Shaders/Lighting/PBR.ovfxh index dba06e6ee..4df6c21b6 100644 --- a/Resources/Engine/Shaders/Lighting/PBR.ovfxh +++ b/Resources/Engine/Shaders/Lighting/PBR.ovfxh @@ -2,15 +2,16 @@ #include ":Shaders/Common/Utils.ovfxh" #include ":Shaders/Common/Constants.ovfxh" #include ":Shaders/Common/Buffers/LightsSSBO.ovfxh" +#include ":Shaders/Lighting/Shared.ovfxh" float DistributionGGX(vec3 N, vec3 H, float roughness) { - float a = roughness*roughness; - float a2 = a*a; + float a = roughness * roughness; + float a2 = a * a; float NdotH = max(dot(N, H), 0.0); - float NdotH2 = NdotH*NdotH; + float NdotH2 = NdotH * NdotH; - float num = a2; + float num = a2; float denom = (NdotH2 * (a2 - 1.0) + 1.0); denom = PI * denom * denom; @@ -20,9 +21,9 @@ float DistributionGGX(vec3 N, vec3 H, float roughness) float GeometrySchlickGGX(float NdotV, float roughness) { float r = (roughness + 1.0); - float k = (r*r) / 8.0; + float k = (r * r) / 8.0; - float num = NdotV; + float num = NdotV; float denom = NdotV * (1.0 - k) + k; return num / denom; @@ -31,56 +32,44 @@ float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness) { float NdotV = max(dot(N, V), 0.0); float NdotL = max(dot(N, L), 0.0); - float ggx2 = GeometrySchlickGGX(NdotV, roughness); - float ggx1 = GeometrySchlickGGX(NdotL, roughness); + float ggx2 = GeometrySchlickGGX(NdotV, roughness); + float ggx1 = GeometrySchlickGGX(NdotL, roughness); return ggx1 * ggx2; } -vec3 fresnelSchlick(float cosTheta, vec3 F0) +vec3 FresnelSchlick(float cosTheta, vec3 F0) { return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0); } -float LuminosityFromAttenuation(mat4 p_Light, vec3 fragPos) +vec3 ComputeAmbientBoxLight(mat4 light, vec3 fragPos) { - const vec3 lightPosition = p_Light[0].rgb; - const float constant = p_Light[0][3]; - const float linear = p_Light[1][3]; - const float quadratic = p_Light[2][3]; - - const float distanceToLight = length(lightPosition - fragPos); - const float attenuation = (constant + linear * distanceToLight + quadratic * (distanceToLight * distanceToLight)); - return 1.0 / attenuation; -} - -vec3 CalcAmbientBoxLight(mat4 p_Light, vec3 fragPos) -{ - const vec3 lightPosition = p_Light[0].rgb; - const vec3 lightColor = UnPack(p_Light[2][0]); - const float intensity = p_Light[3][3]; - const vec3 size = vec3(p_Light[0][3], p_Light[1][3], p_Light[2][3]); + const vec3 lightPosition = light[0].rgb; + const vec3 lightColor = UnPack(light[2][0]); + const float intensity = light[3][3]; + const vec3 size = vec3(light[0][3], light[1][3], light[2][3]); - return PointInAABB(fragPos, lightPosition, size) ? lightColor * intensity : vec3(0.0); + return IsPointInAABB(fragPos, lightPosition, size) ? lightColor * intensity : vec3(0.0); } -vec3 CalcAmbientSphereLight(mat4 p_Light, vec3 fragPos) +vec3 ComputeAmbientSphereLight(mat4 light, vec3 fragPos) { - const vec3 lightPosition = p_Light[0].rgb; - const vec3 lightColor = UnPack(p_Light[2][0]); - const float intensity = p_Light[3][3]; - const float radius = p_Light[0][3]; + const vec3 lightPosition = light[0].rgb; + const vec3 lightColor = UnPack(light[2][0]); + const float intensity = light[3][3]; + const float radius = light[0][3]; - return PointInSphere(fragPos, lightPosition, radius) ? lightColor * intensity : vec3(0.0); + return IsPointInSphere(fragPos, lightPosition, radius) ? lightColor * intensity : vec3(0.0); } -vec4 PBR(vec2 texCoords, vec3 normal, vec3 viewPos, vec3 fragPos, vec4 inAlbedo, float inMetallic, float inRoughness, sampler2D albedoMap, sampler2D metallicMap, sampler2D roughnessMap, sampler2D aoMap) +vec4 ComputePBRLighting(vec2 texCoords, vec3 normal, vec3 viewPos, vec3 fragPos, vec4 inAlbedo, float inMetallic, float inRoughness, sampler2D albedoMap, sampler2D metallicMap, sampler2D roughnessMap, sampler2D aoMap) { - vec4 albedoRGBA = texture(albedoMap, texCoords) * inAlbedo; - vec3 albedo = pow(albedoRGBA.rgb, vec3(2.2)); - float metallic = texture(metallicMap, texCoords).r * inMetallic; - float roughness = texture(roughnessMap, texCoords).r * inRoughness; - float ao = texture(aoMap, texCoords).r; + vec4 albedoRGBA = texture(albedoMap, texCoords) * inAlbedo; + vec3 albedo = pow(albedoRGBA.rgb, vec3(2.2)); + float metallic = texture(metallicMap, texCoords).r * inMetallic; + float roughness = texture(roughnessMap, texCoords).r * inRoughness; + float ao = texture(aoMap, texCoords).r; vec3 N = normalize(normal); vec3 V = normalize(viewPos - fragPos); @@ -94,63 +83,66 @@ vec4 PBR(vec2 texCoords, vec3 normal, vec3 viewPos, vec3 fragPos, vec4 inAlbedo, for (int i = 0; i < ssbo_Lights.length(); ++i) { - if (int(ssbo_Lights[i][3][0]) == 3) + const mat4 light = ssbo_Lights[i]; + const int lightType = int(light[3][0]); + + if (lightType == 3) { - ambientSum += CalcAmbientBoxLight(ssbo_Lights[i], fragPos); + ambientSum += ComputeAmbientBoxLight(light, fragPos); } - else if (int(ssbo_Lights[i][3][0]) == 4) + else if (lightType == 4) { - ambientSum += CalcAmbientSphereLight(ssbo_Lights[i], fragPos); + ambientSum += ComputeAmbientSphereLight(light, fragPos); } else { // calculate per-light radiance - vec3 L = int(ssbo_Lights[i][3][0]) == 1 ? -ssbo_Lights[i][1].rgb : normalize(ssbo_Lights[i][0].rgb - fragPos); + vec3 L = lightType == 1 ? -light[1].rgb : normalize(light[0].rgb - fragPos); vec3 H = normalize(V + L); - float distance = length(ssbo_Lights[i][0].rgb - fragPos); + float distance = length(light[0].rgb - fragPos); float lightCoeff = 0.0; - switch(int(ssbo_Lights[i][3][0])) + switch(int(light[3][0])) { case 0: - lightCoeff = LuminosityFromAttenuation(ssbo_Lights[i], fragPos) * ssbo_Lights[i][3][3]; + lightCoeff = LuminosityFromAttenuation(light, fragPos) * light[3][3]; break; case 1: - lightCoeff = ssbo_Lights[i][3][3]; + lightCoeff = light[3][3]; break; case 2: - const vec3 lightForward = ssbo_Lights[i][1].rgb; - const float cutOff = cos(radians(ssbo_Lights[i][3][1])); - const float outerCutOff = cos(radians(ssbo_Lights[i][3][1] + ssbo_Lights[i][3][2])); + const vec3 lightForward = light[1].rgb; + const float cutOff = cos(radians(light[3][1])); + const float outerCutOff = cos(radians(light[3][1] + light[3][2])); - const vec3 lightDirection = normalize(ssbo_Lights[i][0].rgb - fragPos); - const float luminosity = LuminosityFromAttenuation(ssbo_Lights[i], fragPos); + const vec3 lightDirection = normalize(light[0].rgb - fragPos); + const float luminosity = LuminosityFromAttenuation(light, fragPos); /* Calculate the spot intensity */ - const float theta = dot(lightDirection, normalize(-lightForward)); - const float epsilon = cutOff - outerCutOff; - const float spotIntensity = clamp((theta - outerCutOff) / epsilon, 0.0, 1.0); + const float theta = dot(lightDirection, normalize(-lightForward)); + const float epsilon = cutOff - outerCutOff; + const float spotIntensity = clamp((theta - outerCutOff) / epsilon, 0.0, 1.0); - lightCoeff = luminosity * spotIntensity * ssbo_Lights[i][3][3]; + lightCoeff = luminosity * spotIntensity * light[3][3]; break; } - vec3 radiance = UnPack(ssbo_Lights[i][2][0]) * lightCoeff; + vec3 radiance = UnPack(light[2][0]) * lightCoeff; // cook-torrance brdf float NDF = DistributionGGX(N, H, roughness); - float G = GeometrySmith(N, V, L, roughness); - vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0); + float G = GeometrySmith(N, V, L, roughness); + vec3 F = FresnelSchlick(max(dot(H, V), 0.0), F0); vec3 kS = F; vec3 kD = vec3(1.0) - kS; kD *= 1.0 - metallic; - vec3 numerator = NDF * G * F; + vec3 numerator = NDF * G * F; float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0); - vec3 specular = numerator / max(denominator, 0.001); + vec3 specular = numerator / max(denominator, 0.001); // add to outgoing radiance Lo float NdotL = max(dot(N, L), 0.0); diff --git a/Resources/Engine/Shaders/Lighting/Shared.ovfxh b/Resources/Engine/Shaders/Lighting/Shared.ovfxh new file mode 100644 index 000000000..7c8dd53e2 --- /dev/null +++ b/Resources/Engine/Shaders/Lighting/Shared.ovfxh @@ -0,0 +1,12 @@ +float LuminosityFromAttenuation(mat4 light, vec3 fragPos) +{ + const vec3 lightPosition = light[0].rgb; + const float constant = light[0][3]; + const float linear = light[1][3]; + const float quadratic = light[2][3]; + + const float distanceToLight = length(lightPosition - fragPos); + const float attenuation = (constant + linear * distanceToLight + quadratic * (distanceToLight * distanceToLight)); + + return 1.0 / attenuation; +} diff --git a/Resources/Engine/Shaders/Standard.ovfx b/Resources/Engine/Shaders/Standard.ovfx index aded59e4a..7cfdd69f1 100644 --- a/Resources/Engine/Shaders/Standard.ovfx +++ b/Resources/Engine/Shaders/Standard.ovfx @@ -25,7 +25,7 @@ void main() vs_out.FragPos = vec3(ubo_Model * vec4(geo_Pos, 1.0)); vs_out.TexCoords = geo_TexCoords; vs_out.Normal = normalize(mat3(transpose(inverse(ubo_Model))) * geo_Normal); - vs_out.TBN = COMPUTE_TBN(ubo_Model, geo_Normal, geo_Tangent, geo_Bitangent); + vs_out.TBN = ConstructTBN(ubo_Model, geo_Normal, geo_Tangent, geo_Bitangent); vs_out.TangentViewPos = transpose(vs_out.TBN) * ubo_ViewPos; vs_out.TangentFragPos = transpose(vs_out.TBN) * vs_out.FragPos; @@ -65,13 +65,13 @@ out vec4 FRAGMENT_COLOR; void main() { - vec2 texCoords = TRANSFORM_TEX_COORDS(fs_in.TexCoords, u_TextureTiling, u_TextureOffset); - texCoords = APPLY_PARALLAX_MAPPING(texCoords, u_HeightMap, fs_in.TangentViewPos, fs_in.TangentFragPos, u_HeightScale); + vec2 texCoords = TileAndOffsetTexCoords(fs_in.TexCoords, u_TextureTiling, u_TextureOffset); + texCoords = ApplyParallaxMapping(texCoords, u_HeightMap, fs_in.TangentViewPos, fs_in.TangentFragPos, u_HeightScale); - if (!IS_MASKED(u_MaskMap, texCoords)) + if (!IsMasked(u_MaskMap, texCoords)) { - vec3 normal = COMPUTE_NORMAL(u_EnableNormalMapping, texCoords, fs_in.Normal, u_NormalMap, fs_in.TBN); - FRAGMENT_COLOR = BLINN_PHONG(texCoords, normal, ubo_ViewPos, fs_in.FragPos, u_Diffuse, u_Specular, u_DiffuseMap, u_SpecularMap, u_Shininess); + vec3 normal = ComputeNormal(u_EnableNormalMapping, texCoords, fs_in.Normal, u_NormalMap, fs_in.TBN); + FRAGMENT_COLOR = ComputeBlinnPhongLighting(texCoords, normal, ubo_ViewPos, fs_in.FragPos, u_Diffuse, u_Specular, u_DiffuseMap, u_SpecularMap, u_Shininess); } else { diff --git a/Resources/Engine/Shaders/StandardPBR.ovfx b/Resources/Engine/Shaders/StandardPBR.ovfx index 515d97a0d..28862cae4 100644 --- a/Resources/Engine/Shaders/StandardPBR.ovfx +++ b/Resources/Engine/Shaders/StandardPBR.ovfx @@ -25,7 +25,7 @@ void main() vs_out.FragPos = vec3(ubo_Model * vec4(geo_Pos, 1.0)); vs_out.TexCoords = geo_TexCoords; vs_out.Normal = normalize(mat3(transpose(inverse(ubo_Model))) * geo_Normal); - vs_out.TBN = COMPUTE_TBN(ubo_Model, geo_Normal, geo_Tangent, geo_Bitangent); + vs_out.TBN = ConstructTBN(ubo_Model, geo_Normal, geo_Tangent, geo_Bitangent); vs_out.TangentViewPos = transpose(vs_out.TBN) * ubo_ViewPos; vs_out.TangentFragPos = transpose(vs_out.TBN) * vs_out.FragPos; @@ -67,13 +67,13 @@ out vec4 FRAGMENT_COLOR; void main() { - vec2 texCoords = TRANSFORM_TEX_COORDS(fs_in.TexCoords, u_TextureTiling, u_TextureOffset); - texCoords = APPLY_PARALLAX_MAPPING(texCoords, u_HeightMap, fs_in.TangentViewPos, fs_in.TangentFragPos, u_HeightScale); + vec2 texCoords = TileAndOffsetTexCoords(fs_in.TexCoords, u_TextureTiling, u_TextureOffset); + texCoords = ApplyParallaxMapping(texCoords, u_HeightMap, fs_in.TangentViewPos, fs_in.TangentFragPos, u_HeightScale); - if (!IS_MASKED(u_MaskMap, texCoords)) + if (!IsMasked(u_MaskMap, texCoords)) { - vec3 normal = COMPUTE_NORMAL(u_EnableNormalMapping, texCoords, fs_in.Normal, u_NormalMap, fs_in.TBN); - FRAGMENT_COLOR = PBR(texCoords, normal, ubo_ViewPos, fs_in.FragPos, u_Albedo, u_Metallic, u_Roughness, u_AlbedoMap, u_MetallicMap, u_RoughnessMap, u_AmbientOcclusionMap); + vec3 normal = ComputeNormal(u_EnableNormalMapping, texCoords, fs_in.Normal, u_NormalMap, fs_in.TBN); + FRAGMENT_COLOR = ComputePBRLighting(texCoords, normal, ubo_ViewPos, fs_in.FragPos, u_Albedo, u_Metallic, u_Roughness, u_AlbedoMap, u_MetallicMap, u_RoughnessMap, u_AmbientOcclusionMap); } else { diff --git a/Resources/Engine/Shaders/Unlit.ovfx b/Resources/Engine/Shaders/Unlit.ovfx index 5bcbb8984..a01db0cad 100644 --- a/Resources/Engine/Shaders/Unlit.ovfx +++ b/Resources/Engine/Shaders/Unlit.ovfx @@ -42,6 +42,6 @@ out vec4 FRAGMENT_COLOR; void main() { - vec2 texCoords = TRANSFORM_TEX_COORDS(fs_in.TexCoords, u_TextureTiling, u_TextureOffset); + vec2 texCoords = TileAndOffsetTexCoords(fs_in.TexCoords, u_TextureTiling, u_TextureOffset); FRAGMENT_COLOR = texture(u_DiffuseMap, texCoords) * u_Diffuse; } From f91b9a202180db53651cde3c8c9b804336b4947a Mon Sep 17 00:00:00 2001 From: Adrien GIVRY Date: Sun, 24 Mar 2024 14:33:30 -0400 Subject: [PATCH 12/12] Added option to create an empty shader and a partial shader from asset browser --- .../src/OvEditor/Panels/AssetBrowser.cpp | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/Sources/Overload/OvEditor/src/OvEditor/Panels/AssetBrowser.cpp b/Sources/Overload/OvEditor/src/OvEditor/Panels/AssetBrowser.cpp index 1a7889378..3d9840085 100644 --- a/Sources/Overload/OvEditor/src/OvEditor/Panels/AssetBrowser.cpp +++ b/Sources/Overload/OvEditor/src/OvEditor/Panels/AssetBrowser.cpp @@ -206,6 +206,8 @@ class FolderContextualMenu : public BrowserItemContextualMenu auto& createShaderMenu = createMenu.CreateWidget("Shader"); auto& createMaterialMenu = createMenu.CreateWidget("Material"); + auto& createEmptyShaderMenu = createShaderMenu.CreateWidget("Empty"); + auto& createPartialShaderMenu = createShaderMenu.CreateWidget("Partial"); auto& createStandardShaderMenu = createShaderMenu.CreateWidget("Standard template"); auto& createStandardPBRShaderMenu = createShaderMenu.CreateWidget("Standard PBR template"); auto& createUnlitShaderMenu = createShaderMenu.CreateWidget("Unlit template"); @@ -226,6 +228,8 @@ class FolderContextualMenu : public BrowserItemContextualMenu auto& createUnlitMaterial = createUnlitMaterialMenu.CreateWidget(""); auto& createLambertMaterial = createLambertMaterialMenu.CreateWidget(""); + auto& createEmptyShader = createEmptyShaderMenu.CreateWidget(""); + auto& createPartialShader = createPartialShaderMenu.CreateWidget(""); auto& createStandardShader = createStandardShaderMenu.CreateWidget(""); auto& createStandardPBRShader = createStandardPBRShaderMenu.CreateWidget(""); auto& createUnlitShader = createUnlitShaderMenu.CreateWidget(""); @@ -238,6 +242,8 @@ class FolderContextualMenu : public BrowserItemContextualMenu createUnlitShaderMenu.ClickedEvent += [&createUnlitShader] { createUnlitShader.content = ""; }; createLambertShaderMenu.ClickedEvent += [&createLambertShader] { createLambertShader.content = ""; }; createEmptyMaterialMenu.ClickedEvent += [&createEmptyMaterial] { createEmptyMaterial.content = ""; }; + createEmptyShaderMenu.ClickedEvent += [&createEmptyShader] { createEmptyShader.content = ""; }; + createPartialShaderMenu.ClickedEvent += [&createPartialShader] { createPartialShader.content = ""; }; createStandardMaterialMenu.ClickedEvent += [&createStandardMaterial] { createStandardMaterial.content = ""; }; createStandardPBRMaterialMenu.ClickedEvent += [&createStandardPBRMaterial] { createStandardPBRMaterial.content = ""; }; createUnlitMaterialMenu.ClickedEvent += [&createUnlitMaterial] { createUnlitMaterial.content = ""; }; @@ -280,6 +286,46 @@ class FolderContextualMenu : public BrowserItemContextualMenu Close(); }; + createEmptyShader.EnterPressedEvent += [this](std::string newShaderName) + { + size_t fails = 0; + std::string finalPath; + + do + { + finalPath = filePath + '\\' + (!fails ? newShaderName : newShaderName + " (" + std::to_string(fails) + ')') + ".ovfx"; + + ++fails; + } while (std::filesystem::exists(finalPath)); + + { + std::ofstream outfile(finalPath); + } + + ItemAddedEvent.Invoke(finalPath); + Close(); + }; + + createPartialShader.EnterPressedEvent += [this](std::string newShaderName) + { + size_t fails = 0; + std::string finalPath; + + do + { + finalPath = filePath + '\\' + (!fails ? newShaderName : newShaderName + " (" + std::to_string(fails) + ')') + ".ovfxh"; + + ++fails; + } while (std::filesystem::exists(finalPath)); + + { + std::ofstream outfile(finalPath); + } + + ItemAddedEvent.Invoke(finalPath); + Close(); + }; + createStandardShader.EnterPressedEvent += [this](std::string newShaderName) { size_t fails = 0;