浏览代码

Porting more nodes to not be function nodes

/main
Tim Cooper 8 年前
当前提交
5e6630fb
共有 12 个文件被更改,包括 241 次插入572 次删除
  1. 87
      MaterialGraphProject/Assets/Andre/Nodes/LightProbeNode.cs
  2. 107
      MaterialGraphProject/Assets/Andre/Nodes/ReflectionProbeNode.cs
  3. 4
      MaterialGraphProject/Assets/Eduardo/ChannelBlendNode.cs
  4. 91
      MaterialGraphProject/Assets/Eduardo/HeightToNormalNode.cs
  5. 61
      MaterialGraphProject/Assets/Rinaldo/FractalNode.cs
  6. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/GradientNodePresenter.cs
  7. 7
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/HLSLNode.cs
  8. 166
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Math/Range/RemapNode.cs
  9. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Procedural/HexNode.cs
  10. 116
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Procedural/VoronoiNoise.cs
  11. 66
      MaterialGraphProject/Assets/_MingWai/ColorBalanceNode.cs
  12. 98
      MaterialGraphProject/Assets/_MingWai/GradientNode.cs

87
MaterialGraphProject/Assets/Andre/Nodes/LightProbeNode.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
#if UNITY_EDITOR
using UnityEditor;
#endif

{
[Title("Input/Scene Data/Light Probe")]
public class LightProbeNode : AbstractMaterialNode, IGeneratesBodyCode, IMayRequireNormal
public class LightProbeNode : CodeFunctionNode
protected const string kUVSlotName = "Normal";
protected const string kOutputSlotRGBAName = "RGBA";
public const int NormalSlot = 0;
public const int OutputSlotRgbaId = 1;
UpdateNodeAfterDeserialization();
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new MaterialSlot(OutputSlotRgbaId, kOutputSlotRGBAName, kOutputSlotRGBAName, SlotType.Output, SlotValueType.Vector3, Vector3.zero));
AddSlot(new MaterialSlot(NormalSlot, kUVSlotName, kUVSlotName, SlotType.Input, SlotValueType.Vector3, Vector3.zero, false));
RemoveSlotsNameNotMatching(validSlots);
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_LightProbe", BindingFlags.Static | BindingFlags.NonPublic);
}
public override PreviewMode previewMode {
static string Unity_LightProbe(
[Slot(0, Binding.Normal)] Vector3 worldSpaceNormal,
[Slot(1, Binding.None)] out Vector4 color)
{
color = Vector4.one;
return
@"
{
color = ShadeSH9(float4(worlsSpaceNormal , 1));
}
";
}
public override PreviewMode previewMode {
}
protected int[] validSlots
{
get { return new[] {OutputSlotRgbaId, NormalSlot}; }
}
// Node generations
public virtual void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)
{
var normalSlot = FindInputSlot<MaterialSlot>(NormalSlot);
if (normalSlot == null)
return;
var normalName = "worldSpaceNormal";
var edgesNorm = owner.GetEdges(normalSlot.slotReference).ToList();
if (edgesNorm.Count > 0)
{
var edge = edgesNorm[0];
var fromNode = owner.GetNodeFromGuid<AbstractMaterialNode>(edge.outputSlot.nodeGuid);
normalName = ShaderGenerator.AdaptNodeOutput(fromNode, edge.outputSlot.slotId, ConcreteSlotValueType.Vector3, true);
}
//float3 shl = ShadeSH9(float4(worldNormal,1));
string body = "ShadeSH9(float4(" + normalName + ".xyz , 1))";
visitor.AddShaderChunk(precision + "3 " + GetVariableNameForNode() + " = " + body + ";", true);
}
public override string GetVariableNameForSlot(int slotId)
{
string slotOutput;
switch (slotId)
{
case NormalSlot:
slotOutput = "_normalDir";
break;
default:
slotOutput = "";
break;
}
return GetVariableNameForNode() + slotOutput;
}
public bool RequiresViewDirection()
{
return true;
}
public bool RequiresNormal()
{
return true;
}
}
}

107
MaterialGraphProject/Assets/Andre/Nodes/ReflectionProbeNode.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
#if UNITY_EDITOR
using UnityEditor;
#endif

