浏览代码

[shader graph]Data refactor 3

/main
Tim Cooper 9 年前
当前提交
af2e75ab
共有 35 个文件被更改,包括 987 次插入681 次删除
  1. 1
      UnityProject/Assets/UnityShaderEditor/Editor/Source/AssetCallbacks/CreateShaderGraph.cs
  2. 106
      UnityProject/Assets/UnityShaderEditor/Editor/Source/BaseMaterialGraph.cs
  3. 14
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/DrawableMaterialNode.cs
  4. 18
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/MaterialGraphDataSource.cs
  5. 15
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/MaterialWindow.cs
  6. 8
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/NodeAnchor.cs
  7. 12
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/NullInputProxy.cs
  8. 55
      UnityProject/Assets/UnityShaderEditor/Editor/Source/MaterialGraph.cs
  9. 5
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/AddNode.cs
  10. 875
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/BaseMaterialNode.cs
  11. 17
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/Function2Input.cs
  12. 39
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/PixelShaderNode.cs
  13. 61
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/PropertyNode.cs
  14. 26
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/Slot.cs
  15. 53
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/Vector4Node.cs
  16. 41
      UnityProject/Assets/UnityShaderEditor/Editor/Source/PixelGraph.cs
  17. 47
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Util/NodeUtils.cs
  18. 30
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Util/ShaderGenerator.cs
  19. 30
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Edge.cs
  20. 12
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Edge.cs.meta
  21. 8
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/DrawMode.cs
  22. 12
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/DrawMode.cs.meta
  23. 9
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/Precision.cs
  24. 12
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/Precision.cs.meta
  25. 8
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/PreviewMode.cs
  26. 12
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/PreviewMode.cs.meta
  27. 15
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/PreviewProperty.cs
  28. 12
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/PreviewProperty.cs.meta
  29. 12
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/PropertyType.cs
  30. 12
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/PropertyType.cs.meta
  31. 12
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/Slot.cs.meta
  32. 11
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/TitleAttribute.cs
  33. 12
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/TitleAttribute.cs.meta
  34. 44
      UnityProject/Assets/UnityShaderEditor/Editor/Source/SlotReference.cs
  35. 12
      UnityProject/Assets/UnityShaderEditor/Editor/Source/SlotReference.cs.meta

1
UnityProject/Assets/UnityShaderEditor/Editor/Source/AssetCallbacks/CreateShaderGraph.cs


var graph = CreateInstance<MaterialGraph>();
graph.name = Path.GetFileName(pathName);
AssetDatabase.CreateAsset(graph, pathName);
graph.AddSubAssetsToAsset();
}
}
}

106
UnityProject/Assets/UnityShaderEditor/Editor/Source/BaseMaterialGraph.cs


namespace UnityEditor.MaterialGraph
{
[Serializable]
public class SlotReference
public abstract class BaseMaterialGraph : ISerializationCallbackReceiver
private string m_NodeGUIDSerialized;
[NonSerialized]
private GUID m_NodeGUID;
private List<Edge> m_Edges = new List<Edge>();
private string m_SlotName;
private List<BaseMaterialNode> m_Nodes = new List<BaseMaterialNode>();
public SlotReference(GUID nodeGuid, string slotName)
{
m_NodeGUID = nodeGuid;
m_SlotName = slotName;
}
private PreviewRenderUtility m_PreviewUtility;
public GUID nodeGuid
{
get { return m_NodeGUID; }
}
private MaterialGraph m_Owner;
public string slotName
{
get { return m_SlotName; }
}
public void BeforeSerialize()
public BaseMaterialGraph(MaterialGraph owner)
m_NodeGUIDSerialized = m_NodeGUID.ToString();
}
public void AfterDeserialize()
{
m_NodeGUID = new GUID(m_NodeGUIDSerialized);
}
}
[Serializable]
public class Edge
{
[SerializeField]
private SlotReference m_OutputSlot;
[SerializeField]
private SlotReference m_InputSlot;
public Edge(SlotReference outputSlot, SlotReference inputSlot)
{
m_OutputSlot = outputSlot;
m_InputSlot = inputSlot;
}
public SlotReference outputSlot
{
get { return m_OutputSlot; }
m_Owner = owner;
public SlotReference inputSlot
{
get { return m_InputSlot; }
}
}
public IEnumerable<BaseMaterialNode> nodes { get { return m_Nodes; } }
public IEnumerable<Edge> edges { get { return m_Edges; } }
public abstract class BaseMaterialGraph
{
private PreviewRenderUtility m_PreviewUtility;
public PreviewRenderUtility previewUtility
{
get

m_PreviewUtility = new PreviewRenderUtility();
// EditorUtility.SetCameraAnimateMaterials(m_PreviewUtility.m_Camera, true);
// EditorUtility.SetCameraAnimateMaterials(m_PreviewUtility.m_Camera, true);
}
return m_PreviewUtility;

private List<BaseMaterialNode> m_Nodes = new List<BaseMaterialNode>();
private List<Edge> m_Edges = new List<Edge>();
protected List<BaseMaterialNode> nodes
public bool requiresRepaint
get
{
return m_Nodes;
}
}
get { return m_Nodes.Any(x => x is IRequiresTime); }
}
public bool requiresRepaint
public MaterialGraph owner
get { return nodes.Any(x => x is IRequiresTime); }
get { return m_Owner; }
public void RemoveEdge(Edge e)
{
m_Edges.Remove(e);

m_Nodes.Remove(node);
}
public BaseMaterialNode GetNodeFromGUID(GUID guid)
public BaseMaterialNode GetNodeFromGUID(Guid guid)
{
return m_Nodes.FirstOrDefault(x => x.guid == guid);
}

