浏览代码
First pass Gradient Nodes
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 次删除
-
1MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/AbstractMaterialGraph.cs
-
35MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/MaterialSlot.cs
-
2MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/AbstractMaterialNode.cs
-
7MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/HLSLNode.cs
-
2MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/GradientNodeOld.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
-
9MaterialGraphProject/Assets/ShaderGraph.cginc
-
9MaterialGraphProject/Assets/ShaderGraph.cginc.meta
-
78MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/Gradient2DNode.cs
-
8MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/Gradient2DNode.cs.meta
-
250MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/GradientAssetNode.cs
-
12MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/GradientAssetNode.cs.meta
-
0/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/GradientNodeOld.cs.meta
-
0/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Gradient/GradientNodeOld.cs
|
|||
// Gradient Type |
|||
struct Gradient |
|||
{ |
|||
int type; |
|||
int colorsLength; |
|||
int alphasLength; |
|||
float4 colors[8]; |
|||
float2 alphas[8]; |
|||
}; |
|
|||
fileFormatVersion: 2 |
|||
guid: 2a7388374397c47f1a598ae9ea3f2069 |
|||
ShaderImporter: |
|||
externalObjects: {} |
|||
defaultTextures: [] |
|||
nonModifiableTextures: [] |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
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); |
|||
} |
|||
";
|
|||
} |
|||
} |
|||
} |
|
|||
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(); |
|||
} |
|||
|
|||
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(); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 8a8144c53ee5f4f479dc46b7c8eabc78 |
|||
timeCreated: 1445864587 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue