浏览代码

Add changes from previous branch

/main
Matt Dean 6 年前
当前提交
589cb99a
共有 24 个文件被更改,包括 722 次插入0 次删除
  1. 10
      com.unity.shadergraph/Editor/Data/Graphs/MaterialSlot.cs
  2. 18
      com.unity.shadergraph/Editor/Data/Graphs/PreviewProperty.cs
  3. 2
      com.unity.shadergraph/Editor/Data/Implementation/NodeUtils.cs
  4. 7
      com.unity.shadergraph/Editor/Data/Nodes/CodeFunctionNode.cs
  5. 1
      com.unity.shadergraph/Editor/Data/Nodes/PropertyType.cs
  6. 3
      com.unity.shadergraph/Editor/Data/Nodes/SlotValue.cs
  7. 3
      com.unity.shadergraph/Editor/Data/Nodes/Utility/SubGraphNode.cs
  8. 9
      com.unity.shadergraph/ShaderGraphLibrary/Functions.hlsl
  9. 79
      com.unity.shadergraph/Editor/Data/Graphs/GradientInputMaterialSlot.cs
  10. 11
      com.unity.shadergraph/Editor/Data/Graphs/GradientInputMaterialSlot.cs.meta
  11. 35
      com.unity.shadergraph/Editor/Data/Graphs/GradientMaterialSlot.cs
  12. 11
      com.unity.shadergraph/Editor/Data/Graphs/GradientMaterialSlot.cs.meta
  13. 131
      com.unity.shadergraph/Editor/Data/Graphs/GradientShaderProperty.cs
  14. 3
      com.unity.shadergraph/Editor/Data/Graphs/GradientShaderProperty.cs.meta
  15. 8
      com.unity.shadergraph/Editor/Data/Nodes/Input/Gradient.meta
  16. 100
      com.unity.shadergraph/Editor/Drawing/Controls/GradientControl.cs
  17. 11
      com.unity.shadergraph/Editor/Drawing/Controls/GradientControl.cs.meta
  18. 72
      com.unity.shadergraph/Editor/Drawing/Views/Slots/GradientSlotControlView.cs
  19. 3
      com.unity.shadergraph/Editor/Drawing/Views/Slots/GradientSlotControlView.cs.meta
  20. 109
      com.unity.shadergraph/Editor/Data/Nodes/Input/Gradient/GradientAssetNode.cs
  21. 11
      com.unity.shadergraph/Editor/Data/Nodes/Input/Gradient/GradientAssetNode.cs.meta
  22. 74
      com.unity.shadergraph/Editor/Data/Nodes/Input/Gradient/SampleGradientNode.cs
  23. 11
      com.unity.shadergraph/Editor/Data/Nodes/Input/Gradient/SampleGradientNode.cs.meta

10
com.unity.shadergraph/Editor/Data/Graphs/MaterialSlot.cs


return "(T)";
case ConcreteSlotValueType.Cubemap:
return "(C)";
case ConcreteSlotValueType.Gradient:
return "(G)";
default:
return "(E)";
}

return slotType == SlotType.Input
? new CubemapInputMaterialSlot(slotId, displayName, shaderOutputName, shaderStage, hidden)
: new CubemapMaterialSlot(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden);
case SlotValueType.Gradient:
return slotType == SlotType.Input
? new GradientInputMaterialSlot(slotId, displayName, shaderOutputName, shaderStage, hidden)
: new GradientMaterialSlot(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden);
case SlotValueType.DynamicVector:
return new DynamicVectorMaterialSlot(slotId, displayName, shaderOutputName, slotType, defaultValue, shaderStage, hidden);
case SlotValueType.Vector4:

return inputType == SlotValueType.Texture2D;
case SlotValueType.Cubemap:
return inputType == SlotValueType.Cubemap;
case SlotValueType.Gradient:
return inputType == SlotValueType.Gradient;
case SlotValueType.DynamicVector:
case SlotValueType.Vector4:
case SlotValueType.Vector3:

return PropertyType.Texture;
case ConcreteSlotValueType.Cubemap:
return PropertyType.Cubemap;
case ConcreteSlotValueType.Gradient:
return PropertyType.Gradient;
case ConcreteSlotValueType.Boolean:
return PropertyType.Boolean;
case ConcreteSlotValueType.Vector1:

18
com.unity.shadergraph/Editor/Data/Graphs/PreviewProperty.cs


[FieldOffset(0)]
public Cubemap cubemapValue;
[FieldOffset(0)]
public Gradient gradientValue;
[FieldOffset(0)]
public Vector4 vector4Value;
[FieldOffset(0)]
public float floatValue;

if (propType != PropertyType.Cubemap)
throw new ArgumentException(string.Format(k_SetErrorMessage, PropertyType.Cubemap, propType));
m_Data.cubemapValue = value;
}
}
public Gradient gradientValue
{
get
{
if (propType != PropertyType.Gradient)
throw new ArgumentException(string.Format(k_GetErrorMessage, PropertyType.Gradient, propType));
return m_Data.gradientValue;
}
set
{
if (propType != PropertyType.Gradient)
throw new ArgumentException(string.Format(k_SetErrorMessage, PropertyType.Gradient, propType));
m_Data.gradientValue = value;
}
}

2
com.unity.shadergraph/Editor/Data/Implementation/NodeUtils.cs


return "Texture2D";
case ConcreteSlotValueType.Cubemap:
return "Cubemap";
case ConcreteSlotValueType.Gradient:
return "Gradient";
case ConcreteSlotValueType.Matrix2:
return p + "2x2";
case ConcreteSlotValueType.Matrix3:

7
com.unity.shadergraph/Editor/Data/Nodes/CodeFunctionNode.cs


protected struct SamplerState
{}
protected struct Gradient
{}
protected struct DynamicDimensionVector
{}

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

1
com.unity.shadergraph/Editor/Data/Nodes/PropertyType.cs


Color,
Texture,
Cubemap,
Gradient,
Boolean,
Vector1,
Vector2,

3
com.unity.shadergraph/Editor/Data/Nodes/SlotValue.cs


Matrix2,
Texture2D,
Cubemap,
Gradient,
DynamicVector,
Vector4,
Vector3,

Matrix2,
Texture2D,
Cubemap,
Gradient,
Vector4,
Vector3,
Vector2,

"typeMatrix",
"typeTexture2D",
"typeCubemap",
"typeGradient",
"typeFloat4",
"typeFloat3",
"typeFloat2",

3
com.unity.shadergraph/Editor/Data/Nodes/Utility/SubGraphNode.cs


case PropertyType.Cubemap:
slotType = SlotValueType.Cubemap;
break;
case PropertyType.Gradient:
slotType = SlotValueType.Gradient;
break;
case PropertyType.Vector1:
slotType = SlotValueType.Vector1;
break;

9
com.unity.shadergraph/ShaderGraphLibrary/Functions.hlsl


return o;
}
struct Gradient
{
int type;
int colorsLength;
int alphasLength;
float4 colors[8];
float2 alphas[8];
};
#endif // UNITY_SHADER_GRAPH_INCLUDED

79
com.unity.shadergraph/Editor/Data/Graphs/GradientInputMaterialSlot.cs


using System;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Drawing.Slots;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class GradientInputMaterialSlot : GradientMaterialSlot
{
[SerializeField]
private Gradient m_Gradient = new Gradient();
public Gradient gradient
{
get { return m_Gradient; }
set { m_Gradient = value; }
}
public GradientInputMaterialSlot()
{
}
public GradientInputMaterialSlot(
int slotId,
string displayName,
string shaderOutputName,
ShaderStage shaderStage = ShaderStage.Dynamic,
bool hidden = false)
: base(slotId, displayName, shaderOutputName, SlotType.Input, shaderStage, hidden)
{
}
public override VisualElement InstantiateControl()
{
return new GradientSlotControlView(this);
}
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 string.Format("Unity{0}()", matOwner.GetVariableNameForSlot(id));
}
public override void AddDefaultProperty(PropertyCollector properties, 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)));
var prop = new GradientShaderProperty();
prop.overrideReferenceName = matOwner.GetVariableNameForSlot(id);
prop.generatePropertyBlock = false;
prop.value = gradient;
properties.AddShaderProperty(prop);
}
public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty(PropertyType.Gradient)
{
name = name,
gradientValue = gradient
};
return pp;
}
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
var slot = foundSlot as GradientInputMaterialSlot;
if (slot != null)
m_Gradient = slot.gradient;
}
}
}

