浏览代码

[shader graph]

*Unified deletion architecture - Delete edges before nodes
*Input proxies now aware of their type
*Fix shader builder bug when mapping from vector1 -> vector2. Some compilers need this to be explicit )swizzle with .xx).
/main
Tim Cooper 9 年前
当前提交
6aaca4fc
共有 13 个文件被更改,包括 187 次插入39 次删除
  1. 10
      UnityProject/Assets/UnityShaderEditor/Editor/Source/BaseMaterialGraph.cs
  2. 15
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/DrawableMaterialNode.cs
  3. 41
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/MaterialGraphDataSource.cs
  4. 2
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/MaterialWindow.cs
  5. 5
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/NullInputProxy.cs
  6. 12
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/BaseMaterialNode.cs
  7. 4
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/PixelShaderNode.cs
  8. 42
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/SlotValue.cs
  9. 17
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/TextureNode.cs
  10. 6
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Util/ShaderGenerator.cs
  11. 48
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/DeleteSelected.cs
  12. 12
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/DeleteSelected.cs.meta
  13. 12
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/NullInputProxy.cs.meta

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


public override void RemoveEdge(Edge e)
{
base.RemoveEdge(e);
var toNode = e.toSlot.node as BaseMaterialNode;
if (toNode == null)
return;
}
public void RemoveEdgeNoRevalidate(Edge e)
{
base.RemoveEdge(e);
}
public override Edge Connect(Slot fromSlot, Slot toSlot)

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


}
scale = new Vector3(pos.x, pos.y + 10.0f, 0.0f);
KeyDown += OnDeleteNode;
OnWidget += MarkDirtyIfNeedsTime;
AddManipulator(new ImguiContainer());

Invalidate();
ListPool<BaseMaterialNode>.Release(childrenNodes);
return true;
}
private bool OnDeleteNode(CanvasElement element, Event e, Canvas2D canvas)
{
if (e.type == EventType.Used)
return false;
if (e.keyCode == KeyCode.Delete)
{
m_Data.DeleteElement(this);
return true;
}
return false;
}
public override void UpdateModel(UpdateType t)

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


{
var baseNode = drawableMaterialNode.m_Node;
// grab the input slots where there are no edges
foreach (var slot in baseNode.inputSlots.Where(x => x.edges.Count == 0))
foreach (var slot in baseNode.GetDrawableInputProxies())
{
// if there is no anchor, continue
// this can happen if we are in collapsed mode

public void DeleteElement(CanvasElement e)
{
Debug.Log("Trying to delete " + e);
if (e is Edge<NodeAnchor>)
// do nothing here, we want to use the 'correct'
// delete elements.
}
public void DeleteElements(List<CanvasElement> elements)
{
// delete selected edges first
foreach (var e in elements.Where(x => x is Edge<NodeAnchor>))
graph.currentGraph.RemoveEdge(edge);
graph.currentGraph.RemoveEdgeNoRevalidate(edge);
}
// now delete edges that the selected nodes use
foreach (var e in elements.Where(x => x is DrawableMaterialNode))
{
var node = ((DrawableMaterialNode) e).m_Node;
foreach (var slot in node.slots)
{
for (int index = slot.edges.Count -1; index >= 0; --index)
{
var edge = slot.edges[index];
Debug.Log("Deleting edge " + edge);
graph.currentGraph.RemoveEdgeNoRevalidate(edge);
}
}
else if (e is DrawableMaterialNode)
// now delete the nodes
foreach (var e in elements.Where(x => x is DrawableMaterialNode))
e.ParentCanvas().ReloadData();
e.ParentCanvas().Repaint();
graph.currentGraph.RevalidateGraph();
}
public void Connect(NodeAnchor a, NodeAnchor b)

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


m_Canvas.AddManipulator(new RectangleSelect());
m_Canvas.AddManipulator(new ScreenSpaceGrid());
m_Canvas.AddManipulator(new ContextualMenu(DoAddNodeMenu));
m_Canvas.AddManipulator(new DeleteSelected(m_DataSource.DeleteElements, m_Canvas));
}
Rebuild();

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


var bmn = (BaseMaterialNode) m_InputSlot.node;
var rect = new Rect(0, 0, scale.x, scale.y);
EditorGUI.DrawRect(rect, new Color(0.0f, 0.0f, 0.0f, 0.7f));
var def = bmn.GetSlotDefaultValue(m_InputSlot.name);
var changed = def.OnGUI(rect);
var changed = bmn.DrawSlotDefaultInput(rect, m_InputSlot);
if (changed)
DrawableMaterialNode.RepaintDependentNodes(bmn);
}

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


