浏览代码

Added Function4Input & Function4InputTest

/main
Florent Guinier 7 年前
当前提交
24f3679d
共有 4 个文件被更改,包括 279 次插入0 次删除
  1. 110
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/Function4InputTests.cs
  2. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/Function4InputTests.cs.meta
  3. 145
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Function4Input.cs
  4. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Function4Input.cs.meta

110
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/Function4InputTests.cs


using System;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.Graphing;
using UnityEngine.MaterialGraph;
namespace UnityEditor.MaterialGraph.UnitTests
{
[TestFixture]
public class Function4InputTests
{
private class Function4InputTestNode : Function4Input, IGeneratesFunction
{
public Function4InputTestNode()
{
name = "Function4InputTestNode";
}
protected override string GetFunctionName()
{
return "unity_test_" + precision;
}
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
{
var outputString = new ShaderGenerator();
outputString.AddShaderChunk(GetFunctionPrototype("arg1", "arg2", "arg3", "arg4"), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk("return arg1 + arg2 + arg3 + arg4;", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
visitor.AddShaderChunk(outputString.GetShaderString(0), true);
}
}
private UnityEngine.MaterialGraph.MaterialGraph m_Graph;
private Vector1Node m_InputOne;
private Vector1Node m_InputTwo;
private Vector1Node m_InputThree;
private Vector1Node m_InputFour;
private Function4InputTestNode m_TestNode;
[TestFixtureSetUp]
public void RunBeforeAnyTests()
{
Debug.unityLogger.logHandler = new ConsoleLogHandler();
}
[SetUp]
public void TestSetUp()
{
m_Graph = new UnityEngine.MaterialGraph.MaterialGraph();
m_InputOne = new Vector1Node();
m_InputTwo = new Vector1Node();
m_InputThree = new Vector1Node();
m_InputFour = new Vector1Node();
m_TestNode = new Function4InputTestNode();
m_Graph.AddNode(m_InputOne);
m_Graph.AddNode(m_InputTwo);
m_Graph.AddNode(m_InputThree);
m_Graph.AddNode(m_InputFour);
m_Graph.AddNode(m_TestNode);
m_Graph.AddNode(new MetallicMasterNode());
m_InputOne.value = 0.2f;
m_InputTwo.value = 0.3f;
m_InputThree.value = 0.6f;
m_InputFour.value = 0.6f;
m_Graph.Connect(m_InputOne.GetSlotReference(Vector1Node.OutputSlotId), m_TestNode.GetSlotReference(Function4Input.InputSlot1Id));
m_Graph.Connect(m_InputTwo.GetSlotReference(Vector1Node.OutputSlotId), m_TestNode.GetSlotReference(Function4Input.InputSlot2Id));
m_Graph.Connect(m_InputThree.GetSlotReference(Vector1Node.OutputSlotId), m_TestNode.GetSlotReference(Function4Input.InputSlot3Id));
m_Graph.Connect(m_InputThree.GetSlotReference(Vector1Node.OutputSlotId), m_TestNode.GetSlotReference(Function4Input.InputSlot4Id));
m_Graph.Connect(m_TestNode.GetSlotReference(Function4Input.OutputSlotId), m_Graph.masterNode.GetSlotReference(MetallicMasterNode.NormalSlotId));
}
[Test]
public void TestGenerateNodeCodeGeneratesCorrectCode()
{
string expected = string.Format("half {0} = unity_test_half ({1}, {2}, {3}, {4});{5}"
, m_TestNode.GetVariableNameForSlot(Function3Input.OutputSlotId)
, m_InputOne.GetVariableNameForSlot(Vector1Node.OutputSlotId)
, m_InputTwo.GetVariableNameForSlot(Vector1Node.OutputSlotId)
, m_InputThree.GetVariableNameForSlot(Vector1Node.OutputSlotId)
, m_InputFour.GetVariableNameForSlot(Vector1Node.OutputSlotId)
, Environment.NewLine);
ShaderGenerator visitor = new ShaderGenerator();
m_TestNode.GenerateNodeCode(visitor, GenerationMode.ForReals);
Assert.AreEqual(expected, visitor.GetShaderString(0));
}
[Test]
public void TestGenerateNodeFunctionGeneratesCorrectCode()
{
string expected =
"inline half unity_test_half (half arg1, half arg2, half arg3, half arg4)" + Environment.NewLine
+ "{" + Environment.NewLine
+ "\treturn arg1 + arg2 + arg3 + arg4;" + Environment.NewLine
+ "}" + Environment.NewLine;
ShaderGenerator visitor = new ShaderGenerator();
m_TestNode.GenerateNodeFunction(visitor, GenerationMode.ForReals);
Assert.AreEqual(expected, visitor.GetShaderString(0));
}
}
}

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


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

145
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Function4Input.cs


using UnityEngine.Graphing;
namespace UnityEngine.MaterialGraph
{
public abstract class Function4Input : AbstractMaterialNode, IGeneratesBodyCode
{
protected const string kInputSlot1ShaderName = "Input1";
protected const string kInputSlot2ShaderName = "Input2";
protected const string kInputSlot3ShaderName = "Input3";
protected const string kInputSlot4ShaderName = "Input4";
protected const string kOutputSlotShaderName = "Output";
public const int InputSlot1Id = 0;
public const int InputSlot2Id = 1;
public const int InputSlot3Id = 2;
public const int InputSlot4Id = 3;
public const int OutputSlotId = 4;
public override bool hasPreview
{
get { return true; }
}
protected Function4Input()
{
UpdateNodeAfterDeserialization();
}
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(GetInputSlot1());
AddSlot(GetInputSlot2());
AddSlot(GetInputSlot3());
AddSlot(GetInputSlot4());
AddSlot(GetOutputSlot());
RemoveSlotsNameNotMatching(validSlots);
}
protected int[] validSlots
{
get { return new[] { InputSlot1Id, InputSlot2Id, InputSlot3Id, InputSlot4Id, OutputSlotId }; }
}
protected virtual MaterialSlot GetInputSlot1()
{
return new MaterialSlot(InputSlot1Id, GetInputSlot1Name(), kInputSlot1ShaderName, SlotType.Input, SlotValueType.Dynamic, Vector4.zero);
}
protected virtual MaterialSlot GetInputSlot2()
{
return new MaterialSlot(InputSlot2Id, GetInputSlot2Name(), kInputSlot2ShaderName, SlotType.Input, SlotValueType.Dynamic, Vector4.zero);
}
protected virtual MaterialSlot GetInputSlot3()
{
return new MaterialSlot(InputSlot3Id, GetInputSlot3Name(), kInputSlot3ShaderName, SlotType.Input, SlotValueType.Dynamic, Vector4.zero);
}
protected virtual MaterialSlot GetInputSlot4()
{
return new MaterialSlot(InputSlot4Id, GetInputSlot4Name(), kInputSlot4ShaderName, SlotType.Input, SlotValueType.Dynamic, Vector4.zero);
}
protected virtual MaterialSlot GetOutputSlot()
{
return new MaterialSlot(OutputSlotId, GetOutputSlotName(), kOutputSlotShaderName, SlotType.Output, SlotValueType.Dynamic, Vector4.zero);
}
protected virtual string GetInputSlot1Name()
{
return "Input1";
}
protected virtual string GetInputSlot2Name()
{
return "Input2";
}
protected virtual string GetInputSlot3Name()
{
return "Input3";
}
protected virtual string GetInputSlot4Name()
{
return "Input4";
}
protected virtual string GetOutputSlotName()
{
return "Output";
}
protected abstract string GetFunctionName();
protected virtual string GetFunctionPrototype(string arg1Name, string arg2Name, string arg3Name, string arg4Name)
{
return "inline " + precision + outputDimension + " " + GetFunctionName() + " ("
+ precision + input1Dimension + " " + arg1Name + ", "
+ precision + input2Dimension + " " + arg2Name + ", "
+ precision + input3Dimension + " " + arg3Name + ", "
+ precision + input4Dimension + " " + arg4Name + ")";
}
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)
{
NodeUtils.SlotConfigurationExceptionIfBadConfiguration(this, new[] { InputSlot1Id, InputSlot2Id, InputSlot3Id }, new[] { OutputSlotId });
string input1Value = GetSlotValue(InputSlot1Id, generationMode);
string input2Value = GetSlotValue(InputSlot2Id, generationMode);
string input3Value = GetSlotValue(InputSlot3Id, generationMode);
string input4Value = GetSlotValue(InputSlot4Id, generationMode);
visitor.AddShaderChunk(precision + outputDimension + " " + GetVariableNameForSlot(OutputSlotId) + " = " + GetFunctionCallBody(input1Value, input2Value, input3Value, input4Value) + ";", true);
}
protected virtual string GetFunctionCallBody(string inputValue1, string inputValue2, string inputValue3, string inputValue4)
{
return GetFunctionName() + " (" + inputValue1 + ", " + inputValue2 + ", " + inputValue3 + ", " + inputValue4 + ")";
}
public string outputDimension
{
get { return ConvertConcreteSlotValueTypeToString(FindOutputSlot<MaterialSlot>(OutputSlotId).concreteValueType); }
}
private string input1Dimension
{
get { return ConvertConcreteSlotValueTypeToString(FindInputSlot<MaterialSlot>(InputSlot1Id).concreteValueType); }
}
private string input2Dimension
{
get { return ConvertConcreteSlotValueTypeToString(FindInputSlot<MaterialSlot>(InputSlot2Id).concreteValueType); }
}
public string input3Dimension
{
get { return ConvertConcreteSlotValueTypeToString(FindInputSlot<MaterialSlot>(InputSlot3Id).concreteValueType); }
}
public string input4Dimension
{
get { return ConvertConcreteSlotValueTypeToString(FindInputSlot<MaterialSlot>(InputSlot4Id).concreteValueType); }
}
}
}

12
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Function4Input.cs.meta


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