浏览代码

Merge branch 'master' into node-validation

/main
Matt Dean 7 年前
当前提交
41b314d2
共有 63 个文件被更改,包括 1943 次插入1603 次删除
  1. 3
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Implementation/SerializableGraph.cs
  2. 12
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Implementation/SerializableGraphObject.cs
  3. 15
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Implementation/SerializableSlot.cs
  4. 2
      MaterialGraphProject/Assets/NewNodes/Editor/Keep/UVTriPlanar.cs
  5. 458
      MaterialGraphProject/Assets/PartyPreview.ShaderGraph
  6. 485
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/MaterialSlot.cs
  7. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SerializableTexture.cs
  8. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/TextureShaderProperty.cs
  9. 25
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/AbstractMaterialNode.cs
  10. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Art/Filters/ConvolutionFilterNode.cs
  11. 219
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/TextureNode.cs
  12. 34
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/TextureSamplerNode.cs
  13. 64
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/PropertyNode.cs
  14. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/AbstractSubGraphNode.cs
  15. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/SubGraphNode.cs
  16. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SurfaceModel/ProjectSpecificMasterNode.cs
  17. 9
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/IMGUISlotEditorView.cs
  18. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/MaterialNodeView.cs
  19. 28
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/BitangentMaterialSlot.cs
  20. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/BitangentMaterialSlot.cs.meta
  21. 114
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/DynamicVectorMaterialSlot.cs
  22. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/DynamicVectorMaterialSlot.cs.meta
  23. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/IMaterialSlotHasVaule.cs
  24. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/IMaterialSlotHasVaule.cs.meta
  25. 43
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix2MaterialSlot.cs
  26. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix2MaterialSlot.cs.meta
  27. 42
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix3MaterialSlot.cs
  28. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix3MaterialSlot.cs.meta
  29. 43
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix4MaterialSlot.cs
  30. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix4MaterialSlot.cs.meta
  31. 31
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/NormalMaterialSlot.cs
  32. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/NormalMaterialSlot.cs.meta
  33. 30
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/PositionMaterialSlot.cs
  34. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/PositionMaterialSlot.cs.meta
  35. 35
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SamplerStateMaterialSlot.cs
  36. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SamplerStateMaterialSlot.cs.meta
  37. 25
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/ScreenPositionMaterialSlot.cs
  38. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/ScreenPositionMaterialSlot.cs.meta
  39. 36
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SpaceMaterialSlot.cs
  40. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SpaceMaterialSlot.cs.meta
  41. 30
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/TangentMaterialSlot.cs
  42. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/TangentMaterialSlot.cs.meta
  43. 72
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Texture2DInputMaterialSlot.cs
  44. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Texture2DInputMaterialSlot.cs.meta
  45. 31
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Texture2DMaterialSlot.cs
  46. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Texture2DMaterialSlot.cs.meta
  47. 48
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/UVMaterialSlot.cs
  48. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/UVMaterialSlot.cs.meta
  49. 87
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector1MaterialSlot.cs
  50. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector1MaterialSlot.cs.meta
  51. 87
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector2MaterialSlot.cs
  52. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector2MaterialSlot.cs.meta
  53. 87
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector3MaterialSlot.cs
  54. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector3MaterialSlot.cs.meta
  55. 88
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector4MaterialSlot.cs
  56. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector4MaterialSlot.cs.meta
  57. 25
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/VertexColorMaterialSlot.cs
  58. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/VertexColorMaterialSlot.cs.meta
  59. 30
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/ViewDirectionMaterialSlot.cs
  60. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/ViewDirectionMaterialSlot.cs.meta
  61. 432
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/CodeFunctionNode.cs
  62. 507
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/HLSLNode.cs
  63. 0
      /MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/CodeFunctionNode.cs.meta

3
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Implementation/SerializableGraph.cs


m_SerializableEdges = null;
foreach (var edge in m_Edges)
AddEdgeToNodeEdges(edge);
OnEnable();
ValidateGraph();
}
public virtual void ValidateGraph()

12
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Implementation/SerializableGraphObject.cs


m_DeserializedGraph = deserializedGraph; // graph.ReplaceWith(m_DeserializedGraph);
}
void Validate()
{
if (graph != null)
{
graph.OnEnable();
graph.ValidateGraph();
}
}
Validate();
Undo.undoRedoPerformed += UndoRedoPerformed;
UndoRedoPerformed();
}

15
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Implementation/SerializableSlot.cs


using System;
using System.Linq;
using UnityEngine;
namespace UnityEditor.Graphing

unchecked
{
return (m_Id * 397) ^ (owner != null ? owner.GetHashCode() : 0);
}
}
public bool isConnected
{
get
{
// node and graph respectivly
if (owner == null || owner.owner == null)
return false;
var graph = owner.owner;
var edges = graph.GetEdges(slotReference);
return edges.Any();
}
}
}

2
MaterialGraphProject/Assets/NewNodes/Editor/Keep/UVTriPlanar.cs


