浏览代码

Secret implicit node feature (ease automatic connection considering "virtual" default input node) : plug it for TextureNode

/main
Paul Demeulenaere 8 年前
当前提交
0c053056
共有 4 个文件被更改,包括 61 次插入9 次删除
  1. 32
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Implementation/SerializableGraph.cs
  2. 6
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Interfaces/ISlot.cs
  3. 20
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/MaterialSlot.cs
  4. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/TextureNode.cs

32
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Implementation/SerializableGraph.cs


[NonSerialized]
private List<INode> m_Nodes = new List<INode>();
[NonSerialized]
private List<INode> m_VirtualNodes = new List<INode>();
[SerializeField]
List<SerializationHelper.JSONSerializedElement> m_SerializableNodes = new List<SerializationHelper.JSONSerializedElement>();

public INode GetNodeFromGuid(Guid guid)
{
return m_Nodes.FirstOrDefault(x => x.guid == guid);
return m_Nodes.Concat(m_VirtualNodes).FirstOrDefault(x => x.guid == guid);
return m_Nodes.Where(x => x.guid == guid).OfType<T>().FirstOrDefault();
return m_Nodes.Concat(m_VirtualNodes).Where(x => x.guid == guid).OfType<T>().FirstOrDefault();
}
public IEnumerable<IEdge> GetEdges(SlotReference s)

return m_Edges.Where(x =>
var edges = m_Edges.Where(x =>
|| x.inputSlot.nodeGuid == s.nodeGuid && x.inputSlot.slotId == s.slotId);
|| x.inputSlot.nodeGuid == s.nodeGuid && x.inputSlot.slotId == s.slotId).ToList();
//If we didn't find any connection, we fallback to GenerateDefaultInput if it's provided, generate a virtual edge to a virtual node (UV input for instance)
if (edges.Count == 0)
{
var slot = GetNodeFromGuid(s.nodeGuid).FindInputSlot<ISlot>(s.slotId);
if (slot is IGenerateDefaultInput)
{
var defaultInputProvider = slot as IGenerateDefaultInput;
var defaultNode = defaultInputProvider.defaultNode;
var node = m_VirtualNodes.FirstOrDefault(o => o != null && defaultNode.GetType() == o.GetType());
if (node == null)
{
defaultNode.owner = this;
m_VirtualNodes.Add(defaultNode);
node = defaultNode;
}
edges.Add(new Edge(new SlotReference(node.guid, defaultInputProvider.defaultSlotID), s));
}
}
return edges;
}
public virtual void OnBeforeSerialize()

m_Edges = SerializationHelper.Deserialize<IEdge>(m_SerializableEdges);
m_SerializableEdges = null;
m_VirtualNodes = new List<INode>();
ValidateGraph();
}

6
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Interfaces/ISlot.cs


SlotReference slotReference { get; }
INode owner { get; set; }
}
public interface IGenerateDefaultInput
{
INode defaultNode { get; }
int defaultSlotID { get; }
}
}

20
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/MaterialSlot.cs


return EditorGUI.EndChangeCheck();
}*/
}
public class MaterialSlotDefaultInput : MaterialSlot, IGenerateDefaultInput
{
public MaterialSlotDefaultInput(int slotId, string displayName, string shaderOutputName, SlotType slotType, SlotValueType valueType, INode _defaultNode, int _defaultSlotID)
: base(slotId, displayName, shaderOutputName, slotType, valueType, Vector4.zero)
{
defaultNode = _defaultNode;
defaultSlotID = _defaultSlotID;
}
public INode defaultNode
{
get; private set;
}
public int defaultSlotID
{
get; private set;
}
}
}

12
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/TextureNode.cs


namespace UnityEngine.MaterialGraph
{
[Title("Input/Texture Node")]
public class TextureNode : PropertyNode, IGeneratesBodyCode, IRequiresMeshUV
public class TextureNode : PropertyNode, IGeneratesBodyCode
{
protected const string kUVSlotName = "UV";
protected const string kOutputSlotRGBAName = "RGBA";

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));
AddSlot(new MaterialSlotDefaultInput(UvSlotId, kUVSlotName, kUVSlotName, SlotType.Input, SlotValueType.Vector2, new UVNode(), UVNode.OutputSlotId));
RemoveSlotsNameNotMatching(validSlots);
}

if (uvSlot == null)
return;
var uvName = "IN.meshUV0.xy";
var uvName = "error";
var edges = owner.GetEdges(uvSlot.slotReference).ToList();
if (edges.Count > 0)

uvName = ShaderGenerator.AdaptNodeOutput(fromNode, edge.outputSlot.slotId, ConcreteSlotValueType.Vector2, true);
}
else
{
Debug.LogError("Default slot behavior should return a virtual edge to virtual node");
}
string body = "tex2D (" + propertyName + ", " + uvName + ")";

正在加载...
取消
保存