11
com.unity.shadergraph/Editor/Data/Graphs/GradientInputMaterialSlot.cs.meta


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

35
com.unity.shadergraph/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 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)
{
}
}
}

11
com.unity.shadergraph/Editor/Data/Graphs/GradientMaterialSlot.cs.meta


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

131
com.unity.shadergraph/Editor/Data/Graphs/GradientShaderProperty.cs


using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.ShaderGraph.Drawing.Controls;
using UnityEngine;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
public static class GradientUtils
{
public static void GetGradientDeclaration(Gradient gradient, ref ShaderStringBuilder s)
{
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 < gradient.colorKeys.Length; i++)
colors[i] = GetColorKey(i, gradient.colorKeys[i].color, 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 < gradient.alphaKeys.Length; i++)
alphas[i] = GetAlphaKey(i, gradient.alphaKeys[i].alpha, gradient.alphaKeys[i].time);
s.AppendLine("Gradient g;");
s.AppendLine("g.type = 0;");
s.AppendLine("g.colorsLength = {0};",
gradient.colorKeys.Length);
s.AppendLine("g.alphasLength = {0};",
gradient.alphaKeys.Length);
for(int i = 0; i < colors.Length; i++)
s.AppendLine(colors[i]);
for(int i = 0; i < alphas.Length; i++)
s.AppendLine(alphas[i]);
}
public static bool CheckEquivalency(Gradient A, Gradient B)
{
var currentColorKeys = A.colorKeys;
var currentAlphaKeys = A.alphaKeys;
var newColorKeys = B.colorKeys;
var newAlphaKeys = B.alphaKeys;
if (currentColorKeys.Length != newColorKeys.Length || currentAlphaKeys.Length != newAlphaKeys.Length)
{
return false;
}
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)
return false;
}
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)
return false;
}
}
return true;
}
private static 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);
}
private static string GetAlphaKey(int index, float alpha, float time)
{
return string.Format("g.alphas[{0}] = float2({1}, {2});", index, alpha, time);
}
}
[Serializable]
public class GradientShaderProperty : AbstractShaderProperty<Gradient>
{
public GradientShaderProperty()
{
displayName = "Gradient";
value = new Gradient();
}
public override PropertyType propertyType
{
get { return PropertyType.Gradient; }
}
public override Vector4 defaultValue
{
get { return new Vector4(); }
}
public override string GetPropertyBlockString()
{
return string.Empty;
}
public override string GetPropertyDeclarationString(string delimiter = ";")
{
ShaderStringBuilder s = new ShaderStringBuilder();
s.AppendLine("Gradient Unity{0} ()", referenceName);
using (s.BlockScope())
{
GradientUtils.GetGradientDeclaration(value, ref s);
s.AppendLine("return g;", true);
}
return s.ToString();
}
public override PreviewProperty GetPreviewMaterialProperty()
{
return new PreviewProperty(PropertyType.Gradient)
{
name = referenceName,
gradientValue = value
};
}
public override INode ToConcreteNode()
{
return new GradientAssetNode { gradient = value };
}
}
}

3
com.unity.shadergraph/Editor/Data/Graphs/GradientShaderProperty.cs.meta


fileFormatVersion: 2
guid: 544078fab8a6b410f907cc3ae58d977f
timeCreated: 1505346949

8
com.unity.shadergraph/Editor/Data/Nodes/Input/Gradient.meta


fileFormatVersion: 2
guid: 6c3178830acf943f8bf39e5a651057f2
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

100
com.unity.shadergraph/Editor/Drawing/Controls/GradientControl.cs