var def = node.GetSlotDefaultValue(slot.name);
return def.OnGUI();
}
public virtual bool DrawSlotDefaultInput(Rect rect, Slot inputSlot)
{
var def = GetSlotDefaultValue(inputSlot.name);
var inputSlotType = GetConcreteInputSlotValueType(inputSlot);
return def.OnGUI(rect, inputSlotType);
}
public virtual IEnumerable<Slot> GetDrawableInputProxies()
{
return inputSlots.Where(x => x.edges.Count == 0);
}
}
}

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


}
return false;
}
public override IEnumerable<Slot> GetDrawableInputProxies()
{
return new List<Slot>();
}
public override bool hasPreview
{

42
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/SlotValue.cs


m_DefaultVector = EditorGUILayout.Vector4Field("Value", m_DefaultVector);
return EditorGUI.EndChangeCheck();
}
public bool OnGUI(Rect rect)
public bool OnGUI(Rect rect, ConcreteSlotValueType inputSlotType)
m_DefaultVector = EditorGUI.Vector4Field(rect, GUIContent.none, m_DefaultVector);
var rectXmax = rect.xMax;
switch (inputSlotType)
{
case ConcreteSlotValueType.Vector1:
rect.x = rectXmax - 50;
rect.width = 50;
EditorGUIUtility.labelWidth = 15;
EditorGUI.DrawRect(rect, new Color(0.0f, 0.0f, 0.0f, 0.7f));
m_DefaultVector.x = EditorGUI.FloatField(rect, "X", m_DefaultVector.x);
break;
case ConcreteSlotValueType.Vector2:
rect.x = rectXmax - 90;
rect.width = 90;
EditorGUI.DrawRect(rect, new Color(0.0f, 0.0f, 0.0f, 0.7f));
var result2 = new Vector4(m_DefaultVector.x, m_DefaultVector.y);
result2 = EditorGUI.Vector2Field(rect, GUIContent.none, result2);
m_DefaultVector.x = result2.x;
m_DefaultVector.y = result2.y;
break;
case ConcreteSlotValueType.Vector3:
rect.x = rectXmax - 140;
rect.width = 140;
EditorGUI.DrawRect(rect, new Color(0.0f, 0.0f, 0.0f, 0.7f));
var result3 = new Vector3(m_DefaultVector.x, m_DefaultVector.y, m_DefaultVector.z);
result3 = EditorGUI.Vector3Field(rect, GUIContent.none, result3);
m_DefaultVector.x = result3.x;
m_DefaultVector.y = result3.y;
m_DefaultVector.z = result3.z;
break;
default:
rect.x = rectXmax - 190;
rect.width = 190;
EditorGUI.DrawRect(rect, new Color(0.0f, 0.0f, 0.0f, 0.7f));
m_DefaultVector = EditorGUI.Vector4Field(rect, GUIContent.none, m_DefaultVector);
break;
}
}
}

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


}
public override PropertyType propertyType { get { return PropertyType.Texture2D; } }
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;
}
}
}

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


switch (convertFromType)
{
case ConcreteSlotValueType.Vector1:
return string.Format("({0})", rawOutput);
return string.Format("({0}.xx)", rawOutput);
case ConcreteSlotValueType.Vector3:
case ConcreteSlotValueType.Vector4:
return string.Format("({0}.xy)", rawOutput);

switch (convertFromType)
{
case ConcreteSlotValueType.Vector1:
return string.Format("({0})", rawOutput);
return string.Format("({0}.xxx)", rawOutput);
case ConcreteSlotValueType.Vector4:
return string.Format("({0}.xyz)", rawOutput);
default:

switch (convertFromType)
{
case ConcreteSlotValueType.Vector1:
return string.Format("({0})", rawOutput);
return string.Format("({0}.xxxx)", rawOutput);
default:
return kErrorString;
}

48
UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/DeleteSelected.cs


using System.Collections.Generic;
using UnityEditor.Experimental;
using UnityEngine;
namespace UnityEditor.MaterialGraph
{
internal class DeleteSelected : IManipulate
{
public delegate void DeleteElements(List<CanvasElement> elements);
private readonly DeleteElements m_DeletionCallback;
private readonly Canvas2D m_Canvas;
public DeleteSelected(DeleteElements deletionCallback, Canvas2D canvas)
{
m_DeletionCallback = deletionCallback;
m_Canvas = canvas;
}
public bool GetCaps(ManipulatorCapability cap)
{
return false;
}
public void AttachTo(CanvasElement element)
{
element.KeyDown += KeyDown;
}
private bool KeyDown(CanvasElement element, Event e, Canvas2D parent)
{
if (e.type == EventType.Used)
return false;
if (e.keyCode == KeyCode.Delete)
{
if (m_DeletionCallback != null)
{
m_DeletionCallback(parent.selection);
m_Canvas.ReloadData();
m_Canvas.Repaint();
return true;
}
}
return false;
}
}
}

12
UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/DeleteSelected.cs.meta


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

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


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