/*
namespace UnityEngine.MaterialGraph
namespace UnityEditor.ShaderGraph
{
[Title("UV/Tri-Planar Mapping")]
public class UVTriPlanar : FunctionNInNOut, IGeneratesFunction, IMayRequireNormal, IMayRequireWorldPosition

458
MaterialGraphProject/Assets/PartyPreview.ShaderGraph
文件差异内容过多而无法显示
查看文件

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


namespace UnityEditor.ShaderGraph
{
public interface IMaterialSlotHasVaule<T>
{
T defaultValue { get; }
T value { get; }
}
[Serializable]
public class Vector1MaterialSlot : MaterialSlot, IMaterialSlotHasVaule<float>
{
[SerializeField]
private float m_Value;
[SerializeField]
private float m_DefaultValue;
public Vector1MaterialSlot()
{
}
public Vector1MaterialSlot(
int slotId,
string displayName,
string shaderOutputName,
SlotType slotType,
float value,
ShaderStage shaderStage = ShaderStage.Dynamic,
bool hidden = false)
:base(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden)
{
m_DefaultValue = value;
m_Value = value;
}
public float defaultValue { get { return m_DefaultValue; } }
public float value
{
get { return m_Value; }
set { m_Value = value; }
}
protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
return value.ToString();
}
public override SlotValueType valueType { get { return SlotValueType.Vector1; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Vector1; } }
public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty
{
m_Name = name,
m_PropType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType),
m_Vector4 = new Vector4(value, value, value, value),
m_Float = value,
m_Color = new Vector4(value, value, value, value),
};
return pp;
}
}
[Serializable]
public class Vector2MaterialSlot : MaterialSlot, IMaterialSlotHasVaule<Vector2>
{
[SerializeField]
private Vector2 m_Value;
[SerializeField]
private Vector2 m_DefaultValue;
public Vector2MaterialSlot()
{
}
public Vector2MaterialSlot(
int slotId,
string displayName,
string shaderOutputName,
SlotType slotType,
Vector2 value,
ShaderStage shaderStage = ShaderStage.Dynamic,
bool hidden = false)
:base(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden)
{
m_Value = value;
}
public Vector2 defaultValue { get { return m_DefaultValue; } }
public Vector2 value
{
get { return m_Value; }
set { m_Value = value; }
}
protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
return precision + "2 (" + value.x + "," + value.y + ")";
}
public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty
{
m_Name = name,
m_PropType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType),
m_Vector4 = new Vector4(value.x, value.y, 0, 0),
m_Float = value.x,
m_Color = new Vector4(value.x, value.x, 0, 0),
};
return pp;
}
public override SlotValueType valueType { get { return SlotValueType.Vector2; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Vector2; } }
}
[Serializable]
public class Vector3MaterialSlot : MaterialSlot, IMaterialSlotHasVaule<Vector3>
{
[SerializeField]
private Vector3 m_Value;
[SerializeField]
private Vector3 m_DefaultValue;
public Vector3MaterialSlot()
{
}
public Vector3MaterialSlot(
int slotId,
string displayName,
string shaderOutputName,
SlotType slotType,
Vector3 value,
ShaderStage shaderStage = ShaderStage.Dynamic,
bool hidden = false)
:base(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden)
{
m_Value = value;
}
public Vector3 defaultValue { get { return m_DefaultValue; } }
public Vector3 value
{
get { return m_Value; }
set { m_Value = value; }
}
protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
return precision + "3 (" + value.x + "," + value.y + "," + value.z + ")";
}
public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty
{
m_Name = name,
m_PropType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType),
m_Vector4 = new Vector4(value.x, value.y, value.z, 0),
m_Float = value.x,
m_Color = new Vector4(value.x, value.x, value.z, 0),
};
return pp;
}
public override SlotValueType valueType { get { return SlotValueType.Vector3; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Vector3; } }
}
[Serializable]
public class Vector4MaterialSlot : MaterialSlot, IMaterialSlotHasVaule<Vector4>
{
[SerializeField]
private Vector4 m_Value;
[SerializeField]
private Vector4 m_DefaultValue;
public Vector4MaterialSlot()
{
}
public Vector4MaterialSlot(
int slotId,
string displayName,
string shaderOutputName,
SlotType slotType,
Vector4 value,
ShaderStage shaderStage = ShaderStage.Dynamic,
bool hidden = false)
:base(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden)
{
m_Value = value;
}
public Vector4 defaultValue { get { return m_DefaultValue; } }
public Vector4 value
{
get { return m_Value; }
set { m_Value = value; }
}
protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
return precision + "4 (" + value.x + "," + value.y + "," + value.z + "," + value.w + ")";
}
public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty
{
m_Name = name,
m_PropType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType),
m_Vector4 = new Vector4(value.x, value.y, value.z, value.w),
m_Float = value.x,
m_Color = new Vector4(value.x, value.x, value.z, value.w),
};
return pp;
}
public override SlotValueType valueType { get { return SlotValueType.Vector4; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Vector4; } }
}
[Serializable]
public class Matrix2MaterialSlot : MaterialSlot
{
public Matrix2MaterialSlot()
{
}
public Matrix2MaterialSlot(
int slotId,
string displayName,
string shaderOutputName,
SlotType slotType,
ShaderStage shaderStage = ShaderStage.Dynamic,
bool hidden = false)
:base(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden)
{
}
protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
return precision + "2x2 (1,0,0,1)";
}
public override SlotValueType valueType { get { return SlotValueType.Matrix2; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Matrix2; } }
}
[Serializable]
public class Matrix3MaterialSlot : MaterialSlot
{
public Matrix3MaterialSlot()
{
}
public Matrix3MaterialSlot(
int slotId,
string displayName,
string shaderOutputName,
SlotType slotType,
ShaderStage shaderStage = ShaderStage.Dynamic,
bool hidden = false)
:base(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden)
{
}
protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
return precision + "3x3 (1,0,0,0,1,0,0,0,1)";
}
public override SlotValueType valueType { get { return SlotValueType.Matrix3; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Matrix3; } }
}
[Serializable]
public class Matrix4MaterialSlot : MaterialSlot
{
public Matrix4MaterialSlot()
{
}
public Matrix4MaterialSlot(
int slotId,
string displayName,
string shaderOutputName,
SlotType slotType,
ShaderStage shaderStage = ShaderStage.Dynamic,
bool hidden = false)
:base(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden)
{
}
protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
return precision + "4x4 (1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1)";
}
public override SlotValueType valueType { get { return SlotValueType.Matrix4; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Matrix4; } }
}
[Serializable]
public class Texture2DMaterialSlot : MaterialSlot
{
public Texture2DMaterialSlot()
{
}
public Texture2DMaterialSlot(
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 DefaultTextureName = "ShaderGraph_DefaultTexture";
public override SlotValueType valueType { get { return SlotValueType.Texture2D; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Texture2D; } }
}
[Serializable]
public class SamplerStateMaterialSlot : MaterialSlot
{
public SamplerStateMaterialSlot()
{
}
public SamplerStateMaterialSlot(
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.SamplerState; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.SamplerState; } }
}
[Serializable]
public class DynamicVectorMaterialSlot : MaterialSlot, IMaterialSlotHasVaule<Vector4>
{
[SerializeField]
private Vector4 m_Value;
[SerializeField]
private Vector4 m_DefaultValue;
private ConcreteSlotValueType m_ConcreteValueType = ConcreteSlotValueType.Vector4;
public DynamicVectorMaterialSlot()
{
}
public DynamicVectorMaterialSlot(
int slotId,
string displayName,
string shaderOutputName,
SlotType slotType,
Vector4 value,
ShaderStage shaderStage = ShaderStage.Dynamic,
bool hidden = false)
:base(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden)
{
m_Value = value;
}
public Vector4 defaultValue { get { return m_DefaultValue; } }
public Vector4 value
{
get { return m_Value; }
set { m_Value = value; }
}
public override SlotValueType valueType { get { return SlotValueType.Dynamic; } }
public override ConcreteSlotValueType concreteValueType
{
get { return m_ConcreteValueType; }
}
public void SetConcreteType(ConcreteSlotValueType valueType)
{
m_ConcreteValueType = valueType;
}
public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty
{
m_Name = name,
m_PropType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType),
m_Vector4 = new Vector4(value.x, value.y, value.z, value.w),
m_Float = value.x,
m_Color = new Vector4(value.x, value.x, value.z, value.w),
};
return pp;
}
protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
return precision + "4 (" + value.x + "," + value.y + "," + value.z + "," + value.w + ")";
}
}
[Serializable]
public abstract class MaterialSlot : SerializableSlot
{

case SlotValueType.Matrix2:
return new Matrix2MaterialSlot(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden);
case SlotValueType.Texture2D:
return new Texture2DMaterialSlot(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden);
return slotType == SlotType.Input
? new Texture2DInputMaterialSlot(slotId, displayName, shaderOutputName, shaderStage, hidden)
: new Texture2DMaterialSlot(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden);
case SlotValueType.Dynamic:
return new DynamicVectorMaterialSlot(slotId, displayName, shaderOutputName, slotType, defaultValue, shaderStage, hidden);
case SlotValueType.Vector4:

if (matOwner == null)
throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
if (concreteValueType == ConcreteSlotValueType.Texture2D)
return Texture2DMaterialSlot.DefaultTextureName;
if (generationMode.IsPreview())
return matOwner.GetVariableNameForSlot(id);

return "error";
}
public void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{
// share tex2d for all non connected slots :)
if (concreteValueType == ConcreteSlotValueType.Texture2D)
{
var prop = new TextureShaderProperty();
prop.overrideReferenceName = Texture2DMaterialSlot.DefaultTextureName;
prop.modifiable = false;
prop.generatePropertyBlock = true;
properties.AddShaderProperty(prop);
return;
}
if (concreteValueType == ConcreteSlotValueType.SamplerState)
return;
if (!generationMode.IsPreview())
return;
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)));
IShaderProperty property;
switch (concreteValueType)
{
case ConcreteSlotValueType.Vector4:
property = new Vector4ShaderProperty();
break;
case ConcreteSlotValueType.Vector3:
property = new Vector3ShaderProperty();
break;
case ConcreteSlotValueType.Vector2:
property = new Vector2ShaderProperty();
break;
case ConcreteSlotValueType.Vector1:
property = new FloatShaderProperty();
break;
default:
throw new ArgumentOutOfRangeException();
}
property.overrideReferenceName = matOwner.GetVariableNameForSlot(id);
property.generatePropertyBlock = false;
properties.AddShaderProperty(property);
}
public abstract void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode);
protected static PropertyType ConvertConcreteSlotValueTypeToPropertyType(ConcreteSlotValueType slotValue)
{

{
return null;
}
public abstract void CopyValuesFrom(MaterialSlot foundSlot);
}
}

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SerializableTexture.cs


using System;
using UnityEditor;
using UnityEngine;
namespace UnityEditor.ShaderGraph

{
[SerializeField] private string m_SerializedTexture;
[SerializeField]
private string m_SerializedTexture;
[Serializable]
private class TextureHelper

#if UNITY_EDITOR
public Texture texture
{
get

m_SerializedTexture = EditorJsonUtility.ToJson(tex, true);
}
}
#else
public Texture defaultTexture {get; set; }
#endif
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/TextureShaderProperty.cs


public class TextureShaderProperty : AbstractShaderProperty<SerializableTexture>
{
[SerializeField]
private bool m_Modifiable;
private bool m_Modifiable = true;
public TextureShaderProperty()
{

var result = new StringBuilder();
if (!m_Modifiable)
{
result.Append("[HideInInspector] ");
result.Append("[NonModifiableTextureData] ");
}
result.Append("[NoScaleOffset] ");

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


public virtual void CollectShaderProperties(PropertyCollector properties, GenerationMode generationMode)
{
if (!generationMode.IsPreview())
return;
foreach (var inputSlot in GetInputSlots<MaterialSlot>())
{
var edges = owner.GetEdges(inputSlot.slotReference);

private ConcreteSlotValueType ConvertDynamicInputTypeToConcrete(IEnumerable<ConcreteSlotValueType> inputTypes)
{
var concreteSlotValueTypes = inputTypes as IList<ConcreteSlotValueType> ?? inputTypes.ToList();
var inputTypesDistinct = concreteSlotValueTypes.Distinct().ToList();
switch (inputTypesDistinct.Count)
{

public override void ValidateNode()
{
var isInError = false;
var isInError = false;
// all children nodes needs to be updated first
// so do that here

var edges = owner.GetEdges(inputSlot.slotReference);
foreach (var edge in edges)
foreach (var edge in edges)
{
var fromSocketRef = edge.outputSlot;
var outputNode = owner.GetNodeFromGuid(fromSocketRef.nodeGuid);

{
return false;
}
public static string GetSlotDimension(ConcreteSlotValueType slotValue)
{
switch (slotValue)

if (foundSlot == null)
return;
// now copy over values :)
if (addingSlot is DynamicVectorMaterialSlot && foundSlot is DynamicVectorMaterialSlot)
(addingSlot as DynamicVectorMaterialSlot).value = (foundSlot as DynamicVectorMaterialSlot).value;
if (addingSlot is Vector1MaterialSlot && foundSlot is Vector1MaterialSlot)
(addingSlot as Vector1MaterialSlot).value = (foundSlot as Vector1MaterialSlot).value;
if (addingSlot is Vector2MaterialSlot && foundSlot is Vector2MaterialSlot)
(addingSlot as Vector2MaterialSlot).value = (foundSlot as Vector2MaterialSlot).value;
if (addingSlot is Vector3MaterialSlot && foundSlot is Vector3MaterialSlot)
(addingSlot as Vector3MaterialSlot).value = (foundSlot as Vector3MaterialSlot).value;
if (addingSlot is Vector4MaterialSlot && foundSlot is Vector4MaterialSlot)
(addingSlot as Vector4MaterialSlot).value = (foundSlot as Vector4MaterialSlot).value;
addingSlot.CopyValuesFrom(foundSlot);
}
}
}

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Art/Filters/ConvolutionFilterNode.cs


using System.Linq;
using System;
/*namespace UnityEngine.MaterialGraph
/*namespace UnityEditor.ShaderGraph
{
[Title("Art/Filters/Convolution")]
public class ConvolutionFilterNode : Function2Input, IGeneratesFunction, IGeneratesBodyCode, IMayRequireMeshUV

219
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/TextureNode.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
/* [Title("Input/Texture/Texture")]
public class TextureNode : PropertyNode, IGeneratesBodyCode, IMayRequireMeshUV
{
protected const string kUVSlotName = "UV";
protected const string kOutputSlotRGBAName = "RGBA";
protected const string kOutputSlotRName = "R";
protected const string kOutputSlotGName = "G";
protected const string kOutputSlotBName = "B";
protected const string kOutputSlotAName = "A";
public const int UvSlotId = 0;
public const int OutputSlotRgbaId = 1;
public const int OutputSlotRId = 2;
public const int OutputSlotGId = 3;
public const int OutputSlotBId = 4;
public const int OutputSlotAId = 5;
[SerializeField]
private string m_SerializedTexture;
[SerializeField]
private TextureType m_TextureType;
[Serializable]
private class TextureHelper
{
public Texture texture;
}
public override bool hasPreview { get { return true; } }
#if UNITY_EDITOR
public Texture defaultTexture
{
get
{
if (string.IsNullOrEmpty(m_SerializedTexture))
return null;
var tex = new TextureHelper();
EditorJsonUtility.FromJsonOverwrite(m_SerializedTexture, tex);
return tex.texture;
}
set
{
if (defaultTexture == value)
return;
var tex = new TextureHelper();
tex.texture = value;
m_SerializedTexture = EditorJsonUtility.ToJson(tex, true);
if (onModified != null)
{
onModified(this, ModificationScope.Node);
}
}
}
#else
public Texture defaultTexture {get; set; }
#endif
public TextureType textureType
{
get { return m_TextureType; }
set
{
if (m_TextureType == value)
return;
m_TextureType = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
}
}
public TextureNode()
{
name = "Texture";
UpdateNodeAfterDeserialization();
}
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new MaterialSlot(OutputSlotRgbaId, kOutputSlotRGBAName, kOutputSlotRGBAName, SlotType.Output, SlotValueType.Vector4, Vector4.zero));
AddSlot(new MaterialSlot(OutputSlotRId, kOutputSlotRName, kOutputSlotRName, SlotType.Output, SlotValueType.Vector1, Vector4.zero));
AddSlot(new MaterialSlot(OutputSlotGId, kOutputSlotGName, kOutputSlotGName, SlotType.Output, SlotValueType.Vector1, Vector4.zero));
AddSlot(new MaterialSlot(OutputSlotBId, kOutputSlotBName, kOutputSlotBName, SlotType.Output, SlotValueType.Vector1, Vector4.zero));
AddSlot(new MaterialSlot(OutputSlotAId, kOutputSlotAName, kOutputSlotAName, SlotType.Output, SlotValueType.Vector1, Vector4.zero));
AddSlot(new MaterialSlot(UvSlotId, kUVSlotName, kUVSlotName, SlotType.Input, SlotValueType.Vector2, Vector4.zero, false));
RemoveSlotsNameNotMatching(validSlots);
}
protected int[] validSlots
{
get { return new[] {OutputSlotRgbaId, OutputSlotRId, OutputSlotGId, OutputSlotBId, OutputSlotAId, UvSlotId}; }
}
// Node generations
public virtual void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)
{
var uvSlot = FindInputSlot<MaterialSlot>(UvSlotId);
if (uvSlot == null)
return;
var uvName = string.Format("{0}.xy", UVChannel.uv0.GetUVName());
var edges = owner.GetEdges(uvSlot.slotReference).ToList();
if (edges.Count > 0)
{
var edge = edges[0];
var fromNode = owner.GetNodeFromGuid<AbstractMaterialNode>(edge.outputSlot.nodeGuid);
uvName = ShaderGenerator.AdaptNodeOutput(fromNode, edge.outputSlot.slotId, ConcreteSlotValueType.Vector2, true);
}
string body = "tex2D (" + propertyName + ", " + uvName + ")";
if (m_TextureType == TextureType.Bump)
body = precision + "4(UnpackNormal(" + body + "), 0)";
visitor.AddShaderChunk(precision + "4 " + GetVariableNameForNode() + " = " + body + ";", true);
}
public override string GetVariableNameForSlot(int slotId)
{
string slotOutput;
switch (slotId)
{
case OutputSlotRId:
slotOutput = ".r";
break;
case OutputSlotGId:
slotOutput = ".g";
break;
case OutputSlotBId:
slotOutput = ".b";
break;
case OutputSlotAId:
slotOutput = ".a";
break;
default:
slotOutput = "";
break;
}
return GetVariableNameForNode() + slotOutput;
}
public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
{
properties.Add(GetPreviewProperty());
}
// Properties
public override void GeneratePropertyBlock(PropertyGenerator visitor, GenerationMode generationMode)
{
visitor.AddShaderProperty(
new TexturePropertyChunk(
propertyName,
description,
defaultTexture, m_TextureType,
PropertyChunk.HideState.Visible,
exposedState == ExposedState.Exposed ?
TexturePropertyChunk.ModifiableState.Modifiable
: TexturePropertyChunk.ModifiableState.NonModifiable));
}
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)
{
visitor.AddShaderChunk("sampler2D " + propertyName + ";", true);
}
/*
public override bool DrawSlotDefaultInput(Rect rect, Slot inputSlot)
{
var uvSlot = FindInputSlot(kUVSlotName);
if (uvSlot != inputSlot)
return base.DrawSlotDefaultInput(rect, inputSlot);
var rectXmax = rect.xMax;
rect.x = rectXmax - 70;
rect.width = 70;
EditorGUI.DrawRect(rect, new Color(0.0f, 0.0f, 0.0f, 0.7f));
GUI.Label(rect, "From Mesh");
return false;
}
*
public override PreviewProperty GetPreviewProperty()
{
return new PreviewProperty
{
m_Name = propertyName,
m_PropType = PropertyType.Texture,
m_Texture = defaultTexture
};
}
public override PropertyType propertyType { get { return PropertyType.Texture; } }
public bool RequiresMeshUV(UVChannel channel)
{
if (channel != UVChannel.uv0)
{
return false;
}
var uvSlot = FindInputSlot<MaterialSlot>(UvSlotId);
if (uvSlot == null)
return true;
var edges = owner.GetEdges(uvSlot.slotReference).ToList();
return edges.Count == 0;
}
}*/
}

