浏览代码

[Material Graph]More tests + tidy custom serialization.

/main
Tim Cooper 9 年前
当前提交
7cf4d1f0
共有 5 个文件被更改,包括 217 次插入25 次删除
  1. 28
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Tests/SerializedGraphTests.cs
  2. 9
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Implementation/SerializableGraph.cs
  3. 54
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Util/SerializationHelper.cs
  4. 145
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/MaterialNodeTests.cs
  5. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/ShaderGeneratorTests.cs

28
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Tests/SerializedGraphTests.cs


Assert.AreEqual(1, slots.Count());
Assert.AreEqual("input2", slots.FirstOrDefault().name);
}
[Test]
public void TestCyclicConnectionsAreNotAllowedOnGraph()
{
var graph = new SerializableGraph();
var nodeA = new SerializableNode();
var inputSlotA = new SerializableSlot("input", "input", SlotType.Input, 0);
var outputSlotA = new SerializableSlot("output", "output", SlotType.Output, 0);
nodeA.AddSlot(inputSlotA);
nodeA.AddSlot(outputSlotA);
graph.AddNode(nodeA);
var nodeB = new SerializableNode();
var inputSlotB = new SerializableSlot("input", "input", SlotType.Input, 0);
var outputSlotB = new SerializableSlot("output", "output", SlotType.Output, 0);
nodeB.AddSlot(inputSlotB);
nodeB.AddSlot(outputSlotB);
graph.AddNode(nodeB);
Assert.AreEqual(2, graph.GetNodes<INode>().Count());
graph.Connect(nodeA.GetSlotReference("output"), nodeB.GetSlotReference("input"));
Assert.AreEqual(1, graph.edges.Count());
var edge = graph.Connect(nodeB.GetSlotReference("output"), nodeA.GetSlotReference("input"));
Assert.IsNull(edge);
Assert.AreEqual(1, graph.edges.Count());
}
}
}

9
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Implementation/SerializableGraph.cs


if (fromNode == null || toNode == null)
return null;
// if fromNode is already connected to toNode
// do now allow a connection as toNode will then
// have an edge to fromNode creating a cycle.
// if this is parsed it will lead to an infinite loop.
var dependentNodes = new List<INode>();
NodeUtils.CollectNodesNodeFeedsInto(dependentNodes, toNode);
if (dependentNodes.Contains(fromNode))
return null;
var fromSlot = fromNode.FindSlot<ISlot>(fromSlotRef.slotName);
var toSlot = toNode.FindSlot<ISlot>(toSlotRef.slotName);

54
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Util/SerializationHelper.cs


public static class SerializationHelper
{
[Serializable]
public struct TypeSerializationInfo
{
[SerializeField]
public string fullName;
[SerializeField]
public string assemblyName;
public bool IsValid()
{
return !string.IsNullOrEmpty(fullName) && !string.IsNullOrEmpty(assemblyName);
}
public string SearchString()
{
if (!IsValid())
return string.Empty;
return string.Format("{0}, {1}", fullName, assemblyName);
}
}
[Serializable]
public string typeName;
public TypeSerializationInfo typeInfo;
private static string GetTypeSerializableAsString(Type type)
private static TypeSerializationInfo GetTypeSerializableAsString(Type type)
if (type == null)
return string.Empty;
return string.Format("{0}, {1}", type.FullName, type.Assembly.GetName().Name);
return new TypeSerializationInfo
{
fullName = type.FullName,
assemblyName = type.Assembly.GetName().Name
};
private static Type GetTypeFromSerializedString(string type)
private static Type GetTypeFromSerializedString(TypeSerializationInfo typeInfo)
if (string.IsNullOrEmpty(type))
if (!typeInfo.IsValid())
return Type.GetType(type);
return Type.GetType(typeInfo.SearchString());
}
public static List<JSONSerializedElement> Serialize<T>(List<T> list)

