using System; using System.Collections.Generic; using System.Linq; using RMGUI.GraphView; using UnityEngine; using UnityEngine.Graphing; using UnityEngine.MaterialGraph; namespace UnityEditor.Graphing.Drawing { [Serializable] public class MaterialGraphDataSource : IGraphElementDataSource { private List m_Elements = new List(); public IGraphAsset graphAsset { get; private set; } void OnNodeChanged(INode inNode) { var dependentNodes = new List(); NodeUtils.CollectNodesNodeFeedsInto(dependentNodes, inNode); foreach (var node in dependentNodes) { var theElements = m_Elements.OfType().ToList(); var found = theElements.Where(x => x.node.guid == node.guid).ToList(); foreach (var drawableNodeData in found) drawableNodeData.MarkDirtyHack(); } } public MaterialGraphDataSource(IGraphAsset graphAsset) { this.graphAsset = graphAsset; if (graphAsset == null) return; var drawableNodes = new List(); foreach (var node in graphAsset.graph.GetNodes()) { MaterialNodeData nodeData; if (node is ColorNode) nodeData = ScriptableObject.CreateInstance(); else nodeData = ScriptableObject.CreateInstance(); node.onModified += OnNodeChanged; nodeData.Initialize(node); drawableNodes.Add(nodeData); } var drawableEdges = new List(); foreach (var addedNode in drawableNodes) { var baseNode = addedNode.node; foreach (var slot in baseNode.GetOutputSlots()) { var sourceAnchors = addedNode.elements.OfType(); var sourceAnchor = sourceAnchors.FirstOrDefault(x => x.slot == slot); var edges = baseNode.owner.GetEdges(new SlotReference(baseNode.guid, slot.id)); foreach (var edge in edges) { var toNode = baseNode.owner.GetNodeFromGuid(edge.inputSlot.nodeGuid); var toSlot = toNode.FindInputSlot(edge.inputSlot.slotId); var targetNode = drawableNodes.FirstOrDefault(x => x.node == toNode); var targetAnchors = targetNode.elements.OfType(); var targetAnchor = targetAnchors.FirstOrDefault(x => x.slot == toSlot); var edgeData = ScriptableObject.CreateInstance(); edgeData.left = sourceAnchor; edgeData.right = targetAnchor; drawableEdges.Add(edgeData); } } } m_Elements.AddRange(drawableNodes.OfType()); m_Elements.AddRange(drawableEdges.OfType()); } public IEnumerable elements { get { return m_Elements; } } public void AddElement(GraphElementData element) { m_Elements.Add(element); } public void RemoveElement(GraphElementData element) { m_Elements.RemoveAll(x => x == element); } } }