Exemple #1
0
        internal static void InsertCopyPasteGraph(this MaterialGraphView graphView, CopyPasteGraph copyGraph)
        {
            if (copyGraph == null)
            {
                return;
            }

            using (var remappedNodesDisposable = ListPool <INode> .GetDisposable())
                using (var remappedEdgesDisposable = ListPool <IEdge> .GetDisposable())
                {
                    var remappedNodes = remappedNodesDisposable.value;
                    var remappedEdges = remappedEdgesDisposable.value;
                    graphView.graph.PasteGraph(copyGraph, remappedNodes, remappedEdges);

                    // Add new elements to selection
                    graphView.ClearSelection();
                    graphView.graphElements.ForEach(element =>
                    {
                        var edge = element as Edge;
                        if (edge != null && remappedEdges.Contains(edge.userData as IEdge))
                        {
                            graphView.AddToSelection(edge);
                        }

                        var nodeView = element as MaterialNodeView;
                        if (nodeView != null && remappedNodes.Contains(nodeView.node))
                        {
                            graphView.AddToSelection(nodeView);
                        }
                    });
                }
        }
Exemple #2
0
        internal static void InsertCopyPasteGraph(this MaterialGraphView graphView, CopyPasteGraph copyGraph)
        {
            if (copyGraph == null)
            {
                return;
            }

            // Make new properties from the copied graph
            foreach (AbstractShaderProperty property in copyGraph.properties)
            {
                string propertyName = graphView.graph.SanitizePropertyName(property.displayName);
                AbstractShaderProperty copiedProperty = property.Copy();
                copiedProperty.displayName = propertyName;
                graphView.graph.AddShaderProperty(copiedProperty);

                // Update the property nodes that depends on the copied node
                var dependentPropertyNodes = copyGraph.GetNodes <PropertyNode>().Where(x => x.propertyGuid == property.guid);
                foreach (var node in dependentPropertyNodes)
                {
                    node.owner        = graphView.graph;
                    node.propertyGuid = copiedProperty.guid;
                }
            }

            using (var remappedNodesDisposable = ListPool <AbstractMaterialNode> .GetDisposable())
            {
                using (var remappedEdgesDisposable = ListPool <IEdge> .GetDisposable())
                {
                    var remappedNodes = remappedNodesDisposable.value;
                    var remappedEdges = remappedEdgesDisposable.value;
                    graphView.graph.PasteGraph(copyGraph, remappedNodes, remappedEdges);

                    if (graphView.graph.assetGuid != copyGraph.sourceGraphGuid)
                    {
                        // Compute the mean of the copied nodes.
                        Vector2 centroid = Vector2.zero;
                        var     count    = 1;
                        foreach (var node in remappedNodes)
                        {
                            var position = node.drawState.position.position;
                            centroid = centroid + (position - centroid) / count;
                            ++count;
                        }

                        // Get the center of the current view
                        var viewCenter = graphView.contentViewContainer.WorldToLocal(graphView.layout.center);

                        foreach (var node in remappedNodes)
                        {
                            var drawState    = node.drawState;
                            var positionRect = drawState.position;
                            var position     = positionRect.position;
                            position += viewCenter - centroid;
                            positionRect.position = position;
                            drawState.position    = positionRect;
                            node.drawState        = drawState;
                        }
                    }

                    // Add new elements to selection
                    graphView.ClearSelection();
                    graphView.graphElements.ForEach(element =>
                    {
                        var edge = element as Edge;
                        if (edge != null && remappedEdges.Contains(edge.userData as IEdge))
                        {
                            graphView.AddToSelection(edge);
                        }

                        var nodeView = element as MaterialNodeView;
                        if (nodeView != null && remappedNodes.Contains(nodeView.node))
                        {
                            graphView.AddToSelection(nodeView);
                        }
                    });
                }
            }
        }
