From e8dfab9b28ab71356546db1cbd5ee882341a69c4 Mon Sep 17 00:00:00 2001 From: Silverlan Date: Sat, 23 Nov 2024 21:31:37 +0100 Subject: [PATCH] feat(shader_graph): update ui elements --- .../lua/gui/shader_graph/node.lua | 33 +++++++---- .../lua/gui/shader_graph/node_socket.lua | 4 +- .../lua/gui/shader_graph/shader_graph.lua | 58 +++++++++++++++---- .../rendering/shader_graph/nodes/camera.hpp | 14 +++++ .../src/rendering/shader_graph_manager.cpp | 1 + 5 files changed, 85 insertions(+), 25 deletions(-) diff --git a/assets/addons/shader_graph/lua/gui/shader_graph/node.lua b/assets/addons/shader_graph/lua/gui/shader_graph/node.lua index 847b8b6b4..bcae8f9be 100644 --- a/assets/addons/shader_graph/lua/gui/shader_graph/node.lua +++ b/assets/addons/shader_graph/lua/gui/shader_graph/node.lua @@ -18,6 +18,9 @@ function Element:OnInitialize() local box = gui.create("WIVBox", self, 0, 0, self:GetWidth(), self:GetHeight()) box:SetName("global_container") box:SetFixedWidth(true) + box:AddCallback("SetSize", function() + self:SetHeight(box:GetBottom()) + end) local outputControls = gui.create("WIPFMControlsMenu", box, 0, 0, box:GetWidth(), box:GetHeight()) outputControls:SetAutoFillContentsToHeight(false) @@ -28,9 +31,6 @@ function Element:OnInitialize() gui.create("WIPFMControlsMenu", box, 0, outputControls:GetBottom(), box:GetWidth(), box:GetHeight()) inputControls:SetAutoFillContentsToHeight(false) inputControls:SetFixedHeight(false) - inputControls:AddCallback("SetSize", function() - self:SetHeight(inputControls:GetBottom()) - end) self.m_inputControls = inputControls self.m_inputs = {} @@ -47,12 +47,17 @@ end function Element:GetNode() return self.m_node end -function Element:AddControl(socketType, title, id) +function Element:AddControl(socketType, title, id, type) local ctrlMenu = (socketType == gui.GraphNodeSocket.SOCKET_TYPE_INPUT) and self.m_inputControls or self.m_outputControls - local elCtrl = ctrlMenu:AddSliderControl(title, id, 0.01, 0.0, 0.1, function(el, value) - -- - end, 0.001) + local elCtrl + if socketType == gui.GraphNodeSocket.SOCKET_TYPE_INPUT and type ~= nil then + local wrapper = ctrlMenu:AddPropertyControl(type, id, title, {}) + elCtrl = wrapper:GetWrapperElement() + else + local el, wrapper = ctrlMenu:AddText(title, id, "") + elCtrl = wrapper + end local el = gui.create("WIGraphNodeSocket", elCtrl) el:SetSocket(self, id, socketType) el:SetMouseInputEnabled(true) @@ -67,7 +72,7 @@ function Element:AddControl(socketType, title, id) socketElement = el, controlElement = elCtrl, } - return el + return el, elCtrl end function Element:GetSocket(socketType, name) local t = (socketType == gui.GraphNodeSocket.SOCKET_TYPE_INPUT) and self.m_inputs or self.m_outputs @@ -82,12 +87,16 @@ end function Element:GetOutputSocket(name) return self:GetSocket(gui.GraphNodeSocket.SOCKET_TYPE_OUTPUT, name) end -function Element:AddInput(name) - return self:AddControl(gui.GraphNodeSocket.SOCKET_TYPE_INPUT, name, name) +function Element:AddInput(name, type) + local elSocket, elCtrl = self:AddControl(gui.GraphNodeSocket.SOCKET_TYPE_INPUT, name, name, type) + elSocket:SetX(elSocket:GetWidth() * -0.5) + elSocket:SetY(elCtrl:GetHeight() * 0.5 - elSocket:GetHeight() * 0.5) + return elSocket end function Element:AddOutput(name) - local elSocket = self:AddControl(gui.GraphNodeSocket.SOCKET_TYPE_OUTPUT, name, name) - elSocket:SetX(self:GetWidth() - elSocket:GetWidth()) + local elSocket, elCtrl = self:AddControl(gui.GraphNodeSocket.SOCKET_TYPE_OUTPUT, name, name) + elSocket:SetX(elCtrl:GetWidth() - elSocket:GetWidth() * 0.5) + elSocket:SetY(elCtrl:GetHeight() * 0.5 - elSocket:GetHeight() * 0.5) elSocket:SetAnchor(1, 0, 1, 0) return elSocket end diff --git a/assets/addons/shader_graph/lua/gui/shader_graph/node_socket.lua b/assets/addons/shader_graph/lua/gui/shader_graph/node_socket.lua index 20f6eb0b1..f3cf9adda 100644 --- a/assets/addons/shader_graph/lua/gui/shader_graph/node_socket.lua +++ b/assets/addons/shader_graph/lua/gui/shader_graph/node_socket.lua @@ -14,8 +14,8 @@ function Element:OnInitialize() self:SetSize(16, 16) - local bg = gui.create("WIRect", self, 0, 0, self:GetWidth(), self:GetHeight(), 0, 0, 1, 1) - bg:SetColor(Color.Red) + local bg = gui.create("WITexturedRect", self, 0, 0, self:GetWidth(), self:GetHeight(), 0, 0, 1, 1) + bg:SetMaterial("circle") self.m_bg = bg end function Element:SetSocket(node, socket, socketType) diff --git a/assets/addons/shader_graph/lua/gui/shader_graph/shader_graph.lua b/assets/addons/shader_graph/lua/gui/shader_graph/shader_graph.lua index bb0ff88ee..327bdd4eb 100644 --- a/assets/addons/shader_graph/lua/gui/shader_graph/shader_graph.lua +++ b/assets/addons/shader_graph/lua/gui/shader_graph/shader_graph.lua @@ -17,6 +17,7 @@ function Element:OnInitialize() self.m_nameToElementData = {} self.m_nodeData = {} self.m_linkElements = {} + self:Clear() self:SetSize(1280, 1024) self:SetMouseInputEnabled(true) @@ -80,31 +81,55 @@ function Element:MouseCallback(button, state, mods) pFileDialog:SetRootPath("scripts/shader_data/graphs") pFileDialog:Update() end) - pContext:AddItem("Add Node", function() - local graphNode = self.m_graph:AddNode("math") - if graphNode ~= nil then - self:AddNode(graphNode) - self:InitializeLinks() - end + pContext:AddItem("Generate GLSL", function() + util.set_clipboard_string(self.m_graph:GenerateGlsl()) end) + local reg = shader.get_graph_node_registry("object") + if reg ~= nil then + local nodeTypes = reg:GetNodeTypes() + table.sort(nodeTypes) + local pItem, pSubMenu = pContext:AddSubMenu("Add Node") + for _, name in pairs(nodeTypes) do + pSubMenu:AddItem(name, function(pItem) + local graphNode = self.m_graph:AddNode(name) + if graphNode ~= nil then + local frame = self:AddNode(graphNode) + local pos = self:GetCursorPos() + frame:SetPos(pos.x - frame:GetWidth() * 0.5, pos.y - frame:GetHeight() * 0.5) + self:InitializeLinks() + end + end) + end + pSubMenu:Update() + end pContext:Update() return util.EVENT_REPLY_HANDLED end end end -function Element:SetGraph(graph) +function Element:GetGraph() + return self.m_graph +end +function Element:Clear() self:ClearLinks() for _, t in ipairs(self.m_nodeData) do util.remove(t.frame) end self.m_nodeData = {} self.m_nameToElementData = {} + self.m_graph = shader.create_graph("object") +end +function Element:SetGraph(graph) + self:Clear() self.m_graph = graph local nodes = self.m_graph:GetNodes() + local offset = 0 for _, graphNode in ipairs(nodes) do - self:AddNode(graphNode) + local frame = self:AddNode(graphNode) + frame:SetX(offset) + offset = offset + frame:GetWidth() + 80 end self:InitializeLinks() @@ -146,6 +171,7 @@ function Element:AddLink(elOutputSocket, elInputSocket) local l = gui.create("WIElementConnectorLine", self) l:SetSize(self:GetSize()) l:SetAnchor(0, 0, 1, 1) + l:SetZPos(-1) l:Setup(elOutputSocket, elInputSocket) table.insert(self.m_linkElements, l) end @@ -172,7 +198,15 @@ function Element:AddNode(graphNode) frame:SetTitle(name) frame:SetDetachButtonEnabled(false) frame:SetCloseButtonEnabled(false) + frame:SetResizable(false) frame:SetSize(128, 128) + frame:SetZPos(0) + frame:AddCallback("OnDragStart", function(el, x, y) + el:SetZPos(1) + end) + frame:AddCallback("OnDragEnd", function(el, x, y) + el:SetZPos(0) + end) frame:SetMouseInputEnabled(true) frame:AddCallback("OnMouseEvent", function(el, button, state, mods) @@ -201,16 +235,17 @@ function Element:AddNode(graphNode) local elNode = gui.create("WIGraphNode", frame) elNode:SetNode(graphNode:GetName()) elNode:SetY(31) - frame:SetX(#self.m_nodeData * 200) + elNode:AddCallback("SetSize", function() + frame:SetHeight(elNode:GetBottom()) + end) for _, output in ipairs(graphNode:GetOutputs()) do local socket = output:GetSocket() local elOutput = elNode:AddOutput(socket.name) end for _, input in ipairs(graphNode:GetInputs()) do local socket = input:GetSocket() - local elInput = elNode:AddInput(socket.name) + local elInput = elNode:AddInput(socket.name, shader.Socket.to_udm_type(socket.type)) end - --x = elNode:GetRight() + 80 elNode:AddCallback("OnSocketClicked", function(elNode, elSocket, socketType, id) if util.is_valid(self.m_outSocket) == false then @@ -243,5 +278,6 @@ function Element:AddNode(graphNode) } table.insert(self.m_nodeData, t) self.m_nameToElementData[graphNode:GetName()] = t + return frame end gui.register("WIShaderGraph", Element) diff --git a/core/client/include/pragma/rendering/shader_graph/nodes/camera.hpp b/core/client/include/pragma/rendering/shader_graph/nodes/camera.hpp index 11244ded4..b6c1dd2df 100644 --- a/core/client/include/pragma/rendering/shader_graph/nodes/camera.hpp +++ b/core/client/include/pragma/rendering/shader_graph/nodes/camera.hpp @@ -29,4 +29,18 @@ namespace pragma::rendering::shader_graph { }; }; +#if 0 +layout(std140, LAYOUT_ID(SCENE, RENDER_SETTINGS)) uniform RenderSettings +{ + vec4 posCam; // w component is fov + int flags; + float shadowRatioX; + float shadowRatioY; + float nearZ; + float farZ; + int shaderQuality; // 1 = lowest, 10 = highest +} +u_renderSettings; +#endif + #endif diff --git a/core/client/src/rendering/shader_graph_manager.cpp b/core/client/src/rendering/shader_graph_manager.cpp index 298e6876e..429aae7bc 100644 --- a/core/client/src/rendering/shader_graph_manager.cpp +++ b/core/client/src/rendering/shader_graph_manager.cpp @@ -12,6 +12,7 @@ extern DLLCLIENT CEngine *c_engine; using namespace pragma::rendering; +#pragma optimize("", off) void ShaderGraphTypeManager::RegisterGraph(const std::string &identifier, std::shared_ptr graph) { auto fragFilePath = util::FilePath(ShaderGraphManager::GetShaderFilePath(m_typeName, identifier));