return m_Edges.Where(x =>
(x.outputSlot.nodeGuid == s.nodeGuid && x.outputSlot.slotName == s.name)
(x.outputSlot.nodeGuid == s.nodeGuid && x.outputSlot.slotName == s.name)
|| x.inputSlot.nodeGuid == s.nodeGuid && x.inputSlot.slotName == s.name);
}

// do expensive shader regeneration
RemoveEdge(edge);
}
var newEdge = new Edge(new SlotReference(outputSlot.nodeGuid, outputSlot.name), new SlotReference(inputSlot.nodeGuid, inputSlot.name));
m_Edges.Add(newEdge);

public virtual void RevalidateGraph()
{
var bmns = nodes.Where(x => x is BaseMaterialNode).Cast<BaseMaterialNode>().ToList();
var bmns = m_Nodes.Where(x => x is BaseMaterialNode).Cast<BaseMaterialNode>().ToList();
{
}
}
public void AddNode(BaseMaterialNode node)

}
public void OnBeforeSerialize()
{
}
public void OnAfterDeserialize()
{
foreach (var node in nodes)
{
node.owner = this;
}
}
}
}

14
UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/DrawableMaterialNode.cs


foreach (var slot in node.inputSlots)
{
pos.y += 22;
AddChild(new NodeAnchor(pos, typeof (Vector4), slot, data, Direction.Input));
AddChild(new NodeAnchor(pos, typeof (Vector4), node, slot, data, Direction.Input));
}
var inputYMax = pos.y + 22;

foreach (var slot in node.outputSlots)
{
var edges = node.owner.GetEdges(slot);
if (node.drawMode == DrawMode.Collapsed && slot.edges.Count == 0)
if (node.drawMode == DrawMode.Collapsed && !edges.Any())
AddChild(new NodeAnchor(pos, typeof (Vector4), slot, data, Direction.Output));
AddChild(new NodeAnchor(pos, typeof (Vector4), node, slot, data, Direction.Output));
}
pos.y += 22;

var pos = m_Node.position;
pos.min = translation;
m_Node.position = pos;
EditorUtility.SetDirty (m_Node);
}
public override void Render(Rect parentRect, Canvas2D canvas)

return;
}
//TODO:DATA
/*
if (m_Node.hasPreview
&& m_Node.drawMode != DrawMode.Collapsed
&& m_PreviewArea.width > 0

GUI.DrawTexture(m_PreviewArea, m_Node.RenderPreview(new Rect(0, 0, m_PreviewArea.width, m_PreviewArea.height)), ScaleMode.StretchToFill, false);
GL.sRGBWrite = false;
}
}*/
base.Render(parentRect, canvas);
}

18
UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/MaterialGraphDataSource.cs


using System.Linq;
using UnityEditor.Experimental;
using UnityEditor.Experimental.Graph;
using UnityEditor.Graphs;
using UnityEngine;
namespace UnityEditor.MaterialGraph

{
var sourceAnchor = (NodeAnchor)drawableMaterialNode.Children().FirstOrDefault(x => x is NodeAnchor && ((NodeAnchor) x).m_Slot == slot);
foreach (var edge in slot.edges)
var edges = baseNode.owner.GetEdges(slot);
foreach (var edge in edges)
var targetNode = m_DrawableNodes.FirstOrDefault(x => x.m_Node == edge.toSlot.node);
var targetAnchor = (NodeAnchor)targetNode.Children().FirstOrDefault(x => x is NodeAnchor && ((NodeAnchor) x).m_Slot == edge.toSlot);
var toNode = baseNode.owner.GetNodeFromGUID(edge.inputSlot.nodeGuid);
var toSlot = toNode.FindInputSlot(edge.inputSlot.slotName);
var targetNode = m_DrawableNodes.FirstOrDefault(x => x.m_Node == toNode);
var targetAnchor = (NodeAnchor)targetNode.Children().FirstOrDefault(x => x is NodeAnchor && ((NodeAnchor) x).m_Slot == toSlot);
drawableEdges.Add(new Edge<NodeAnchor>(this, sourceAnchor, targetAnchor));
}
}

if (sourceAnchor == null)
continue;
nullInputSlots.Add(new NullInputProxy(slot, sourceAnchor));
nullInputSlots.Add(new NullInputProxy(baseNode, slot, sourceAnchor));
}
}
var toReturn = new List<CanvasElement>();