Exemple #3
0
        public void HandleGraphChanges()
        {
            previewManager.HandleGraphChanges();
            previewManager.RenderPreviews();
            m_BlackboardProvider.HandleGraphChanges();

            foreach (var node in m_Graph.removedNodes)
            {
                node.UnregisterCallback(OnNodeChanged);
                var nodeView = m_GraphView.nodes.ToList().OfType <MaterialNodeView>().FirstOrDefault(p => p.node != null && p.node.guid == node.guid);
                if (nodeView != null)
                {
                    nodeView.Dispose();
                    nodeView.userData = null;
                    m_GraphView.RemoveElement(nodeView);
                }
            }

            foreach (var node in m_Graph.addedNodes)
            {
                AddNode(node);
            }

            foreach (var node in m_Graph.pastedNodes)
            {
                var nodeView = m_GraphView.nodes.ToList().OfType <MaterialNodeView>().FirstOrDefault(p => p.node != null && p.node.guid == node.guid);
                m_GraphView.AddToSelection(nodeView);
            }

            var nodesToUpdate = m_NodeViewHashSet;

            nodesToUpdate.Clear();

            foreach (var edge in m_Graph.removedEdges)
            {
                var edgeView = m_GraphView.graphElements.ToList().OfType <Edge>().FirstOrDefault(p => p.userData is IEdge && Equals((IEdge)p.userData, edge));
                if (edgeView != null)
                {
                    var nodeView = edgeView.input.node as MaterialNodeView;
                    if (nodeView != null && nodeView.node != null)
                    {
                        nodesToUpdate.Add(nodeView);
                    }
                    edgeView.output.Disconnect(edgeView);
                    edgeView.input.Disconnect(edgeView);

                    edgeView.output = null;
                    edgeView.input  = null;

                    m_GraphView.RemoveElement(edgeView);
                }
            }

            foreach (var edge in m_Graph.addedEdges)
            {
                var edgeView = AddEdge(edge);
                if (edgeView != null)
                {
                    nodesToUpdate.Add((MaterialNodeView)edgeView.input.node);
                }
            }

            foreach (var node in nodesToUpdate)
            {
                node.UpdatePortInputVisibilities();
            }

            UpdateEdgeColors(nodesToUpdate);
        }
