浏览代码

Input Part 6 (Gradient)

- Merge branch Gradient
- Added Gradient MaterialSlot and ShaderProperty
- Added defaulty handling
- Fixed SamplerState variable name bug
/main
Matt Dean 7 年前
当前提交
324ba49c
共有 21 个文件被更改,包括 446 次插入2 次删除
  1. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/AbstractMaterialGraph.cs
  2. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/MaterialSlot.cs
  3. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/AbstractMaterialNode.cs
  4. 7
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/CodeFunctionNode.cs
  5. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/GradientNodeOld.cs
  6. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/SamplerStateNode.cs
  7. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/PropertyType.cs
  8. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/SlotValue.cs
  9. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/AbstractSubGraphNode.cs
  10. 46
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/GradientMaterialSlot.cs
  11. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/GradientMaterialSlot.cs.meta
  12. 58
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/GradientShaderProperty.cs
  13. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/GradientShaderProperty.cs.meta
  14. 81
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/Gradient2DNode.cs
  15. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/Gradient2DNode.cs.meta
  16. 156
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/GradientAssetNode.cs
  17. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/GradientAssetNode.cs.meta
  18. 34
      MaterialGraphProject/Assets/UnityShaderEditor/ShaderGraph.cginc
  19. 9
      MaterialGraphProject/Assets/UnityShaderEditor/ShaderGraph.cginc.meta
  20. 0
      /MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/GradientNodeOld.cs.meta
  21. 0
      /MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/GradientNodeOld.cs

1
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/AbstractMaterialGraph.cs


finalShader.AddShaderChunk("CGINCLUDE", false);
finalShader.AddShaderChunk("#include \"UnityCG.cginc\"", false);
finalShader.AddShaderChunk("#include \"//Assets/UnityShaderEditor/ShaderGraph.cginc\"", false);
finalShader.AddShaderChunk(shaderFunctionVisitor.GetShaderString(2), false);
finalShader.AddShaderChunk(graphVertexInput, false);
finalShader.AddShaderChunk(surfaceInputs.GetShaderString(2), false);

8
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/MaterialSlot.cs


return "(SS)";
case ConcreteSlotValueType.Texture2D:
return "(T)";
case ConcreteSlotValueType.Gradient:
return "(G)";
default:
return "(E)";
}