34
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/TextureSamplerNode.cs


AddSlot(new Vector1MaterialSlot(OutputSlotGId, kOutputSlotGName, kOutputSlotGName, SlotType.Output, 0));
AddSlot(new Vector1MaterialSlot(OutputSlotBId, kOutputSlotBName, kOutputSlotBName, SlotType.Output, 0));
AddSlot(new Vector1MaterialSlot(OutputSlotAId, kOutputSlotAName, kOutputSlotAName, SlotType.Output, 0));
AddSlot(new Texture2DMaterialSlot(TextureInputId, kTextureInputName, kTextureInputName, SlotType.Input));
AddSlot(new Vector2MaterialSlot(UVInput, kUVInputName, kUVInputName, SlotType.Input, Vector4.zero));
AddSlot(new Texture2DInputMaterialSlot(TextureInputId, kTextureInputName, kTextureInputName));
AddSlot(new UVMaterialSlot(UVInput, kUVInputName, kUVInputName, UVChannel.uv0));
AddSlot(new SamplerStateMaterialSlot(SamplerInput, kSamplerInputName, kSamplerInputName, SlotType.Input));
RemoveSlotsNameNotMatching(new[] { OutputSlotRGBAId, OutputSlotRId, OutputSlotGId, OutputSlotBId, OutputSlotAId, TextureInputId, UVInput, SamplerInput });
}

{
//Texture input slot
var textureSlot = FindInputSlot<MaterialSlot>(TextureInputId);
var edgesTexture = owner.GetEdges(textureSlot.slotReference);
// if no texture connected return nothing
if (!edgesTexture.Any())
{
visitor.AddShaderChunk(precision + "4 " + GetVariableNameForSlot(OutputSlotRGBAId) + " = " + precision + "4(0,0,0,0);", true);
return;
}
//UV input slot
var uvSlot = FindInputSlot<MaterialSlot>(UVInput);
var uvName = string.Format("{0}.xy", UVChannel.uv0.GetUVName());
var edgesUV = owner.GetEdges(uvSlot.slotReference);
if (edgesUV.Any())
uvName = GetSlotValue(UVInput, generationMode);
var uvName = GetSlotValue(UVInput, generationMode);
//Sampler input slot
var samplerSlot = FindInputSlot<MaterialSlot>(SamplerInput);

public bool RequiresMeshUV(UVChannel channel)
{
if (channel != UVChannel.uv0)
{
var uvSlot = FindInputSlot<MaterialSlot>(UVInput) as UVMaterialSlot;
if (uvSlot == null)
}
var uvSlot = FindInputSlot<MaterialSlot>(UVInput);
if (uvSlot == null)
return true;
if (uvSlot.isConnected)
return false;
var edges = owner.GetEdges(uvSlot.slotReference).ToList();
return edges.Count == 0;
return uvSlot.RequiresMeshUV(channel);
}
}*/
}

64
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/PropertyNode.cs


namespace UnityEditor.ShaderGraph
{
[Title("Property Node")]
public class PropertyNode : AbstractMaterialNode, IGeneratesBodyCode, IMayRequireMeshUV, IOnAssetEnabled
public class PropertyNode : AbstractMaterialNode, IGeneratesBodyCode, IOnAssetEnabled
[SerializeField] private string m_PropertyGuidSerialized;
[SerializeField]
private string m_PropertyGuidSerialized;
public const int ROutputSlotId = 1;
public const int GOutputSlotId = 2;
public const int BOutputSlotId = 3;
public const int AOutputSlotId = 4;
public const int TOutputSlotId = 5;
public const int UVInput = 6;
public PropertyNode()
{

}
else if (property is TextureShaderProperty)
{
AddSlot(new Vector4MaterialSlot(OutputSlotId, "RGBA", "RGBA", SlotType.Output, Vector4.zero));
AddSlot(new Vector1MaterialSlot(ROutputSlotId, "R", "R", SlotType.Output, 0));
AddSlot(new Vector1MaterialSlot(GOutputSlotId, "G", "G", SlotType.Output, 0));
AddSlot(new Vector1MaterialSlot(BOutputSlotId, "B", "B", SlotType.Output, 0));
AddSlot(new Vector1MaterialSlot(AOutputSlotId, "A", "A", SlotType.Output, 0));
AddSlot(new Texture2DMaterialSlot(TOutputSlotId, "T", "T", SlotType.Output));
AddSlot(new Vector2MaterialSlot(UVInput, "UV", "UV", SlotType.Input, Vector4.zero));
RemoveSlotsNameNotMatching(new[] {OutputSlotId, ROutputSlotId, GOutputSlotId, BOutputSlotId, AOutputSlotId, TOutputSlotId, UVInput});
AddSlot(new Texture2DMaterialSlot(OutputSlotId, "T", "T", SlotType.Output));
RemoveSlotsNameNotMatching(new[] {OutputSlotId});
}
}

, property.referenceName);
visitor.AddShaderChunk(result, true);
}
else if (property is TextureShaderProperty)
{
//UV input slot
var uvSlot = FindInputSlot<MaterialSlot>(UVInput);
var uvName = string.Format("{0}.xy", UVChannel.uv0.GetUVName());
var edgesUV = owner.GetEdges(uvSlot.slotReference);
if (edgesUV.Any())
uvName = GetSlotValue(UVInput, generationMode);
//Sampler input slot
var result = string.Format("{0}4 {1} = UNITY_SAMPLE_TEX2D({2},{3});"
, precision
, GetVariableNameForSlot(OutputSlotId)
, property.referenceName
, uvName);
visitor.AddShaderChunk(result, true);
visitor.AddShaderChunk(string.Format("{0} {1} = {2}.r;", precision, GetVariableNameForSlot(ROutputSlotId), GetVariableNameForSlot(OutputSlotId)), true);
visitor.AddShaderChunk(string.Format("{0} {1} = {2}.g;", precision, GetVariableNameForSlot(GOutputSlotId), GetVariableNameForSlot(OutputSlotId)), true);
visitor.AddShaderChunk(string.Format("{0} {1} = {2}.b;", precision, GetVariableNameForSlot(BOutputSlotId), GetVariableNameForSlot(OutputSlotId)), true);
visitor.AddShaderChunk(string.Format("{0} {1} = {2}.a;", precision, GetVariableNameForSlot(AOutputSlotId), GetVariableNameForSlot(OutputSlotId)), true);
}
}
public bool RequiresMeshUV(UVChannel channel)
{
if (channel != UVChannel.uv0)
return false;
var uvSlot = FindInputSlot<MaterialSlot>(UVInput);
if (uvSlot == null)
return true;
var edges = owner.GetEdges(uvSlot.slotReference).ToList();
return edges.Count == 0;
}
[PropertyControl]