Exemple #4
0
        public void HandleGraphChanges()
        {
            previewManager.HandleGraphChanges();
            previewManager.RenderPreviews();
            m_BlackboardProvider.HandleGraphChanges();

            foreach (GroupData groupData in m_Graph.removedGroups)
            {
                var group = m_GraphView.graphElements.ToList().OfType <ShaderGroup>().ToList().First(g => g.userData == groupData);
                m_GraphView.RemoveElement(group);
            }

            foreach (var node in m_Graph.removedNodes)
            {
                node.UnregisterCallback(OnNodeChanged);
                var nodeView = m_GraphView.nodes.ToList().OfType <IShaderNodeView>()
                               .FirstOrDefault(p => p.node != null && p.node.guid == node.guid);
                if (nodeView != null)
                {
                    nodeView.Dispose();
                    m_GraphView.RemoveElement((Node)nodeView);
                }
            }

            foreach (var groupData in m_Graph.addedGroups)
            {
                AddGroup(groupData);
            }

            foreach (var node in m_Graph.addedNodes)
            {
                AddNode(node);
            }

            foreach (var groupChange in m_Graph.nodeGroupChanges)
            {
                var nodeView = m_GraphView.GetNodeByGuid(groupChange.nodeGuid.ToString());
                if (nodeView != null)
                {
                    var groupView = nodeView.GetContainingScope() as ShaderGroup;
                    if (groupView?.userData.guid != groupChange.newGroupGuid)
                    {
                        groupView?.RemoveElement(nodeView);
                        if (groupChange.newGroupGuid != Guid.Empty)
                        {
                            var newGroupView = m_GraphView.graphElements.ToList()
                                               .OfType <ShaderGroup>()
                                               .First(x => x.userData.guid == groupChange.newGroupGuid);
                            newGroupView.AddElement(nodeView);
                        }
                    }
                }
            }

            foreach (var groupData in m_Graph.pastedGroups)
            {
                var group = m_GraphView.graphElements.ToList().OfType <ShaderGroup>().ToList().First(g => g.userData == groupData);
                m_GraphView.AddToSelection(group);
            }

            foreach (var node in m_Graph.pastedNodes)
            {
                var nodeView = m_GraphView.nodes.ToList().OfType <IShaderNodeView>()
                               .FirstOrDefault(p => p.node != null && p.node.guid == node.guid);
                m_GraphView.AddToSelection((Node)nodeView);
            }

            var nodesToUpdate = m_NodeViewHashSet;

            nodesToUpdate.Clear();

            foreach (var edge in m_Graph.removedEdges)
            {
                var edgeView = m_GraphView.graphElements.ToList().OfType <Edge>()
                               .FirstOrDefault(p => p.userData is IEdge && Equals((IEdge)p.userData, edge));
                if (edgeView != null)
                {
                    var nodeView = (IShaderNodeView)edgeView.input.node;
                    if (nodeView.node != null)
                    {
                        nodesToUpdate.Add(nodeView);
                    }

                    edgeView.output.Disconnect(edgeView);
                    edgeView.input.Disconnect(edgeView);

                    edgeView.output = null;
                    edgeView.input  = null;

                    m_GraphView.RemoveElement(edgeView);
                }
            }

            foreach (var edge in m_Graph.addedEdges)
            {
                var edgeView = AddEdge(edge);
                if (edgeView != null)
                {
                    nodesToUpdate.Add((IShaderNodeView)edgeView.input.node);
                }
            }

            foreach (var node in nodesToUpdate)
            {
                if (node is MaterialNodeView materialNodeView)
                {
                    materialNodeView.UpdatePortInputVisibilities();
                }
            }

            UpdateEdgeColors(nodesToUpdate);

            UpdateBadges();
        }
        internal static void InsertCopyPasteGraph(this MaterialGraphView graphView, CopyPasteGraph copyGraph)
        {
            if (copyGraph == null)
            {
                return;
            }

            // Keywords need to be tested against variant limit based on multiple factors
            bool keywordsDirty = false;

            // Make new inputs from the copied graph
            foreach (ShaderInput input in copyGraph.inputs)
            {
                ShaderInput copiedInput = input.Copy();
                graphView.graph.SanitizeGraphInputName(copiedInput);
                graphView.graph.SanitizeGraphInputReferenceName(copiedInput, input.overrideReferenceName);
                graphView.graph.AddGraphInput(copiedInput);

                switch (input)
                {
                case AbstractShaderProperty property:
                    // Update the property nodes that depends on the copied node
                    var dependentPropertyNodes = copyGraph.GetNodes <PropertyNode>().Where(x => x.propertyGuid == input.guid);
                    foreach (var node in dependentPropertyNodes)
                    {
                        node.owner        = graphView.graph;
                        node.propertyGuid = copiedInput.guid;
                    }
                    break;

                case ShaderKeyword shaderKeyword:
                    // Update the keyword nodes that depends on the copied node
                    var dependentKeywordNodes = copyGraph.GetNodes <KeywordNode>().Where(x => x.keywordGuid == input.guid);
                    foreach (var node in dependentKeywordNodes)
                    {
                        node.owner       = graphView.graph;
                        node.keywordGuid = copiedInput.guid;
                    }

                    // Pasting a new Keyword so need to test against variant limit
                    keywordsDirty = true;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            // Pasting a Sub Graph node that contains Keywords so need to test against variant limit
            foreach (SubGraphNode subGraphNode in copyGraph.GetNodes <SubGraphNode>())
            {
                if (subGraphNode.asset.keywords.Count > 0)
                {
                    keywordsDirty = true;
                }
            }

            // Test Keywords against variant limit
            if (keywordsDirty)
            {
                graphView.graph.OnKeywordChangedNoValidate();
            }

            using (var remappedNodesDisposable = ListPool <AbstractMaterialNode> .GetDisposable())
            {
                using (var remappedEdgesDisposable = ListPool <IEdge> .GetDisposable())
                {
                    var remappedNodes = remappedNodesDisposable.value;
                    var remappedEdges = remappedEdgesDisposable.value;
                    graphView.graph.PasteGraph(copyGraph, remappedNodes, remappedEdges);

                    if (graphView.graph.assetGuid != copyGraph.sourceGraphGuid)
                    {
                        // Compute the mean of the copied nodes.
                        Vector2 centroid = Vector2.zero;
                        var     count    = 1;
                        foreach (var node in remappedNodes)
                        {
                            var position = node.drawState.position.position;
                            centroid = centroid + (position - centroid) / count;
                            ++count;
                        }

                        // Get the center of the current view
                        var viewCenter = graphView.contentViewContainer.WorldToLocal(graphView.layout.center);

                        foreach (var node in remappedNodes)
                        {
                            var drawState    = node.drawState;
                            var positionRect = drawState.position;
                            var position     = positionRect.position;
                            position += viewCenter - centroid;
                            positionRect.position = position;
                            drawState.position    = positionRect;
                            node.drawState        = drawState;
                        }
                    }

                    // Add new elements to selection
                    graphView.ClearSelection();
                    graphView.graphElements.ForEach(element =>
                    {
                        if (element is Edge edge && remappedEdges.Contains(edge.userData as IEdge))
                        {
                            graphView.AddToSelection(edge);
                        }

                        if (element is IShaderNodeView nodeView && remappedNodes.Contains(nodeView.node))
                        {
                            graphView.AddToSelection((Node)nodeView);
                        }
                    });
                }
            }
        }
Exemple #6
0
 private void SetSelfSelected()
 {
     m_GraphView.ClearSelection();
     m_GraphView.AddToSelection(this);
 }