using System;
using System.Reflection;
using UnityEngine;
using UnityEditor.Graphing;
using UnityEngine.Experimental.UIElements;
using UnityEditor.ShaderGraph;
namespace UnityEditor.ShaderGraph.Drawing.Controls
{
[AttributeUsage(AttributeTargets.Property)]
public class GradientControlAttribute : Attribute, IControlAttribute
{
string m_Label;
public GradientControlAttribute(string label = null)
{
m_Label = label;
}
public VisualElement InstantiateControl(AbstractMaterialNode node, PropertyInfo propertyInfo)
{
return new GradientControlView(m_Label, node, propertyInfo);
}
}
[Serializable]
public class GradientObject : ScriptableObject
{
public Gradient gradient = new Gradient();
}
public class GradientControlView : VisualElement, INodeModificationListener
{
GUIContent m_Label;
AbstractMaterialNode m_Node;
PropertyInfo m_PropertyInfo;
string m_PrevWindow = "";
[SerializeField]
GradientObject m_GradientObject;
[SerializeField]
SerializedObject m_SerializedObject;
[SerializeField]
SerializedProperty m_SerializedProperty;
IMGUIContainer m_Container;
public GradientControlView(string label, AbstractMaterialNode node, PropertyInfo propertyInfo)
{
m_Label = new GUIContent(label ?? ObjectNames.NicifyVariableName(propertyInfo.Name));
m_Node = node;
m_PropertyInfo = propertyInfo;
if (propertyInfo.PropertyType != typeof(Gradient))
throw new ArgumentException("Property must be of type Gradient.", "propertyInfo");
m_GradientObject = ScriptableObject.CreateInstance<GradientObject>();
m_GradientObject.gradient = new Gradient();
m_SerializedObject = new SerializedObject(m_GradientObject);
m_SerializedProperty = m_SerializedObject.FindProperty("gradient");
m_Container = new IMGUIContainer(OnGUIHandler);
Add(m_Container);
}
public void OnNodeModified(ModificationScope scope)
{
if (scope == ModificationScope.Graph)
m_Container.Dirty(ChangeType.Repaint);
}
void OnGUIHandler()
{
m_SerializedObject.Update();
var gradient = (Gradient)m_PropertyInfo.GetValue(m_Node, null);
m_GradientObject.gradient.SetKeys(gradient.colorKeys, gradient.alphaKeys);
using (var changeCheckScope = new EditorGUI.ChangeCheckScope())
{
EditorGUILayout.PropertyField(m_SerializedProperty, m_Label, true, null);
m_SerializedObject.ApplyModifiedProperties();
if (changeCheckScope.changed)
{
m_Node.owner.owner.RegisterCompleteObjectUndo("Change " + m_Node.name);
m_PropertyInfo.SetValue(m_Node, m_GradientObject.gradient, null);
}
}
var e = Event.current;
if (EditorWindow.focusedWindow != null && m_PrevWindow != EditorWindow.focusedWindow.ToString() && EditorWindow.focusedWindow.ToString() != "(UnityEditor.GradientPicker)")
{
m_PrevWindow = EditorWindow.focusedWindow.ToString();
}
}
}
}

11
com.unity.shadergraph/Editor/Drawing/Controls/GradientControl.cs.meta


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

72
com.unity.shadergraph/Editor/Drawing/Views/Slots/GradientSlotControlView.cs