public override string GetVariableNameForSlot(int slotId)
{
if (slotId != TOutputSlotId)
var graph = owner as AbstractMaterialGraph;
var property = graph.properties.FirstOrDefault(x => x.guid == propertyGuid);
if (!(property is TextureShaderProperty))
var graph = owner as AbstractMaterialGraph;
var property = graph.properties.FirstOrDefault(x => x.guid == propertyGuid);
return property.referenceName;
}

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


}
var id = prop.guid.GetHashCode();
AddSlot(MaterialSlot.CreateMaterialSlot(slotType, id, prop.displayName, prop.referenceName, SlotType.Input, prop.defaultValue));
MaterialSlot slot = MaterialSlot.CreateMaterialSlot(slotType, id, prop.displayName, prop.referenceName, SlotType.Input, prop.defaultValue);
// copy default for texture for niceness
if (slotType == SlotValueType.Texture2D && propType == PropertyType.Texture)
{
var tSlot = slot as Texture2DInputMaterialSlot;
var tProp = prop as TextureShaderProperty;
if (tSlot != null && tProp != null)
tSlot.texture = tProp.value.texture;
}
AddSlot(slot);
validNames.Add(id);
}

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/SubGraphNode.cs


using UnityEditor;
#endif
using UnityEditor.Graphing;
using UnityEditor.Graphs;
namespace UnityEditor.ShaderGraph
{

, IOnAssetEnabled
{
[SerializeField]
private string m_SerializedSubGraph = string.Empty;

prop.overrideReferenceName = fromNode.GetSlotValue(slot.id, generationMode);
}
}
else if (inSlot.concreteValueType == ConcreteSlotValueType.Texture2D)
else if (inSlot is Texture2DInputMaterialSlot)
prop.overrideReferenceName = Texture2DMaterialSlot.DefaultTextureName;
prop.overrideReferenceName = ((Texture2DInputMaterialSlot)inSlot).GetDefaultValue(generationMode);
}
else
{

outputString.AddShaderChunk("// Subgraph ends", false);
shaderBodyVisitor.AddShaderChunk(outputString.GetShaderString(0), true);
}
public void OnEnable()
{
UpdateSlots();
}
}
}

4
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SurfaceModel/ProjectSpecificMasterNode.cs


using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEngine.Graphing;
using UnityEditor.Graphing;
namespace UnityEngine.MaterialGraph
namespace UnityEditor.ShaderGraph
{
[Serializable]
public abstract class ProjectSpecificMasterNode : AbstractMasterNode

9
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/IMGUISlotEditorView.cs


using UnityEngine.Experimental.UIElements;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph;
using UnityEngine;
namespace UnityEditor.ShaderGraph.Drawing.Inspector
{

var dynSlot = slot as Vector2MaterialSlot;
dynSlot.value = EditorGUILayout.Vector2Field(slot.displayName, dynSlot.value);
}
if (slot is Vector3MaterialSlot)
{
var dynSlot = slot as Vector3MaterialSlot;

{
var dynSlot = slot as Vector4MaterialSlot;
dynSlot.value = EditorGUILayout.Vector4Field(slot.displayName, dynSlot.value);
}
if (slot is Texture2DInputMaterialSlot)
{
var dynslot = slot as Texture2DInputMaterialSlot;
dynslot.texture = EditorGUILayout.MiniThumbnailObjectField(new GUIContent("Texture"), dynslot.texture, typeof(Texture), null) as Texture;
}
return EditorGUI.EndChangeCheck();
}

4
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/MaterialNodeView.cs


void UpdatePreviewExpandedState(bool expanded)
{
node.previewExpanded = expanded;
if (m_PreviewContainer == null)
return;
if (expanded)
{
m_PreviewContainer.AddToClassList("expanded");

void UpdateTitle()
{
var subGraphNode = node as SubGraphNode;
if (subGraphNode != null)
if (subGraphNode != null && subGraphNode.subGraphAsset != null)
title = subGraphNode.subGraphAsset.name;
else
title = node.name;

28
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/BitangentMaterialSlot.cs


using System;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class BitangentMaterialSlot : SpaceMaterialSlot, IMayRequireBitangent
{
public BitangentMaterialSlot() : base()
{}
public BitangentMaterialSlot(int slotId, string displayName, string shaderOutputName, CoordinateSpace space,
ShaderStage shaderStage = ShaderStage.Dynamic, bool hidden = false)
: base(slotId, displayName, shaderOutputName, space, shaderStage, hidden)
{}
public override string GetDefaultValue(GenerationMode generationMode)
{
return space.ToVariableName(InterpolatorType.BiTangent);
}
public NeededCoordinateSpace RequiresBitangent()
{
if (isConnected)
return NeededCoordinateSpace.None;
return space.ToNeededCoordinateSpace();
}
}
}

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


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

114
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/DynamicVectorMaterialSlot.cs


using System;
using UnityEditor.Graphing;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class DynamicVectorMaterialSlot : MaterialSlot, IMaterialSlotHasVaule<Vector4>
{
[SerializeField]
private Vector4 m_Value;
[SerializeField]
private Vector4 m_DefaultValue;
private ConcreteSlotValueType m_ConcreteValueType = ConcreteSlotValueType.Vector4;
public DynamicVectorMaterialSlot()
{
}
public DynamicVectorMaterialSlot(
int slotId,
string displayName,
string shaderOutputName,
SlotType slotType,
Vector4 value,
ShaderStage shaderStage = ShaderStage.Dynamic,
bool hidden = false)
:base(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden)
{
m_Value = value;
}
public Vector4 defaultValue { get { return m_DefaultValue; } }
public Vector4 value
{
get { return m_Value; }
set { m_Value = value; }
}
public override SlotValueType valueType { get { return SlotValueType.Dynamic; } }
public override ConcreteSlotValueType concreteValueType
{
get { return m_ConcreteValueType; }
}
public void SetConcreteType(ConcreteSlotValueType valueType)
{
m_ConcreteValueType = valueType;
}
public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty
{
m_Name = name,
m_PropType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType),
m_Vector4 = new Vector4(value.x, value.y, value.z, value.w),
m_Float = value.x,
m_Color = new Vector4(value.x, value.x, value.z, value.w),
};
return pp;
}
protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
return precision + "4 (" + value.x + "," + value.y + "," + value.z + "," + value.w + ")";
}
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{
if (!generationMode.IsPreview())
return;
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)));
IShaderProperty property;
switch (concreteValueType)
{
case ConcreteSlotValueType.Vector4:
property = new Vector4ShaderProperty();
break;
case ConcreteSlotValueType.Vector3:
property = new Vector3ShaderProperty();
break;
case ConcreteSlotValueType.Vector2:
property = new Vector2ShaderProperty();
break;
case ConcreteSlotValueType.Vector1:
property = new FloatShaderProperty();
break;
default:
throw new ArgumentOutOfRangeException();
}
property.overrideReferenceName = matOwner.GetVariableNameForSlot(id);
property.generatePropertyBlock = false;
properties.AddShaderProperty(property);
}
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
var slot = foundSlot as DynamicVectorMaterialSlot;
if (slot != null)
value = slot.value;
}
}
}

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


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

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


namespace UnityEditor.ShaderGraph
{
public interface IMaterialSlotHasVaule<T>
{
T defaultValue { get; }
T value { get; }
}
}

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


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

43
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix2MaterialSlot.cs


using System;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class Matrix2MaterialSlot : MaterialSlot
{
public Matrix2MaterialSlot()
{
}
public Matrix2MaterialSlot(
int slotId,
string displayName,
string shaderOutputName,
SlotType slotType,
ShaderStage shaderStage = ShaderStage.Dynamic,
bool hidden = false)
:base(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden)
{
}
protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
return precision + "2x2 (1,0,0,1)";
}
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{}
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
}
public override SlotValueType valueType { get { return SlotValueType.Matrix2; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Matrix2; } }
}
}

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


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

42
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix3MaterialSlot.cs


using System;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class Matrix3MaterialSlot : MaterialSlot
{
public Matrix3MaterialSlot()
{
}
public Matrix3MaterialSlot(
int slotId,
string displayName,
string shaderOutputName,
SlotType slotType,
ShaderStage shaderStage = ShaderStage.Dynamic,
bool hidden = false)
:base(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden)
{
}
protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
return precision + "3x3 (1,0,0,0,1,0,0,0,1)";
}
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{
}
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
}
public override SlotValueType valueType { get { return SlotValueType.Matrix3; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Matrix3; } }
}
}

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


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

43
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Matrix4MaterialSlot.cs


using System;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class Matrix4MaterialSlot : MaterialSlot
{
public Matrix4MaterialSlot()
{
}
public Matrix4MaterialSlot(
int slotId,
string displayName,
string shaderOutputName,
SlotType slotType,
ShaderStage shaderStage = ShaderStage.Dynamic,
bool hidden = false)
:base(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden)
{
}
protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
return precision + "4x4 (1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1)";
}
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{
}
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
}
public override SlotValueType valueType { get { return SlotValueType.Matrix4; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Matrix4; } }
}
}

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


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

31
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/NormalMaterialSlot.cs


using System;
using UnityEditor.Graphing;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class NormalMaterialSlot : SpaceMaterialSlot, IMayRequireNormal
{
public NormalMaterialSlot()
{}
public NormalMaterialSlot(int slotId, string displayName, string shaderOutputName, CoordinateSpace space,
ShaderStage shaderStage = ShaderStage.Dynamic, bool hidden = false)
: base(slotId, displayName, shaderOutputName, space, shaderStage, hidden)
{}
public override string GetDefaultValue(GenerationMode generationMode)
{
return space.ToVariableName(InterpolatorType.Normal);
}
public NeededCoordinateSpace RequiresNormal()
{
if (isConnected)
return NeededCoordinateSpace.None;
return space.ToNeededCoordinateSpace();
}
}
}

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


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

30
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/PositionMaterialSlot.cs


using System;
using UnityEditor.Graphing;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class PositionMaterialSlot : SpaceMaterialSlot, IMayRequirePosition
{
public PositionMaterialSlot()
{}
public PositionMaterialSlot(int slotId, string displayName, string shaderOutputName, CoordinateSpace space,
ShaderStage shaderStage = ShaderStage.Dynamic, bool hidden = false)
: base(slotId, displayName, shaderOutputName, space, shaderStage, hidden)
{}
public override string GetDefaultValue(GenerationMode generationMode)
{
return space.ToVariableName(InterpolatorType.Position);
}
public NeededCoordinateSpace RequiresPosition()
{
if (isConnected)
return NeededCoordinateSpace.None;
return space.ToNeededCoordinateSpace();
}
}
}

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


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

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