{
[Title("Input/Scene Data/Reflection Probe")]
public class ReflectionProbeNode : AbstractMaterialNode, IGeneratesBodyCode, IMayRequireViewDirection, IMayRequireNormal
public class ReflectionProbeNode : CodeFunctionNode
protected const string kUVSlotName = "RefVector";
protected const string kOutputSlotRGBAName = "RGBA";
protected const string kInputSlotLodName = "MipLevel";
public const int NormalSlot = 0;
public const int InputSlotLod = 2;
public const int OutputSlotRgbaId = 1;
public override bool hasPreview { get { return true; } }
UpdateNodeAfterDeserialization();
}
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new MaterialSlot(OutputSlotRgbaId, kOutputSlotRGBAName, kOutputSlotRGBAName, SlotType.Output, SlotValueType.Vector4, Vector4.zero));
AddSlot(new MaterialSlot(NormalSlot, kUVSlotName, kUVSlotName, SlotType.Input, SlotValueType.Vector3, Vector3.zero, false));
AddSlot (new MaterialSlot(InputSlotLod, kInputSlotLodName, kInputSlotLodName, SlotType.Input, SlotValueType.Vector1, Vector2.zero));
RemoveSlotsNameNotMatching(validSlots);
}
public override PreviewMode previewMode {

}
protected int[] validSlots
{
get { return new[] {OutputSlotRgbaId, NormalSlot, InputSlotLod}; }
}
// Node generations
public virtual void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)
{
var uvSlot = FindInputSlot<MaterialSlot>(NormalSlot);
if (uvSlot == null)
return;
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_ReflectionProbe", BindingFlags.Static | BindingFlags.NonPublic);
}
var lodID = FindInputSlot<MaterialSlot>(InputSlotLod);
if (lodID == null)
return;
var uvName = "reflect(-worldSpaceViewDirection, worldSpaceNormal)";
var lodValue = lodID.currentValue.x.ToString ();
var edgesUV = owner.GetEdges(uvSlot.slotReference).ToList();
var edgesLOD = owner.GetEdges (lodID.slotReference).ToList ();
if (edgesUV.Count > 0)
{
var edge = edgesUV[0];
var fromNode = owner.GetNodeFromGuid<AbstractMaterialNode>(edge.outputSlot.nodeGuid);
uvName = ShaderGenerator.AdaptNodeOutput(fromNode, edge.outputSlot.slotId, ConcreteSlotValueType.Vector3, true);
}
if (edgesLOD.Count > 0)
{
var edge = edgesLOD[0];
var fromNode = owner.GetNodeFromGuid<AbstractMaterialNode>(edge.outputSlot.nodeGuid);
lodValue = GetSlotValue (edge.inputSlot.slotId, GenerationMode.ForReals);
}
string body = "DecodeHDR(UNITY_SAMPLE_TEXCUBE_LOD(unity_SpecCube0, " + uvName + ".xyz ," + lodValue + "), unity_SpecCube0_HDR)";
visitor.AddShaderChunk(precision + "3 " + GetVariableNameForNode() + " = " + body + ";", true);
}
public override string GetVariableNameForSlot(int slotId)
{
string slotOutput;
switch (slotId)
{
case InputSlotLod:
slotOutput = "_lod";
break;
case NormalSlot:
slotOutput = "_refDir";
break;
default:
slotOutput = "";
break;
}
return GetVariableNameForNode() + slotOutput;
}
public bool RequiresViewDirection()
{
return true;
}
public bool RequiresNormal()
{
return true;
}
static string Unity_ReflectionProbe(
[Slot(0, Binding.ViewDirection)] Vector3 viewDirection,
[Slot(1, Binding.Normal)] Vector3 worldSpaceNormal,
[Slot(2, Binding.None)] Vector1 lod,
[Slot(3, Binding.None)] out Vector4 color)
{
color = Vector4.one;
return
@"
{
{precision}3 reflect = reflect(-viewDirection, worldSpaceNormal);
color = DecodeHDR(UNITY_SAMPLE_TEXCUBE_LOD(unity_SpecCube0, reflect, lod), unity_SpecCube0_HDR);
}
";
}
}
}

4
MaterialGraphProject/Assets/Eduardo/ChannelBlendNode.cs


namespace UnityEngine.MaterialGraph
{
[Title("Art/ChannelBlend")]
/* [Title("Art/ChannelBlend")]
public class ChannelBlend : FunctionNInNOut, IGeneratesFunction
{

visitor.AddShaderChunk(outputString.GetShaderString(0), true);
}
}
}*/
}

