浏览代码

[Material Graph]Adding property node tests + pixel node tests + tidy API as I go (light functions specifically)

/main
Tim Cooper 8 年前
当前提交
cc192491
共有 25 个文件被更改,包括 534 次插入105 次删除
  1. 4
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/CopySelected.cs
  2. 10
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Testing/IntegrationTests/SerializationTests.cs
  3. 4
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Implementation/SerializableGraph.cs
  4. 14
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Implementation/SerializableNode.cs
  5. 97
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Util/SerializationHelper.cs
  6. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/NodeDrawers/PixelShaderNodeUI.cs
  7. 51
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Light/BaseLightFunction.cs
  8. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbsoluteNode.cs
  9. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbstractMaterialNode.cs
  10. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/ColorNode.cs
  11. 71
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/PixelShaderNode.cs
  12. 7
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/PropertyNode.cs
  13. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/TextureNode.cs
  14. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/UnpackNormalNode.cs
  15. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Vector1Node.cs
  16. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Vector2Node.cs
  17. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Vector3Node.cs
  18. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Vector4Node.cs
  19. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Util/ShaderGenerator.cs
  20. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/FixedSlotTests.cs.meta
  21. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/MaterialSlotTests.cs.meta
  22. 94
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/PixelShaderNodeTests.cs
  23. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/PixelShaderNodeTests.cs.meta
  24. 209
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/PropertyNodeTests.cs
  25. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/PropertyNodeTests.cs.meta

4
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Drawing/CopySelected.cs


public virtual void OnBeforeSerialize()
{
m_SerializableNodes = SerializationHelper.Serialize(m_Nodes);
m_SerializableEdges = SerializationHelper.Serialize(m_Edges);
m_SerializableNodes = SerializationHelper.Serialize<INode>(m_Nodes);
m_SerializableEdges = SerializationHelper.Serialize<IEdge>(m_Edges);
}
public virtual void OnAfterDeserialize()

10
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Testing/IntegrationTests/SerializationTests.cs


SimpleSerializeClass.instance
};
var serialized = SerializationHelper.Serialize(toSerialize);
var serialized = SerializationHelper.Serialize<SimpleSerializeClass>(toSerialize);
Assert.AreEqual(1, serialized.Count);
var loaded = SerializationHelper.Deserialize<SimpleSerializeClass>(serialized);

ChildClassB.instance
};
var serialized = SerializationHelper.Serialize(toSerialize);
var serialized = SerializationHelper.Serialize<SimpleSerializeClass>(toSerialize);
Assert.AreEqual(3, serialized.Count);
var loaded = SerializationHelper.Deserialize<SimpleSerializeClass>(serialized);

ChildClassB.instance
};
var serialized = SerializationHelper.Serialize(toSerialize);
var serialized = SerializationHelper.Serialize<ITestInterface>(toSerialize);
Assert.AreEqual(3, serialized.Count);
var loaded = SerializationHelper.Deserialize<SimpleSerializeClass>(serialized);

SimpleSerializeClass.instance
};
var serialized = SerializationHelper.Serialize(toSerialize);
var serialized = SerializationHelper.Serialize<SimpleSerializeClass>(toSerialize);
Assert.AreEqual(1, serialized.Count);
var container = new SerializationContainer

new SerializableSlot(1, "OutSlot", SlotType.Output, 5),
};
var serialized = SerializationHelper.Serialize(toSerialize);
var serialized = SerializationHelper.Serialize<SerializableSlot>(toSerialize);
var loaded = SerializationHelper.Deserialize<SerializableSlot>(serialized);
Assert.AreEqual(2, loaded.Count);

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


public virtual void OnBeforeSerialize()
{
m_SerializableNodes = SerializationHelper.Serialize(m_Nodes);
m_SerializableEdges = SerializationHelper.Serialize(m_Edges);
m_SerializableNodes = SerializationHelper.Serialize<INode>(m_Nodes);
m_SerializableEdges = SerializationHelper.Serialize<IEdge>(m_Edges);
}
public virtual void OnAfterDeserialize()

14
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Implementation/SerializableNode.cs