using System;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class SamplerStateMaterialSlot : MaterialSlot
{
public SamplerStateMaterialSlot()
{
}
public SamplerStateMaterialSlot(
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.SamplerState; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.SamplerState; } }
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{
}
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
}
}
}

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


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

25
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/ScreenPositionMaterialSlot.cs


using System;
using UnityEditor.Graphing;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class ScreenPositionMaterialSlot : Vector4MaterialSlot, IMayRequireScreenPosition
{
public ScreenPositionMaterialSlot(int slotId, string displayName, string shaderOutputName,
ShaderStage shaderStage = ShaderStage.Dynamic, bool hidden = false)
: base(slotId, displayName, shaderOutputName, SlotType.Input, Vector3.zero, shaderStage, hidden)
{ }
public override string GetDefaultValue(GenerationMode generationMode)
{
return ShaderGeneratorNames.ScreenPosition;
}
public bool RequiresScreenPosition()
{
return !isConnected;
}
}
}

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


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

36
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SpaceMaterialSlot.cs


using System;
using UnityEditor.Graphing;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public abstract class SpaceMaterialSlot : Vector3MaterialSlot
{
[SerializeField]
private CoordinateSpace m_Space = CoordinateSpace.World;
public CoordinateSpace space
{
get { return m_Space; }
set { m_Space = value; }
}
protected SpaceMaterialSlot()
{}
protected SpaceMaterialSlot(int slotId, string displayName, string shaderOutputName, CoordinateSpace space,
ShaderStage shaderStage = ShaderStage.Dynamic, bool hidden = false)
: base(slotId, displayName, shaderOutputName, SlotType.Input, Vector3.zero, shaderStage, hidden)
{
this.space = space;
}
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
var slot = foundSlot as NormalMaterialSlot;
if (slot != null)
space = slot.space;
}
}
}

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


fileFormatVersion: 2
guid: 66199806246c45e493afdfc09208cbdc
timeCreated: 1509290088

30
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/TangentMaterialSlot.cs


using System;
using UnityEditor.Graphing;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class TangentMaterialSlot : SpaceMaterialSlot, IMayRequireTangent
{
public TangentMaterialSlot()
{}
public TangentMaterialSlot(int slotId, string displayName, string shaderOutputName, CoordinateSpace space,
ShaderStage shaderStage = ShaderStage.Dynamic, bool hidden = false)
: base(slotId, displayName, shaderOutputName, space, shaderStage, hidden)
{}
public override string GetDefaultValue(GenerationMode generationMode)
{
return space.ToVariableName(InterpolatorType.Tangent);
}
public NeededCoordinateSpace RequiresTangent()
{
if (isConnected)
return NeededCoordinateSpace.None;
return space.ToNeededCoordinateSpace();
}
}
}

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


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

72
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Texture2DInputMaterialSlot.cs


using System;
using UnityEditor.Graphing;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class Texture2DInputMaterialSlot : Texture2DMaterialSlot
{
[SerializeField]
private SerializableTexture m_Texture = new SerializableTexture();
public Texture texture
{
get { return m_Texture.texture; }
set { m_Texture.texture = value; }
}
public Texture2DInputMaterialSlot()
{}
public Texture2DInputMaterialSlot(
int slotId,
string displayName,
string shaderOutputName,
ShaderStage shaderStage = ShaderStage.Dynamic,
bool hidden = false)
: base(slotId, displayName, shaderOutputName, SlotType.Input, shaderStage, hidden)
{}
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 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 TextureShaderProperty();
prop.overrideReferenceName = matOwner.GetVariableNameForSlot(id);
prop.modifiable = false;
prop.generatePropertyBlock = true;
prop.value.texture = texture;
properties.AddShaderProperty(prop);
}
public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty
{
m_Name = name,
m_PropType = PropertyType.Texture,
m_Texture = texture
};
return pp;
}
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
var slot = foundSlot as Texture2DInputMaterialSlot;
if (slot != null)
m_Texture = slot.m_Texture;
}
}
}

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


fileFormatVersion: 2
guid: ebeb3a21d05d49caa5d3318fb49b7612
timeCreated: 1509276977

31
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Texture2DMaterialSlot.cs


using System;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class Texture2DMaterialSlot : MaterialSlot
{
public Texture2DMaterialSlot()
{}
public Texture2DMaterialSlot(
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.Texture2D; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Texture2D; } }
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{}
public override void CopyValuesFrom(MaterialSlot foundSlot)
{}
}
}

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


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

48
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/UVMaterialSlot.cs


using System;
using UnityEditor.Graphing;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class UVMaterialSlot : Vector2MaterialSlot, IMayRequireMeshUV
{
private UVChannel m_Channel = UVChannel.uv0;
public UVChannel channel
{
get { return m_Channel; }
set { m_Channel = value; }
}
public UVMaterialSlot()
{}
public UVMaterialSlot(int slotId, string displayName, string shaderOutputName, UVChannel channel,
ShaderStage shaderStage = ShaderStage.Dynamic, bool hidden = false)
: base(slotId, displayName, shaderOutputName, SlotType.Input, Vector2.zero, shaderStage, hidden)
{
this.channel = channel;
}
public override string GetDefaultValue(GenerationMode generationMode)
{
return string.Format("{0}.xy", channel.GetUVName());
}
public bool RequiresMeshUV(UVChannel channel)
{
if (isConnected)
return false;
return m_Channel == channel;
}
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
var slot = foundSlot as UVMaterialSlot;
if (slot != null)
channel = slot.channel;
}
}
}

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


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

87
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector1MaterialSlot.cs


using System;
using UnityEditor.Graphing;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class Vector1MaterialSlot : MaterialSlot, IMaterialSlotHasVaule<float>
{
[SerializeField]
private float m_Value;
[SerializeField]
private float m_DefaultValue;
public Vector1MaterialSlot()
{}
public Vector1MaterialSlot(
int slotId,
string displayName,
string shaderOutputName,
SlotType slotType,
float value,
ShaderStage shaderStage = ShaderStage.Dynamic,
bool hidden = false)
:base(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden)
{
m_DefaultValue = value;
m_Value = value;
}
public float defaultValue { get { return m_DefaultValue; } }
public float value
{
get { return m_Value; }
set { m_Value = value; }
}
protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
return value.ToString();
}
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{
if (!generationMode.IsPreview())
return;
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 property = new FloatShaderProperty()
{
overrideReferenceName = matOwner.GetVariableNameForSlot(id),
generatePropertyBlock = false,
value = value
};
properties.AddShaderProperty(property);
}
public override SlotValueType valueType { get { return SlotValueType.Vector1; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Vector1; } }
public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty
{
m_Name = name,
m_PropType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType),
m_Vector4 = new Vector4(value, value, value, value),
m_Float = value,
m_Color = new Vector4(value, value, value, value),
};
return pp;
}
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
var slot = foundSlot as Vector1MaterialSlot;
if (slot != null)
value = slot.value;
}
}
}

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


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

87
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector2MaterialSlot.cs


using System;
using UnityEditor.Graphing;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class Vector2MaterialSlot : MaterialSlot, IMaterialSlotHasVaule<Vector2>
{
[SerializeField]
private Vector2 m_Value;
[SerializeField]
private Vector2 m_DefaultValue;
public Vector2MaterialSlot()
{}
public Vector2MaterialSlot(
int slotId,
string displayName,
string shaderOutputName,
SlotType slotType,
Vector2 value,
ShaderStage shaderStage = ShaderStage.Dynamic,
bool hidden = false)
:base(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden)
{
m_Value = value;
}
public Vector2 defaultValue { get { return m_DefaultValue; } }
public Vector2 value
{
get { return m_Value; }
set { m_Value = value; }
}
protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
return precision + "2 (" + value.x + "," + value.y + ")";
}
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{
if (!generationMode.IsPreview())
return;
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 property = new Vector2ShaderProperty
{
overrideReferenceName = matOwner.GetVariableNameForSlot(id),
generatePropertyBlock = false,
value = value
};
properties.AddShaderProperty(property);
}
public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty
{
m_Name = name,
m_PropType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType),
m_Vector4 = new Vector4(value.x, value.y, 0, 0),
m_Float = value.x,
m_Color = new Vector4(value.x, value.x, 0, 0),
};
return pp;
}
public override SlotValueType valueType { get { return SlotValueType.Vector2; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Vector2; } }
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
var slot = foundSlot as Vector2MaterialSlot;
if (slot != null)
value = slot.value;
}
}
}

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


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

87
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector3MaterialSlot.cs


using System;
using UnityEditor.Graphing;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class Vector3MaterialSlot : MaterialSlot, IMaterialSlotHasVaule<Vector3>
{
[SerializeField]
private Vector3 m_Value;
[SerializeField]
private Vector3 m_DefaultValue;
public Vector3MaterialSlot()
{
}
public Vector3MaterialSlot(
int slotId,
string displayName,
string shaderOutputName,
SlotType slotType,
Vector3 value,
ShaderStage shaderStage = ShaderStage.Dynamic,
bool hidden = false)
:base(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden)
{
m_Value = value;
}
public Vector3 defaultValue { get { return m_DefaultValue; } }
public Vector3 value
{
get { return m_Value; }
set { m_Value = value; }
}
protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
return precision + "3 (" + value.x + "," + value.y + "," + value.z + ")";
}
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{
if (!generationMode.IsPreview())
return;
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 property = new Vector3ShaderProperty()
{
overrideReferenceName = matOwner.GetVariableNameForSlot(id),
generatePropertyBlock = false,
value = value
};
properties.AddShaderProperty(property);
}
public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty
{
m_Name = name,
m_PropType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType),
m_Vector4 = new Vector4(value.x, value.y, value.z, 0),
m_Float = value.x,
m_Color = new Vector4(value.x, value.x, value.z, 0),
};
return pp;
}
public override SlotValueType valueType { get { return SlotValueType.Vector3; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Vector3; } }
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
var slot = foundSlot as Vector3MaterialSlot;
if (slot != null)
value = slot.value;
}
}
}

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


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

88
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector4MaterialSlot.cs