91
MaterialGraphProject/Assets/Eduardo/HeightToNormalNode.cs


using UnityEngine.Graphing;
using System.Linq;
using System.Collections;
using System.Reflection;
public class HeightToNormalNode : FunctionNInNOut, IGeneratesFunction, IGenerateProperties
public class HeightToNormalNode : CodeFunctionNode
AddSlot("HeightMap", "heightmap", Graphing.SlotType.Input, SlotValueType.Texture2D, Vector4.zero);
AddSlot("UV", "texCoord", Graphing.SlotType.Input, SlotValueType.Vector2, Vector4.zero);
AddSlot("Offset", "texOffset", Graphing.SlotType.Input, SlotValueType.Vector1, new Vector4(0.005f, 0,0,0));
AddSlot("Strength", "strength", Graphing.SlotType.Input, SlotValueType.Vector1, new Vector4(8,0,0,0));
AddSlot("Normal", "normalRes", Graphing.SlotType.Output, SlotValueType.Vector3, Vector4.zero);
UpdateNodeAfterDeserialization();
protected override string GetFunctionName()
protected override MethodInfo GetFunctionToConvert()
return "unity_HeightToNormal";
return GetType().GetMethod("Unity_HeightToNormal", BindingFlags.Static | BindingFlags.NonPublic);
public override bool hasPreview
static string Unity_HeightToNormal(
[Slot(0, Binding.None)] Texture2D heightmap,
[Slot(1, Binding.MeshUV0)] Vector1 texCoord,
[Slot(2, Binding.None, 0.005f, 0, 0, 0)] Vector1 texOffset,
[Slot(3, Binding.None, 8f, 0, 0, 0)] Vector1 strength,
[Slot(4, Binding.None)] out Vector1 normal)
get { return true; }
return
@"
{
float2 offsetU = float2(texCoord.x + texOffset, texCoord.y);
float2 offsetV = float2(texCoord.x, texCoord.y + texOffset);
float normalSample = 0;
float uSample = 0;
float vSample = 0;
#ifdef UNITY_COMPILER_HLSL
normalSample = heightmap.Sample(my_linear_repeat_sampler, texCoord).r;
uSample = heightmap.Sample(my_linear_repeat_sampler, offsetU).r;
vSample = heightmap.Sample(my_linear_repeat_sampler, offsetV).r;
#endif
float uMinusNormal = uSample - normalSample;
float vMinusNormal = vSample - normalSample;
uMinusNormal = uMinusNormal * strength;
vMinusNormal = vMinusNormal * strength;
float3 va = float3(1, 0, uMinusNormal);
float3 vb = float3(0, 1, vMinusNormal);
normals = cross(va, vb);
}
";
}
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)

visitor.AddShaderChunk("#ifdef UNITY_COMPILER_HLSL", false);
visitor.AddShaderChunk("SamplerState my_linear_repeat_sampler;", false);
visitor.AddShaderChunk("#endif", false);
}
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
{
var outputString = new ShaderGenerator();
outputString.AddShaderChunk(GetFunctionPrototype(), false);
outputString.AddShaderChunk("{", false);
outputString.AddShaderChunk("float2 offsetU = float2(texCoord.x + texOffset, texCoord.y);", false);
outputString.AddShaderChunk("float2 offsetV = float2(texCoord.x, texCoord.y + texOffset);", false);
outputString.AddShaderChunk("float normalSample = 0;", false);
outputString.AddShaderChunk("float uSample = 0;", false);
outputString.AddShaderChunk("float vSample = 0;", false);
visitor.AddShaderChunk("#ifdef UNITY_COMPILER_HLSL", false);
outputString.AddShaderChunk("normalSample = heightmap.Sample(my_linear_repeat_sampler, texCoord).r;", false);
outputString.AddShaderChunk("uSample = heightmap.Sample(my_linear_repeat_sampler, offsetU).r;", false);
outputString.AddShaderChunk("vSample = heightmap.Sample(my_linear_repeat_sampler, offsetV).r;", false);
visitor.AddShaderChunk("#endif", false);
outputString.AddShaderChunk("float uMinusNormal = uSample - normalSample;", false);
outputString.AddShaderChunk("float vMinusNormal = vSample - normalSample;", false);
outputString.AddShaderChunk("uMinusNormal = uMinusNormal * strength;", false);
outputString.AddShaderChunk("vMinusNormal = vMinusNormal * strength;", false);
outputString.AddShaderChunk("float3 va = float3(1, 0, uMinusNormal);", false);
outputString.AddShaderChunk("float3 vb = float3(0, 1, vMinusNormal);", false);
outputString.AddShaderChunk("normalRes = cross(va, vb);", false);
outputString.AddShaderChunk("}", false);
visitor.AddShaderChunk(outputString.GetShaderString(0), true);
}
}
}