{
return GetOutputSlots<T>().FirstOrDefault(x => x.id == slotId);
}
public virtual IEnumerable<ISlot> GetInputsWithNoConnection()
{
return GetInputSlots<ISlot>().Where(x => !owner.GetEdges(GetSlotReference(x.id)).Any());
}
m_SerializableSlots = SerializationHelper.Serialize(m_Slots);
m_SerializableSlots = SerializationHelper.Serialize<ISlot>(m_Slots);
}
public virtual void OnAfterDeserialize()

foreach (var s in m_Slots)
s.owner = this;
UpdateNodeAfterDeserialization();
}
public virtual IEnumerable<ISlot> GetInputsWithNoConnection()
{
return GetInputSlots<ISlot>().Where(x => !owner.GetEdges(GetSlotReference(x.id)).Any());
}
public virtual void UpdateNodeAfterDeserialization()

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


public string JSONnodeData;
}
public static JSONSerializedElement nullElement
{
get
{
return new JSONSerializedElement();
}
}
private static TypeSerializationInfo GetTypeSerializableAsString(Type type)
{
return new TypeSerializationInfo

return Type.GetType(typeInfo.SearchString());
}
public static JSONSerializedElement Serialize<T>(T item)
{
if (item == null)
throw new ArgumentNullException("item", "Can not serialize null element");
var typeInfo = GetTypeSerializableAsString(item.GetType());
var data = JsonUtility.ToJson(item, true);
if (string.IsNullOrEmpty(data))
throw new ArgumentException(string.Format("Can not serialize {0}", item)); ;
return new JSONSerializedElement
{
typeInfo = typeInfo,
JSONnodeData = data
};
}
public static T Deserialize<T>(JSONSerializedElement item, params object[] constructorArgs) where T : class
{
if (!item.typeInfo.IsValid() || string.IsNullOrEmpty(item.JSONnodeData))
throw new ArgumentException(string.Format("Can not deserialize {0}, it is invalid", item)); ;
var type = GetTypeFromSerializedString(item.typeInfo);
if (type == null)
throw new ArgumentException(string.Format("Can not deserialize {0}, type {1} is invalid", item.typeInfo));
T instance;
try
{
instance = Activator.CreateInstance(type, constructorArgs) as T;
}
catch (Exception e)
{
throw new Exception(string.Format("Could not construct instance of: {0}", type), e);
}
if (instance != null)
{
JsonUtility.FromJsonOverwrite(item.JSONnodeData, instance);
return instance;
}
return null;
}
public static List<JSONSerializedElement> Serialize<T>(IEnumerable<T> list)
{
var result = new List<JSONSerializedElement>();

foreach (var element in list)
{
if (element == null)
continue;
var typeInfo = GetTypeSerializableAsString(element.GetType());
var data = JsonUtility.ToJson(element, true);
if (string.IsNullOrEmpty(data))
continue;
result.Add(new JSONSerializedElement()
try
typeInfo = typeInfo,
JSONnodeData = data
});
result.Add(Serialize(element));
}
catch (Exception e)
{
Debug.LogException(e);
}
}
return result;
}

foreach (var element in list)
{
if (!element.typeInfo.IsValid() || string.IsNullOrEmpty(element.JSONnodeData))
continue;
var type = GetTypeFromSerializedString(element.typeInfo);
if (type == null)
{
Debug.LogWarningFormat("Could not find node of type {0} in loaded assemblies", element.typeInfo.SearchString());
continue;
}
T instance;
{
instance = Activator.CreateInstance(type, constructorArgs) as T;
}
catch (Exception e)
Debug.LogWarningFormat("Could not construct instance of: {0} - {1}", type, e);
continue;
result.Add(Deserialize<T>(element));
if (instance != null)
catch (Exception e)
JsonUtility.FromJsonOverwrite(element.JSONnodeData, instance);
result.Add(instance);
Debug.LogException(e);
}
}
return result;

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/NodeDrawers/PixelShaderNodeUI.cs


return base.Render(area);
var lightFunctions = PixelShaderNode.GetLightFunctions();
var lightFunction = localNode.GetLightFunction();
var lightFunction = localNode.lightFunction;
int lightFuncIndex = 0;
if (lightFunction != null)