using System;
using UnityEditor.Graphing;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class Vector4MaterialSlot : MaterialSlot, IMaterialSlotHasVaule<Vector4>
{
[SerializeField]
private Vector4 m_Value;
[SerializeField]
private Vector4 m_DefaultValue;
public Vector4MaterialSlot()
{
}
public Vector4MaterialSlot(
int slotId,
string displayName,
string shaderOutputName,
SlotType slotType,
Vector4 value,
ShaderStage shaderStage = ShaderStage.Dynamic,
bool hidden = false)
:base(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden)
{
m_Value = value;
}
public Vector4 defaultValue { get { return m_DefaultValue; } }
public Vector4 value
{
get { return m_Value; }
set { m_Value = value; }
}
protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
return precision + "4 (" + value.x + "," + value.y + "," + value.z + "," + value.w + ")";
}
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{
if (!generationMode.IsPreview())
return;
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 property = new Vector4ShaderProperty()
{
overrideReferenceName = matOwner.GetVariableNameForSlot(id),
generatePropertyBlock = false,
value = value
};
properties.AddShaderProperty(property);
}
public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty
{
m_Name = name,
m_PropType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType),
m_Vector4 = new Vector4(value.x, value.y, value.z, value.w),
m_Float = value.x,
m_Color = new Vector4(value.x, value.x, value.z, value.w),
};
return pp;
}
public override SlotValueType valueType { get { return SlotValueType.Vector4; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Vector4; } }
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
var slot = foundSlot as Vector4MaterialSlot;
if (slot != null)
value = slot.value;
}
}
}

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


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

25
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/VertexColorMaterialSlot.cs


using System;
using UnityEditor.Graphing;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class VertexColorMaterialSlot : Vector4MaterialSlot, IMayRequireScreenPosition
{
public VertexColorMaterialSlot(int slotId, string displayName, string shaderOutputName,
ShaderStage shaderStage = ShaderStage.Dynamic, bool hidden = false)
: base(slotId, displayName, shaderOutputName, SlotType.Input, Vector3.zero, shaderStage, hidden)
{ }
public override string GetDefaultValue(GenerationMode generationMode)
{
return ShaderGeneratorNames.VertexColor;
}
public bool RequiresScreenPosition()
{
return !isConnected;
}
}
}

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


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

30
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/ViewDirectionMaterialSlot.cs


using System;
using UnityEditor.Graphing;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class ViewDirectionMaterialSlot : SpaceMaterialSlot, IMayRequireViewDirection
{
public ViewDirectionMaterialSlot()
{}
public ViewDirectionMaterialSlot(int slotId, string displayName, string shaderOutputName, CoordinateSpace space,
ShaderStage shaderStage = ShaderStage.Dynamic, bool hidden = false)
: base(slotId, displayName, shaderOutputName, space, shaderStage, hidden)
{}
public override string GetDefaultValue(GenerationMode generationMode)
{
return space.ToVariableName(InterpolatorType.ViewDirection);
}
public NeededCoordinateSpace RequiresViewDirection()
{
if (isConnected)
return NeededCoordinateSpace.None;
return space.ToNeededCoordinateSpace();
}
}
}

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


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

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


using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using JetBrains.Annotations;
using UnityEngine;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
public abstract class CodeFunctionNode : AbstractMaterialNode
, IGeneratesBodyCode
, IGeneratesFunction
, IMayRequireNormal
, IMayRequireTangent
, IMayRequireBitangent
, IMayRequireMeshUV
, IMayRequireScreenPosition
, IMayRequireViewDirection
, IMayRequirePosition
, IMayRequireVertexColor
{
[NonSerialized]
private List<SlotAttribute> m_Slots = new List<SlotAttribute>();
public override bool hasPreview
{
get { return true; }
}
protected CodeFunctionNode()
{
UpdateNodeAfterDeserialization();
}
protected struct Vector1
{}
protected struct Texture2D
{}
protected struct SamplerState
{}
protected struct DynamicDimensionVector
{}
protected enum Binding
{
None,
ObjectSpaceNormal,
ObjectSpaceTangent,
ObjectSpaceBitangent,
ObjectSpacePosition,
ViewSpaceNormal,
ViewSpaceTangent,
ViewSpaceBitangent,
ViewSpacePosition,
WorldSpaceNormal,
WorldSpaceTangent,
WorldSpaceBitangent,
WorldSpacePosition,
TangentSpaceNormal,
TangentSpaceTangent,
TangentSpaceBitangent,
TangentSpacePosition,
MeshUV0,
MeshUV1,
MeshUV2,
MeshUV3,
ScreenPosition,
ObjectSpaceViewDirection,
ViewSpaceViewDirection,
WorldSpaceViewDirection,
TangentSpaceViewDirection,
VertexColor,
}
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)]
protected class SlotAttribute : Attribute
{
public int slotId { get; private set; }
public Binding binding { get; private set; }
public bool hidden { get; private set; }
public Vector4? defaultValue { get; private set; }
public SlotAttribute(int mSlotId, Binding mImplicitBinding)
{
slotId = mSlotId;
binding = mImplicitBinding;
defaultValue = null;
}
public SlotAttribute(int mSlotId, Binding mImplicitBinding, bool mHidden)
{
slotId = mSlotId;
binding = mImplicitBinding;
hidden = mHidden;
defaultValue = null;
}
public SlotAttribute(int mSlotId, Binding mImplicitBinding, float defaultX, float defaultY, float defaultZ, float defaultW)
{
slotId = mSlotId;
binding = mImplicitBinding;
defaultValue = new Vector4(defaultX, defaultY, defaultZ, defaultW);
}
}
protected abstract MethodInfo GetFunctionToConvert();
private static SlotValueType ConvertTypeToSlotValueType(ParameterInfo p)
{
Type t = p.ParameterType;
if (p.ParameterType.IsByRef)
t = p.ParameterType.GetElementType();
if (t == typeof(Vector1))
{
return SlotValueType.Vector1;
}
if (t == typeof(Vector2))
{
return SlotValueType.Vector2;
}
if (t == typeof(Vector3))
{
return SlotValueType.Vector3;
}
if (t == typeof(Vector4))
{
return SlotValueType.Vector4;
}
if (t == typeof(Texture2D))
{
return SlotValueType.Texture2D;
}
if (t == typeof(SamplerState))
{
return SlotValueType.SamplerState;
}
if (t == typeof(DynamicDimensionVector))
{
return SlotValueType.Dynamic;
}
if (t == typeof(Matrix4x4))
{
return SlotValueType.Matrix4;
}
throw new ArgumentException("Unsupported type " + t);
}
public sealed override void UpdateNodeAfterDeserialization()
{
var method = GetFunctionToConvert();
if (method == null)
throw new ArgumentException("Mapped method is null on node" + this);
if (method.ReturnType != typeof(string))
throw new ArgumentException("Mapped function should return string");
// validate no duplicates
var slotAtributes = method.GetParameters().Select(GetSlotAttribute).ToList();
if (slotAtributes.Any(x => x == null))
throw new ArgumentException("Missing SlotAttribute on " + method.Name);
if (slotAtributes.GroupBy(x => x.slotId).Any(x => x.Count() > 1))
throw new ArgumentException("Duplicate SlotAttribute on " + method.Name);
List<MaterialSlot> slots = new List<MaterialSlot>();
foreach (var par in method.GetParameters())
{
var attribute = GetSlotAttribute(par);
MaterialSlot s;
if (attribute.binding == Binding.None || par.IsOut)
{
s = MaterialSlot.CreateMaterialSlot(
ConvertTypeToSlotValueType(par),
attribute.slotId,
par.Name,
par.Name,
par.IsOut ? SlotType.Output : SlotType.Input,
attribute.defaultValue ?? Vector4.zero,
hidden: attribute.hidden);
}
else
{
s = CreateBoundSlot(attribute.binding, attribute.slotId, par.Name, par.Name, attribute.hidden);
}
slots.Add(s);
m_Slots.Add(attribute);
}
foreach (var slot in slots)
{
AddSlot(slot);
}
RemoveSlotsNameNotMatching(slots.Select(x => x.id));
}
private static MaterialSlot CreateBoundSlot(Binding attributeBinding, int slotId, string displayName, string shaderOutputName, bool hidden)
{
switch (attributeBinding)
{
case Binding.ObjectSpaceNormal:
return new NormalMaterialSlot(slotId, displayName, shaderOutputName, CoordinateSpace.Object);
case Binding.ObjectSpaceTangent:
return new TangentMaterialSlot(slotId, displayName, shaderOutputName, CoordinateSpace.Object);
case Binding.ObjectSpaceBitangent:
return new BitangentMaterialSlot(slotId, displayName, shaderOutputName, CoordinateSpace.Object);
case Binding.ObjectSpacePosition:
return new PositionMaterialSlot(slotId, displayName, shaderOutputName, CoordinateSpace.Object);
case Binding.ViewSpaceNormal:
return new NormalMaterialSlot(slotId, displayName, shaderOutputName, CoordinateSpace.View);
case Binding.ViewSpaceTangent:
return new TangentMaterialSlot(slotId, displayName, shaderOutputName, CoordinateSpace.View);
case Binding.ViewSpaceBitangent:
return new BitangentMaterialSlot(slotId, displayName, shaderOutputName, CoordinateSpace.View);
case Binding.ViewSpacePosition:
return new PositionMaterialSlot(slotId, displayName, shaderOutputName, CoordinateSpace.View);
case Binding.WorldSpaceNormal:
return new NormalMaterialSlot(slotId, displayName, shaderOutputName, CoordinateSpace.World);
case Binding.WorldSpaceTangent:
return new TangentMaterialSlot(slotId, displayName, shaderOutputName, CoordinateSpace.World);
case Binding.WorldSpaceBitangent:
return new BitangentMaterialSlot(slotId, displayName, shaderOutputName, CoordinateSpace.World);
case Binding.WorldSpacePosition:
return new PositionMaterialSlot(slotId, displayName, shaderOutputName, CoordinateSpace.World);
case Binding.TangentSpaceNormal:
return new NormalMaterialSlot(slotId, displayName, shaderOutputName, CoordinateSpace.Tangent);
case Binding.TangentSpaceTangent:
return new TangentMaterialSlot(slotId, displayName, shaderOutputName, CoordinateSpace.Tangent);
case Binding.TangentSpaceBitangent:
return new BitangentMaterialSlot(slotId, displayName, shaderOutputName, CoordinateSpace.Tangent);
case Binding.TangentSpacePosition:
return new PositionMaterialSlot(slotId, displayName, shaderOutputName, CoordinateSpace.Tangent);
case Binding.MeshUV0:
return new UVMaterialSlot(slotId, displayName, shaderOutputName, UVChannel.uv0);
case Binding.MeshUV1:
return new UVMaterialSlot(slotId, displayName, shaderOutputName, UVChannel.uv1);
case Binding.MeshUV2:
return new UVMaterialSlot(slotId, displayName, shaderOutputName, UVChannel.uv2);
case Binding.MeshUV3:
return new UVMaterialSlot(slotId, displayName, shaderOutputName, UVChannel.uv3);
case Binding.ScreenPosition:
return new ScreenPositionMaterialSlot(slotId, displayName, shaderOutputName);
case Binding.ObjectSpaceViewDirection:
return new ViewDirectionMaterialSlot(slotId, displayName, shaderOutputName, CoordinateSpace.Object);
case Binding.ViewSpaceViewDirection:
return new ViewDirectionMaterialSlot(slotId, displayName, shaderOutputName, CoordinateSpace.View);
case Binding.WorldSpaceViewDirection:
return new ViewDirectionMaterialSlot(slotId, displayName, shaderOutputName, CoordinateSpace.World);
case Binding.TangentSpaceViewDirection:
return new ViewDirectionMaterialSlot(slotId, displayName, shaderOutputName, CoordinateSpace.Tangent);
case Binding.VertexColor:
return new VertexColorMaterialSlot(slotId, displayName, shaderOutputName);
default:
throw new ArgumentOutOfRangeException("attributeBinding", attributeBinding, null);
}
}
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)
{
foreach (var outSlot in GetOutputSlots<MaterialSlot>())
{
visitor.AddShaderChunk(GetParamTypeName(outSlot) + " " + GetVariableNameForSlot(outSlot.id) + ";", true);
}
string call = GetFunctionName() + "(";
bool first = true;
foreach (var slot in GetSlots<MaterialSlot>().OrderBy(x => x.id))
{
if (!first)
{
call += ", ";
}
first = false;
if (slot.isInputSlot)
call += GetSlotValue(slot.id, generationMode);
else
call += GetVariableNameForSlot(slot.id);
}
call += ");";
visitor.AddShaderChunk(call, true);
}
private string GetParamTypeName(MaterialSlot slot)
{
return ConvertConcreteSlotValueTypeToString(precision, slot.concreteValueType);
}
private string GetFunctionName()
{
var function = GetFunctionToConvert();
return function.Name + "_" + (function.IsStatic ? string.Empty : GuidEncoder.Encode(guid) + "_") + precision;
}
private string GetFunctionHeader()
{
string header = "void " + GetFunctionName() + "(";
var first = true;
foreach (var slot in GetSlots<MaterialSlot>().OrderBy(x => x.id))
{
if (!first)
header += ", ";
first = false;
if (slot.isOutputSlot)
header += "out ";
header += GetParamTypeName(slot) + " " + slot.shaderOutputName;
}
header += ")";
return header;
}
private static object GetDefault(Type type)
{
return type.IsValueType ? Activator.CreateInstance(type) : null;
}
private string GetFunctionBody(MethodInfo info)
{
var args = new List<object>();
foreach (var param in info.GetParameters())
args.Add(GetDefault(param.ParameterType));
var result = info.Invoke(this, args.ToArray()) as string;
if (string.IsNullOrEmpty(result))
return string.Empty;
result = result.Replace("{precision}", precision.ToString());
foreach (var slot in GetSlots<MaterialSlot>())
{
var toReplace = string.Format("{{slot{0}dimension}}", slot.id);
var replacement = GetSlotDimension(slot.concreteValueType);
result = result.Replace(toReplace, replacement);
}
return result;
}
public virtual void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
{
string function = GetFunctionHeader() + GetFunctionBody(GetFunctionToConvert());
visitor.AddShaderChunk(function, true);
}
private static SlotAttribute GetSlotAttribute([NotNull] ParameterInfo info)
{
var attrs = info.GetCustomAttributes(typeof(SlotAttribute), false).OfType<SlotAttribute>().ToList();
return attrs.FirstOrDefault();
}
public NeededCoordinateSpace RequiresNormal()
{
var binding = NeededCoordinateSpace.None;
foreach (var slot in GetInputSlots<MaterialSlot>().OfType<IMayRequireNormal>())
binding |= slot.RequiresNormal();
return binding;
}
public NeededCoordinateSpace RequiresViewDirection()
{
var binding = NeededCoordinateSpace.None;
foreach (var slot in GetInputSlots<MaterialSlot>().OfType<IMayRequireViewDirection>())
binding |= slot.RequiresViewDirection();
return binding;
}
public NeededCoordinateSpace RequiresPosition()
{
var binding = NeededCoordinateSpace.None;
foreach (var slot in GetInputSlots<MaterialSlot>().OfType<IMayRequirePosition>())
binding |= slot.RequiresPosition();
return binding;
}
public NeededCoordinateSpace RequiresTangent()
{
var binding = NeededCoordinateSpace.None;
foreach (var slot in GetInputSlots<MaterialSlot>().OfType<IMayRequireTangent>())
binding |= slot.RequiresTangent();
return binding;
}
public NeededCoordinateSpace RequiresBitangent()
{
var binding = NeededCoordinateSpace.None;
foreach (var slot in GetInputSlots<MaterialSlot>().OfType<IMayRequireBitangent>())
binding |= slot.RequiresBitangent();
return binding;
}
public bool RequiresMeshUV(UVChannel channel)
{
foreach (var slot in GetInputSlots<MaterialSlot>().OfType<IMayRequireMeshUV>())
{
if (slot.RequiresMeshUV(channel))
return true;
}
return false;
}
public bool RequiresScreenPosition()
{
foreach (var slot in GetInputSlots<MaterialSlot>().OfType<IMayRequireScreenPosition>())
{
if (slot.RequiresScreenPosition())
return true;
}
return false;
}
public bool RequiresVertexColor()
{
foreach (var slot in GetInputSlots<MaterialSlot>().OfType<IMayRequireVertexColor>())
{
if (slot.RequiresVertexColor())
return true;
}
return false;
}
}
}