61
MaterialGraphProject/Assets/Rinaldo/FractalNode.cs


using UnityEngine.Graphing;
using System.Linq;
using System.Collections;
using System.Reflection;
public class FractalNode : FunctionNInNOut, IGeneratesFunction
public class FractalNode : CodeFunctionNode
AddSlot("UV", "texCoord", Graphing.SlotType.Input, SlotValueType.Vector2, Vector4.zero);
AddSlot("Pan", "Pan", Graphing.SlotType.Input, SlotValueType.Vector2, new Vector4(0.5f,0,0,0));
AddSlot("Zoom", "Zoom", Graphing.SlotType.Input, SlotValueType.Vector1, new Vector4(3,0,0,0));
AddSlot("Aspect", "Aspect", Graphing.SlotType.Input, SlotValueType.Vector1, new Vector4(0.9f,0,0,0));
AddSlot("FracResult", "fractalRes", Graphing.SlotType.Output, SlotValueType.Dynamic, Vector4.zero);
UpdateNodeAfterDeserialization();
protected override string GetFunctionName()
protected override MethodInfo GetFunctionToConvert()
return "unity_Fractal";
return GetType().GetMethod("Unity_Fractal", BindingFlags.Static | BindingFlags.NonPublic);
public override bool hasPreview
static string Unity_Fractal(
[Slot(0, Binding.MeshUV0)] Vector2 uv,
[Slot(1, Binding.None, 0.5f, 0, 0, 0)] Vector2 pan,
[Slot(2, Binding.None, 3, 0, 0, 0)] Vector1 zoom,
[Slot(3, Binding.None, 0.9f, 0, 0, 0)] Vector1 aspect,
[Slot(4, Binding.None)] out Vector1 result)
get { return true; }
}
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
{
var outputString = new ShaderGenerator();
outputString.AddShaderChunk(GetFunctionPrototype(), false);
outputString.AddShaderChunk("{", false);
outputString.AddShaderChunk("const int Iterations = 128;", false);
outputString.Indent();
outputString.AddShaderChunk("float2 c = (texCoord - 0.5) * Zoom * float2(1, Aspect) - Pan;", false);
outputString.AddShaderChunk("float2 v = 0;", false);
outputString.AddShaderChunk("for (int n = 0; n < Iterations && dot(v,v) < 4; n++)", false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk("v = float2(v.x * v.x - v.y * v.y, v.x * v.y * 2) + c;", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
outputString.Deindent();
outputString.AddShaderChunk("fractalRes = (dot(v, v) > 4) ? (float)n / (float)Iterations : 0;", false);
outputString.AddShaderChunk("}", false);
visitor.AddShaderChunk(outputString.GetShaderString(0), true);
return
@"
{
const int Iterations = 128;
float2 c = (uv - 0.5) * zoom * float2(1, aspect) - pan;
float2 v = 0;
for (int n = 0; n < Iterations && dot(v,v) < 4; n++)
{
v = float2(v.x * v.x - v.y * v.y, v.x * v.y * 2) + c;
}
result = (dot(v, v) > 4) ? (float)n / (float)Iterations : 0;
}
";
}
}
}

8
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/GradientNodePresenter.cs


var cNode = node as UnityEngine.MaterialGraph.GradientNode;
if (cNode == null)
return;
if(gradientobj == null || prevnode != cNode)
{
prevnode = cNode;

gradientobj.gradient = cNode.gradient;
}
hserializedObject = new SerializedObject(gradientobj);
hcolorGradient = hserializedObject.FindProperty("gradient");
}

cNode.gradient = gradientobj.gradient;
Event e = Event.current;
cNode.UpdateGradient();
cNode.UpdateGradient();
prevWindow = EditorWindow.focusedWindow.ToString();
Debug.Log("Update Gradient Shader");
}

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


get { return true; }
}
public CodeFunctionNode()
protected CodeFunctionNode()
{
UpdateNodeAfterDeserialization();
}