public void DeleteElements(List<CanvasElement> elements)
{
// delete selected edges first
foreach (var e in elements.Where(x => x is Edge<NodeAnchor>))
/* foreach (var e in elements.Where(x => x is Edge<NodeAnchor>))
{
//find the edge
var localEdge = (Edge<NodeAnchor>) e;

Debug.Log("Deleting node " + e + " " + node);
graph.currentGraph.RemoveNode(node);
}
graph.currentGraph.RevalidateGraph();
graph.currentGraph.RevalidateGraph();*/
}
public void Connect(NodeAnchor a, NodeAnchor b)

{
private BaseMaterialNode m_Node;
public FloatingPreview(Rect position, Node node)
public FloatingPreview(Rect position, BaseMaterialNode node)
{
m_Node = node as BaseMaterialNode;
m_Translation = new Vector2(position.x, position.y);

15
UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/MaterialWindow.cs


if (posObj == null)
return;
var node = (BaseMaterialNode)CreateInstance(posObj.m_Type);
node.OnCreate();
BaseMaterialNode node = null;
try
{
var constructorInfo = posObj.m_Type.GetConstructor(new[] {typeof(BaseMaterialGraph)});
if (constructorInfo != null)
node = (BaseMaterialNode)constructorInfo.Invoke(new object[] { m_MaterialGraph.currentGraph });
}
catch
{
Debug.LogWarningFormat("Could not construct instance of: {0} as there is no single argument constuctor that takes a BaseMaterialGraph", posObj.m_Type);
return;
}
node.position = new Rect(posObj.m_Pos.x, posObj.m_Pos.y, node.position.width, node.position.height);
m_MaterialGraph.currentGraph.AddNode(node);

8
UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/NodeAnchor.cs


protected Direction m_Direction;
private MaterialGraphDataSource m_Data;
public Slot m_Slot;
public BaseMaterialNode m_Node;
public NodeAnchor(Vector3 position, Type type, Slot slot, MaterialGraphDataSource data, Direction direction)
public NodeAnchor(Vector3 position, Type type, BaseMaterialNode node, Slot slot, MaterialGraphDataSource data, Direction direction)
{
m_Type = type;
scale = new Vector3(15.0f, 15.0f, 1.0f);

Type constructedClass = genericClass.MakeGenericType(type);
m_Source = Activator.CreateInstance(constructedClass);
m_Data = data;
m_Node = node;
m_Slot = slot;
}

Rect labelRect;
if (m_Direction == Direction.Input)
{
text += " " + ConcreteSlotValueTypeAsString(((BaseMaterialNode) m_Slot.node).GetConcreteInputSlotValueType(m_Slot));
text += " " + ConcreteSlotValueTypeAsString(m_Node.GetConcreteInputSlotValueType(m_Slot));
text += " " + ConcreteSlotValueTypeAsString(((BaseMaterialNode) m_Slot.node).GetConcreteOutputSlotValueType(m_Slot));
text += " " + ConcreteSlotValueTypeAsString(m_Node.GetConcreteOutputSlotValueType(m_Slot));
Vector2 sizeOfText = GUIStyle.none.CalcSize(new GUIContent(text));
labelRect = new Rect(translation.x - sizeOfText.x - 4.0f, translation.y, sizeOfText.x + 4.0f, sizeOfText.y + 4.0f);
}

12
UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/NullInputProxy.cs


public class NullInputProxy : CanvasElement
{
private Slot m_InputSlot;
private BaseMaterialNode m_Node;
public NullInputProxy(Slot inputSlot, NodeAnchor nodeAnchor)
public NullInputProxy(BaseMaterialNode node, Slot inputSlot, NodeAnchor nodeAnchor)
m_Node = node;
m_NodeAnchor = nodeAnchor;
var size = m_NodeAnchor.scale;

var position = m_NodeAnchor.canvasBoundingRect.min;
position.x -= kWidth;
translation = position;
var bmn = (BaseMaterialNode) m_InputSlot.node;
var changed = bmn.DrawSlotDefaultInput(rect, m_InputSlot);
var changed = m_Node.DrawSlotDefaultInput(rect, m_InputSlot);
DrawableMaterialNode.RepaintDependentNodes(bmn);
DrawableMaterialNode.RepaintDependentNodes(m_Node);
}
public override void UpdateModel(UpdateType t)

55
UnityProject/Assets/UnityShaderEditor/Editor/Source/MaterialGraph.cs


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

{
public class MaterialGraph : ScriptableObject
public class MaterialGraph : ScriptableObject, ISerializationCallbackReceiver
[NonSerialized]
private Material m_Material;
private MaterialOptions m_MaterialOptions;
private MaterialOptions m_MaterialOptions = new MaterialOptions();
public MaterialOptions materialOptions { get { return m_MaterialOptions; } }
private MaterialGraph()
{
m_PixelGraph = new PixelGraph(this);
}
public BaseMaterialGraph currentGraph { get { return m_PixelGraph; } }
public MaterialOptions materialOptions
{
get { return m_MaterialOptions; }
}
public void OnEnable()
public BaseMaterialGraph currentGraph
if (m_MaterialOptions == null)
{
m_MaterialOptions = CreateInstance<MaterialOptions>();
m_MaterialOptions.Init();
m_MaterialOptions.hideFlags = HideFlags.HideInHierarchy;
}
get { return m_PixelGraph; }
}
if (m_PixelGraph == null)
{
m_PixelGraph = CreateInstance<PixelGraph>();
m_PixelGraph.hideFlags = HideFlags.HideInHierarchy;
m_PixelGraph.name = name;
}
public void OnBeforeSerialize()
{}
public void OnAfterDeserialize()
{
public void AddSubAssetsToAsset()
{
AssetDatabase.AddObjectToAsset(m_MaterialOptions, this);
AssetDatabase.AddObjectToAsset(m_PixelGraph, this);
m_PixelGraph.AddSubAssetsToAsset();
}
private Material m_Material;
return m_PixelGraph.GetMaterial();
}

var shaderString = ShaderGenerator.GenerateSurfaceShader(this, name, false, out configuredTextures);
File.WriteAllText(path, shaderString);
AssetDatabase.Refresh(); // Investigate if this is optimal
var shader = AssetDatabase.LoadAssetAtPath(path, typeof(Shader)) as Shader;
if (shader == null)
return;

5
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/AddNode.cs


using UnityEngine;
public AddNode()
public AddNode(BaseMaterialGraph owner)
: base(owner)
{
name = "AddNode";
}

875
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/BaseMaterialNode.cs
文件差异内容过多而无法显示
查看文件

17
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/Function2Input.cs


using UnityEditor.Graphs;
using UnityEngine;
namespace UnityEditor.MaterialGraph

get { return true; }
}
public Function2Input()
public Function2Input(BaseMaterialGraph owner)
: base(owner)
{
AddSlot(GetInputSlot1());
AddSlot(GetInputSlot2());

protected virtual Slot GetInputSlot1()
{
var slot = new Slot(SlotType.InputSlot, GetInputSlot1Name());
return new Slot(slot, SlotValueType.Dynamic);
return new Slot(guid, GetInputSlot1Name(), GetInputSlot1Name(), Slot.SlotType.Input, SlotValueType.Dynamic, Vector4.zero);
protected virtual MaterialGraphSlot GetInputSlot2()
protected virtual Slot GetInputSlot2()
var slot = new Slot(SlotType.InputSlot, GetInputSlot2Name());
return new MaterialGraphSlot(slot, SlotValueType.Dynamic);
return new Slot(guid, GetInputSlot2Name(), GetInputSlot2Name(), Slot.SlotType.Input, SlotValueType.Dynamic, Vector4.zero);
protected virtual MaterialGraphSlot GetOutputSlot()
protected virtual Slot GetOutputSlot()
var slot = new Slot(SlotType.OutputSlot, GetOutputSlotName());
return new MaterialGraphSlot(slot, SlotValueType.Dynamic);
return new Slot(guid, GetOutputSlotName(), GetOutputSlotName(), Slot.SlotType.Output, SlotValueType.Dynamic, Vector4.zero);
}
protected virtual string GetInputSlot1Name()

39
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/PixelShaderNode.cs


using System.Linq;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor.Graphs;
//[Title("Output/Pixel Shader")]
[Serializable]
[SerializeField]
private string m_LightFunctionClassName;
[Serializable]
class NodeSpecificData : BaseMaterialNode.NodeSpecificData
{
[SerializeField]
public string m_LightFunctionClassName;
}
private NodeSpecificData m_NodeSpecificData = new NodeSpecificData();
private string lightFunctionClassName
{
get { return m_NodeSpecificData.m_LightFunctionClassName; }
set { m_NodeSpecificData.m_LightFunctionClassName = value; }
}
public PixelShaderNode()
public PixelShaderNode(PixelGraph owner)
: base(owner)
{
name = "PixelMaster";
GetLightFunction().DoSlotsForConfiguration(this);

private BaseLightFunction GetLightFunction()
{
var lightFunctions = GetLightFunctions();
var lightFunction = lightFunctions.FirstOrDefault(x => x.GetType().ToString() == m_LightFunctionClassName);
var lightFunction = lightFunctions.FirstOrDefault(x => x.GetType().ToString() == lightFunctionClassName);
if (lightFunction == null && lightFunctions.Count > 0)
lightFunction = lightFunctions[0];

EditorGUI.BeginChangeCheck();
lightFuncIndex = EditorGUI.Popup(new Rect(drawArea.x, drawArea.y, drawArea.width, EditorGUIUtility.singleLineHeight), lightFuncIndex, lightFunctions.Select(x => x.GetLightFunctionName()).ToArray(), EditorStyles.popup);
m_LightFunctionClassName = lightFunctions[lightFuncIndex].GetType().ToString();
lightFunctionClassName = lightFunctions[lightFuncIndex].GetType().ToString();
if (EditorGUI.EndChangeCheck())
{
var function = GetLightFunction();

protected override bool UpdatePreviewShader()
{
if (hasError)
// if (hasError)
return false;
var shaderName = "Hidden/PreviewShader/" + name + "_" + guid;

hasError = !InternalUpdatePreviewShader(resultShader);
return true;
}
protected override void DelegateOnBeforeSerialize()
{
m_JSONNodeSpecificData = JsonUtility.ToJson(m_NodeSpecificData);
}
protected override void DelegateOnAfterDeserialize()
{
if (!string.IsNullOrEmpty(m_JSONNodeSpecificData))
JsonUtility.FromJsonOverwrite(m_JSONNodeSpecificData, m_NodeSpecificData);
}
}
}

61
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/PropertyNode.cs


using System;
using UnityEditor.Graphs;
using UnityEngine;
namespace UnityEditor.MaterialGraph

[SerializeField]
private string m_PropertyName;
protected class NodeSpecificData : BaseMaterialNode.NodeSpecificData
{
[SerializeField]
public string m_PropertyName;
[SerializeField]
public string m_Description;
[SerializeField]
public bool m_Exposed;
}
protected void ApplyNodeSpecificData(NodeSpecificData data)
{
m_NodeSpecificData.m_PropertyName = data.m_PropertyName;
m_NodeSpecificData.m_Description = data.m_Description;
m_NodeSpecificData.m_Exposed = data.m_Exposed;
}
[SerializeField]
private string m_Description;
private NodeSpecificData m_NodeSpecificData = new NodeSpecificData();
[SerializeField]
private bool m_Exposed;
public PropertyNode(BaseMaterialGraph owner) : base(owner)
{}
get { return m_Exposed; }
get { return m_NodeSpecificData.m_Exposed; }
}
public string description

if (string.IsNullOrEmpty(m_Description))
if (string.IsNullOrEmpty(m_NodeSpecificData.m_Description))
return m_Description;
return m_NodeSpecificData.m_Description;
set { m_Description = value; }
set { m_NodeSpecificData.m_Description = value; }
}
public virtual string propertyName

if (!exposed || string.IsNullOrEmpty(m_PropertyName))
if (!exposed || string.IsNullOrEmpty(m_NodeSpecificData.m_PropertyName))
return m_PropertyName + "_Uniform";
return m_NodeSpecificData.m_PropertyName + "_Uniform";
set { m_PropertyName = value; }
set { m_NodeSpecificData.m_PropertyName = value; }
}
public abstract PropertyType propertyType { get; }