lightFuncIndex = EditorGUI.Popup(new Rect(area.x, area.y, area.width, EditorGUIUtility.singleLineHeight), lightFuncIndex, lightFunctions.Select(x => x.GetLightFunctionName()).ToArray(), EditorStyles.popup);
localNode.m_LightFunctionClassName = lightFunctions[lightFuncIndex].GetType().ToString();
lightFuncIndex = EditorGUI.Popup(new Rect(area.x, area.y, area.width, EditorGUIUtility.singleLineHeight), lightFuncIndex, lightFunctions.Select(x => x.lightFunctionName).ToArray(), EditorStyles.popup);
localNode.lightFunction = lightFunctions[lightFuncIndex];
var toReturn = GUIModificationType.None;
if (EditorGUI.EndChangeCheck())
{

51
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Light/BaseLightFunction.cs


using System;
[Serializable]
public virtual string GetLightFunctionName() { return ""; }
public virtual string GetSurfaceOutputStructureName() { return ""; }
public virtual string lightFunctionName
{
get { return ""; }
}
public virtual string surfaceOutputStructureName
{
get { return ""; }
}
visitor.AddPragmaChunk(GetLightFunctionName());
visitor.AddPragmaChunk(lightFunctionName);
visitor.AddPragmaChunk(GetSurfaceOutputStructureName());
visitor.AddPragmaChunk(surfaceOutputStructureName);
}
public abstract void DoSlotsForConfiguration(PixelShaderNode node);

}
}
class PBRMetalicLightFunction : BaseLightFunction
[Serializable]
public class PBRMetalicLightFunction : BaseLightFunction
{
public const string AlbedoSlotName = "Albedo";
public const string MetallicSlotName = "Metallic";

public const string AlphaSlotName = "Alpha";
public const string LightFunctionName = "Standard";
public const string SurfaceOutputStructureName = "SurfaceOutputStandard";
public const int AlbedoSlotId = 0;
public const int MetallicSlotId = 2;
public const int EmissionSlotId = 3;

public override string GetLightFunctionName() { return "Standard"; }
public override string GetSurfaceOutputStructureName() { return "SurfaceOutputStandard"; }
public override string lightFunctionName
{
get { return LightFunctionName; }
}
public override string surfaceOutputStructureName
{
get { return SurfaceOutputStructureName; }
}
public override void DoSlotsForConfiguration(PixelShaderNode node)
{
node.AddSlot(new MaterialSlot(AlbedoSlotId, AlbedoSlotName, AlbedoSlotName, SlotType.Input, SlotValueType.Vector3, Vector4.zero));

}
}
class PBRSpecularLightFunction : BaseLightFunction
[Serializable]
public class PBRSpecularLightFunction : BaseLightFunction
{
public const string kAlbedoSlotName = "Albedo";
public const string kSpecularSlotName = "Specular";

public const int kOcclusionSlotId = 5;
public const int kAlphaSlotId = 6;
public override string GetLightFunctionName() { return "StandardSpecular"; }
public override string GetSurfaceOutputStructureName() { return "SurfaceOutputStandardSpecular"; }
public override string lightFunctionName
{
get { return "StandardSpecular"; }
}
public override string surfaceOutputStructureName
{
get { return "SurfaceOutputStandardSpecular"; }
}
public override void DoSlotsForConfiguration(PixelShaderNode node)
{
node.AddSlot(new MaterialSlot(kAlbedoSlotId, kAlbedoSlotName, kAlbedoSlotName, SlotType.Input, SlotValueType.Vector3, Vector4.zero));

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbsoluteNode.cs


namespace UnityEngine.MaterialGraph
{
[Title("Math/Absolute Node")]
class AbsoluteNode : Function1Input
public class AbsoluteNode : Function1Input
{
public AbsoluteNode()
{

4
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbstractMaterialNode.cs


{
return name + "_" + guid.ToString().Replace("-", "_");
}
var addingSlot = (MaterialSlot) slot;
var addingSlot = (MaterialSlot)slot;
var foundSlot = FindSlot<MaterialSlot>(slot.id);
// this will remove the old slot and add a new one

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/ColorNode.cs


public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)
{
if (exposedState == ExposedState.Exposed || generationMode.IsPreview())
visitor.AddShaderChunk("float4 " + propertyName + ";", true);
visitor.AddShaderChunk(precision + "4 " + propertyName + ";", true);
}
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)

71
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/PixelShaderNode.cs


public class PixelShaderNode : AbstractMaterialNode, IGeneratesBodyCode
{
[SerializeField]
public string m_LightFunctionClassName;
private string lightFunctionClassName
{
get { return m_LightFunctionClassName; }
set { m_LightFunctionClassName = value; }
}
private SerializationHelper.JSONSerializedElement m_SerializedLightFunction;
[NonSerialized]
private BaseLightFunction m_LightFunction = new PBRMetalicLightFunction();
public BaseLightFunction lightFunction
{
get { return m_LightFunction; }
set
{
if (m_LightFunction == value)
return;
m_LightFunction = value;
UpdateNodeAfterDeserialization();
}
}
public PixelShaderNode()
{
name = "PixelMaster";

public sealed override void UpdateNodeAfterDeserialization()
{
GetLightFunction().DoSlotsForConfiguration(this);
m_LightFunction.DoSlotsForConfiguration(this);
}
protected override bool generateDefaultInputs { get { return false; } }

return s_LightFunctions;
}
public BaseLightFunction GetLightFunction()
{
var lightFunctions = GetLightFunctions();
var lightFunction = lightFunctions.FirstOrDefault(x => x.GetType().ToString() == lightFunctionClassName);
if (lightFunction == null && lightFunctions.Count > 0)
lightFunction = lightFunctions[0];
return lightFunction;
}
var lightFunction = GetLightFunction();
lightFunction.GenerateLightFunctionName(visitor);
lightFunction.GenerateLightFunctionBody(visitor);
}

var lightFunction = GetLightFunction();
var lightFunction = GetLightFunction();
var firstPassSlotId = lightFunction.GetFirstPassSlotId();
// do the normal slot first so that it can be used later in the shader :)
var firstPassSlot = FindInputSlot<MaterialSlot>(firstPassSlotId);

shaderBody.AddShaderChunk("o." + slot.shaderOutputName + " = " + fromNode.GetVariableNameForSlot(outputRef.slotId) + ";", true);
}
}
}
public override string GetVariableNameForSlot(int slotId)
{
return GetVariableNameForNode();
}
/* public override float GetNodeUIHeight(float width)

{
get { return true; }
}
public override void OnBeforeSerialize()
{
base.OnBeforeSerialize();
try
{
m_SerializedLightFunction = SerializationHelper.Serialize(m_LightFunction);
}
catch (Exception e)
{
Debug.LogException(e);
}
}
public override void OnAfterDeserialize()
{
try
{
m_LightFunction = SerializationHelper.Deserialize<BaseLightFunction>(m_SerializedLightFunction);
}
catch (Exception e)
{
Debug.LogException(e);
}
base.OnAfterDeserialize();
}
/*
protected override bool UpdatePreviewShader()
{

7
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/PropertyNode.cs


public ExposedState exposedState
{
get { return m_Exposed; }
set { m_Exposed = value; }
}
public string description

if (string.IsNullOrEmpty(m_Description))
return propertyName;
return m_PropertyName;
return m_Description;
}

public virtual string propertyName
public string propertyName
{
get
{

var propNodes = owner.GetNodes<PropertyNode>();
foreach (var n in propNodes)
{
if (n == this)
if (n == this || n.exposedState == ExposedState.NotExposed)
continue;;
if (n.propertyName == propertyName)

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/TextureNode.cs


string body = "tex2D (" + propertyName + ", " + uvName + ")";
if (m_TextureType == TextureType.Bump)
body = precision + "4(UnpackNormal(" + body + "), 0)";
visitor.AddShaderChunk("float4 " + GetVariableNameForNode() + " = " + body + ";", true);
visitor.AddShaderChunk(precision + "4 " + GetVariableNameForNode() + " = " + body + ";", true);
}
public override string GetVariableNameForSlot(int slotId)

4
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/UnpackNormalNode.cs


get { return false; }
}
protected virtual MaterialSlot GetInputSlot()
protected override MaterialSlot GetInputSlot()
protected virtual MaterialSlot GetOutputSlot()
protected override MaterialSlot GetOutputSlot()
{
return new MaterialSlot(OutputSlotId, GetOutputSlotName(), kOutputSlotShaderName, SlotType.Output, SlotValueType.Vector3, Vector4.zero);
}

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Vector1Node.cs


public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)
{
if (exposedState == ExposedState.Exposed || generationMode.IsPreview())
visitor.AddShaderChunk("float " + propertyName + ";", true);
visitor.AddShaderChunk(precision + " " + propertyName + ";", true);
}
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Vector2Node.cs


public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)
{
if (exposedState == ExposedState.Exposed || generationMode.IsPreview())
visitor.AddShaderChunk("float2 " + propertyName + ";", true);
visitor.AddShaderChunk(precision + "2 " + propertyName + ";", true);
}
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Vector3Node.cs


public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)
{
if (exposedState == ExposedState.Exposed || generationMode.IsPreview())
visitor.AddShaderChunk("float3 " + propertyName + ";", true);
visitor.AddShaderChunk(precision + "3 " + propertyName + ";", true);
}
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Vector4Node.cs


public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)
{
if (exposedState == ExposedState.Exposed || generationMode.IsPreview())
visitor.AddShaderChunk("float4 " + propertyName + ";", true);
visitor.AddShaderChunk(precision + "4 " + propertyName + ";", true);
}
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Util/ShaderGenerator.cs


private readonly List<ShaderChunk> m_ShaderChunks = new List<ShaderChunk>();
private int m_IndentLevel;
private string m_Pragma;
private string m_Pragma = string.Empty;
public void AddPragmaChunk(string s)
{

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/FixedSlotTests.cs.meta


fileFormatVersion: 2
guid: 3fe1a12f346cde14f8ec5d42b2e4b70f
timeCreated: 1469709562
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/MaterialSlotTests.cs.meta


fileFormatVersion: 2
guid: a55bb4eb9ba26c846902a616c2ea29ff
timeCreated: 1469709562
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

94
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/PixelShaderNodeTests.cs


using System;
using System.Linq;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.Graphing;
using UnityEngine.MaterialGraph;
namespace UnityEditor.MaterialGraph.UnitTests
{
[TestFixture]
public class PixelShaderNodeTests
{
private PixelGraph m_Graph;
private Vector1Node m_InputOne;
private AbsoluteNode m_Abs;
private PixelShaderNode m_PixelNode;
[TestFixtureSetUp]
public void RunBeforeAnyTests()
{
Debug.logger.logHandler = new ConsoleLogHandler();
}
[SetUp]
public void TestSetUp()
{
m_Graph = new PixelGraph();
m_InputOne = new Vector1Node();
m_Abs = new AbsoluteNode();
m_PixelNode = new PixelShaderNode();
m_PixelNode.lightFunction = new PBRMetalicLightFunction();
m_Graph.AddNode(m_InputOne);
m_Graph.AddNode(m_PixelNode);
m_Graph.AddNode(m_Abs);
m_InputOne.value = 0.2f;
m_Graph.Connect(m_InputOne.GetSlotReference(Vector1Node.OutputSlotId), m_PixelNode.GetSlotReference(BaseLightFunction.NormalSlotId));
m_Graph.Connect(m_InputOne.GetSlotReference(Vector1Node.OutputSlotId), m_Abs.GetSlotReference(Function1Input.InputSlotId));
m_Graph.Connect(m_Abs.GetSlotReference(Function1Input.OutputSlotId), m_PixelNode.GetSlotReference(PBRMetalicLightFunction.AlbedoSlotId));
}
[Test]
public void TestNodeGeneratesLightFuntionProperly()
{
var generator = new ShaderGenerator();
m_PixelNode.GenerateLightFunction(generator);
Assert.AreEqual(string.Empty, generator.GetShaderString(0));
Assert.AreEqual(PBRMetalicLightFunction.LightFunctionName, generator.GetPragmaString());
}
[Test]
public void TestNodeGenerateSurfaceOutputProperly()
{
var generator = new ShaderGenerator();
m_PixelNode.GenerateSurfaceOutput(generator);
Assert.AreEqual(string.Empty, generator.GetShaderString(0));
Assert.AreEqual(PBRMetalicLightFunction.SurfaceOutputStructureName, generator.GetPragmaString());
}
[Test]
public void TestNodeGeneratesCorrectNodeCode()
{
string expected = string.Format("half {0} = 0.2;\r\n"
+ "o.Normal = {0};\r\n"
+ "half {1} = abs ({0});\r\n"
+ "o.Albedo = {1};\r\n"
, m_InputOne.GetVariableNameForSlot(Vector1Node.OutputSlotId)
, m_Abs.GetVariableNameForSlot(Function1Input.OutputSlotId));
var generator = new ShaderGenerator();
m_PixelNode.GenerateNodeCode(generator, GenerationMode.SurfaceShader);
Console.WriteLine(generator.GetShaderString(0));
Assert.AreEqual(expected, generator.GetShaderString(0));
Assert.AreEqual(string.Empty, generator.GetPragmaString());
}
[Test]
public void TestPixelShaderNodeReturnsBuiltinPBRLights()
{
var lightingFuncs = PixelShaderNode.GetLightFunctions();
Assert.AreEqual(1, lightingFuncs.OfType<PBRMetalicLightFunction>().Count());
Assert.AreEqual(1, lightingFuncs.OfType<PBRSpecularLightFunction>().Count());
}
}
}

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/PixelShaderNodeTests.cs.meta


fileFormatVersion: 2
guid: caaa2ea3bc3f57c429e630f9324ac930
timeCreated: 1469792183
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

209
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/PropertyNodeTests.cs


using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.Graphing;
using UnityEngine.MaterialGraph;
namespace UnityEditor.MaterialGraph.UnitTests
{
[TestFixture]
public class PropertyNodeTests
{
private class TestPropertyNode : PropertyNode
{
public const string TestPropertyName = "TestName";
public override PropertyType propertyType
{
get { return PropertyType.Float;}
}
public override PreviewProperty GetPreviewProperty()
{
return new PreviewProperty()
{
m_Name = TestPropertyName
};
}
}
private PixelGraph m_Graph;
private Vector1Node m_Vector1Node;
private Vector2Node m_Vector2Node;
private Vector3Node m_Vector3Node;
private Vector4Node m_Vector4Node;
private ColorNode m_ColorNode;
private TextureNode m_TextureNode;
private TestPropertyNode m_PropertyNode;
private const string kPropertyName = "PropertyName";
public const string kDescription = "NewDescription";
[TestFixtureSetUp]
public void RunBeforeAnyTests()
{
Debug.logger.logHandler = new ConsoleLogHandler();
}
[SetUp]
public void TestSetUp()
{
m_Graph = new PixelGraph();
m_Vector1Node = new Vector1Node();
m_Vector2Node = new Vector2Node();
m_Vector3Node = new Vector3Node();
m_Vector4Node = new Vector4Node();
m_ColorNode = new ColorNode();
m_TextureNode = new TextureNode();
m_PropertyNode = new TestPropertyNode();
m_Graph.AddNode(m_Vector1Node);
m_Graph.AddNode(m_Vector2Node);
m_Graph.AddNode(m_Vector3Node);
m_Graph.AddNode(m_Vector4Node);
m_Graph.AddNode(m_ColorNode);
m_Graph.AddNode(m_TextureNode);
m_Graph.AddNode(m_PropertyNode);
}
[Test]
public void TestExposedPropertyReturnsRawName()
{
m_PropertyNode.exposedState = PropertyNode.ExposedState.Exposed;
m_PropertyNode.propertyName = kPropertyName;
Assert.AreEqual(kPropertyName + "_Uniform", m_PropertyNode.propertyName);
}
[Test]
public void TestNonExposedPropertyReturnsGeneratedName()
{
var expected = string.Format("{0}_{1}_Uniform", m_PropertyNode.name, m_PropertyNode.guid.ToString().Replace("-", "_"));
m_PropertyNode.exposedState = PropertyNode.ExposedState.NotExposed;
m_PropertyNode.propertyName = kPropertyName;
Assert.AreEqual(expected, m_PropertyNode.propertyName);
}
[Test]
public void TestPropertyNodeDescriptionWorks()
{
m_PropertyNode.propertyName = kPropertyName;
m_PropertyNode.description = kDescription;
Assert.AreEqual(kDescription, m_PropertyNode.description);
}
[Test]
public void TestPropertyNodeDescriptionReturnsPropertyNameWhenNoDescriptionSet()
{
m_PropertyNode.propertyName = kPropertyName;
m_PropertyNode.description = string.Empty;
Assert.AreEqual(kPropertyName, m_PropertyNode.description);
}
[Test]
public void TestPropertyNodeReturnsPreviewProperty()
{
var props = new List<PreviewProperty>();
m_PropertyNode.CollectPreviewMaterialProperties(props);
Assert.AreEqual(props.Count, 1);
Assert.AreEqual(TestPropertyNode.TestPropertyName, props[0].m_Name);
}
[Test]
public void TestDuplicatedPropertyNameGeneratesErrorWhenExposed()
{
const string failName = "SameName";
m_Vector1Node.exposedState = PropertyNode.ExposedState.Exposed;
m_Vector1Node.propertyName = failName;
m_Vector2Node.exposedState = PropertyNode.ExposedState.Exposed;
m_Vector2Node.propertyName = failName;
m_Vector1Node.ValidateNode();
m_Vector2Node.ValidateNode();
Assert.IsTrue(m_Vector1Node.hasError);
Assert.IsTrue(m_Vector2Node.hasError);
}
[Test]
public void TestDuplicatedPropertyNameGeneratesNoErrorWhenNotExposed()
{
const string failName = "SameName";
m_Vector1Node.exposedState = PropertyNode.ExposedState.NotExposed;
m_Vector1Node.propertyName = failName;
m_Vector2Node.exposedState = PropertyNode.ExposedState.Exposed;
m_Vector2Node.propertyName = failName;
m_Vector1Node.ValidateNode();
m_Vector2Node.ValidateNode();
Assert.IsFalse(m_Vector1Node.hasError);
Assert.IsFalse(m_Vector2Node.hasError);
}
[Test]
public void TestVector1NodeTypeIsCorrect()
{
Assert.AreEqual(PropertyType.Float, m_Vector1Node.propertyType);
}
[Test]
public void TestVector1NodeReturnsCorrectValue()
{
m_Vector1Node.value = 0.6f;
Assert.AreEqual(0.6f, m_Vector1Node.value);
}
[Test]
public void TestVector1NodeReturnsPreviewProperty()
{
var props = new List<PreviewProperty>();
m_Vector1Node.value = 0.6f;
m_Vector1Node.CollectPreviewMaterialProperties(props);
Assert.AreEqual(props.Count, 1);
Assert.AreEqual(m_Vector1Node.propertyName, props[0].m_Name);
Assert.AreEqual(m_Vector1Node.propertyType, props[0].m_PropType);
Assert.AreEqual(0.6f, props[0].m_Float);
}
[Test]
public void TestVector1NodeGeneratesCorrectPropertyBlock()
{
m_Vector1Node.value = 0.6f;
m_Vector1Node.exposedState = PropertyNode.ExposedState.NotExposed;
var generator = new PropertyGenerator();
m_Vector1Node.GeneratePropertyBlock(generator, GenerationMode.SurfaceShader);
Assert.AreEqual(string.Empty, generator.GetShaderString(0));
var expected = m_Vector1Node.propertyName
+ "(\""
+ m_Vector1Node.description
+ "\", Float) = "
+ m_Vector1Node.value
+ "\n";
m_Vector1Node.exposedState = PropertyNode.ExposedState.Exposed;
m_Vector1Node.GeneratePropertyBlock(generator, GenerationMode.SurfaceShader);
Assert.AreEqual(expected, generator.GetShaderString(0));
}
[Test]
public void TestVector1NodeGeneratesCorrectPropertyUsages()
{
m_Vector1Node.value = 0.6f;
m_Vector1Node.exposedState = PropertyNode.ExposedState.NotExposed;
var generator = new ShaderGenerator();
m_Vector1Node.GeneratePropertyUsages(generator, GenerationMode.SurfaceShader);
Assert.AreEqual(string.Empty, generator.GetShaderString(0));
var expected = m_Vector1Node.precision
+ " "
+ m_Vector1Node.propertyName
+ ";\r\n";
m_Vector1Node.exposedState = PropertyNode.ExposedState.Exposed;
m_Vector1Node.GeneratePropertyUsages(generator, GenerationMode.SurfaceShader);
Assert.AreEqual(expected, generator.GetShaderString(0));
}
}
}

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/PropertyNodeTests.cs.meta


fileFormatVersion: 2
guid: fc814c257422bf1449688696b94fae44
timeCreated: 1469792183
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存