{
switch (type)
{
case SlotValueType.Gradient:
return new GradientMaterialSlot(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden);
case SlotValueType.SamplerState:
return new SamplerStateMaterialSlot(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden);
case SlotValueType.Matrix4:

{
switch (concreteValueType)
{
case ConcreteSlotValueType.Gradient:
return inputType == ConcreteSlotValueType.Gradient;
case ConcreteSlotValueType.SamplerState:
return inputType == ConcreteSlotValueType.SamplerState;
case ConcreteSlotValueType.Matrix4:

return PropertyType.Matrix4;
case ConcreteSlotValueType.SamplerState:
return PropertyType.SamplerState;
case ConcreteSlotValueType.Gradient:
return PropertyType.Gradient;
default:
return PropertyType.Vector4;
}

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/AbstractMaterialNode.cs


return "Matrix4x4";
case ConcreteSlotValueType.SamplerState:
return "SamplerState";
case ConcreteSlotValueType.Gradient:
return "Gradient";
default:
return "Error";
}

7
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/CodeFunctionNode.cs


protected struct DynamicDimensionVector
{}
protected struct Gradient
{}
protected enum Binding
{
None,

if (t == typeof(SamplerState))
{
return SlotValueType.SamplerState;
}
if (t == typeof(Gradient))
{
return SlotValueType.Gradient;
}
if (t == typeof(DynamicDimensionVector))
{

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/GradientNodeOld.cs


namespace UnityEditor.ShaderGraph
{
/*
[Title("Input/Gradient/Gradient 2D")]
public class GradientNode : AbstractMaterialNode, IGeneratesBodyCode
{

visitor.AddShaderChunk(string.Format("{0} {1} = {2}.a;", precision, GetVariableNameForSlot(AOutputSlotId), rgbaOutputName), false);
}
}
*/
}

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/SamplerStateNode.cs


public override string GetVariableNameForNode()
{
string ss = name + "_"
string ss = GetHLSLSafeName(name) + "_"
+ Enum.GetName(typeof(TextureSamplerState.FilterMode), filter) + "_"
+ Enum.GetName(typeof(TextureSamplerState.WrapMode), wrap) + "_sampler";
return ss;

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/PropertyType.cs


Matrix2,
Matrix3,
Matrix4,
SamplerState
SamplerState,
Gradient
}
}

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/SlotValue.cs


[Serializable]
public enum SlotValueType
{
Gradient,
SamplerState,
Matrix4,
Matrix3,

public enum ConcreteSlotValueType
{
Gradient,
SamplerState,
Matrix4,
Matrix3,

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/AbstractSubGraphNode.cs


case PropertyType.Matrix4:
slotType = SlotValueType.Matrix4;
break;
case PropertyType.Gradient:
slotType = SlotValueType.Gradient;
break;
default:
throw new ArgumentOutOfRangeException();
}

46
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/GradientMaterialSlot.cs


using System;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class GradientMaterialSlot : MaterialSlot
{
public GradientMaterialSlot()
{
}
public GradientMaterialSlot(
int slotId,
string displayName,
string shaderOutputName,
SlotType slotType,
ShaderStage shaderStage = ShaderStage.Dynamic,
bool hidden = false)
: base(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden)
{
}
public static readonly string DefaultGradientName = "ShaderGraph_DefaultGradient()";
public override SlotValueType valueType { get { return SlotValueType.Gradient; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Gradient; } }
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{
}
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
}
public override string GetDefaultValue(GenerationMode generationMode)
{
var matOwner = owner as AbstractMaterialNode;
if (matOwner == null)
throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
return DefaultGradientName;
}
}
}

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/GradientMaterialSlot.cs.meta


fileFormatVersion: 2
guid: 0aac1b4e9fc8a1e4999e9069cf862832
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

58
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/GradientShaderProperty.cs


using System;
using System.Text;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class GradientShaderProperty : AbstractShaderProperty<Gradient>
{
[SerializeField]
private bool m_Modifiable = true;
public GradientShaderProperty()
{
value = new Gradient();
}
public override PropertyType propertyType
{
get { return PropertyType.Gradient; }
}
public bool modifiable
{
get { return m_Modifiable; }
set { m_Modifiable = value; }
}
public override Vector4 defaultValue
{
get { return new Vector4(); }
}
public override string GetPropertyBlockString()
{
return "";
}
public override string GetPropertyDeclarationString()
{
return "";
}
public override string GetInlinePropertyDeclarationString()
{
return "Gradient ShaderGraph_DefaultGradient;";
}
public override PreviewProperty GetPreviewMaterialProperty()
{
return new PreviewProperty()
{
m_Name = referenceName,
m_PropType = PropertyType.Gradient
};
}
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/GradientShaderProperty.cs.meta


fileFormatVersion: 2
guid: 186ecc17664ca614d9663a3fa18dede9
timeCreated: 1505346949

81
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/Gradient2DNode.cs


using System.Reflection;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Title("Input/Gradient/Gradient 2D")]
public class Gradient2DNode : CodeFunctionNode, IGeneratesBodyCode
{
public Gradient2DNode()
{
name = "Gradient 2D";
}
public override bool hasPreview
{
get { return true; }
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_Gradient2D", BindingFlags.Static | BindingFlags.NonPublic);
}
//visitor.AddShaderChunk("Gradient ShaderGraph_DefaultGradient;", true);
//visitor.AddShaderChunk("DefaultGradient(ShaderGraph_DefaultGradient);", true);
static string Unity_Gradient2D(
[Slot(0, Binding.None)] Gradient Gradient,
[Slot(1, Binding.None)] Vector1 Time,
[Slot(2, Binding.None)] out Vector4 Out)
{
Out = Vector4.zero;
return
@"
{
int colorKey1 = 0;
int colorKey2 = Gradient.colorsLength-1;
for(int c1 = 0; c1 < Gradient.colorsLength; c1++)
{
if(Gradient.colors[c1].w <= Time)
colorKey1 = c1;
else
break;
}
for(int c2 = Gradient.colorsLength-1; c2 >= 0; c2--)
{
if(Gradient.colors[c2].w >= Time)
colorKey2 = c2;
else
break;
}
int alphaKey1 = 0;
int alphaKey2 = Gradient.alphasLength-1;
for(int a1 = 0; a1 < Gradient.alphasLength; a1++)
{
if(Gradient.alphas[a1].y <= Time)
alphaKey1 = a1;
else
break;
}
for(int a2 = Gradient.alphasLength-1; a2 >= 0; a2--)
{
if(Gradient.alphas[a2].y >= Time)
alphaKey2 = a2;
else
break;
}
float colorPos = (Time - Gradient.colors[colorKey1].w) / (Gradient.colors[colorKey2].w - Gradient.colors[colorKey1].w);
float3 color = lerp(Gradient.colors[colorKey1].rgb, Gradient.colors[colorKey2].rgb, colorPos);
float alphaPos = (Time - Gradient.alphas[alphaKey1].y) / (Gradient.alphas[alphaKey2].y - Gradient.alphas[alphaKey1].y);
float alpha = lerp(Gradient.alphas[alphaKey1].r, Gradient.alphas[alphaKey2].r, alphaPos);
Out = float4(color, alpha);
}
";
}
}
}

8
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/Gradient2DNode.cs.meta


fileFormatVersion: 2
guid: a74d6bcb761144a2492e3c46e09d2689
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

156
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/GradientAssetNode.cs


using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.ShaderGraph.Drawing.Controls;
using UnityEngine;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
[Title("Input/Gradient/Gradient Asset")]
public class GradientAssetNode : AbstractMaterialNode, IGeneratesBodyCode, IGeneratesFunction
{
[SerializeField]
private float m_Value;
public const int OutputSlotId = 0;
private const string kOutputSlotName = "Out";
public GradientAssetNode()
{
name = "Gradient Asset";
UpdateNodeAfterDeserialization();
}
Gradient m_Gradient = new Gradient();
[SerializeField]
Vector4[] m_SerializableColorKeys = { new Vector4(1f, 1f, 1f, 0f), new Vector4(0f, 0f, 0f, 1f), };
[SerializeField]
Vector2[] m_SerializableAlphaKeys = { new Vector2(1f, 0f), new Vector2(1f, 1f) };
[GradientControl("")]
public Gradient gradient
{
get
{
return m_Gradient;
}
set
{
var scope = ModificationScope.Nothing;
var currentColorKeys = m_Gradient.colorKeys;
var currentAlphaKeys = m_Gradient.alphaKeys;
var newColorKeys = value.colorKeys;
var newAlphaKeys = value.alphaKeys;
if (currentColorKeys.Length != newColorKeys.Length || currentAlphaKeys.Length != newAlphaKeys.Length)
{
scope = scope < ModificationScope.Graph ? ModificationScope.Graph : scope;
}
else
{
for (var i = 0; i < currentColorKeys.Length; i++)
{
if (currentColorKeys[i].color != newColorKeys[i].color || Mathf.Abs(currentColorKeys[i].time - newColorKeys[i].time) > 1e-9)
scope = scope < ModificationScope.Node ? ModificationScope.Node : scope;
}
for (var i = 0; i < currentAlphaKeys.Length; i++)
{
if (Mathf.Abs(currentAlphaKeys[i].alpha - newAlphaKeys[i].alpha) > 1e-9 || Mathf.Abs(currentAlphaKeys[i].time - newAlphaKeys[i].time) > 1e-9)
scope = scope < ModificationScope.Node ? ModificationScope.Node : scope;
}
}
if (scope > ModificationScope.Nothing)
{
gradient.SetKeys(newColorKeys, newAlphaKeys);
if (onModified != null)
onModified(this, scope);
}
}
}
public override void OnAfterDeserialize()
{
base.OnAfterDeserialize();
m_Gradient = new Gradient();
var colorKeys = m_SerializableColorKeys.Select(k => new GradientColorKey(new Color(k.x, k.y, k.z, 1f), k.w)).ToArray();
var alphaKeys = m_SerializableAlphaKeys.Select(k => new GradientAlphaKey(k.x, k.y)).ToArray();
m_SerializableAlphaKeys = null;
m_SerializableColorKeys = null;
m_Gradient.SetKeys(colorKeys, alphaKeys);
}
public override void OnBeforeSerialize()
{
base.OnBeforeSerialize();
m_SerializableColorKeys = gradient.colorKeys.Select(k => new Vector4(k.color.r, k.color.g, k.color.b, k.time)).ToArray();
m_SerializableAlphaKeys = gradient.alphaKeys.Select(k => new Vector2(k.alpha, k.time)).ToArray();
}
public override bool hasPreview { get { return false; } }
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new GradientMaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output,0));
RemoveSlotsNameNotMatching(new[] { OutputSlotId });
}
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)
{
visitor.AddShaderChunk("Gradient " + GetVariableNameForNode() + ";", true);
visitor.AddShaderChunk(string.Format("Unity_{0} ({0});", GetVariableNameForNode()), true);
}
string GetColorKey(int index, Color color, float time)
{
return string.Format("g.colors[{0}] = float4({1}, {2}, {3}, {4});", index, color.r, color.g, color.b, time);
}
string GetAlphaKey(int index, float alpha, float time)
{
return string.Format("g.alphas[{0}] = float2({1}, {2});", index, alpha, time);
}
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
{
string[] colors = new string[8];
for(int i = 0; i < colors.Length; i++)
colors[i] = string.Format("g.colors[{0}] = float4(0, 0, 0, 0);", i.ToString());
for(int i = 0; i < m_Gradient.colorKeys.Length; i++)
colors[i] = GetColorKey(i, m_Gradient.colorKeys[i].color, m_Gradient.colorKeys[i].time);
string[] alphas = new string[8];
for(int i = 0; i < colors.Length; i++)
alphas[i] = string.Format("g.alphas[{0}] = float2(0, 0);", i.ToString());
for(int i = 0; i < m_Gradient.alphaKeys.Length; i++)
alphas[i] = GetAlphaKey(i, m_Gradient.alphaKeys[i].alpha, m_Gradient.alphaKeys[i].time);
visitor.AddShaderChunk(string.Format("void Unity_{0} (out Gradient Out)", GetVariableNameForNode()), true);
visitor.AddShaderChunk("{", true);
visitor.AddShaderChunk("Gradient g;", true);
visitor.AddShaderChunk("g.type = 0;", true);
visitor.AddShaderChunk(string.Format("g.colorsLength = {0};", m_Gradient.colorKeys.Length), true);
visitor.AddShaderChunk(string.Format("g.alphasLength = {0};", m_Gradient.alphaKeys.Length), true);
for(int i = 0; i < colors.Length; i++)
visitor.AddShaderChunk(colors[i], true);
for(int i = 0; i < alphas.Length; i++)
visitor.AddShaderChunk(alphas[i], true);
visitor.AddShaderChunk("Out = g;", true);
visitor.AddShaderChunk("}", true);
}
public override string GetVariableNameForSlot(int slotId)
{
return GetVariableNameForNode();
}
}
}

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/GradientAssetNode.cs.meta


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