if (!exposed)
return false;
var allNodes = pixelGraph.nodes;
var allNodes = owner.nodes;
foreach (var n in allNodes.OfType<PropertyNode>())
{
if (n == this)

public override bool OnGUI()
{
EditorGUI.BeginChangeCheck();
m_Exposed = EditorGUILayout.Toggle("Exposed Property", m_Exposed);
if (m_Exposed)
m_PropertyName = EditorGUILayout.DelayedTextField("Property Name", m_PropertyName);
m_NodeSpecificData.m_Exposed = EditorGUILayout.Toggle("Exposed Property", m_NodeSpecificData.m_Exposed);
if (m_NodeSpecificData.m_Exposed)
m_NodeSpecificData.m_PropertyName = EditorGUILayout.DelayedTextField("Property Name", m_NodeSpecificData.m_PropertyName);
var bmg = (graph as BaseMaterialGraph);
if (bmg == null)
return false;
bmg.RevalidateGraph();
owner.RevalidateGraph();
if (m_Exposed)
m_Description = EditorGUILayout.TextField("Description", m_Description);
if (m_NodeSpecificData.m_Exposed)
m_NodeSpecificData.m_Description = EditorGUILayout.TextField("Description", m_NodeSpecificData.m_Description);
modified |= base.OnGUI();
return modified;

26
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/Slot.cs


namespace UnityEditor.MaterialGraph
{
[Serializable]
public class Slot
public class Slot : ISerializationCallbackReceiver
{
public enum SlotType
{

[SerializeField]
private string m_NodeGUIDSerialized;
[SerializeField]
private ConcreteSlotValueType m_ConcreteValueType;
private GUID m_NodeGUID;
private Guid m_NodeGUID;
public Slot(GUID nodeGuid, string name, string displayName, SlotType slotType, SlotValueType valueType, Vector4 defaultValue)
public Slot(Guid nodeGuid, string name, string displayName, SlotType slotType, SlotValueType valueType, Vector4 defaultValue)
{
m_Name = name;
m_DisplayName = displayName;

get { return m_DisplayName; }
}
public GUID nodeGuid
public Guid nodeGuid
{
get { return m_NodeGUID; }
}

set { m_CurrentValue = value; }
}
public ConcreteSlotValueType concreteValueType
{
get { return m_ConcreteValueType; }
set { m_ConcreteValueType = value; }
}
public string GetInputName (BaseMaterialNode node)
{
return string.Format( "{0}_{1}", node.name, name);

return EditorGUI.EndChangeCheck();
}
public void BeforeSerialize()
public virtual void OnBeforeSerialize()
public void AfterDeserialize()
public virtual void OnAfterDeserialize()
m_NodeGUID = new GUID(m_NodeGUIDSerialized);
if (!string.IsNullOrEmpty(m_NodeGUIDSerialized))
m_NodeGUID = new Guid(m_NodeGUIDSerialized);
else
m_NodeGUID = Guid.NewGuid();
}
}
}

53
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/Vector4Node.cs


using System;
using UnityEditor.Graphs;
using UnityEngine;
namespace UnityEditor.MaterialGraph

{
private const string kOutputSlotName = "Value";
protected class NodeSpecificData : PropertyNode.NodeSpecificData
{
[SerializeField]
public Vector4 m_Value;
}
protected void ApplyNodeSpecificData(NodeSpecificData data)
{
base.ApplyNodeSpecificData(data);
m_NodeSpecificData.m_Value = data.m_Value;
}
[SerializeField]
private Vector4 m_Value;
protected override void DelegateOnBeforeSerialize()
{
m_JSONNodeSpecificData = JsonUtility.ToJson(m_NodeSpecificData);
}
public override void OnCreate()
protected override void DelegateOnAfterDeserialize()
base.OnCreate();
name = "V4Node";
if (string.IsNullOrEmpty(m_JSONNodeSpecificData))
return;
var data = JsonUtility.FromJson<NodeSpecificData>(m_JSONNodeSpecificData);
ApplyNodeSpecificData(data);
InternalValidate();
public override void OnEnable()
private void InternalValidate()
base.OnEnable();
AddSlot(new MaterialGraphSlot(new Slot(name: SlotType.OutputSlot, slotType: kOutputSlotName), SlotValueType.Vector4));
AddSlot(new Slot(guid, kOutputSlotName, kOutputSlotName, Slot.SlotType.Output, SlotValueType.Vector4, Vector4.zero));
private const string kOutputSlotName = "Value";
private NodeSpecificData m_NodeSpecificData = new NodeSpecificData();
public Vector4Node(BaseMaterialGraph owner) : base(owner)
{
name = "V4Node";
InternalValidate();
}
public override PropertyType propertyType
{
get { return PropertyType.Vector4; }

{
if (exposed)
visitor.AddShaderProperty(new VectorPropertyChunk(propertyName, description, m_Value, false));
visitor.AddShaderProperty(new VectorPropertyChunk(propertyName, description, m_NodeSpecificData.m_Value, false));
}
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode, ConcreteSlotValueType valueType)

if (exposed || generationMode.IsPreview())
return;
visitor.AddShaderChunk(precision + "4 " + propertyName + " = " + precision + "4 (" + m_Value.x + ", " + m_Value.y + ", " + m_Value.z + ", " + m_Value.w + ");", true);
visitor.AddShaderChunk(precision + "4 " + propertyName + " = " + precision + "4 (" + m_NodeSpecificData.m_Value.x + ", " + m_NodeSpecificData.m_Value.y + ", " + m_NodeSpecificData.m_Value.z + ", " + m_NodeSpecificData.m_Value.w + ");", true);
}
public override GUIModificationType NodeUI(Rect drawArea)

EditorGUI.BeginChangeCheck();
m_Value = EditorGUI.Vector4Field(new Rect(drawArea.x, drawArea.y, drawArea.width, EditorGUIUtility.singleLineHeight), "Value", m_Value);
m_NodeSpecificData.m_Value = EditorGUI.Vector4Field(new Rect(drawArea.x, drawArea.y, drawArea.width, EditorGUIUtility.singleLineHeight), "Value", m_NodeSpecificData.m_Value);
if (EditorGUI.EndChangeCheck())
return GUIModificationType.Repaint;
return GUIModificationType.None;

{
m_Name = propertyName,
m_PropType = PropertyType.Vector4,
m_Vector4 = m_Value
m_Vector4 = m_NodeSpecificData.m_Value
};
}
}

