浏览代码

Merge pull request #189 from Unity-Technologies/blackboard

Blackboard improvements
/main
GitHub 7 年前
当前提交
e9df888c
共有 6 个文件被更改,包括 180 次插入29 次删除
  1. 39
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/AbstractMaterialGraph.cs
  2. 14
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Blackboard/BlackboardField.cs
  3. 32
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Blackboard/BlackboardProvider.cs
  4. 7
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Blackboard/BlackboardSection.cs
  5. 95
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Manipulators/GraphDropTarget.cs
  6. 22
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/MaterialGraphView.cs

39
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/AbstractMaterialGraph.cs


get { return m_RemovedProperties; }
}
[NonSerialized]
List<IShaderProperty> m_MovedProperties = new List<IShaderProperty>();
public IEnumerable<IShaderProperty> movedProperties
{
get { return m_MovedProperties; }
}
[SerializeField]
SerializableGuid m_GUID = new SerializableGuid();

m_RemovedEdges.Clear();
m_AddedProperties.Clear();
m_RemovedProperties.Clear();
m_MovedProperties.Clear();
}
public virtual void AddNode(INode node)

ValidateGraph();
}
public void MoveShaderProperty(IShaderProperty property, int newIndex)
{
if (newIndex > m_Properties.Count || newIndex < 0)
throw new ArgumentException("New index is not within properties list.");
var currentIndex = m_Properties.IndexOf(property);
if (currentIndex == -1)
throw new ArgumentException("Property is not in graph.");
if (newIndex == currentIndex)
return;
m_Properties.RemoveAt(currentIndex);
if (newIndex > currentIndex)
newIndex--;
var isLast = newIndex == m_Properties.Count;
if (isLast)
m_Properties.Add(property);
else
m_Properties.Insert(newIndex, property);
if (!m_MovedProperties.Contains(property))
m_MovedProperties.Add(property);
}
public int GetShaderPropertyIndex(IShaderProperty property)
{
return m_Properties.IndexOf(property);
}
{
m_AddedProperties.RemoveAll(x => x.guid == guid);
m_MovedProperties.RemoveAll(x => x.guid == guid);
}
}
static List<IEdge> s_TempEdges = new List<IEdge>();

14
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Blackboard/BlackboardField.cs


using UnityEditor.Experimental.UIElements.GraphView;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
using Debug = System.Diagnostics.Debug;
namespace UnityEditor.ShaderGraph.Drawing
{

void Handler(IMGUIEvent evt, List<ISelectable> selection, IDropTarget dropTarget)
{
if (dropTarget == null || !dropTarget.CanAcceptDrop(selection))
return;
var propagation = EventPropagation.Continue;
if (evt.imguiEvent.type == EventType.DragUpdated)
propagation = dropTarget.DragUpdated(evt, selection, dropTarget);
else if (evt.imguiEvent.type == EventType.DragPerform)
propagation = dropTarget.DragPerform(evt, selection, dropTarget);
else if (evt.imguiEvent.type == EventType.DragExited)
propagation = dropTarget.DragExited();
// if (propagation == EventPropagation.Stop)
// evt.StopPropagation();
}
private void OnTextFieldKeyPressed(KeyDownEvent e)

32
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Blackboard/BlackboardProvider.cs


using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.Graphing;
using UnityEngine;
using UnityEngine.Experimental.UIElements;

blackboard.Add(m_Section);
}
void MoveItemRequested(Blackboard blackboard, int i, VisualElement visualElement)
void MoveItemRequested(Blackboard blackboard, int newIndex, VisualElement visualElement)
Debug.Log(i);
var property = visualElement.userData as IShaderProperty;
if (property == null)
return;
m_Graph.MoveShaderProperty(property, newIndex);
}
void AddItemRequested(Blackboard blackboard)

}
foreach (var property in m_Graph.addedProperties)
AddProperty(property);
AddProperty(property, index: m_Graph.GetShaderPropertyIndex(property));
if (m_Graph.movedProperties.Any())
{
foreach (var row in m_PropertyRows.Values)
row.RemoveFromHierarchy();
foreach (var property in m_Graph.properties)
m_Section.Add(m_PropertyRows[property.guid]);
}
void AddProperty(IShaderProperty property, bool create = false)
void AddProperty(IShaderProperty property, bool create = false, int index = -1)
throw new ArgumentException("Property already exists");
return;
m_Section.Add(row);
row.userData = property;
if (index < 0)
index = m_PropertyRows.Count;
if (index == m_PropertyRows.Count)
m_Section.Add(row);
else
m_Section.Insert(index, row);
m_PropertyRows[property.guid] = row;
if (create)

m_Graph.AddShaderProperty(property);
}
}

7
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Blackboard/BlackboardSection.cs


SetDragIndicatorVisible(true);
m_DragIndicator.layout = new Rect(0, indicatorY - m_DragIndicator.layout.height / 2, layout.width, m_DragIndicator.layout.height);
return EventPropagation.Stop;
}
else
{

return EventPropagation.Continue;
return EventPropagation.Stop;
}
int IndexOf(VisualElement element)

EventPropagation IDropTarget.DragExited()
{
SetDragIndicatorVisible(false);
return EventPropagation.Stop;
return EventPropagation.Continue;
}
}
}

