浏览代码

First pass Gradient Nodes

- Added ShaderGraph.cginc
- Added Gradient Type
- Added GradientMaterialSlot
- Added Gradient SlotValueType
- Added Gradient ConcreteSlotValueType
- Added GradientAsset node
- Added Gradient2D node
- Added Gradient PropertyType
/main
Matt Dean 7 年前
当前提交
516051d4
共有 16 个文件被更改,包括 420 次插入1 次删除
  1. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/AbstractMaterialGraph.cs
  2. 35
      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/HLSLNode.cs
  5. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/GradientNodeOld.cs
  6. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/PropertyType.cs
  7. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/SlotValue.cs
  8. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/AbstractSubGraphNode.cs
  9. 9
      MaterialGraphProject/Assets/ShaderGraph.cginc
  10. 9
      MaterialGraphProject/Assets/ShaderGraph.cginc.meta
  11. 78
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/Gradient2DNode.cs
  12. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/Gradient2DNode.cs.meta
  13. 250
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/GradientAssetNode.cs
  14. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/GradientAssetNode.cs.meta
  15. 0
      /MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/GradientNodeOld.cs.meta
  16. 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/ShaderGraph.cginc\"", false);
finalShader.AddShaderChunk(shaderFunctionVisitor.GetShaderString(2), false);
finalShader.AddShaderChunk(graphVertexInput, false);
finalShader.AddShaderChunk(surfaceInputs.GetShaderString(2), false);

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


}
[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; } }
}
[Serializable]
public class SamplerStateMaterialSlot : MaterialSlot
{
public SamplerStateMaterialSlot()

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:

if (concreteValueType == ConcreteSlotValueType.Texture2D)
return Texture2DMaterialSlot.DefaultTextureName;
if (concreteValueType == ConcreteSlotValueType.Gradient)
return GradientMaterialSlot.DefaultGradientName;
if (generationMode.IsPreview())
return matOwner.GetVariableNameForSlot(id);

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/HLSLNode.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);
}
}
*/
}

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();
}

9
MaterialGraphProject/Assets/ShaderGraph.cginc


// Gradient Type
struct Gradient
{
int type;
int colorsLength;
int alphasLength;
float4 colors[8];
float2 alphas[8];
};

9
MaterialGraphProject/Assets/ShaderGraph.cginc.meta


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

78
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
{
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);
}
static string Unity_Gradient2D(
[Slot(0, Binding.None)] Gradient g,
[Slot(1, Binding.None)] Vector1 Time,
[Slot(2, Binding.None)] out Vector4 Out)
{
Out = Vector4.zero;
return
@"
{
int colorKey1 = 0;
int colorKey2 = g.colorsLength-1;
for(int i = 0; i < g.colorsLength; i++)
{
if(g.colors[i].w <= Time)
colorKey1 = i;
else
break;
}
for(int i = g.colorsLength-1; i >= 0; i--)
{
if(g.colors[i].w >= Time)
colorKey2 = i;
else
break;
}
int alphaKey1 = 0;
int alphaKey2 = g.alphasLength-1;
for(int i = 0; i < g.alphasLength; i++)
{
if(g.alphas[i].y <= Time)
alphaKey1 = i;
else
break;
}
for(int i = g.alphasLength-1; i >= 0; i--)
{
if(g.alphas[i].y >= Time)
alphaKey2 = i;
else
break;
}
float colorPos = (Time - g.colors[colorKey1].w) / (g.colors[colorKey2].w - g.colors[colorKey1].w);
float3 color = lerp(g.colors[colorKey1].rgb, g.colors[colorKey2].rgb, colorPos);
float alphaPos = (Time - g.alphas[alphaKey1].y) / (g.alphas[alphaKey2].y - g.alphas[alphaKey1].y);
float alpha = lerp(g.alphas[alphaKey1].r, g.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:

250
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();
}
string GetColorKeyName(int index)
{
return string.Format("{0}_ck{1}", GetVariableNameForNode(), index);
}
string GetAlphaKeyName(int index)
{
return string.Format("{0}_ak{1}", GetVariableNameForNode(), index);
}
public override void CollectShaderProperties(PropertyCollector properties, GenerationMode generationMode)
{
base.CollectShaderProperties(properties, generationMode);
var colorKeys = m_Gradient.colorKeys;
var alphaKeys = m_Gradient.alphaKeys;
for (var i = 0; i < colorKeys.Length; i++)
{
var colorKey = colorKeys[i];
properties.AddShaderProperty(new Vector4ShaderProperty
{
overrideReferenceName = GetColorKeyName(i),
generatePropertyBlock = false,
value = new Vector4(colorKey.color.r, colorKey.color.g, colorKey.color.b, colorKey.time)
});
}
for (var i = 0; i < alphaKeys.Length; i++)
{
properties.AddShaderProperty(new Vector2ShaderProperty
{
overrideReferenceName = GetAlphaKeyName(i),
generatePropertyBlock = false,
value = new Vector2(alphaKeys[i].alpha, alphaKeys[i].time)
});
}
}
public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
{
base.CollectPreviewMaterialProperties(properties);
var colorKeys = m_Gradient.colorKeys;
var alphaKeys = m_Gradient.alphaKeys;
for (var i = 0; i < colorKeys.Length; i++)
{
var colorKey = colorKeys[i];
properties.Add(new PreviewProperty()
{
m_Name = GetColorKeyName(i),
m_PropType = PropertyType.Vector4,
m_Vector4 = new Vector4(colorKey.color.r, colorKey.color.g, colorKey.color.b, colorKey.time)
});
}
for (var i = 0; i < alphaKeys.Length; i++)
{
properties.Add(new PreviewProperty
{
m_Name = GetAlphaKeyName(i),
m_PropType = PropertyType.Vector2,
m_Vector4 = new Vector2(alphaKeys[i].alpha, alphaKeys[i].time)
});
}
}
/// -------------------------------
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)
{
//if (generationMode.IsPreview())
// return;
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)
{
//if (generationMode.IsPreview())
// return;
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("g.colors[0] = float4(1,0,0,0);", true);
visitor.AddShaderChunk("g.colors[1] = float4(0,1,0,1);", true);
visitor.AddShaderChunk("g.colors[2] = float4(1,0,0,0);", true);
visitor.AddShaderChunk("g.colors[3] = float4(0,1,0,1);", true);
visitor.AddShaderChunk("g.colors[4] = float4(1,0,0,0);", true);
visitor.AddShaderChunk("g.colors[5] = float4(0,1,0,1);", true);
visitor.AddShaderChunk("g.colors[6] = float4(1,0,0,0);", true);
visitor.AddShaderChunk("g.colors[7] = float4(0,1,0,1);", true);
visitor.AddShaderChunk("g.alphas[0] = float2(1,0);", true);
visitor.AddShaderChunk("g.alphas[1] = float2(1,1);", true);
visitor.AddShaderChunk("g.alphas[2] = float2(1,0);", true);
visitor.AddShaderChunk("g.alphas[3] = float2(1,1);", true);
visitor.AddShaderChunk("g.alphas[4] = float2(1,0);", true);
visitor.AddShaderChunk("g.alphas[5] = float2(1,1);", true);
visitor.AddShaderChunk("g.alphas[6] = float2(1,0);", true);
visitor.AddShaderChunk("g.alphas[7] = float2(1,1);", 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:

/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

正在加载...
取消
保存