浏览代码

Remove Function3Input (use code node) :)

/main
Tim Cooper 8 年前
当前提交
4b1458e8
共有 16 个文件被更改,包括 208 次插入705 次删除
  1. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/Function4InputTests.cs
  2. 73
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Art/Adjustments/ContrastNode.cs
  3. 51
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Math/Interpolation/InverseLerpNode.cs
  4. 35
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Math/Interpolation/LerpNode.cs
  5. 35
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Math/Interpolation/SmoothStepNode.cs
  6. 35
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Math/Range/ClampNode.cs
  7. 71
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Procedural/BoxNode.cs
  8. 64
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Procedural/CheckerboardNode.cs
  9. 77
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Procedural/LineNode.cs
  10. 82
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/UV/SpherizeNode.cs
  11. 70
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/UV/UVPannerNode.cs
  12. 69
      MaterialGraphProject/Assets/_MingWai/RandomRangeNode.cs
  13. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/Function3InputTests.cs.meta
  14. 104
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/Function3InputTests.cs
  15. 125
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Function3Input.cs
  16. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Function3Input.cs.meta

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


public void TestGenerateNodeCodeGeneratesCorrectCode()
{
string expected = string.Format("half {0} = unity_test_half ({1}, {2}, {3}, {4});{5}"
, m_TestNode.GetVariableNameForSlot(Function3Input.OutputSlotId)
, m_TestNode.GetVariableNameForSlot(Function4Input.OutputSlotId)
, m_InputOne.GetVariableNameForSlot(Vector1Node.OutputSlotId)
, m_InputTwo.GetVariableNameForSlot(Vector1Node.OutputSlotId)
, m_InputThree.GetVariableNameForSlot(Vector1Node.OutputSlotId)

73
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Art/Adjustments/ContrastNode.cs


using UnityEngine.Graphing;
using System.Reflection;
public class ContrastNode : Function3Input, IGeneratesFunction
public class ContrastNode : CodeFunctionNode
{
public ContrastNode()
{

protected override string GetFunctionName ()
{
return "unity_contrast_" + precision;
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_Contrast", BindingFlags.Static | BindingFlags.NonPublic);
}
protected override MaterialSlot GetInputSlot1 ()
{
return new MaterialSlot (InputSlot1Id, GetInputSlot1Name (), kInputSlot1ShaderName, SlotType.Input, SlotValueType.Vector3, Vector4.zero);
}
protected override MaterialSlot GetInputSlot2()
{
return new MaterialSlot(InputSlot2Id, GetInputSlot2Name(), kInputSlot2ShaderName, SlotType.Input, SlotValueType.Vector1, Vector4.zero);
}
protected override MaterialSlot GetInputSlot3()
{
return new MaterialSlot(InputSlot3Id, GetInputSlot3Name(), kInputSlot3ShaderName, SlotType.Input, SlotValueType.Vector1, Vector4.zero);
}
protected override MaterialSlot GetOutputSlot ()
{
return new MaterialSlot (OutputSlotId, GetOutputSlotName (), kOutputSlotShaderName, SlotType.Output, SlotValueType.Vector3, Vector4.zero);
}
protected override string GetInputSlot2Name()
{
return "Contrast";
}
protected override string GetInputSlot3Name()
{
return "Midpoint";
}
// Contrast (reacts better when applied in log)
// Optimal range: [0.0, 2.0]]
// From PostProcessing
public void GenerateNodeFunction (ShaderGenerator visitor, GenerationMode generationMode)
{
var outputString = new ShaderGenerator ();
outputString.AddShaderChunk (GetFunctionPrototype ("arg1", "arg2", "arg3"), false);
outputString.AddShaderChunk ("{", false);
outputString.Indent ();
outputString.AddShaderChunk ("return (arg1 - arg3) * arg2 + arg3;", false);
outputString.Deindent ();
outputString.AddShaderChunk ("}", false);
visitor.AddShaderChunk (outputString.GetShaderString (0), true);
}
static string Unity_Contrast(
[Slot(0, Binding.None)] Vector3 input,
[Slot(1, Binding.None)] Vector1 contrast,
[Slot(2, Binding.None)] Vector1 midPoint,
[Slot(3, Binding.None)] out Vector3 result)
{
result = Vector2.zero;
return
@"
{
// Contrast (reacts better when applied in log)
// Optimal range: [0.0, 2.0]]
// From PostProcessing
result = (input - midpoint) * contrast + midPoint;
}";
}
}
}

51
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Math/Interpolation/InverseLerpNode.cs


using System.Reflection;
public class InverseLerpNode : Function3Input, IGeneratesFunction
public class InverseLerpNode : CodeFunctionNode
{
public InverseLerpNode ()
{

protected override string GetFunctionName ()
{
return "unity_inverselerp_" + precision;
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_InverseLerp", BindingFlags.Static | BindingFlags.NonPublic);
}
protected override string GetInputSlot1Name()
{
return "InputA";
}
protected override string GetInputSlot2Name()
{
return "InputB";
}
protected override string GetInputSlot3Name()
{
return "T";
}
public void GenerateNodeFunction (ShaderGenerator visitor, GenerationMode generationMode)
{
var outputString = new ShaderGenerator ();
outputString.AddShaderChunk (GetFunctionPrototype ("arg1", "arg2", "arg3"), false);
outputString.AddShaderChunk ("{", false);
outputString.Indent ();
outputString.AddShaderChunk ("return (arg3 - arg1)/(arg2 - arg1);", false);
outputString.Deindent ();
outputString.AddShaderChunk ("}", false);
visitor.AddShaderChunk (outputString.GetShaderString (0), true);
}
static string Unity_InverseLerp(
[Slot(0, Binding.None)] DynamicDimensionVector inputA,
[Slot(1, Binding.None)] DynamicDimensionVector inputB,
[Slot(2, Binding.None)] DynamicDimensionVector t,
[Slot(3, Binding.None)] out DynamicDimensionVector result)
{
return
@"
{
result = (t - inputA)/(inputB - inputA);
}";
}
}
}

35
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Math/Interpolation/LerpNode.cs


using System.Reflection;
public class LerpNode : Function3Input
public class LerpNode : CodeFunctionNode
{
public LerpNode()
{

protected override string GetInputSlot1Name()
{
return "InputA";
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_Lerp", BindingFlags.Static | BindingFlags.NonPublic);
}
protected override string GetInputSlot2Name()
{
return "InputB";
}
protected override string GetInputSlot3Name()
{
return "T";
}
protected override string GetFunctionName() {return "lerp"; }
static string Unity_Lerp(
[Slot(0, Binding.None)] DynamicDimensionVector inputA,
[Slot(1, Binding.None)] DynamicDimensionVector inputB,
[Slot(2, Binding.None)] DynamicDimensionVector t,
[Slot(3, Binding.None)] out DynamicDimensionVector result)
{
return
@"
{
result = lerp(inputA, inputB, t);
}";
}
}
}

35
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Math/Interpolation/SmoothStepNode.cs


using System.Reflection;
class SmoothStepNode : Function3Input
class SmoothStepNode : CodeFunctionNode
{
public SmoothStepNode()
{

protected override string GetInputSlot1Name()
{
return "InputA";
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_Smoothstep", BindingFlags.Static | BindingFlags.NonPublic);
}
protected override string GetInputSlot2Name()
{
return "InputB";
}
protected override string GetInputSlot3Name()
{
return "T";
}
protected override string GetFunctionName() {return "smoothstep"; }
static string Unity_Smoothstep(
[Slot(0, Binding.None)] DynamicDimensionVector inputA,
[Slot(1, Binding.None)] DynamicDimensionVector inputB,
[Slot(2, Binding.None)] DynamicDimensionVector t,
[Slot(3, Binding.None)] out DynamicDimensionVector result)
{
return
@"
{
result = smoothstep(inputA, inputB, t);
}";
}
}
}

35
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Math/Range/ClampNode.cs


using System.Reflection;
public class ClampNode : Function3Input
public class ClampNode : CodeFunctionNode
{
public ClampNode()
{

protected override string GetInputSlot1Name()
{
return "Input";
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_Smoothstep", BindingFlags.Static | BindingFlags.NonPublic);
}
protected override string GetInputSlot2Name()
{
return "Min";
}
protected override string GetInputSlot3Name()
{
return "Max";
}
protected override string GetFunctionName() {return "clamp"; }
static string Unity_Smoothstep(
[Slot(0, Binding.None)] DynamicDimensionVector input,
[Slot(1, Binding.None)] DynamicDimensionVector min,
[Slot(2, Binding.None)] DynamicDimensionVector max,
[Slot(3, Binding.None)] out DynamicDimensionVector result)
{
return
@"
{
result = clamp(input, min, max);
}";
}
}
}

71
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Procedural/BoxNode.cs


namespace UnityEngine.MaterialGraph
using System.Reflection;
namespace UnityEngine.MaterialGraph
public class BoxNode : Function3Input, IGeneratesFunction
public class BoxNode : CodeFunctionNode
{
public BoxNode()
{

protected override string GetFunctionName()
{
return "unity_boxnode_" + precision;
}
protected override string GetInputSlot1Name()
{
return "X and Y";
}
protected override string GetInputSlot2Name()
protected override MethodInfo GetFunctionToConvert()
return "X Min and Max";
return GetType().GetMethod("Unity_Boxnode", BindingFlags.Static | BindingFlags.NonPublic);
protected override string GetInputSlot3Name()
static string Unity_Boxnode(
[Slot(0, Binding.None)] Vector2 xy,
[Slot(1, Binding.None)] Vector2 xMinAndMax,
[Slot(2, Binding.None)] Vector2 yMinAndMax,
[Slot(3, Binding.None)] out Vector1 result)
return "Y Min and Max";
}
protected override MaterialSlot GetInputSlot1()
{
return new MaterialSlot(InputSlot1Id, GetInputSlot1Name(), kInputSlot1ShaderName, UnityEngine.Graphing.SlotType.Input, SlotValueType.Vector2, Vector3.zero);
}
protected override MaterialSlot GetInputSlot2()
{
return new MaterialSlot(InputSlot2Id, GetInputSlot2Name(), kInputSlot2ShaderName, UnityEngine.Graphing.SlotType.Input, SlotValueType.Vector2, Vector3.zero);
}
protected override MaterialSlot GetInputSlot3()
{
return new MaterialSlot(InputSlot3Id, GetInputSlot3Name(), kInputSlot3ShaderName, UnityEngine.Graphing.SlotType.Input, SlotValueType.Vector2, Vector2.zero);
}
protected override MaterialSlot GetOutputSlot()
{
return new MaterialSlot(OutputSlotId, GetOutputSlotName(), kOutputSlotShaderName, UnityEngine.Graphing.SlotType.Output, SlotValueType.Vector1, Vector2.zero);
}
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
{
var outputString = new ShaderGenerator();
outputString.AddShaderChunk(GetFunctionPrototype("xy", "xMinAndMax", "yMinAndMax"), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk("float x = step( xMinAndMax.x, xy.x ) - step( xMinAndMax.y, xy.x );", false);
outputString.AddShaderChunk("float y = step( yMinAndMax.x, xy.y ) - step( yMinAndMax.y, xy.y );", false);
outputString.AddShaderChunk("return x * y;", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
visitor.AddShaderChunk(outputString.GetShaderString(0), true);
return
@"
{
{precision} x = step( xMinAndMax.x, xy.x ) - step( xMinAndMax.y, xy.x );
{precision} y = step( yMinAndMax.x, xy.y ) - step( yMinAndMax.y, xy.y );
result = x * y;
}";
}
}
}

64
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Procedural/CheckerboardNode.cs


namespace UnityEngine.MaterialGraph
using System.Reflection;
namespace UnityEngine.MaterialGraph
public class CheckerboardNode : Function3Input, IGeneratesFunction
public class CheckerboardNode : CodeFunctionNode
{
public CheckerboardNode()
{

protected override string GetFunctionName()
{
return "unity_checkerboard_" + precision;
}
protected override string GetInputSlot1Name()
{
return "UV";
}
protected override string GetInputSlot2Name()
{
return "HorizontalTileScale";
}
protected override string GetInputSlot3Name()
{
return "VerticalTileScale";
}
protected override MaterialSlot GetInputSlot1()
{
return new MaterialSlot(InputSlot1Id, GetInputSlot1Name(), kInputSlot1ShaderName, UnityEngine.Graphing.SlotType.Input, SlotValueType.Vector2, Vector2.zero);
}
protected override MaterialSlot GetInputSlot2()
{
return new MaterialSlot(InputSlot2Id, GetInputSlot2Name(), kInputSlot2ShaderName, UnityEngine.Graphing.SlotType.Input, SlotValueType.Vector1, new Vector4(10,0,0,0));
}
protected override MaterialSlot GetInputSlot3()
{
return new MaterialSlot(InputSlot3Id, GetInputSlot3Name(), kInputSlot3ShaderName, UnityEngine.Graphing.SlotType.Input, SlotValueType.Vector1, new Vector4(10, 0, 0, 0));
}
protected override MaterialSlot GetOutputSlot()
protected override MethodInfo GetFunctionToConvert()
return new MaterialSlot(OutputSlotId, GetOutputSlotName(), kOutputSlotShaderName, UnityEngine.Graphing.SlotType.Output, SlotValueType.Vector1, Vector2.zero);
return GetType().GetMethod("Unity_Checkerboard", BindingFlags.Static | BindingFlags.NonPublic);
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
static string Unity_Checkerboard(
[Slot(0, Binding.MeshUV0)] Vector2 uv,
[Slot(1, Binding.None)] Vector1 horizontalTileScale,
[Slot(2, Binding.None)] Vector1 verticalTileScale,
[Slot(3, Binding.None)] out Vector1 result)
var outputString = new ShaderGenerator();
outputString.AddShaderChunk(GetFunctionPrototype("uv", "horizontalTileScale", "verticalTileScale"), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk("return abs(floor(fmod(floor(uv.x * horizontalTileScale) + floor(uv.y * verticalTileScale), 2.0)));", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
visitor.AddShaderChunk(outputString.GetShaderString(0), true);
return
@"
{
result = abs(floor(fmod(floor(uv.x * horizontalTileScale) + floor(uv.y * verticalTileScale), 2.0)));
}";
}
}
}

77
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Procedural/LineNode.cs


namespace UnityEngine.MaterialGraph
using System.Reflection;
namespace UnityEngine.MaterialGraph
public class LineNode : Function3Input, IGeneratesFunction
public class LineNode : CodeFunctionNode
{
public LineNode()
{

protected override string GetFunctionName()
protected override MethodInfo GetFunctionToConvert()
return "unity_linenode_" + precision;
return GetType().GetMethod("Unity_Linenode", BindingFlags.Static | BindingFlags.NonPublic);
protected override string GetInputSlot1Name()
static string Unity_Linenode(
[Slot(0, Binding.MeshUV0)] Vector2 uv,
[Slot(1, Binding.None)] Vector2 startPoint,
[Slot(2, Binding.None)] Vector2 endPoint,
[Slot(3, Binding.None)] out Vector1 result)
return "UV";
}
protected override string GetInputSlot2Name()
{
return "StartPoint";
}
protected override string GetInputSlot3Name()
{
return "EndPoint";
}
protected override MaterialSlot GetInputSlot1()
{
return new MaterialSlot(InputSlot1Id, GetInputSlot1Name(), kInputSlot1ShaderName, UnityEngine.Graphing.SlotType.Input, SlotValueType.Vector2, Vector2.zero);
}
protected override MaterialSlot GetInputSlot2()
{
return new MaterialSlot(InputSlot2Id, GetInputSlot2Name(), kInputSlot2ShaderName, UnityEngine.Graphing.SlotType.Input, SlotValueType.Vector2, Vector2.zero);
}
protected override MaterialSlot GetInputSlot3()
{
return new MaterialSlot(InputSlot3Id, GetInputSlot3Name(), kInputSlot3ShaderName, UnityEngine.Graphing.SlotType.Input, SlotValueType.Vector2, Vector2.zero);
}
protected override MaterialSlot GetOutputSlot()
{
return new MaterialSlot(OutputSlotId, GetOutputSlotName(), kOutputSlotShaderName, UnityEngine.Graphing.SlotType.Output, SlotValueType.Vector1, Vector2.zero);
}
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
{
var outputString = new ShaderGenerator();
outputString.AddShaderChunk(GetFunctionPrototype("uv", "a", "b"), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk("float2 aTob = b - a;", false);
outputString.AddShaderChunk("float2 aTop = uv - a;", false);
outputString.AddShaderChunk("float t = dot(aTop, aTob) / dot(aTob, aTob);", false);
outputString.AddShaderChunk("t = clamp(t, 0.0, 1.0);", false);
outputString.AddShaderChunk("float d = 1.0 / length(uv - (a + aTob * t));", false);
outputString.AddShaderChunk("return clamp(d, 0.0, 1.0);", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
visitor.AddShaderChunk(outputString.GetShaderString(0), true);
return
@"
{
{precision}2 aTob = endPoint - startPoint;
{precision}2 aTop = uv - startPoint;
{precision} t = dot(aTop, aTob) / dot(aTob, aTob);
t = clamp(t, 0.0, 1.0);
{precision} d = 1.0 / length(uv - (startPoint + aTob * t));
return clamp(d, 0.0, 1.0);
}";
}
}
}

82
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/UV/SpherizeNode.cs


namespace UnityEngine.MaterialGraph
using System.Reflection;
namespace UnityEngine.MaterialGraph
public class SpherizeNode : Function3Input, IGeneratesFunction
public class SpherizeNode : CodeFunctionNode
{
public SpherizeNode()
{

protected override string GetFunctionName()
{
return "unity_spherize_" + precision;
}
protected override string GetInputSlot1Name()
{
return "UV";
}
protected override MaterialSlot GetInputSlot1()
{
return new MaterialSlot(InputSlot1Id, GetInputSlot1Name(), kInputSlot1ShaderName, UnityEngine.Graphing.SlotType.Input, SlotValueType.Vector2, Vector2.zero);
}
protected override string GetInputSlot2Name()
{
return "Position";
}
protected override MaterialSlot GetInputSlot2()
{
return new MaterialSlot(InputSlot2Id, GetInputSlot2Name(), kInputSlot2ShaderName, UnityEngine.Graphing.SlotType.Input, SlotValueType.Vector2, Vector2.zero);
}
protected override string GetInputSlot3Name()
{
return "RadiusAndStrength";
}
protected override MaterialSlot GetInputSlot3()
{
return new MaterialSlot(InputSlot3Id, GetInputSlot3Name(), kInputSlot3ShaderName, UnityEngine.Graphing.SlotType.Input, SlotValueType.Vector2, Vector3.zero);
}
protected override MaterialSlot GetOutputSlot()
protected override MethodInfo GetFunctionToConvert()
return new MaterialSlot(OutputSlotId, GetOutputSlotName(), kOutputSlotShaderName, UnityEngine.Graphing.SlotType.Output, SlotValueType.Vector2, Vector2.zero);
return GetType().GetMethod("Unity_Spherize", BindingFlags.Static | BindingFlags.NonPublic);
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
static string Unity_Spherize(
[Slot(0, Binding.MeshUV0)] Vector2 uv,
[Slot(1, Binding.None)] Vector2 position,
[Slot(2, Binding.None)] Vector2 radiusAndStrength,
[Slot(3, Binding.None)] out Vector2 result)
var outputString = new ShaderGenerator();
outputString.AddShaderChunk(GetFunctionPrototype("uv", "position", "radiusAndStrength"), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
//vec2 fromUVToPoint = pos - uv;
//dist = length(fromUVToPoint);
//float mag = (1.0 - (dist / radius)) * strength;
//mag *= step(dist, radius);
//return uv + (mag * fromUVToPoint);
outputString.AddShaderChunk("float2 fromUVToPoint = position - uv;", false);
outputString.AddShaderChunk("float dist = length(fromUVToPoint);", false);
outputString.AddShaderChunk("float mag = ((1.0 - (dist / radiusAndStrength.x)) * radiusAndStrength.y) * step(dist, radiusAndStrength.x);", false);
outputString.AddShaderChunk("return uv + (mag * fromUVToPoint);", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
visitor.AddShaderChunk(outputString.GetShaderString(0), true);
result = Vector2.zero;
return
@"
{
{precision}2 fromUVToPoint = position - uv;
{precision} dist = length(fromUVToPoint);
{precision} mag = ((1.0 - (dist / radiusAndStrength.x)) * radiusAndStrength.y) * step(dist, radiusAndStrength.x);
return uv + (mag * fromUVToPoint);
}";
}
}
}

70
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/UV/UVPannerNode.cs


namespace UnityEngine.MaterialGraph
using System.Reflection;
namespace UnityEngine.MaterialGraph
public class PannerNode : Function3Input, IGeneratesFunction
public class PannerNode : CodeFunctionNode
{
public PannerNode()
{

protected override string GetFunctionName()
{
return "unity_uvpanner_" + precision;
}
protected override string GetInputSlot1Name()
{
return "UV";
}
protected override string GetInputSlot2Name()
{
return "HorizontalOffset";
}
protected override string GetInputSlot3Name()
{
return "VerticalOffset";
}
protected override MaterialSlot GetInputSlot1()
{
return new MaterialSlot(InputSlot1Id, GetInputSlot1Name(), kInputSlot1ShaderName, UnityEngine.Graphing.SlotType.Input, SlotValueType.Vector2, Vector4.zero);
}
protected override MaterialSlot GetInputSlot2()
{
return new MaterialSlot(InputSlot2Id, GetInputSlot2Name(), kInputSlot2ShaderName, UnityEngine.Graphing.SlotType.Input, SlotValueType.Vector1, Vector4.zero);
}
protected override MaterialSlot GetInputSlot3()
protected override MethodInfo GetFunctionToConvert()
return new MaterialSlot(InputSlot3Id, GetInputSlot3Name(), kInputSlot3ShaderName, UnityEngine.Graphing.SlotType.Input, SlotValueType.Vector1, Vector4.zero);
}
protected override MaterialSlot GetOutputSlot()
{
return new MaterialSlot(OutputSlotId, GetOutputSlotName(), kOutputSlotShaderName, UnityEngine.Graphing.SlotType.Output, SlotValueType.Vector2, Vector2.zero);
return GetType().GetMethod("Unity_UVPanner", BindingFlags.Static | BindingFlags.NonPublic);
public override bool hasPreview
static string Unity_UVPanner(
[Slot(0, Binding.MeshUV0)] Vector2 uv,
[Slot(1, Binding.None)] Vector1 horizontalOffset,
[Slot(2, Binding.None)] Vector1 verticalOffset,
[Slot(3, Binding.None)] out Vector2 result)
get { return true; }
}
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
{
var outputString = new ShaderGenerator();
outputString.AddShaderChunk(GetFunctionPrototype("UV", "HorizontalOffset", "VerticalOffset"), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk("return float2(UV.x + HorizontalOffset, UV.y + VerticalOffset);", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
visitor.AddShaderChunk(outputString.GetShaderString(0), true);
result = Vector2.zero;
return
@"
{
result = float2(uv.x + horizontalOffset, uv.y + verticalOffset);
}";
}
}
}

69
MaterialGraphProject/Assets/_MingWai/RandomRangeNode.cs


namespace UnityEngine.MaterialGraph
using System.Reflection;
namespace UnityEngine.MaterialGraph
public class RandomRangeNode : Function3Input, IGeneratesFunction
public class RandomRangeNode : CodeFunctionNode
{
public RandomRangeNode()
{

protected override string GetFunctionName()
protected override MethodInfo GetFunctionToConvert()
return "unity_randomrange_" + precision;
return GetType().GetMethod("Unity_Randomrange", BindingFlags.Static | BindingFlags.NonPublic);
protected override string GetInputSlot1Name()
static string Unity_Randomrange(
[Slot(0, Binding.None)] Vector2 seed,
[Slot(1, Binding.None)] Vector1 min,
[Slot(2, Binding.None)] Vector1 max,
[Slot(3, Binding.None)] out Vector1 result)
return "Seed";
}
protected override MaterialSlot GetInputSlot1()
{
return new MaterialSlot(InputSlot1Id, GetInputSlot1Name(), kInputSlot1ShaderName, UnityEngine.Graphing.SlotType.Input, SlotValueType.Vector2, Vector2.zero);
}
protected override string GetInputSlot2Name()
{
return "Min";
}
protected override MaterialSlot GetInputSlot2()
{
return new MaterialSlot(InputSlot2Id, GetInputSlot2Name(), kInputSlot2ShaderName, UnityEngine.Graphing.SlotType.Input, SlotValueType.Vector1, Vector2.zero);
}
protected override string GetInputSlot3Name()
{
return "Max";
}
protected override MaterialSlot GetInputSlot3()
{
return new MaterialSlot(InputSlot3Id, GetInputSlot3Name(), kInputSlot3ShaderName, UnityEngine.Graphing.SlotType.Input, SlotValueType.Vector1, Vector3.zero);
}
protected override MaterialSlot GetOutputSlot()
{
return new MaterialSlot(OutputSlotId, GetOutputSlotName(), kOutputSlotShaderName, UnityEngine.Graphing.SlotType.Output, SlotValueType.Vector1, Vector2.zero);
}
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
{
var outputString = new ShaderGenerator();
outputString.AddShaderChunk(GetFunctionPrototype("seed", "min", "max"), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk("float randomno = frac(sin(dot(seed, float2(12.9898, 78.233)))*43758.5453);", false);
outputString.AddShaderChunk("return floor(randomno * (max - min + 1)) + min;", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
visitor.AddShaderChunk(outputString.GetShaderString(0), true);
return
@"
{
{precision} randomno = frac(sin(dot(seed, float2(12.9898, 78.233)))*43758.5453);
retresult = floor(randomno * (max - min + 1)) + min;
}";
}
}
}

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


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

104
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/Function3InputTests.cs


using System;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.Graphing;
using UnityEngine.MaterialGraph;
namespace UnityEditor.MaterialGraph.UnitTests
{
[TestFixture]
public class Function3InputTests
{
private class Function3InputTestNode : Function3Input, IGeneratesFunction
{
public Function3InputTestNode()
{
name = "Function3InputTestNode";
}
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"), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk("return arg1 + arg2 + arg3;", 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 Function3InputTestNode 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_TestNode = new Function3InputTestNode();
m_Graph.AddNode(m_InputOne);
m_Graph.AddNode(m_InputTwo);
m_Graph.AddNode(m_InputThree);
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_Graph.Connect(m_InputOne.GetSlotReference(Vector1Node.OutputSlotId), m_TestNode.GetSlotReference(Function3Input.InputSlot1Id));
m_Graph.Connect(m_InputTwo.GetSlotReference(Vector1Node.OutputSlotId), m_TestNode.GetSlotReference(Function3Input.InputSlot2Id));
m_Graph.Connect(m_InputThree.GetSlotReference(Vector1Node.OutputSlotId), m_TestNode.GetSlotReference(Function3Input.InputSlot3Id));
m_Graph.Connect(m_TestNode.GetSlotReference(Function3Input.OutputSlotId), m_Graph.masterNode.GetSlotReference(MetallicMasterNode.NormalSlotId));
}
[Test]
public void TestGenerateNodeCodeGeneratesCorrectCode()
{
string expected = string.Format("half {0} = unity_test_half ({1}, {2}, {3});{4}"
, m_TestNode.GetVariableNameForSlot(Function3Input.OutputSlotId)
, m_InputOne.GetVariableNameForSlot(Vector1Node.OutputSlotId)
, m_InputTwo.GetVariableNameForSlot(Vector1Node.OutputSlotId)
, m_InputThree.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)" + Environment.NewLine
+ "{" + Environment.NewLine
+ "\treturn arg1 + arg2 + arg3;" + Environment.NewLine
+ "}" + Environment.NewLine;
ShaderGenerator visitor = new ShaderGenerator();
m_TestNode.GenerateNodeFunction(visitor, GenerationMode.ForReals);
Assert.AreEqual(expected, visitor.GetShaderString(0));
}
}
}

125
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Function3Input.cs


using UnityEngine.Graphing;
namespace UnityEngine.MaterialGraph
{
public abstract class Function3Input : AbstractMaterialNode, IGeneratesBodyCode
{
protected const string kInputSlot1ShaderName = "Input1";
protected const string kInputSlot2ShaderName = "Input2";
protected const string kInputSlot3ShaderName = "Input3";
protected const string kOutputSlotShaderName = "Output";
public const int InputSlot1Id = 0;
public const int InputSlot2Id = 1;
public const int InputSlot3Id = 2;
public const int OutputSlotId = 3;
public override bool hasPreview
{
get { return true; }
}
protected Function3Input()
{
UpdateNodeAfterDeserialization();
}
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(GetInputSlot1());
AddSlot(GetInputSlot2());
AddSlot(GetInputSlot3());
AddSlot(GetOutputSlot());
RemoveSlotsNameNotMatching(validSlots);
}
protected int[] validSlots
{
get { return new[] {InputSlot1Id, InputSlot2Id, InputSlot3Id, 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 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 GetOutputSlotName()
{
return "Output";
}
protected abstract string GetFunctionName();
protected virtual string GetFunctionPrototype(string arg1Name, string arg2Name, string arg3Name)
{
return "inline " + precision + outputDimension + " " + GetFunctionName() + " ("
+ precision + input1Dimension + " " + arg1Name + ", "
+ precision + input2Dimension + " " + arg2Name + ", "
+ precision + input3Dimension + " " + arg3Name + ")";
}
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);
visitor.AddShaderChunk(precision + outputDimension + " " + GetVariableNameForSlot(OutputSlotId) + " = " + GetFunctionCallBody(input1Value, input2Value, input3Value) + ";", true);
}
protected virtual string GetFunctionCallBody(string inputValue1, string inputValue2, string inputValue3)
{
return GetFunctionName() + " (" + inputValue1 + ", " + inputValue2 + ", " + inputValue3 + ")";
}
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); }
}
}
}

8
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Function3Input.cs.meta


fileFormatVersion: 2
guid: e7d8fb926f570fb48b10d7d068d54123
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
正在加载...
取消
保存