diff --git a/Editor/Generation/ShaderBuilder.cs b/Editor/Generation/ShaderBuilder.cs index fb36484..e1f057f 100644 --- a/Editor/Generation/ShaderBuilder.cs +++ b/Editor/Generation/ShaderBuilder.cs @@ -178,24 +178,9 @@ public void Build(ShaderNode shaderNode) const string VertexPreview = "Packages/com.z3y.graphlit/Editor/Targets/Preview/Vertex.hlsl"; const string FragmentPreview = "Packages/com.z3y.graphlit/Editor/Targets/Preview/Fragment.hlsl"; - public static void GenerateUnifiedPreview(ShaderGraphView graphView, ShaderNode node, List rightNodes, bool log = false) + public static void GenerateUnifiedPreview(ShaderGraphView graphView, ShaderNode node, List rightNodes, bool log = false, bool allNodes = false) { - var affectedNodes = (new List { node }).Union(rightNodes); - - bool anyPreview = false; - foreach (var x in affectedNodes) - { - x.EvaluateDimensionsForGraphView(); - if (x._previewDisabled || x.DisablePreview) - { - continue; - } - anyPreview = true; - } - if (!anyPreview) - { - return; - } + var affectedNodes = (new List { node }).Union(rightNodes).ToList(); var shaderBuilder = new ShaderBuilder(GenerationMode.Preview, graphView) { @@ -226,7 +211,6 @@ public static void GenerateUnifiedPreview(ShaderGraphView graphView, ShaderNode shaderBuilder.TraverseGraph(node, fragmentVisitor); node.BuilderVisit(fragmentVisitor); shaderBuilder.visitedNodes.Add(node.viewDataKey); - node.EvaluateDimensionsForGraphView(); foreach (var rightNode in rightNodes) { @@ -236,8 +220,19 @@ public static void GenerateUnifiedPreview(ShaderGraphView graphView, ShaderNode shaderBuilder.visitedNodes.Add(rightNode.viewDataKey); rightNode.BuilderVisit(fragmentVisitor); } + } - rightNode.EvaluateDimensionsForGraphView(); + if (allNodes) + { + var nodes = graphView.GetElementsGuidDictionary(); + foreach (var guid in shaderBuilder.visitedNodes) + { + var n = nodes[guid]; + if (!affectedNodes.Contains(n)) + { + affectedNodes.Add(n); + } + } } @@ -285,6 +280,11 @@ public static void GenerateUnifiedPreview(ShaderGraphView graphView, ShaderNode sb.Add("}"); + if (previewId <= 0) + { + return; + } + string result = shaderBuilder.ToString(); //Debug.Log(result); @@ -304,26 +304,6 @@ public static void GenerateUnifiedPreview(ShaderGraphView graphView, ShaderNode } } - static void EvaluateDimensionsRecursive(HashSet visitedNodes, ShaderNode node) - { - if (visitedNodes.Contains(node.viewDataKey)) - { - return; - } - - foreach (var input in node.Inputs) - { - foreach (var edge in input.connections) - { - EvaluateDimensionsRecursive(visitedNodes, (ShaderNode)edge.output.node); - break; - } - } - - node.EvaluateDimensionsForGraphView(); - visitedNodes.Add(node.viewDataKey); - } - public static void GenerateAllPreviews(ShaderGraphView graphView) { graphView.UpdateCachedNodesForBuilder(); @@ -346,24 +326,9 @@ public static void GenerateAllPreviews(ShaderGraphView graphView) } } - var visitedNodes = new HashSet(); foreach (var node in endNodes) { - EvaluateDimensionsRecursive(visitedNodes, node); - } - - var generatedNodes = new HashSet(); - - foreach (var node in nodes) - { - if (!generatedNodes.Contains(node.viewDataKey)) - { - if (!(node._previewDisabled || node.DisablePreview)) - { - generatedNodes.UnionWith(node.GeneratePreviewForAffectedNodes()); - } - generatedNodes.Add(node.viewDataKey); - } + GenerateUnifiedPreview(graphView, node, new List(), false, true); } } diff --git a/Editor/ShaderGraphView.cs b/Editor/ShaderGraphView.cs index 6d7e7da..5665b02 100644 --- a/Editor/ShaderGraphView.cs +++ b/Editor/ShaderGraphView.cs @@ -91,12 +91,12 @@ public ShaderGraphView(ShaderGraphWindow editorWindow, string assetPath) RegisterCallback(OnDragUpdated); RegisterCallback(OnDragPerform); - if (!string.IsNullOrEmpty(assetPath)) + /*if (!string.IsNullOrEmpty(assetPath)) { var assets = AssetDatabase.LoadAllAssetRepresentationsAtPath(assetPath); //i//mportedMaterial = assets.OfType().FirstOrDefault(); //Debug.Log(importedMaterial); - } + }*/ } private GraphViewChange OnGraphViewChanged(GraphViewChange change) @@ -532,7 +532,7 @@ void OnDragPerform(DragPerformEvent evt) evt.StopPropagation(); } - public Dictionary GetElementsGuidDictionary() where T : GraphElement, new() + public Dictionary GetElementsGuidDictionary() where T : GraphElement { var acceleratedGetNode = new Dictionary(); foreach (var item in graphElements.OfType()) diff --git a/Editor/ShaderNode/ShaderNode.cs b/Editor/ShaderNode/ShaderNode.cs index 7525e6c..42c8c01 100644 --- a/Editor/ShaderNode/ShaderNode.cs +++ b/Editor/ShaderNode/ShaderNode.cs @@ -354,117 +354,6 @@ internal set public virtual Precision DefaultPrecisionOverride => Precision.Inherit; protected internal bool _inheritedPrecision; - public Dictionary evaluatedOutputDimensions = new Dictionary(); - public void EvaluateDimensionsForGraphView() - { - var inputDimensions = new Dictionary(); - - foreach (var port in Inputs) - { - int id = port.GetPortID(); - if (!port.connected) - { - if (portDescriptors[id].Type is Float @float) - { - inputDimensions[id] = @float.dimensions; - SetPortColor(port, Float.GetPortColor(@float.dimensions)); - } - continue; - } - - foreach (var edge in port.connections) - { - var node = (ShaderNode)edge.output.node; - if (node is null) - { - continue; - } - - if (node.evaluatedOutputDimensions.TryGetValue(edge.output.GetPortID(), out int value)) - { - inputDimensions[id] = value; - //SetPortColor(port, Float.GetPortColor(value)); - } - break; - } - } - - - int trunc = 4; - int max = 1; - - foreach (var dimension in inputDimensions.Values) - { - if (dimension == 1) - { - continue; - } - max = Mathf.Max(max, dimension); - trunc = Mathf.Min(trunc, dimension); - } - trunc = Mathf.Min(trunc, max); - //Debug.Log(trunc); - - foreach (var desc in portDescriptors.Values) - { - if (desc.Direction == PortDirection.Input) - { - if (desc.Type is Float @float) - { - if (@float.dynamic) - { - var port = Inputs.First(x => x.GetPortID() == desc.ID); - SetPortColor(port, Float.GetPortColor(trunc)); - } - } - } - else - { - - if (desc.Type is Float @float) - { - if (!@float.dynamic) - { - evaluatedOutputDimensions[desc.ID] = @float.dimensions; - } - else - { - evaluatedOutputDimensions[desc.ID] = trunc; - SetPortColor(Outputs.First(x => x.GetPortID() == desc.ID), Float.GetPortColor(trunc)); - } - } - } - } - - - if (this is SwizzleNode swizzle) - { - evaluatedOutputDimensions[1] = Mathf.Clamp(swizzle.swizzle.Length, 1, 4); - SetPortColor(Outputs.First(), Float.GetPortColor(evaluatedOutputDimensions[1])); - return; - } - - if (this is FetchVariableNode fv) - { - var registers = GraphView.cachedRegisterVariablesForBuilder.Where(x => x._name == fv._name); - foreach (var regs in registers) - { - regs.evaluatedOutputDimensions[1] = trunc; - SetPortColor(regs.inputContainer.Children().OfType().First(), Float.GetPortColor(trunc)); - } - } - if (this is RegisterVariableNode reg) - { - var fetches = GraphView.cachedNodesForBuilder.OfType().Where(x => x._name == reg._name); - //Debug.Log(trunc); - foreach (var fetch in fetches) - { - fetch.evaluatedOutputDimensions[1] = trunc; - SetPortColor(fetch.outputContainer.Children().OfType().First(), Float.GetPortColor(trunc)); - } - } - } - public void InheritPreviewAndPrecision() { int is3D = 0; @@ -666,24 +555,6 @@ GeneratedPortData GetDefaultInput(int portID, NodeVisitor visitor) var descriptor = portDescriptors[portID]; string value = SetDefaultBinding(descriptor, visitor); - //var autoWireNodes = GraphView.graphElements.OfType().Where(x => x._autoWire).ToArray(); - - /*foreach (var node in autoWireNodes) - { - if (node._name.ToLower() == descriptor.Name.ToLower()) - { - var input = node.Inputs.First(); - if (!input.connected) - { - break; - } - var incomingPort = input.connections.First().output; - var incomingNode = (ShaderNode)incomingPort.node; - value = incomingNode.PortData[incomingPort.GetPortID()].Name; - break; - } - }*/ - UpdateDefaultValueTooltip(portID, value); return new GeneratedPortData(descriptor.Type, value); @@ -817,6 +688,7 @@ internal void BuilderVisit(NodeVisitor visitor, int[] portsMask = null) } Generate(visitor); + UpdateGraphViewDimensions(); } string DisplayName => Info.name.Split("/")[^1]; public string UniqueVariable => DisplayName.Replace(" ", "") + UniqueVariableID; @@ -943,7 +815,7 @@ public GeneratedPortData Cast(int portID, int targetDimensions, bool updatePort return newData; } - /*public void UpdateGraphView() + public void UpdateGraphViewDimensions() { foreach (var data in PortData) { @@ -961,7 +833,16 @@ public GeneratedPortData Cast(int portID, int targetDimensions, bool updatePort SetPortColor(port, color); } } - }*/ + + if (this is FetchVariableNode fetch) + { + var reg = GraphView.cachedRegisterVariablesForBuilder.FirstOrDefault(x => x._name == fetch._name); + if (reg is not null) + { + SetPortColor(reg.Inputs.First(), Outputs.First().portColor); + } + } + } public static void SetPortColor(Port port, Color color) {