507
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/HLSLNode.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using JetBrains.Annotations;
using UnityEngine;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
public abstract class CodeFunctionNode : AbstractMaterialNode
, IGeneratesBodyCode
, IGeneratesFunction
, IMayRequireNormal
, IMayRequireTangent
, IMayRequireBitangent
, IMayRequireMeshUV
, IMayRequireScreenPosition
, IMayRequireViewDirection
, IMayRequirePosition
, IMayRequireVertexColor
{
[NonSerialized]
private List<SlotAttribute> m_Slots = new List<SlotAttribute>();
public override bool hasPreview
{
get { return true; }
}
protected CodeFunctionNode()
{
UpdateNodeAfterDeserialization();
}
protected struct Vector1
{}
protected struct Texture2D
{}
protected struct SamplerState
{}
protected struct DynamicDimensionVector
{}
protected enum Binding
{
None,
ObjectSpaceNormal,
ObjectSpaceTangent,
ObjectSpaceBitangent,
ObjectSpacePosition,
ViewSpaceNormal,
ViewSpaceTangent,
ViewSpaceBitangent,
ViewSpacePosition,
WorldSpaceNormal,
WorldSpaceTangent,
WorldSpaceBitangent,
WorldSpacePosition,
TangentSpaceNormal,
TangentSpaceTangent,
TangentSpaceBitangent,
TangentSpacePosition,
MeshUV0,
MeshUV1,
MeshUV2,
MeshUV3,
ScreenPosition,
ObjectSpaceViewDirection,
ViewSpaceViewDirection,
WorldSpaceViewDirection,
TangentSpaceViewDirection,
VertexColor,
}
private static string BindChannelToShaderName(Binding channel)
{
switch (channel)
{
case Binding.None:
return "ERROR!";
case Binding.ObjectSpaceNormal:
return CoordinateSpace.Object.ToVariableName(InterpolatorType.Normal);
case Binding.ObjectSpaceTangent:
return CoordinateSpace.Object.ToVariableName(InterpolatorType.Tangent);
case Binding.ObjectSpaceBitangent:
return CoordinateSpace.Object.ToVariableName(InterpolatorType.BiTangent);
case Binding.ObjectSpacePosition:
return CoordinateSpace.Object.ToVariableName(InterpolatorType.Position);
case Binding.ViewSpaceNormal:
return CoordinateSpace.View.ToVariableName(InterpolatorType.Normal);
case Binding.ViewSpaceTangent:
return CoordinateSpace.View.ToVariableName(InterpolatorType.Tangent);
case Binding.ViewSpaceBitangent:
return CoordinateSpace.View.ToVariableName(InterpolatorType.BiTangent);
case Binding.ViewSpacePosition:
return CoordinateSpace.View.ToVariableName(InterpolatorType.Position);
case Binding.WorldSpaceNormal:
return CoordinateSpace.World.ToVariableName(InterpolatorType.Normal);
case Binding.WorldSpaceTangent:
return CoordinateSpace.World.ToVariableName(InterpolatorType.Tangent);
case Binding.WorldSpaceBitangent:
return CoordinateSpace.World.ToVariableName(InterpolatorType.BiTangent);
case Binding.WorldSpacePosition:
return CoordinateSpace.World.ToVariableName(InterpolatorType.Position);
case Binding.TangentSpaceNormal:
return CoordinateSpace.Tangent.ToVariableName(InterpolatorType.Normal);
case Binding.TangentSpaceTangent:
return CoordinateSpace.Tangent.ToVariableName(InterpolatorType.Tangent);
case Binding.TangentSpaceBitangent:
return CoordinateSpace.Tangent.ToVariableName(InterpolatorType.BiTangent);
case Binding.TangentSpacePosition:
return CoordinateSpace.Tangent.ToVariableName(InterpolatorType.Position);
case Binding.MeshUV0:
return UVChannel.uv0.GetUVName();
case Binding.MeshUV1:
return UVChannel.uv1.GetUVName();
case Binding.MeshUV2:
return UVChannel.uv2.GetUVName();
case Binding.MeshUV3:
return UVChannel.uv3.GetUVName();
case Binding.ScreenPosition:
return ShaderGeneratorNames.ScreenPosition;
case Binding.ObjectSpaceViewDirection:
return CoordinateSpace.Object.ToVariableName(InterpolatorType.ViewDirection);
case Binding.ViewSpaceViewDirection:
return CoordinateSpace.View.ToVariableName(InterpolatorType.ViewDirection);
case Binding.WorldSpaceViewDirection:
return CoordinateSpace.View.ToVariableName(InterpolatorType.ViewDirection);
case Binding.TangentSpaceViewDirection:
return CoordinateSpace.View.ToVariableName(InterpolatorType.ViewDirection);
case Binding.VertexColor:
return ShaderGeneratorNames.VertexColor;
default:
throw new ArgumentOutOfRangeException("channel", channel, null);
}
}
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)]
protected class SlotAttribute : Attribute
{
public int slotId { get; private set; }
public Binding binding { get; private set; }
public bool hidden { get; private set; }
public Vector4? defaultValue { get; private set; }
public SlotAttribute(int mSlotId, Binding mImplicitBinding)
{
slotId = mSlotId;
binding = mImplicitBinding;
defaultValue = null;
}
public SlotAttribute(int mSlotId, Binding mImplicitBinding, bool mHidden)
{
slotId = mSlotId;
binding = mImplicitBinding;
hidden = mHidden;
defaultValue = null;
}
public SlotAttribute(int mSlotId, Binding mImplicitBinding, float defaultX, float defaultY, float defaultZ, float defaultW)
{
slotId = mSlotId;
binding = mImplicitBinding;
defaultValue = new Vector4(defaultX, defaultY, defaultZ, defaultW);
}
}
protected static MethodInfo GetMethodInfo(LambdaExpression expression)
{
MethodCallExpression outermostExpression = expression.Body as MethodCallExpression;
 
if (outermostExpression == null)
{
throw new ArgumentException("Invalid Expression. Expression should consist of a Method call only.");
}
 
return outermostExpression.Method;
}
protected abstract MethodInfo GetFunctionToConvert();
private static SlotValueType ConvertTypeToSlotValueType(ParameterInfo p)
{
Type t = p.ParameterType;
if (p.ParameterType.IsByRef)
t = p.ParameterType.GetElementType();
if (t == typeof(Vector1))
{
return SlotValueType.Vector1;
}
if (t == typeof(Vector2))
{
return SlotValueType.Vector2;
}
if (t == typeof(Vector3))
{
return SlotValueType.Vector3;
}
if (t == typeof(Vector4))
{
return SlotValueType.Vector4;
}
if (t == typeof(Texture2D))
{
return SlotValueType.Texture2D;
}
if (t == typeof(SamplerState))
{
return SlotValueType.SamplerState;
}
if (t == typeof(DynamicDimensionVector))
{
return SlotValueType.Dynamic;
}
if (t == typeof(Matrix4x4))
{
return SlotValueType.Matrix4;
}
throw new ArgumentException("Unsupported type " + t);
}
public sealed override void UpdateNodeAfterDeserialization()
{
var method = GetFunctionToConvert();
if (method == null)
throw new ArgumentException("Mapped method is null on node" + this);
if (method.ReturnType != typeof(string))
throw new ArgumentException("Mapped function should return string");
// validate no duplicates
var slotAtributes = method.GetParameters().Select(GetSlotAttribute).ToList();
if (slotAtributes.Any(x => x == null))
throw new ArgumentException("Missing SlotAttribute on " + method.Name);
if (slotAtributes.GroupBy(x => x.slotId).Any(x => x.Count() > 1))
throw new ArgumentException("Duplicate SlotAttribute on " + method.Name);
List<MaterialSlot> slots = new List<MaterialSlot>();
foreach (var par in method.GetParameters())
{
var attribute = GetSlotAttribute(par);
slots.Add(MaterialSlot.CreateMaterialSlot(ConvertTypeToSlotValueType(par), attribute.slotId, par.Name, par.Name, par.IsOut ? SlotType.Output : SlotType.Input,
attribute.defaultValue ?? Vector4.zero, hidden: attribute.hidden));
m_Slots.Add(attribute);
}
foreach (var slot in slots)
{
AddSlot(slot);
}
RemoveSlotsNameNotMatching(slots.Select(x => x.id));
}
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)
{
foreach (var outSlot in GetOutputSlots<MaterialSlot>())
{
visitor.AddShaderChunk(GetParamTypeName(outSlot) + " " + GetVariableNameForSlot(outSlot.id) + ";", true);
}
string call = GetFunctionName() + "(";
bool first = true;
foreach (var arg in GetSlots<MaterialSlot>().OrderBy(x => x.id))
{
if (!first)
{
call += ", ";
}
first = false;
if (arg.isInputSlot)
{
var inEdges = owner.GetEdges(arg.slotReference);
if (!inEdges.Any())
{
var info = m_Slots.FirstOrDefault(x => x.slotId == arg.id);
if (info != null)
{
var bindingInfo = info.binding;
if (bindingInfo != Binding.None)
{
call += BindChannelToShaderName(bindingInfo);
continue;
}
}
}
call += GetSlotValue(arg.id, generationMode);
}
else
call += GetVariableNameForSlot(arg.id);
}
call += ");";
visitor.AddShaderChunk(call, true);
}
private string GetParamTypeName(MaterialSlot slot)
{
return ConvertConcreteSlotValueTypeToString(precision, slot.concreteValueType);
}
private string GetFunctionName()
{
var function = GetFunctionToConvert();
return function.Name + "_" + (function.IsStatic ? string.Empty : GuidEncoder.Encode(guid) + "_") + precision;
}
private string GetFunctionHeader()
{
string header = "void " + GetFunctionName() + "(";
var first = true;
foreach (var slot in GetSlots<MaterialSlot>().OrderBy(x => x.id))
{
if (!first)
header += ", ";
first = false;
if (slot.isOutputSlot)
header += "out ";
header += GetParamTypeName(slot) + " " + slot.shaderOutputName;
}
header += ")";
return header;
}
private static object GetDefault(Type type)
{
return type.IsValueType ? Activator.CreateInstance(type) : null;
}
private string GetFunctionBody(MethodInfo info)
{
var args = new List<object>();
foreach (var param in info.GetParameters())
args.Add(GetDefault(param.ParameterType));
var result = info.Invoke(this, args.ToArray()) as string;
if (string.IsNullOrEmpty(result))
return string.Empty;
result = result.Replace("{precision}", precision.ToString());
foreach (var slot in GetSlots<MaterialSlot>())
{
var toReplace = string.Format("{{slot{0}dimension}}", slot.id);
var replacement = GetSlotDimension(slot.concreteValueType);
result = result.Replace(toReplace, replacement);
}
return result;
}
public virtual void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
{
string function = GetFunctionHeader() + GetFunctionBody(GetFunctionToConvert());
visitor.AddShaderChunk(function, true);
}
private bool NodeRequiresBinding(Binding channel)
{
foreach (var slot in GetSlots<MaterialSlot>())
{
if (SlotRequiresBinding(channel, slot))
return true;
}
return false;
}
private bool SlotRequiresBinding(Binding channel, [NotNull] MaterialSlot slot)
{
if (slot.isOutputSlot)
return false;
var inEdges = owner.GetEdges(slot.slotReference);
if (inEdges.Any())
return false;
var slotAttr = m_Slots.FirstOrDefault(x => x.slotId == slot.id);
if (slotAttr != null && slotAttr.binding == channel)
return true;
return false;
}
private static SlotAttribute GetSlotAttribute([NotNull] ParameterInfo info)
{
var attrs = info.GetCustomAttributes(typeof(SlotAttribute), false).OfType<SlotAttribute>().ToList();
return attrs.FirstOrDefault();
}
public NeededCoordinateSpace RequiresNormal()
{
var binding = NeededCoordinateSpace.None;
if (NodeRequiresBinding(Binding.ObjectSpaceNormal))
binding |= NeededCoordinateSpace.Object;
if (NodeRequiresBinding(Binding.ViewSpaceNormal))
binding |= NeededCoordinateSpace.View;
if (NodeRequiresBinding(Binding.WorldSpaceNormal))
binding |= NeededCoordinateSpace.World;
if (NodeRequiresBinding(Binding.TangentSpaceNormal))
binding |= NeededCoordinateSpace.Tangent;
return binding;
}
public bool RequiresMeshUV(UVChannel channel)
{
switch (channel)
{
case UVChannel.uv0:
return NodeRequiresBinding(Binding.MeshUV0);
case UVChannel.uv1:
return NodeRequiresBinding(Binding.MeshUV1);
case UVChannel.uv2:
return NodeRequiresBinding(Binding.MeshUV2);
case UVChannel.uv3:
return NodeRequiresBinding(Binding.MeshUV3);
default:
throw new ArgumentOutOfRangeException("channel", channel, null);
}
}
public bool RequiresScreenPosition()
{
return NodeRequiresBinding(Binding.ScreenPosition);
}
public NeededCoordinateSpace RequiresViewDirection()
{
var binding = NeededCoordinateSpace.None;
if (NodeRequiresBinding(Binding.ObjectSpaceViewDirection))
binding |= NeededCoordinateSpace.Object;
if (NodeRequiresBinding(Binding.ViewSpaceViewDirection))
binding |= NeededCoordinateSpace.View;
if (NodeRequiresBinding(Binding.WorldSpaceViewDirection))
binding |= NeededCoordinateSpace.World;
if (NodeRequiresBinding(Binding.TangentSpaceNormal))
binding |= NeededCoordinateSpace.Tangent;
return binding;
}
public NeededCoordinateSpace RequiresPosition()
{
var binding = NeededCoordinateSpace.None;
if (NodeRequiresBinding(Binding.ObjectSpacePosition))
binding |= NeededCoordinateSpace.Object;
if (NodeRequiresBinding(Binding.ViewSpacePosition))
binding |= NeededCoordinateSpace.View;
if (NodeRequiresBinding(Binding.WorldSpacePosition))
binding |= NeededCoordinateSpace.World;
if (NodeRequiresBinding(Binding.TangentSpacePosition))
binding |= NeededCoordinateSpace.Tangent;
return binding;
}
public NeededCoordinateSpace RequiresTangent()
{
var binding = NeededCoordinateSpace.None;
if (NodeRequiresBinding(Binding.ObjectSpaceTangent))
binding |= NeededCoordinateSpace.Object;
if (NodeRequiresBinding(Binding.ViewSpaceTangent))
binding |= NeededCoordinateSpace.View;
if (NodeRequiresBinding(Binding.WorldSpaceTangent))
binding |= NeededCoordinateSpace.World;
if (NodeRequiresBinding(Binding.TangentSpaceTangent))
binding |= NeededCoordinateSpace.Tangent;
return binding;
}
public NeededCoordinateSpace RequiresBitangent()
{
var binding = NeededCoordinateSpace.None;
if (NodeRequiresBinding(Binding.ObjectSpaceBitangent))
binding |= NeededCoordinateSpace.Object;
if (NodeRequiresBinding(Binding.ViewSpaceBitangent))
binding |= NeededCoordinateSpace.View;
if (NodeRequiresBinding(Binding.WorldSpaceBitangent))
binding |= NeededCoordinateSpace.World;
if (NodeRequiresBinding(Binding.TangentSpaceBitangent))
binding |= NeededCoordinateSpace.Tangent;
return binding;
}
public bool RequiresVertexColor()
{
return NodeRequiresBinding(Binding.VertexColor);
}
}
}

/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/HLSLNode.cs.meta → /MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/CodeFunctionNode.cs.meta

正在加载...
取消
保存