defaultValue = null;
}
public SlotAttribute(int mslotId, Binding mImplicitBinding, int defaultX, int defaultY, int defaultZ, int defaultW)
public SlotAttribute(int mslotId, Binding mImplicitBinding, float defaultX, float defaultY, float defaultZ, float defaultW)
{
slotId = mslotId;
binding = mImplicitBinding;

private string GetFunctionName()
{
return GetFunctionToConvert().Name + "_" + precision;
var function = GetFunctionToConvert();
return function.Name + "_" + (function.IsStatic ? string.Empty : GuidEncoder.Encode(guid) + "_") + precision;
}
private string GetFunctionHeader()

166
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Math/Range/RemapNode.cs


using UnityEngine.Graphing;
using System.Reflection;
public class RemapNode : AbstractMaterialNode, IGeneratesBodyCode, IGeneratesFunction
public class RemapNode : CodeFunctionNode
protected const string kInputSlot1ShaderName = "Input1";
protected const string kInputSlot2ShaderName = "InMinMax";
protected const string kInputSlot3ShaderName = "OutMinMax";
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;
[SerializeField]
private Vector4 m_Value;
public override bool hasPreview
{
get { return true; }
}
public RemapNode ()
{
name = "Remap";
UpdateNodeAfterDeserialization();
}
public string GetFunctionName ()
{
return "unity_remap_" + precision;
}
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.Vector2, Vector2.zero);
}
protected virtual MaterialSlot GetInputSlot3()
{
return new MaterialSlot(InputSlot3Id, GetInputSlot3Name(), kInputSlot3ShaderName, SlotType.Input, SlotValueType.Vector2, Vector2.zero);
}
protected virtual MaterialSlot GetOutputSlot()
{
return new MaterialSlot(OutputSlotId, GetOutputSlotName(), kOutputSlotShaderName, SlotType.Output, SlotValueType.Dynamic, Vector4.zero);
}
protected virtual string GetInputSlot1Name()
{
return "Input";
}
protected virtual string GetInputSlot2Name()
{
return "InMinMax";
}
protected virtual string GetInputSlot3Name()
{
return "OutMinMax";
}
protected virtual string GetOutputSlotName()
{
return "Output";
}
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);
public RemapNode()
{
name = "Remap";
}
visitor.AddShaderChunk(precision + outputDimension + " " + GetVariableNameForSlot(OutputSlotId) + " = " + GetFunctionCallBody(input1Value, input2Value, input3Value) + ";", true);
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_Remap", BindingFlags.Static | BindingFlags.NonPublic);
}
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
{
//new = arg3.x + (arg1 - arg2.x) * (arg3.y - arg3.x) / (arg2.y - arg2.x)
//low2 + (value - low1) * (high2 - low2) / (high1 - low1)
//old = arg1 * ((arg3.y - arg3.x) / (arg2.y - arg2.x)) + arg3.x
var outputString = new ShaderGenerator ();
outputString.AddShaderChunk (GetFunctionPrototype ("arg1", "arg2", "arg3"), false);
outputString.AddShaderChunk ("{", false);
outputString.Indent ();
outputString.AddShaderChunk ("return arg3.x + (arg1 - arg2.x) * (arg3.y - arg3.x) / (arg2.y - arg2.x);", false);
outputString.Deindent ();
outputString.AddShaderChunk ("}", false);
visitor.AddShaderChunk (outputString.GetShaderString (0), 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); }
}
static string Unity_Remap(
[Slot(0, Binding.None)] DynamicDimensionVector input,
[Slot(1, Binding.None)] Vector2 inMinMax,
[Slot(2, Binding.None)] Vector2 outMinMax,
[Slot(3, Binding.None)] out DynamicDimensionVector result)
{
return
@"
{
result = outMinMax.x + (input - inMinMax.x) * (outMinMax.y - outMinMax.x) / (inMinMax.y - inMinMax.x);
}
";
}
}
}

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Procedural/HexNode.cs


}
static string Unity_Hex(
[Slot(0, Binding.None)] Vector2 uv,
[Slot(0, Binding.MeshUV0)] Vector2 uv,
[Slot(1, Binding.None)] Vector1 thickness,
[Slot(2, Binding.None)] out Vector1 result)
{

116
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Procedural/VoronoiNoise.cs


namespace UnityEngine.MaterialGraph
using System.Reflection;
namespace UnityEngine.MaterialGraph
public class VoronoiNoiseNode : FunctionNInNOut, IGeneratesFunction
public class VoronoiNoiseNode : CodeFunctionNode
AddSlot("UV", "uv", Graphing.SlotType.Input, SlotValueType.Vector2, Vector4.zero);
AddSlot("AngleOffset", "angleOffset", Graphing.SlotType.Input, SlotValueType.Vector1, new Vector4(2.0f,0,0,0));
AddSlot("Cellular", "n1", Graphing.SlotType.Output, SlotValueType.Vector1, Vector4.zero);
AddSlot("Tile", "n2", Graphing.SlotType.Output, SlotValueType.Vector1, Vector4.zero);
AddSlot("1 - Cellular", "n3", Graphing.SlotType.Output, SlotValueType.Vector1, Vector4.zero);
protected override string GetFunctionName()
protected override MethodInfo GetFunctionToConvert()
return "unity_voronoinoise_" + precision;
return GetType().GetMethod("Unity_VoronoiNoise", BindingFlags.Static | BindingFlags.NonPublic);
public override bool hasPreview
static string Unity_VoronoiNoise(
[Slot(0, Binding.MeshUV0)] Vector2 uv,
[Slot(1, Binding.None, 2.0f, 0,0,0)] Vector1 angleOffset,
[Slot(2, Binding.None)] out Vector1 n1,
[Slot(2, Binding.None)] out Vector1 n2,
[Slot(2, Binding.None)] out Vector1 n3)
get
{
return true;
}
}
return
@"
{
float2 g = floor(uv);
float2 f = frac(uv);
float t = 8.0;
float3 res = float3(8.0, 0.0, 0.0);
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
for(int y=-1; y<=1; y++)
{
for(int x=-1; x<=1; x++)
var outputString = new ShaderGenerator();
outputString.AddShaderChunk("inline float2 unity_voronoi_noise_randomVector (float2 uv, float offset)", false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk("float2x2 m = float2x2(15.27, 47.63, 99.41, 89.98);", false);
outputString.AddShaderChunk("uv = frac(sin(mul(uv, m)) * 46839.32);", false);
outputString.AddShaderChunk("return float2(sin(uv.y*+offset)*0.5+0.5, cos(uv.x*offset)*0.5+0.5);", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
outputString.AddShaderChunk(GetFunctionPrototype(), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk("float2 g = floor(uv);", false);
outputString.AddShaderChunk("float2 f = frac(uv);", false);
outputString.AddShaderChunk("float t = 8.0;", false);
outputString.AddShaderChunk("float3 res = float3(8.0, 0.0, 0.0);", false);
outputString.AddShaderChunk("for(int y=-1; y<=1; y++)", false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
float2 lattice = float2(x,y);
float2 offset = unity_voronoi_noise_randomVector(lattice + g, angleOffset);
float d = distance(lattice + offset, f);
outputString.AddShaderChunk("for(int x=-1; x<=1; x++)", false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk("float2 lattice = float2(x,y);", false);
outputString.AddShaderChunk("float2 offset = unity_voronoi_noise_randomVector(lattice + g, angleOffset);", false);
outputString.AddShaderChunk("float d = distance(lattice + offset, f);", false);
outputString.AddShaderChunk("if(d < res.x)", false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk("res = float3(d, offset.x, offset.y);", false);
outputString.AddShaderChunk("n1 = res.x;", false);
outputString.AddShaderChunk("n2 = res.y;", false);
outputString.AddShaderChunk("n3 = 1.0 - res.x;", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
if(d < res.x)
{
outputString.Deindent();
outputString.AddShaderChunk("}", false);
res = float3(d, offset.x, offset.y);
n1 = res.x;
n2 = res.y;
n3 = 1.0 - res.x;
}
}
outputString.Deindent();
outputString.AddShaderChunk("}", false);
}
}
";
}
visitor.AddShaderChunk(outputString.GetShaderString(0), true);
public override void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
{
var preamble = @"
inline float2 unity_voronoi_noise_randomVector (float2 uv, float offset)
{
float2x2 m = float2x2(15.27, 47.63, 99.41, 89.98);
uv = frac(sin(mul(uv, m)) * 46839.32);
return float2(sin(uv.y*+offset)*0.5+0.5, cos(uv.x*offset)*0.5+0.5);
}
";
visitor.AddShaderChunk(preamble, true);
base.GenerateNodeFunction(visitor, generationMode);
}
}
}

66
MaterialGraphProject/Assets/_MingWai/ColorBalanceNode.cs


namespace UnityEngine.MaterialGraph
using System.Reflection;
namespace UnityEngine.MaterialGraph
public class ColorBalanceNode : FunctionNInNOut, IGeneratesFunction
public class ColorBalanceNode : CodeFunctionNode
AddSlot("Color", "inputColor", Graphing.SlotType.Input, SlotValueType.Vector4, Vector4.one);
AddSlot("AdjustRGB", "adjustRGB", Graphing.SlotType.Input, SlotValueType.Vector3, Vector3.zero);
AddSlot("RGBA", "finalColor", Graphing.SlotType.Output, SlotValueType.Vector4, Vector4.zero);
UpdateNodeAfterDeserialization();
protected override string GetFunctionName()
protected override MethodInfo GetFunctionToConvert()
return "unity_colorbalance_" + precision;
return GetType().GetMethod("Unity_ColorBalance", BindingFlags.Static | BindingFlags.NonPublic);
public override bool hasPreview
{
get { return true; }
}
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
static string Unity_ColorBalance(
[Slot(0, Binding.None)] Vector4 inputColor,
[Slot(1, Binding.None)] Vector3 adjustRGB,
[Slot(2, Binding.None)] out Vector4 outColor)
var outputString = new ShaderGenerator();
outputString.AddShaderChunk(GetFunctionPrototype(), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk("float red = 0;", false);
outputString.AddShaderChunk("float green = 0;", false);
outputString.AddShaderChunk("float blue = 0;", false);
outputString.AddShaderChunk("red = 1.00f / (1-adjustRGB.r) * inputColor.r;", false);
outputString.AddShaderChunk("green = 1.00f / (1-adjustRGB.g) * inputColor.g;", false);
outputString.AddShaderChunk("blue = 1.00f / (1-adjustRGB.b) * inputColor.b;", false);
outColor = Vector4.zero;
return
@"
{
float red = 0;
float green = 0;
float blue = 0;
outputString.AddShaderChunk("red = clamp(red,0.00f,1.00f);", false);
outputString.AddShaderChunk("green = clamp(green,0.00f,1.00f);", false);
outputString.AddShaderChunk("blue = clamp(blue,0.00f,1.00f);", false);
red = 1.00f / (1-adjustRGB.r) * inputColor.r;
green = 1.00f / (1-adjustRGB.g) * inputColor.g;
blue = 1.00f / (1-adjustRGB.b) * inputColor.b;
outputString.AddShaderChunk("finalColor.r = red;", false);
outputString.AddShaderChunk("finalColor.g = green;", false);
outputString.AddShaderChunk("finalColor.b = blue;", false);
outputString.AddShaderChunk("finalColor.a = inputColor.a;", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
red = clamp(red,0.00f,1.00f);
green = clamp(green,0.00f,1.00f);
blue = clamp(blue,0.00f,1.00f);
visitor.AddShaderChunk(outputString.GetShaderString(0), true);
outColor.r = red;
outColor.g = green;
outColor.b = blue;
outColor.a = inputColor.a;
}
";
}
}
}

98
MaterialGraphProject/Assets/_MingWai/GradientNode.cs


using System.Collections;
using UnityEngine;
using System.Reflection;
using System.Text;
public class GradientNode : FunctionNInNOut, IGeneratesFunction
public class GradientNode : CodeFunctionNode
{
[SerializeField]
private Gradient m_gradient;

get { return m_gradient; }
set
{
{
}
{
}
public void UpdateGradient()
{
if (onModified != null)

// Debug.Log("UPDATED GRAPH");
}
AddSlot("Value", "v", Graphing.SlotType.Input, SlotValueType.Vector1, Vector4.one);
AddSlot("RGBA", "finalColor", Graphing.SlotType.Output, SlotValueType.Vector4, Vector4.zero);
AddSlot("R", "finalR", Graphing.SlotType.Output, SlotValueType.Vector1, Vector4.zero);
AddSlot("G", "finalG", Graphing.SlotType.Output, SlotValueType.Vector1, Vector4.zero);
AddSlot("B", "finalB", Graphing.SlotType.Output, SlotValueType.Vector1, Vector4.zero);
AddSlot("A", "finalA", Graphing.SlotType.Output, SlotValueType.Vector1, Vector4.zero);
UpdateNodeAfterDeserialization();
protected override string GetFunctionName()
protected override MethodInfo GetFunctionToConvert()
return "unity_Gradient_" + precision;
return GetType().GetMethod("Unity_Gradient", BindingFlags.NonPublic | BindingFlags.Instance);
public override bool hasPreview
{
get { return true; }
}
protected override string GetFunctionCall(GenerationMode generationMode)
string Unity_Gradient(
[Slot(0, Binding.None)] Vector1 value,
[Slot(1, Binding.None)] out Vector4 result)
//here we don't want to generate final value as the parameters are used as output
//TODO allow to define parameters as ouput in the function prototype and handle
//that automatically
return base.GetFunctionCall(GenerationMode.Preview);
}
result = Vector4.zero;
private void GNF(ShaderGenerator visitor, GenerationMode generationMode)
{
var outputString = new ShaderGenerator();
outputString.AddShaderChunk(GetFunctionPrototype(), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
StringBuilder outputString = new StringBuilder();
string start = @"
{
";
outputString.Append(start);
Color c;
float cp;
for (int i = 0; i < colorkeys.Length; i++)

outputString.AddShaderChunk("float3 color" + i + "=float3(" + c.r + "," + c.g + "," + c.b + ");", false);
outputString.AddShaderChunk("float colorp" + i + "=" + cp + ";", false);
outputString.AppendLine(string.Format("\t{{precision}}3 color{0}=float3({1},{2},{3});", i, c.r, c.g, c.b));
outputString.AppendLine(string.Format("\t{{precision}} colorp{0}={1};", i, cp));
outputString.AddShaderChunk("float3 gradcolor = color0;", false);
outputString.AppendLine("\t{precision}3 gradcolor = color0;");
outputString.AddShaderChunk("float colorLerpPosition" + i + "=smoothstep(colorp" + i + ",colorp" + j + ",v);", false);
outputString.AddShaderChunk("gradcolor = lerp(gradcolor,color" + j + ",colorLerpPosition" + i + ");", false);
outputString.AppendLine(string.Format("\t{{precision}} colorLerpPosition{0}=smoothstep(colorp{0},colorp{1},value);",i,j));
outputString.AppendLine(string.Format("\tgradcolor = lerp(gradcolor,color{0},colorLerpPosition{1});",j, i));
float a;
float ap;
for (int i = 0; i < alphakeys.Length; i++)

outputString.AddShaderChunk("float alpha" + i + "=" + a + ";", false);
outputString.AddShaderChunk("float alphap" + i + "=" + ap + ";", false);
outputString.AppendLine(string.Format("\t{{precision}} alpha{0}={1};", i, a));
outputString.AppendLine(string.Format("\t{{precision}} alphap{0}={1};", i, ap));
outputString.AddShaderChunk("float gradalpha = alpha0;", false);
outputString.AppendLine("\t{precision} gradalpha = alpha0;");
outputString.AddShaderChunk("float alphaLerpPosition" + i + "=smoothstep(alphap" + i + ",alphap" + j + ",v);", false);
outputString.AddShaderChunk("gradalpha = lerp(gradalpha,alpha" + j + ",alphaLerpPosition" + i + ");", false);
outputString.AppendLine(string.Format("\t{{precision}} alphaLerpPosition{0}=smoothstep(alphap{0},alphap{1},value);", i, j));
outputString.AppendLine(string.Format("\tgradalpha = lerp(gradalpha,alpha{0},alphaLerpPosition{1});",j, i));
outputString.AddShaderChunk("finalColor = float4(gradcolor,gradalpha);", false);
outputString.AddShaderChunk("finalR = finalColor.r;", false);
outputString.AddShaderChunk("finalG = finalColor.g;", false);
outputString.AddShaderChunk("finalB = finalColor.b;", false);
outputString.AddShaderChunk("finalA = finalColor.a;", false);
//End
outputString.Deindent();
outputString.AddShaderChunk("}", false);
visitor.AddShaderChunk(outputString.GetShaderString(0), true);
//yield return null;
}
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
{
GNF(visitor, generationMode);
outputString.AppendLine("\tresult = float4(gradcolor,gradalpha);");
outputString.AppendLine("}");
return outputString.ToString();
}
}
}
正在加载...
取消
保存