41
UnityProject/Assets/UnityShaderEditor/Editor/Source/PixelGraph.cs


using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

class PixelGraph : BaseMaterialGraph
[Serializable]
public class PixelGraph : BaseMaterialGraph
[NonSerialized]
public PixelShaderNode pixelMasterNode
{
get
{
ConfigureMasterNode(true);
return m_PixelMasterNode;
}
}
private void ConfigureMasterNode(bool addToAsset)
public PixelGraph(MaterialGraph owner) : base (owner)
if (m_PixelMasterNode == null)
m_PixelMasterNode = nodes.FirstOrDefault(x => x.GetType() == typeof(PixelShaderNode)) as PixelShaderNode;
m_PixelMasterNode = nodes.FirstOrDefault(x => x.GetType() == typeof(PixelShaderNode)) as PixelShaderNode;
m_PixelMasterNode = new PixelShaderNode();
m_PixelMasterNode.OnCreate();
m_PixelMasterNode = new PixelShaderNode(this);
AddNode(m_PixelMasterNode);
if (addToAsset)
AddNode(m_PixelMasterNode);
public override void OnEnable()
{
base.OnEnable();
ConfigureMasterNode(false);
}
public void AddSubAssetsToAsset()
public PixelShaderNode pixelMasterNode
AddNodeNoValidate(m_PixelMasterNode);
get
{
return m_PixelMasterNode;
}
[NonSerialized]
private List<BaseMaterialNode> m_ActiveNodes = new List<BaseMaterialNode>();
public IEnumerable<BaseMaterialNode> activeNodes
{

47
UnityProject/Assets/UnityShaderEditor/Editor/Source/Util/NodeUtils.cs


using System;
using System.Linq;
using System.Text;
using UnityEditor.Graphs;
using UnityEngine;
namespace UnityEditor.MaterialGraph

return foundUsedOutputSlots;
}
public static void RecurseNodesToFindValidOutputSlots(Node fromNode, Node currentNode, ICollection<Slot> foundUsedOutputSlots)
public static void RecurseNodesToFindValidOutputSlots(BaseMaterialNode fromNode, BaseMaterialNode currentNode, ICollection<Slot> foundUsedOutputSlots)
{
if (fromNode == null || currentNode == null)
{

var bmn = currentNode as BaseMaterialNode;
if (bmn == null)
return;
bmn.GetValidInputSlots(validSlots);
validSlots.AddRange(currentNode.inputSlots);
for (int i = 0; i < inputSlot.edges.Count; i++)
var edges = currentNode.owner.GetEdges(inputSlot);
foreach (var edge in edges)
var edge = inputSlot.edges[i];
if (edge.fromSlot.node == fromNode && !foundUsedOutputSlots.Contains(edge.fromSlot))
foundUsedOutputSlots.Add(edge.fromSlot);
var outputNode = currentNode.owner.GetNodeFromGUID(edge.outputSlot.nodeGuid);
var outputSlot = outputNode.FindOutputSlot(edge.outputSlot.slotName);
if (outputNode == fromNode && !foundUsedOutputSlots.Contains(outputSlot))
foundUsedOutputSlots.Add(outputSlot);
RecurseNodesToFindValidOutputSlots(fromNode, edge.fromSlot.node, foundUsedOutputSlots);
RecurseNodesToFindValidOutputSlots(fromNode, outputNode, foundUsedOutputSlots);
}
}
ListPool<Slot>.Release(validSlots);