34
MaterialGraphProject/Assets/UnityShaderEditor/ShaderGraph.cginc


// Gradient Type
struct Gradient
{
int type;
int colorsLength;
int alphasLength;
float4 colors[8];
float2 alphas[8];
};
Gradient ShaderGraph_DefaultGradient()
{
Gradient g;
g.type = 0;
g.colorsLength = 2;
g.alphasLength = 2;
g.colors[0] = float4(0, 0, 0, 0);
g.colors[1] = float4(1, 1, 1, 1);
g.colors[2] = float4(0, 0, 0, 0);
g.colors[3] = float4(0, 0, 0, 0);
g.colors[4] = float4(0, 0, 0, 0);
g.colors[5] = float4(0, 0, 0, 0);
g.colors[6] = float4(0, 0, 0, 0);
g.colors[7] = float4(0, 0, 0, 0);
g.alphas[0] = float2(1, 0);
g.alphas[1] = float2(1, 1);
g.alphas[2] = float2(0, 0);
g.alphas[3] = float2(0, 0);
g.alphas[4] = float2(0, 0);
g.alphas[5] = float2(0, 0);
g.alphas[6] = float2(0, 0);
g.alphas[7] = float2(0, 0);
return g;
}

9
MaterialGraphProject/Assets/UnityShaderEditor/ShaderGraph.cginc.meta


fileFormatVersion: 2
guid: 2a7388374397c47f1a598ae9ea3f2069
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/GradientNode.cs.meta → /MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/GradientNodeOld.cs.meta

/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/GradientNode.cs → /MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/GradientNodeOld.cs

正在加载...
取消
保存