if (element == null)
continue;
var typeName = GetTypeSerializableAsString(element.GetType());
var typeInfo = GetTypeSerializableAsString(element.GetType());
if (string.IsNullOrEmpty(typeName) || string.IsNullOrEmpty(data))
if (string.IsNullOrEmpty(data))
typeName = typeName,
typeInfo = typeInfo,
JSONnodeData = data
});
}

foreach (var element in list)
{
if (string.IsNullOrEmpty(element.typeName) || string.IsNullOrEmpty(element.JSONnodeData))
if (!element.typeInfo.IsValid() || string.IsNullOrEmpty(element.JSONnodeData))
var type = GetTypeFromSerializedString(element.typeName);
var type = GetTypeFromSerializedString(element.typeInfo);
Debug.LogWarningFormat("Could not find node of type {0} in loaded assemblies", element.typeName);
Debug.LogWarningFormat("Could not find node of type {0} in loaded assemblies", element.typeInfo.SearchString());
continue;
}

145
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/MaterialNodeTests.cs


using System.Linq;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.Graphing;
using UnityEngine.MaterialGraph;
namespace UnityEditor.MaterialGraph.Tests

{
private PixelGraph m_Graph;
private TestNode m_NodeA;
private TestNode m_NodeB;
class TestNode : AbstractMaterialNode
{
public TestNode()
{
AddSlot(new MaterialSlot("V1Out", "V1Out", SlotType.Output, 0, SlotValueType.Vector1, Vector4.zero));
AddSlot(new MaterialSlot("V2Out", "V2Out", SlotType.Output, 1, SlotValueType.Vector2, Vector4.zero));
AddSlot(new MaterialSlot("V3Out", "V3Out", SlotType.Output, 2, SlotValueType.Vector3, Vector4.zero));
AddSlot(new MaterialSlot("V4Out", "V4Out", SlotType.Output, 3, SlotValueType.Vector4, Vector4.zero));
AddSlot(new MaterialSlot("V1In", "V1In", SlotType.Input, 0, SlotValueType.Vector1, Vector4.zero));
AddSlot(new MaterialSlot("V2In", "V2In", SlotType.Input, 1, SlotValueType.Vector2, Vector4.zero));
AddSlot(new MaterialSlot("V3In", "V3In", SlotType.Input, 2, SlotValueType.Vector3, Vector4.zero));
AddSlot(new MaterialSlot("V4In", "V4In", SlotType.Input, 3, SlotValueType.Vector4, Vector4.zero));
}
}
[SetUp]
public void TestSetUp()
{
m_Graph = new PixelGraph();
m_NodeA = new TestNode();
m_NodeB = new TestNode();
m_Graph.AddNode(m_NodeA);
m_Graph.AddNode(m_NodeB);
}
public void TestCanAddMaterialNodeToMaterialGraph()
public void ConnectV1ToV1Works()
{
m_Graph.Connect(m_NodeA.GetSlotReference("V1Out"), m_NodeB.GetSlotReference("V1In"));
Assert.IsFalse(m_NodeB.hasError);
}
[Test]
public void ConnectV1ToV2Works()
{
m_Graph.Connect(m_NodeA.GetSlotReference("V1Out"), m_NodeB.GetSlotReference("V2In"));
Assert.IsFalse(m_NodeB.hasError);
}
[Test]
public void ConnectV1ToV3Works()
{
m_Graph.Connect(m_NodeA.GetSlotReference("V1Out"), m_NodeB.GetSlotReference("V3In"));
Assert.IsFalse(m_NodeB.hasError);
}
[Test]
public void ConnectV1ToV4Works()
{
m_Graph.Connect(m_NodeA.GetSlotReference("V1Out"), m_NodeB.GetSlotReference("V4In"));
Assert.IsFalse(m_NodeB.hasError);
}
[Test]
public void ConnectV2ToV1Works()
{
m_Graph.Connect(m_NodeA.GetSlotReference("V2Out"), m_NodeB.GetSlotReference("V1In"));
Assert.IsFalse(m_NodeB.hasError);
}
[Test]
public void ConnectV2ToV2Works()
{
m_Graph.Connect(m_NodeA.GetSlotReference("V2Out"), m_NodeB.GetSlotReference("V2In"));
Assert.IsFalse(m_NodeB.hasError);
}
[Test]
public void ConnectV2ToV3Fails()
/*var graph = new PixelGraph();
graph.AddNode(new PixelShaderNode());
m_Graph.Connect(m_NodeA.GetSlotReference("V2Out"), m_NodeB.GetSlotReference("V3In"));
Assert.IsTrue(m_NodeB.hasError);
}
var node = new AbstractMaterialGraphTests.TestableMNode();
graph.AddNode(node);
Assert.AreEqual(0, graph.edges.Count());
Assert.AreEqual(1, graph.GetNodes<AbstractMaterialNode>().Count());*/
[Test]
public void ConnectV2ToV4Fails()
{
m_Graph.Connect(m_NodeA.GetSlotReference("V2Out"), m_NodeB.GetSlotReference("V4In"));
Assert.IsTrue(m_NodeB.hasError);
[Test]
public void ConnectV3ToV1Works()
{
m_Graph.Connect(m_NodeA.GetSlotReference("V3Out"), m_NodeB.GetSlotReference("V1In"));
Assert.IsFalse(m_NodeB.hasError);
}
[Test]
public void ConnectV3ToV2Works()
{
m_Graph.Connect(m_NodeA.GetSlotReference("V3Out"), m_NodeB.GetSlotReference("V2In"));
Assert.IsFalse(m_NodeB.hasError);
}
[Test]
public void ConnectV3ToV3Works()
{
m_Graph.Connect(m_NodeA.GetSlotReference("V3Out"), m_NodeB.GetSlotReference("V3In"));
Assert.IsFalse(m_NodeB.hasError);
}
[Test]
public void ConnectV3ToV4Fails()
{
m_Graph.Connect(m_NodeA.GetSlotReference("V3Out"), m_NodeB.GetSlotReference("V4In"));
Assert.IsTrue(m_NodeB.hasError);
}
[Test]
public void ConnectV4ToV1Works()
{
m_Graph.Connect(m_NodeA.GetSlotReference("V4Out"), m_NodeB.GetSlotReference("V1In"));
Assert.IsFalse(m_NodeB.hasError);
}
[Test]
public void ConnectV4ToV2Works()
{
m_Graph.Connect(m_NodeA.GetSlotReference("V4Out"), m_NodeB.GetSlotReference("V2In"));
Assert.IsFalse(m_NodeB.hasError);
}
[Test]
public void ConnectV4ToV3Works()
{
m_Graph.Connect(m_NodeA.GetSlotReference("V4Out"), m_NodeB.GetSlotReference("V3In"));
Assert.IsFalse(m_NodeB.hasError);
}
[Test]
public void ConnectV4ToV4Works()
{
m_Graph.Connect(m_NodeA.GetSlotReference("V4Out"), m_NodeB.GetSlotReference("V4In"));
Assert.IsFalse(m_NodeB.hasError);
}
}
}

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/ShaderGeneratorTests.cs


}
[Test]
public void AdaptNodeOutput2To3CausesError()
public void AdaptNodeOutput2To3Fails()
{
var node = new TestNode();
var slot = node.FindOutputSlot<MaterialSlot>("V2Out");

[Test]
public void AdaptNodeOutput2To4CausesError()
public void AdaptNodeOutput2To4Fails()
{
var node = new TestNode();
var slot = node.FindOutputSlot<MaterialSlot>("V2Out");

}
[Test]
public void AdaptNodeOutput3To4CausesError()
public void AdaptNodeOutput3To4Fails()
{
var node = new TestNode();
var slot = node.FindOutputSlot<MaterialSlot>("V3Out");

正在加载...
取消
保存