return;
var validSlots = ListPool<Slot>.Get();
node.GetValidInputSlots(validSlots);
validSlots.AddRange(node.inputSlots);
if (slotToUse != null && !validSlots.Contains(slotToUse))
{
ListPool<Slot>.Release(validSlots);

for (int index = 0; index < validSlots.Count; index++)
{
var slot = validSlots[index];
for (int i = 0; i < slot.edges.Count; i++)
var edges = node.owner.GetEdges(slot);
foreach (var edge in edges)
var edge = slot.edges[i];
var inputNode = edge.fromSlot.node as BaseMaterialNode;
CollectChildNodesByExecutionOrder(nodeList, inputNode, null);
var outputNode = node.owner.GetNodeFromGUID(edge.outputSlot.nodeGuid);
CollectChildNodesByExecutionOrder(nodeList, outputNode, null);
}
}

return;
foreach (var slot in node.outputSlots)
foreach (var edge in slot.edges)
CollectDependentNodes(nodeList, edge.toSlot.node as BaseMaterialNode);
{
foreach (var edge in node.owner.GetEdges(slot))
{
var inputNode = node.owner.GetNodeFromGUID(edge.inputSlot.nodeGuid);
CollectDependentNodes(nodeList, inputNode);
}
}
nodeList.Add(node);
}
}