using System;
using UnityEditor.Experimental.UIElements;
using UnityEditor.Graphing;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
using UnityEngine.Experimental.UIElements.StyleSheets;
using UnityEditor.ShaderGraph;
using UnityEditor.ShaderGraph.Drawing.Controls;
using Object = UnityEngine.Object;
namespace UnityEditor.ShaderGraph.Drawing.Slots
{
public class GradientSlotControlView : VisualElement, INodeModificationListener
{
GradientInputMaterialSlot m_Slot;
string m_PrevWindow = "";
[SerializeField]
GradientObject m_GradientObject;
[SerializeField]
SerializedObject m_SerializedObject;
[SerializeField]
SerializedProperty m_SerializedProperty;
IMGUIContainer m_Container;
public GradientSlotControlView(GradientInputMaterialSlot slot)
{
m_Slot = slot;
m_GradientObject = ScriptableObject.CreateInstance<GradientObject>();
m_GradientObject.gradient = new Gradient();
m_SerializedObject = new SerializedObject(m_GradientObject);
m_SerializedProperty = m_SerializedObject.FindProperty("gradient");
m_Container = new IMGUIContainer(OnGUIHandler);
Add(m_Container);
}
public void OnNodeModified(ModificationScope scope)
{
if (scope == ModificationScope.Graph)
m_Container.Dirty(ChangeType.Repaint);
}
void OnGUIHandler()
{
m_SerializedObject.Update();
m_GradientObject.gradient.SetKeys(m_Slot.gradient.colorKeys, m_Slot.gradient.alphaKeys);
using (var changeCheckScope = new EditorGUI.ChangeCheckScope())
{
EditorGUILayout.PropertyField(m_SerializedProperty, new GUIContent(""), true, null);
m_SerializedObject.ApplyModifiedProperties();
if (changeCheckScope.changed)
{
m_Slot.owner.owner.owner.RegisterCompleteObjectUndo("Change Gradient");
m_Slot.gradient = m_GradientObject.gradient;
m_Slot.owner.Dirty(ModificationScope.Graph);
m_Container.Dirty(ChangeType.Repaint);
}
}
var e = Event.current;
if (EditorWindow.focusedWindow != null && m_PrevWindow != EditorWindow.focusedWindow.ToString() && EditorWindow.focusedWindow.ToString() != "(UnityEditor.GradientPicker)")
{
m_PrevWindow = EditorWindow.focusedWindow.ToString();
}
}
}
}

3
com.unity.shadergraph/Editor/Drawing/Views/Slots/GradientSlotControlView.cs.meta


fileFormatVersion: 2
guid: e5c808b828bf44895b35be954f0906e5
timeCreated: 1510659384

109
com.unity.shadergraph/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, IGeneratesFunction
{
[SerializeField]
private float m_Value;
public const int OutputSlotId = 0;
private const string kOutputSlotName = "Out";
public GradientAssetNode()
{
name = "Gradient Asset";
UpdateNodeAfterDeserialization();
}
string GetFunctionName()
{
return string.Format("Unity_{0}", GetVariableNameForNode());
}
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;
if(!GradientUtils.CheckEquivalency(m_Gradient, value))
scope = scope < ModificationScope.Graph ? ModificationScope.Graph : scope;
if (scope > ModificationScope.Nothing)
{
var newColorKeys = value.colorKeys;
var newAlphaKeys = value.alphaKeys;
gradient.SetKeys(newColorKeys, newAlphaKeys);
Dirty(ModificationScope.Graph);
}
}
}
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 GenerateNodeFunction(FunctionRegistry registry, GenerationMode generationMode)
{
registry.ProvideFunction(GetFunctionName(), s =>
{
s.AppendLine("Gradient {0} ()",
GetFunctionName(),
FindOutputSlot<MaterialSlot>(OutputSlotId).concreteValueType.ToString(precision));
using (s.BlockScope())
{
GradientUtils.GetGradientDeclaration(m_Gradient, ref s);
s.AppendLine("return g;", true);
}
});
}
public override string GetVariableNameForSlot(int slotId)
{
return string.Format("{0}()", GetFunctionName());
}
}
}

11
com.unity.shadergraph/Editor/Data/Nodes/Input/Gradient/GradientAssetNode.cs.meta


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

74
com.unity.shadergraph/Editor/Data/Nodes/Input/Gradient/SampleGradientNode.cs


using System.Reflection;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Title("Input", "Gradient", "Sample Gradient")]
public class SampleGradient : CodeFunctionNode, IGeneratesBodyCode
{
public SampleGradient()
{
name = "Sample Gradient";
}
public override bool hasPreview
{
get { return true; }
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_SampleGradient", BindingFlags.Static | BindingFlags.NonPublic);
}
static string Unity_SampleGradient(
[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 = min(1, max(0, (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 = min(1, max(0, (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);
}
";
}
}
}

11
com.unity.shadergraph/Editor/Data/Nodes/Input/Gradient/SampleGradientNode.cs.meta


fileFormatVersion: 2
guid: 5b12835ccaa954dc3ad06e9958e1e740
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存