浏览代码

[Material Graph] Mode node collecton code to NodeUtils away from member functions

/main
Tim Cooper 9 年前
当前提交
0de2d9bd
共有 9 个文件被更改,包括 37 次插入61 次删除
  1. 6
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/DrawableMaterialNode.cs
  2. 2
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/AbstractMaterialNode.cs
  3. 4
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/PixelShaderNode.cs
  4. 2
      UnityProject/Assets/UnityShaderEditor/Editor/Source/PixelGraph.cs
  5. 44
      UnityProject/Assets/UnityShaderEditor/Editor/Source/SerializableGraph/NodeUtils.cs
  6. 3
      UnityProject/Assets/UnityShaderEditor/Editor/Source/SerializableGraph/SerializableGraph.cs
  7. 32
      UnityProject/Assets/UnityShaderEditor/Editor/Source/SerializableGraph/SerializableNode.cs
  8. 2
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Util/ShaderGenerator.cs
  9. 3
      UnityProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/SerializedGraphTests.cs

6
UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/DrawableMaterialNode.cs


namespace UnityEditor.MaterialGraph
{
public sealed class DrawableMaterialNode : CanvasElement
{
private readonly MaterialGraphDataSource m_Data;

private bool MarkDirtyIfNeedsTime(CanvasElement element, Event e, Canvas2D parent)
{
var childrenNodes = ListPool<SerializableNode>.Get();
m_Node.CollectChildNodesByExecutionOrder(childrenNodes);
NodeUtils.DepthFirstCollectNodesFromNode(childrenNodes, m_Node);
if (childrenNodes.Any(x => x is IRequiresTime))
Invalidate();
ListPool<SerializableNode>.Release(childrenNodes);

public static void RepaintDependentNodes(AbstractMaterialNode bmn)
{
var dependentNodes = bmn.CollectDependentNodes();
var dependentNodes = new List<SerializableNode>();
NodeUtils.CollectNodesNodeFeedsInto(dependentNodes, bmn);
foreach (var node in dependentNodes)
node.onNeedsRepaint();
}

2
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/AbstractMaterialNode.cs


public static void UpdateMaterialProperties(AbstractMaterialNode target, Material material)
{
var childNodes = ListPool<SerializableNode>.Get();
target.CollectChildNodesByExecutionOrder(childNodes);
NodeUtils.DepthFirstCollectNodesFromNode(childNodes, target);
var pList = ListPool<PreviewProperty>.Get();
for (var index = 0; index < childNodes.Count; index++)

4
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/PixelShaderNode.cs


// do the normal slot first so that it can be used later in the shader :)
var firstPassSlot = FindInputSlot(firstPassSlotName);
var nodes = ListPool<SerializableNode>.Get();
CollectChildNodesByExecutionOrder(nodes, firstPassSlot, false);
NodeUtils.DepthFirstCollectNodesFromNode(nodes, this, firstPassSlot, false);
for (int index = 0; index < nodes.Count; index++)
{

int pass2StartIndex = nodes.Count;
//Get the rest of the nodes for all the other slots
CollectChildNodesByExecutionOrder(nodes, null, false);
NodeUtils.DepthFirstCollectNodesFromNode(nodes, this, null, false);
for (var i = pass2StartIndex; i < nodes.Count; i++)
{
var node = nodes[i];

2
UnityProject/Assets/UnityShaderEditor/Editor/Source/PixelGraph.cs


get
{
m_ActiveNodes.Clear();
pixelMasterNode.CollectChildNodesByExecutionOrder(m_ActiveNodes);
NodeUtils.DepthFirstCollectNodesFromNode(m_ActiveNodes, pixelMasterNode);
return m_ActiveNodes.OfType<AbstractMaterialNode>();
}
}

44
UnityProject/Assets/UnityShaderEditor/Editor/Source/SerializableGraph/NodeUtils.cs


using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace UnityEditor.MaterialGraph

ListPool<SerializableSlot>.Release(validSlots);
}
public static void CollectChildNodesByExecutionOrder(List<SerializableNode> nodeList, SerializableNode node, SerializableSlot slotToUse)
// CollectNodesNodeFeedsInto looks at the current node and calculates
// which child nodes it depends on for it's calculation.
// Results are returned depth first so by processing each node in
// order you can generate a valid code block.
public static void DepthFirstCollectNodesFromNode(List<SerializableNode> nodeList, SerializableNode node, SerializableSlot slotToUse = null, bool includeSelf = true)
// no where to start
// allready added this node
var validSlots = ListPool<SerializableSlot>.Get();
validSlots.AddRange(node.inputSlots);
if (slotToUse != null && !validSlots.Contains(slotToUse))
{
ListPool<SerializableSlot>.Release(validSlots);
// if we have a slot passed in but can not find it on the node abort
if (slotToUse != null && node.inputSlots.All(x => x.name != slotToUse.name))
}
var validSlots = ListPool<SerializableSlot>.Get();
{
validSlots.Clear();
}
else
validSlots.AddRange(node.inputSlots);
var edges = node.owner.GetEdges(node.GetSlotReference(slot.name));
foreach (var edge in edges)
foreach (var edge in node.owner.GetEdges(node.GetSlotReference(slot.name)))
CollectChildNodesByExecutionOrder(nodeList, outputNode, null);
DepthFirstCollectNodesFromNode(nodeList, outputNode);
nodeList.Add(node);
if (includeSelf)
nodeList.Add(node);
public static void CollectDependentNodes(List<SerializableNode> nodeList, SerializableNode node)
public static void CollectNodesNodeFeedsInto(List<SerializableNode> nodeList, SerializableNode node, bool includeSelf = true)
{
if (node == null)
return;

foreach (var edge in node.owner.GetEdges(node.GetSlotReference(slot.name)))
{
var inputNode = node.owner.GetNodeFromGuid(edge.inputSlot.nodeGuid);
CollectDependentNodes(nodeList, inputNode);
CollectNodesNodeFeedsInto(nodeList, inputNode);
nodeList.Add(node);
if (includeSelf)
nodeList.Add(node);
}
}
}

3
UnityProject/Assets/UnityShaderEditor/Editor/Source/SerializableGraph/SerializableGraph.cs


public IEnumerable<Edge> GetEdges(SlotReference s)
{
if (s == null)
return new Edge[0];
return m_Edges.Where(x =>
(x.outputSlot.nodeGuid == s.nodeGuid && x.outputSlot.slotName == s.slotName)
|| x.inputSlot.nodeGuid == s.nodeGuid && x.inputSlot.slotName == s.slotName);

32
UnityProject/Assets/UnityShaderEditor/Editor/Source/SerializableGraph/SerializableNode.cs


Debug.LogErrorFormat("Output Slot: {0} could be found on node {1}", name, this);
return slot;
}
// CollectDependentNodes looks at the current node and calculates
// which nodes further up the tree (parents) would be effected if this node was changed
// it also includes itself in this list
public IEnumerable<SerializableNode> CollectDependentNodes()
{
var nodeList = new List<SerializableNode>();
NodeUtils.CollectDependentNodes(nodeList, this);
return nodeList;
}
// CollectDependentNodes looks at the current node and calculates
// which child nodes it depends on for it's calculation.
// Results are returned depth first so by processing each node in
// order you can generate a valid code block.
public List<SerializableNode> CollectChildNodesByExecutionOrder(List<SerializableNode> nodeList, SerializableSlot slotToUse = null, bool includeSelf = true)
{
if (slotToUse != null && !m_Slots.Contains(slotToUse))
{
Debug.LogError("Attempting to collect nodes by execution order with an invalid MaterialSlot on: " + name);
return nodeList;
}
NodeUtils.CollectChildNodesByExecutionOrder(nodeList, this, slotToUse);
if (!includeSelf)
nodeList.Remove(this);
return nodeList;
}
public virtual float GetNodeUIHeight(float width)
{
return 0;

2
UnityProject/Assets/UnityShaderEditor/Editor/Source/Util/ShaderGenerator.cs


{
// figure out what kind of preview we want!
var activeNodeList = ListPool<SerializableNode>.Get();
node.CollectChildNodesByExecutionOrder(activeNodeList);
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, node);
var generationMode = GenerationMode.Preview2D;
generatedShaderMode = PreviewMode.Preview2D;

3
UnityProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/SerializedGraphTests.cs


Assert.AreEqual(2, graph.nodes.Count());
graph.Connect(outputNode.GetSlotReference("output"), inputNode.GetSlotReference("input"));
Assert.AreEqual(1, graph.edges.Count());
graph.GetEdges(inputNode.GetSlotReference("iput"));
Assert.AreEqual(1, graph.GetEdges(inputNode.GetSlotReference("input")).Count());
Assert.AreEqual(1, graph.GetEdges(outputNode.GetSlotReference("output")).Count());
Assert.AreEqual(0, graph.GetEdges(outputNode.GetSlotReference("badslot")).Count());

正在加载...
取消
保存