30
UnityProject/Assets/UnityShaderEditor/Editor/Source/Util/ShaderGenerator.cs


using System.IO;
using System.Linq;
using System.Text;
using UnityEditor.Graphs;
public enum TextureType
{
White,
Gray,
Black,
Bump
}
public abstract class PropertyChunk
{
protected string m_PropertyName;

}
private const string kErrorString = @"ERROR!";
public static string AdaptNodeOutput(Slot outputSlot, GenerationMode mode, ConcreteSlotValueType convertToType, bool textureSampleUVHack = false)
public static string AdaptNodeOutput(BaseMaterialNode node, Slot outputSlot, GenerationMode mode, ConcreteSlotValueType convertToType, bool textureSampleUVHack = false)
var node = outputSlot.node as BaseMaterialNode;
var rawOutput = node.GetOutputVariableNameForSlot(node.outputSlots.FirstOrDefault(), mode);
var rawOutput = node.GetOutputVariableNameForSlot(outputSlot, mode);
if (convertFromType == convertToType)
return rawOutput;

}
}
private static string AdaptNodeOutputForPreview(Slot outputSlot, GenerationMode mode, ConcreteSlotValueType convertToType)
private static string AdaptNodeOutputForPreview(BaseMaterialNode node, Slot outputSlot, GenerationMode mode, ConcreteSlotValueType convertToType)
var node = outputSlot.node as BaseMaterialNode;
return AdaptNodeOutput(outputSlot, mode, convertToType);
return AdaptNodeOutput(node, outputSlot, mode, convertToType);
var rawOutput = node.GetOutputVariableNameForSlot(node.outputSlots.FirstOrDefault(), mode);
var rawOutput = node.GetOutputVariableNameForSlot(outputSlot, mode);
// otherwise we need to pad output for the preview!
switch (convertToType)

}
if (generationMode == GenerationMode.Preview2D)
shaderBodyVisitor.AddShaderChunk("return " + AdaptNodeOutputForPreview(node.outputSlots.FirstOrDefault(), generationMode, ConcreteSlotValueType.Vector4) + ";", true);
shaderBodyVisitor.AddShaderChunk("return " + AdaptNodeOutputForPreview(node, node.outputSlots.First(), generationMode, ConcreteSlotValueType.Vector4) + ";", true);
shaderBodyVisitor.AddShaderChunk("o.Emission = " + AdaptNodeOutputForPreview(node.outputSlots.FirstOrDefault(), generationMode, ConcreteSlotValueType.Vector3) + ";", true);
shaderBodyVisitor.AddShaderChunk("o.Emission = " + AdaptNodeOutputForPreview(node, node.outputSlots.First(), generationMode, ConcreteSlotValueType.Vector3) + ";", true);
template = template.Replace("${ShaderName}", shaderName);
template = template.Replace("${ShaderPropertiesHeader}", shaderPropertiesVisitor.GetShaderString(2));

