Tim Cooper
8 年前
当前提交
5e6630fb
共有 12 个文件被更改,包括 241 次插入 和 572 次删除
-
87MaterialGraphProject/Assets/Andre/Nodes/LightProbeNode.cs
-
107MaterialGraphProject/Assets/Andre/Nodes/ReflectionProbeNode.cs
-
4MaterialGraphProject/Assets/Eduardo/ChannelBlendNode.cs
-
91MaterialGraphProject/Assets/Eduardo/HeightToNormalNode.cs
-
61MaterialGraphProject/Assets/Rinaldo/FractalNode.cs
-
8MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/GradientNodePresenter.cs
-
7MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/HLSLNode.cs
-
166MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Math/Range/RemapNode.cs
-
2MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Procedural/HexNode.cs
-
116MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Procedural/VoronoiNoise.cs
-
66MaterialGraphProject/Assets/_MingWai/ColorBalanceNode.cs
-
98MaterialGraphProject/Assets/_MingWai/GradientNode.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; |
|||
} |
|||
";
|
|||
} |
|||
} |
|||
} |
|
|||
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); |
|||
} |
|||
";
|
|||
} |
|||
} |
|||
} |
|
|||
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); |
|||
} |
|||
} |
|||
} |
|
|||
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; |
|||
} |
|||
";
|
|||
} |
|||
} |
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue