浏览代码
Input Part 6 (Gradient)
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 次删除
-
1MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/AbstractMaterialGraph.cs
-
8MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/MaterialSlot.cs
-
2MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/AbstractMaterialNode.cs
-
7MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/CodeFunctionNode.cs
-
2MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/GradientNodeOld.cs
-
2MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/SamplerStateNode.cs
-
3MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/PropertyType.cs
-
2MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/SlotValue.cs
-
3MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/AbstractSubGraphNode.cs
-
46MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/GradientMaterialSlot.cs
-
11MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/GradientMaterialSlot.cs.meta
-
58MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/GradientShaderProperty.cs
-
3MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/GradientShaderProperty.cs.meta
-
81MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/Gradient2DNode.cs
-
8MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/Gradient2DNode.cs.meta
-
156MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/GradientAssetNode.cs
-
12MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/GradientAssetNode.cs.meta
-
34MaterialGraphProject/Assets/UnityShaderEditor/ShaderGraph.cginc
-
9MaterialGraphProject/Assets/UnityShaderEditor/ShaderGraph.cginc.meta
-
0/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/GradientNodeOld.cs.meta
-
0/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/GradientNodeOld.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; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 0aac1b4e9fc8a1e4999e9069cf862832 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
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 |
|||
}; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 186ecc17664ca614d9663a3fa18dede9 |
|||
timeCreated: 1505346949 |
|
|||
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); |
|||
} |
|||
";
|
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: a74d6bcb761144a2492e3c46e09d2689 |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|
|||
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(); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 8a8144c53ee5f4f479dc46b7c8eabc78 |
|||
timeCreated: 1445864587 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
// 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; |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 2a7388374397c47f1a598ae9ea3f2069 |
|||
ShaderImporter: |
|||
externalObjects: {} |
|||
defaultTextures: [] |
|||
nonModifiableTextures: [] |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue