浏览代码

[shader graph]Working node connections.

/main
Tim Cooper 9 年前
当前提交
a184cf99
共有 2 个文件被更改,包括 55 次插入32 次删除
  1. 53
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/DrawableMaterialNode.cs
  2. 34
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/MaterialGraphDataSource.cs

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


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

}
public class DrawableMaterialNode : MoveableBox
public sealed class DrawableMaterialNode : MoveableBox
public BaseMaterialNode m_Node;
m_Node = node;
Vector3 pos = new Vector3(5.0f, 32.0f, 0.0f);
Vector3 pos = new Vector3(5.0f, 10.0f, 0.0f);
AddChild(new NodeAnchor(0, pos, typeof (int), this, data));
pos.y += 22;
AddChild(new NodeAnchor(1, pos, typeof (float), this, data));
pos.y += 22;
AddChild(new NodeAnchor(2, pos, typeof (Vector3), this, data));
pos.y += 22;
AddChild(new NodeAnchor(3, pos, typeof (Texture2D), this, data));
pos.y += 22;
AddChild(new NodeAnchor(4, pos, typeof (Color), this, data));
pos.y += 22;
// input slots
foreach (var slot in node.inputSlots)
{
pos.y += 22;
AddChild(new NodeAnchor(pos, typeof (Vector4), slot, data));
}
AddChild(new NodeOutputAnchor(pos, m_OutputType, this, data));
foreach (var slot in node.outputSlots)
{
pos.y += 22;
AddChild(new NodeOutputAnchor(pos, typeof (Vector4), slot, data));
}
pos.y += 22;
scale = new Vector3(scale.x, pos.y + 12.0f, 0.0f);
}

protected object m_Source;
protected Direction m_Direction;
private MaterialGraphDataSource m_Data;
public DrawableMaterialNode m_Node;
public int m_PortIndex;
public Slot m_Slot;
public NodeAnchor(int portIndex, Vector3 position, Type type, DrawableMaterialNode node, MaterialGraphDataSource data)
public NodeAnchor(Vector3 position, Type type, Slot slot, MaterialGraphDataSource data)
{
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_PortIndex = portIndex;
m_Slot = slot;
}
public override void Render(Rect parentRect, Canvas2D canvas)

base.Render(parentRect, canvas);
EditorGUI.DrawRect(new Rect(translation.x, translation.y, scale.x, scale.y), anchorColor);
Rect labelRect = new Rect(translation.x + scale.x + 10.0f, translation.y, parentRect.width, 20.0f);
GUI.Label(labelRect, m_Type.Name);
GUI.Label(labelRect, m_Slot.name);
}
// IConnect

public class NodeOutputAnchor : NodeAnchor
{
public NodeOutputAnchor(Vector3 position, Type type, DrawableMaterialNode node, MaterialGraphDataSource data)
: base(-1, position, type, node, data)
public NodeOutputAnchor(Vector3 position, Type type, Slot slot, MaterialGraphDataSource data)
: base(position, type, slot, data)
{
m_Direction = Direction.eOutput;
}

Vector2 sizeOfText = GUIStyle.none.CalcSize(new GUIContent(m_Type.Name));
Rect labelRect = new Rect(translation.x - sizeOfText.x - 4.0f, translation.y, sizeOfText.x + 4.0f, sizeOfText.y + 4.0f);
GUI.Label(labelRect, m_Type.Name);
GUI.Label(labelRect, m_Slot.name);
}
};

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


using System.Collections.Generic;
using System.Linq;
using UnityEditor.Experimental;
using UnityEditor.Experimental.Graph;
using UnityEngine;

public CanvasElement[] FetchElements()
{
var elements = new List<CanvasElement>();
var drawableNodes = new List<DrawableMaterialNode>();
// add the nodes
elements.Add(new DrawableMaterialNode(bmn, 200.0f, typeof(Vector4), this));
drawableNodes.Add(new DrawableMaterialNode(bmn, 200.0f, typeof(Vector4), this));
}
// Add the edges now
var drawableEdges = new List<Edge<NodeAnchor>>();
foreach (var drawableMaterialNode in drawableNodes)
{
var baseNode = drawableMaterialNode.m_Node;
foreach (var slot in baseNode.outputSlots)
{
var sourceAnchor = (NodeAnchor)drawableMaterialNode.Children().FirstOrDefault(x => x is NodeAnchor && ((NodeAnchor) x).m_Slot == slot);
foreach (var edge in slot.edges)
{
var targetNode = 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);
drawableEdges.Add(new Edge<NodeAnchor>(this, sourceAnchor, targetAnchor));
}
}
Debug.LogFormat("REturning {0} nodes", elements.Count);
return elements.ToArray();
var toReturn = new List<CanvasElement>();
toReturn.AddRange(drawableNodes.Select(x => (CanvasElement)x));
toReturn.AddRange(drawableEdges.Select(x => (CanvasElement)x));
Debug.LogFormat("REturning {0} nodes", toReturn.Count);
return toReturn.ToArray();
}
public void DeleteElement(CanvasElement e)

public void Connect(NodeAnchor a, NodeAnchor b)
{
//m_Elements.Add(new Edge<NodeAnchor>(this, a, b));
//m_Elements.Add();
}
}
}
正在加载...
取消
保存