30
UnityProject/Assets/UnityShaderEditor/Editor/Source/Edge.cs


using System;
using UnityEngine;
namespace UnityEditor.MaterialGraph
{
[Serializable]
public class Edge
{
[SerializeField]
private SlotReference m_OutputSlot;
[SerializeField]
private SlotReference m_InputSlot;
public Edge(SlotReference outputSlot, SlotReference inputSlot)
{
m_OutputSlot = outputSlot;
m_InputSlot = inputSlot;
}
public SlotReference outputSlot
{
get { return m_OutputSlot; }
}
public SlotReference inputSlot
{
get { return m_InputSlot; }
}
}
}

12
UnityProject/Assets/UnityShaderEditor/Editor/Source/Edge.cs.meta


fileFormatVersion: 2
guid: 6b610a05c25f54d4c90f7ebf82696481
timeCreated: 1463576280
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

8
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/DrawMode.cs


namespace UnityEditor.MaterialGraph
{
public enum DrawMode
{
Full,
Collapsed
}
}

12
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/DrawMode.cs.meta


fileFormatVersion: 2
guid: b11454ef93315fe4abbcb38de383ef76
timeCreated: 1463576280
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

9
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/Precision.cs


namespace UnityEditor.MaterialGraph
{
public enum Precision
{
Default = 0, // half
Full = 1,
Fixed = 2,
}
}

12
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/Precision.cs.meta


fileFormatVersion: 2
guid: 3b4e2f7da2357424ab19865ce322e8be
timeCreated: 1463576280
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

8
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/PreviewMode.cs


namespace UnityEditor.MaterialGraph
{
public enum PreviewMode
{
Preview2D,
Preview3D
}
}

12
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/PreviewMode.cs.meta


fileFormatVersion: 2
guid: 084b4ac1c80bf4746bb42f8d424c058f
timeCreated: 1463576280
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

15
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/PreviewProperty.cs


using UnityEngine;
namespace UnityEditor.MaterialGraph
{
public class PreviewProperty
{
public string m_Name;
public PropertyType m_PropType;
public Color m_Color;
public Texture2D m_Texture;
public Vector4 m_Vector4;
public float m_Float;
}
}

12
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/PreviewProperty.cs.meta


fileFormatVersion: 2
guid: b61922288469cb5438e9b4118b11a4d9
timeCreated: 1463576280
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

12
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/PropertyType.cs


namespace UnityEditor.MaterialGraph
{
public enum PropertyType
{
Color,
Texture2D,
Float,
Vector2,
Vector3,
Vector4
}
}

12
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/PropertyType.cs.meta


fileFormatVersion: 2
guid: 7a76ce662d41461449975822790f4da8
timeCreated: 1463576280
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

12
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/Slot.cs.meta


fileFormatVersion: 2
guid: 7d998d7fbd75b97459f63fb4931d14b5
timeCreated: 1463560120
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

11
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/TitleAttribute.cs


using System;
namespace UnityEditor.MaterialGraph
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue)]
public class TitleAttribute : Attribute
{
public string m_Title;
public TitleAttribute(string title) { m_Title = title; }
}
}

12
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/TitleAttribute.cs.meta


fileFormatVersion: 2
guid: e13b8c11babf46743a112e399a5d68fb
timeCreated: 1463576280
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

44
UnityProject/Assets/UnityShaderEditor/Editor/Source/SlotReference.cs


using System;
using UnityEngine;
namespace UnityEditor.MaterialGraph
{
[Serializable]
public class SlotReference : ISerializationCallbackReceiver
{
[SerializeField]
private string m_SlotName;
[NonSerialized]
private Guid m_NodeGUID;
[SerializeField]
private string m_NodeGUIDSerialized;
public SlotReference(Guid nodeGuid, string slotName)
{
m_NodeGUID = nodeGuid;
m_SlotName = slotName;
}
public Guid nodeGuid
{
get { return m_NodeGUID; }
}
public string slotName
{
get { return m_SlotName; }
}
public void OnBeforeSerialize()
{
m_NodeGUIDSerialized = m_NodeGUID.ToString();
}
public void OnAfterDeserialize()
{
m_NodeGUID = new Guid(m_NodeGUIDSerialized);
}
}
}

12
UnityProject/Assets/UnityShaderEditor/Editor/Source/SlotReference.cs.meta


fileFormatVersion: 2
guid: a178e14960be0e944b4652535f2d12e4
timeCreated: 1463576280
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存