95
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Manipulators/GraphDropTarget.cs


using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.Experimental.UIElements.GraphView;
using UnityEditor.MemoryProfiler;
using Object = UnityEngine.Object;
namespace UnityEditor.ShaderGraph.Drawing
{

MaterialGraphView m_GraphView;
List<ISelectable> m_Selection;
public GraphDropTarget(AbstractMaterialGraph graph)
{

&& (obj is Texture2D || obj is Cubemap || obj is MaterialSubGraphAsset);
}
void CreateNode(Object obj, Vector2 nodePosition)
void CreateNode(object obj, Vector2 nodePosition)
{
var texture2D = obj as Texture2D;
if (texture2D != null)

node.subGraphAsset = subGraphAsset;
m_Graph.AddNode(node);
}
var blackboardField = obj as BlackboardField;
if (blackboardField != null)
{
var property = blackboardField.userData as IShaderProperty;
if (property != null)
{
m_Graph.owner.RegisterCompleteObjectUndo("Drag Shader Property");
var node = new PropertyNode();
var drawState = node.drawState;
drawState.position = new Rect(nodePosition, drawState.position.size);
node.drawState = drawState;
m_Graph.AddNode(node);
node.propertyGuid = property.guid;
}
}
}
void OnIMGUIEvent(IMGUIEvent evt)

var currentTarget = evt.currentTarget as VisualElement;
if (currentTarget == null)
return;
var objects = DragAndDrop.objectReferences;
Object draggedObject = null;
foreach (var obj in objects)
try
if (ValidateObject(obj))
var currentTarget = evt.currentTarget as VisualElement;
if (currentTarget == null)
return;
var pickElement = currentTarget.panel.Pick(target.LocalToWorld(evt.imguiEvent.mousePosition));
if (m_Selection == null)
m_Selection = DragAndDrop.GetGenericData("DragSelection") as List<ISelectable>;
if (!(pickElement is MaterialGraphView))
return;
var objects = DragAndDrop.objectReferences;
if (m_Selection != null)
{
// Handle drop of UIElements
if (m_Selection.OfType<BlackboardField>().Count() != 1)
{
m_Selection = null;
return;
}
DragAndDrop.visualMode = DragAndDropVisualMode.Generic;
if (evt.imguiEvent.type == EventType.DragPerform)
{
var nodePosition = m_GraphView.contentViewContainer.transform.matrix.inverse.MultiplyPoint3x4(m_GraphView.panel.visualTree.ChangeCoordinatesTo(m_GraphView, Event.current.mousePosition));
CreateNode(m_Selection.First(), nodePosition);
DragAndDrop.AcceptDrag();
}
}
else
draggedObject = obj;
break;
// Handle drop of Unity objects
Object draggedObject = null;
foreach (var obj in objects)
{
if (ValidateObject(obj))
{
draggedObject = obj;
break;
}
}
if (draggedObject != null)
{
DragAndDrop.visualMode = DragAndDropVisualMode.Generic;
if (evt.imguiEvent.type == EventType.DragPerform)
{
var nodePosition = m_GraphView.contentViewContainer.transform.matrix.inverse.MultiplyPoint3x4(m_GraphView.panel.visualTree.ChangeCoordinatesTo(m_GraphView, Event.current.mousePosition));
CreateNode(draggedObject, nodePosition);
DragAndDrop.AcceptDrag();
}
}
if (draggedObject == null)
return;
// Debug.LogFormat("{0}: {1}", draggedObject.GetType().Name, draggedObject.name);
DragAndDrop.visualMode = DragAndDropVisualMode.Generic;
if (evt.imguiEvent.type == EventType.DragPerform)
finally
var nodePosition = m_GraphView.contentViewContainer.transform.matrix.inverse.MultiplyPoint3x4(m_GraphView.panel.visualTree.ChangeCoordinatesTo(m_GraphView, Event.current.mousePosition));
CreateNode(draggedObject, nodePosition);
DragAndDrop.AcceptDrag();
if (evt.imguiEvent.type == EventType.DragPerform || evt.imguiEvent.type == EventType.DragExited)
m_Selection = null;
}
}
}

22
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/MaterialGraphView.cs


namespace UnityEditor.ShaderGraph.Drawing
{
public sealed class MaterialGraphView : GraphView
public sealed class MaterialGraphView : GraphView, IDropTarget
{
public AbstractMaterialGraph graph { get; private set; }
public Action onConvertToSubgraphClick { get; set; }

graph.RemoveShaderProperty(property.guid);
}
}
}
public bool CanAcceptDrop(List<ISelectable> selection)
{
return selection.OfType<BlackboardField>().Any();
}
public EventPropagation DragUpdated(IMGUIEvent evt, IEnumerable<ISelectable> selection, IDropTarget dropTarget)
{
return EventPropagation.Continue;
}
public EventPropagation DragPerform(IMGUIEvent evt, IEnumerable<ISelectable> selection, IDropTarget dropTarget)
{
return EventPropagation.Continue;
}
public EventPropagation DragExited()
{
return EventPropagation.Continue;
}
}

正在加载...
取消
保存