浏览代码

Merge pull request #75 from Unity-Technologies/master

Update from master
/main
GitHub 7 年前
当前提交
fcd97941
共有 19 个文件被更改,包括 589 次插入289 次删除
  1. 88
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Implementation/SerializableGraph.cs
  2. 8
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Interfaces/IGraph.cs
  3. 233
      MaterialGraphProject/Assets/PartyPreview.ShaderGraph
  4. 27
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/AbstractMaterialGraph.cs
  5. 27
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/LayeredShaderGraph.cs
  6. 63
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorView.cs
  7. 10
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphEditWindow.cs
  8. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphPreviewGenerator.cs
  9. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/NodeInspectors/BasicNodeInspector.cs
  10. 40
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewSystem.cs
  11. 227
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/GraphEditorView.cs
  12. 7
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/MaterialNodeView.cs
  13. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Resources/Shaders/Checkerboard.shader
  14. 42
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Resources/Styles/MaterialGraph.uss
  15. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/IntegrationTests/ShaderGenerationTest.cs
  16. 53
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/GradientEdge.cs
  17. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/GradientEdge.cs.meta
  18. 28
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/PreviewView.cs
  19. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/PreviewView.cs.meta

88
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Implementation/SerializableGraph.cs


[SerializeField]
List<SerializationHelper.JSONSerializedElement> m_SerializableEdges = new List<SerializationHelper.JSONSerializedElement>();
[NonSerialized]
List<INode> m_AddedNodes = new List<INode>();
[NonSerialized]
List<INode> m_RemovedNodes = new List<INode>();
[NonSerialized]
List<IEdge> m_AddedEdges = new List<IEdge>();
[NonSerialized]
List<IEdge> m_RemovedEdges = new List<IEdge>();
public IEnumerable<INode> addedNodes
{
get { return m_AddedNodes; }
}
public IEnumerable<INode> removedNodes
{
get { return m_RemovedNodes; }
}
public IEnumerable<IEdge> addedEdges
{
get { return m_AddedEdges; }
}
public IEnumerable<IEdge> removedEdges
{
get { return m_RemovedEdges; }
}
public IGraphObject owner { get; set; }
public virtual void ClearChanges()
{
m_AddedNodes.Clear();
m_RemovedNodes.Clear();
m_AddedEdges.Clear();
m_RemovedEdges.Clear();
}
public IEnumerable<T> GetNodes<T>() where T : INode
{
return m_Nodes.Values.OfType<T>();

{
m_Nodes.Add(node.guid, node);
node.owner = this;
NotifyChange(new NodeAddedGraphChange(node));
m_AddedNodes.Add(node);
}
public virtual void RemoveNode(INode node)

m_Nodes.Remove(node.guid);
NotifyChange(new NodeRemovedGraphChange(node));
m_RemovedNodes.Add(node);
ValidateGraph();
}

return;
m_Nodes.Remove(node.guid);
NotifyChange(new NodeRemovedGraphChange(node));
m_RemovedNodes.Add(node);
}
void AddEdgeToNodeEdges(IEdge edge)

return new Dictionary<SerializationHelper.TypeSerializationInfo, SerializationHelper.TypeSerializationInfo>();
}
public virtual IEdge Connect(SlotReference fromSlotRef, SlotReference toSlotRef)
IEdge ConnectNoValidate(SlotReference fromSlotRef, SlotReference toSlotRef)
{
if (fromSlotRef == null || toSlotRef == null)
return null;

var newEdge = new Edge(outputSlot, inputSlot);
m_Edges.Add(newEdge);
NotifyChange(new EdgeAddedGraphChange(newEdge));
m_AddedEdges.Add(newEdge);
return newEdge;
}
public virtual IEdge Connect(SlotReference fromSlotRef, SlotReference toSlotRef)
{
var newEdge = ConnectNoValidate(fromSlotRef, toSlotRef);
ValidateGraph();
return newEdge;
}

if (m_NodeEdges.TryGetValue(e.outputSlot.nodeGuid, out outputNodeEdges))
outputNodeEdges.Remove(e);
NotifyChange(new EdgeRemovedGraphChange(e));
m_RemovedEdges.Add(e);
}
public INode GetNodeFromGuid(Guid guid)

AddEdgeToNodeEdges(edge);
OnEnable();
ValidateGraph();
ValidateGraph();
}
public virtual void ValidateGraph()

//manually modifies serialized data
//of if they delete a node in the inspector
//debug view.
foreach (var edge in edges.ToArray())
//debug view.
foreach (var edge in edges.ToArray())
{
var outputNode = GetNodeFromGuid(edge.outputSlot.nodeGuid);
var inputNode = GetNodeFromGuid(edge.inputSlot.nodeGuid);

removedNodeEdges.Add(edge);
}
foreach (var edge in removedNodeEdges)
RemoveEdge(edge);
RemoveEdgeNoValidate(edge);
}
// Remove all nodes and re-add them.

foreach (var node in m_Nodes.Values)
removedNodeGuids.Add(node.guid);
foreach (var nodeGuid in removedNodeGuids)
RemoveNode(m_Nodes[nodeGuid]);
RemoveNodeNoValidate(m_Nodes[nodeGuid]);
ValidateGraph();
AddNode(node);
AddNodeNoValidate(node);
}
// Add edges from other graph which don't exist in this one.

Connect(edge.outputSlot, edge.inputSlot);
ConnectNoValidate(edge.outputSlot, edge.inputSlot);
ValidateGraph();
}
public void OnEnable()

node.OnEnable();
}
}
public OnGraphChange onChange { get; set; }
public IGraphObject owner { get; set; }
protected void NotifyChange(GraphChange change)
{
if (onChange != null)
onChange(change);
}
}
}

8
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Interfaces/IGraph.cs


namespace UnityEngine.Graphing
{
public delegate void OnGraphChange(GraphChange change);
public interface IGraph : IOnAssetEnabled
{
IEnumerable<T> GetNodes<T>() where T : INode;

IEnumerable<IEdge> GetEdges(SlotReference s);
void ValidateGraph();
void ReplaceWith(IGraph other);
OnGraphChange onChange { get; set; }
IEnumerable<INode> addedNodes { get; }
IEnumerable<INode> removedNodes { get; }
IEnumerable<IEdge> addedEdges { get; }
IEnumerable<IEdge> removedEdges { get; }
void ClearChanges();
}
}

233
MaterialGraphProject/Assets/PartyPreview.ShaderGraph
文件差异内容过多而无法显示
查看文件

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


[SerializeField]
List<SerializationHelper.JSONSerializedElement> m_SerializedProperties = new List<SerializationHelper.JSONSerializedElement>();
[NonSerialized]
List<IShaderProperty> m_AddedProperties = new List<IShaderProperty>();
[NonSerialized]
List<Guid> m_RemovedProperties = new List<Guid>();
public IEnumerable<IShaderProperty> addedProperties
{
get { return m_AddedProperties; }
}
public IEnumerable<Guid> removedProperties
{
get { return m_RemovedProperties; }
}
public override void ClearChanges()
{
base.ClearChanges();
m_AddedProperties.Clear();
m_RemovedProperties.Clear();
}
public override void AddNode(INode node)
{
if (node is AbstractMaterialNode)

return;
m_Properties.Add(property);
NotifyChange(new ShaderPropertyAdded(property));
m_AddedProperties.Add(property);
NotifyChange(new ShaderPropertyRemoved(guid));
m_RemovedProperties.Add(guid);
}
public override Dictionary<SerializationHelper.TypeSerializationInfo, SerializationHelper.TypeSerializationInfo> GetLegacyTypeRemapping()

27
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/LayeredShaderGraph.cs


[NonSerialized]
private List<Layer> m_Layers = new List<Layer>();
[NonSerialized]
List<Layer> m_AddedLayers = new List<Layer>();
[NonSerialized]
List<Guid> m_RemovedLayers = new List<Guid>();
[SerializeField]
List<SerializationHelper.JSONSerializedElement> m_SerializedLayers = new List<SerializationHelper.JSONSerializedElement>();

}
public List<Layer> addedLayers
{
get { return m_AddedLayers; }
}
public List<Guid> removedLayers
{
get { return m_RemovedLayers; }
}
[NonSerialized]
private LayerWeightsOutputNode m_OutputNode;

}
}
public override void ClearChanges()
{
base.ClearChanges();
m_AddedLayers.Clear();
m_RemovedLayers.Clear();
}
public override void AddNode(INode node)
{
if (outputNode != null && node is LayerWeightsOutputNode)

{
var layer = new Layer();
m_Layers.Add(layer);
NotifyChange(new LayerAdded(layer));
m_AddedLayers.Add(layer);
if (outputNode != null)
outputNode.onModified(outputNode, ModificationScope.Graph);

if (num > 0)
{
NotifyChange(new LayerRemoved(id));
m_RemovedLayers.Add(id);
if (outputNode != null)
outputNode.onModified(outputNode, ModificationScope.Graph);

63
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorView.cs


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

VisualElement m_PropertyItems;
VisualElement m_LayerItems;
VisualElement m_ContentContainer;
Experimental.UIElements.ObjectField m_PreviewMeshPicker;
Image m_Preview;
PreviewView m_Preview;
AbstractMaterialGraph m_Graph;
PreviewSystem m_PreviewSystem;

}
bottomContainer.Add(propertiesContainer);
//if (m_Presenter.graph is LayeredShaderGraph)
if (m_Graph is LayeredShaderGraph)
{
var layersContainer = new VisualElement {name = "properties"};
{

bottomContainer.Add(layersContainer);
}
m_Preview = new Image {name = "preview", image = Texture2D.blackTexture};
m_Preview = new PreviewView {name = "preview", image = Texture2D.blackTexture};
m_PreviewMeshPicker = new Experimental.UIElements.ObjectField() { objectType = typeof(Mesh) };
bottomContainer.Add(m_PreviewMeshPicker);
}
Add(bottomContainer);

m_PropertyItems.Add(new ShaderPropertyView(m_Graph, property));
m_Graph.onChange += OnGraphChange;
var layerGraph = m_Graph as LayeredShaderGraph;
if (layerGraph != null)

if (m_MasterNode != null)
{
m_PreviewHandle = m_PreviewSystem.GetPreview(m_MasterNode);
m_PreviewHandle.mesh = null;
m_PreviewHandle.onPreviewChanged += OnPreviewChanged;
}
}

void OnPreviewChanged()
{
m_Preview.image = m_PreviewHandle.texture ?? Texture2D.blackTexture;
m_PreviewHandle.mesh = m_PreviewMeshPicker.value as Mesh;
}
public void UpdateSelection(IEnumerable<INode> nodes)

}
}
void OnGraphChange(GraphChange change)
public void HandleGraphChanges()
var propertyAdded = change as ShaderPropertyAdded;
if (propertyAdded != null)
foreach (var propertyGuid in m_Graph.removedProperties)
m_PropertyItems.Add(new ShaderPropertyView(m_Graph, propertyAdded.shaderProperty));
return;
var propertyView = m_PropertyItems.OfType<ShaderPropertyView>().FirstOrDefault(v => v.property.guid == propertyGuid);if (propertyView != null)
m_PropertyItems.Remove(propertyView);
var propertyRemoved = change as ShaderPropertyRemoved;
if (propertyRemoved != null)
{
var propertyView = m_PropertyItems.OfType<ShaderPropertyView>().FirstOrDefault(v => v.property.guid == propertyRemoved.guid);
if (propertyView != null)
m_PropertyItems.Remove(propertyView);
return;
}
foreach (var property in m_Graph.addedProperties)
m_PropertyItems.Add(new ShaderPropertyView(m_Graph, property));
var layerAdded = change as LayerAdded;
if (layerAdded != null)
m_LayerItems.Add(new ShaderLayerView(layerGraph, layerAdded.layer));
var layerRemoved = change as LayerRemoved;
if (layerRemoved != null)
foreach (var id in layerGraph.removedLayers)
var view = m_LayerItems.OfType<ShaderLayerView>().FirstOrDefault(v => v.layer.guid == layerRemoved.id);
var view = m_LayerItems.OfType<ShaderLayerView>().FirstOrDefault(v => v.layer.guid == id);
foreach (var layer in layerGraph.addedLayers)
m_LayerItems.Add(new ShaderLayerView(layerGraph, layer));
var nodeAdded = change as NodeAddedGraphChange;
if (nodeAdded != null)
if (masterNode != null)
var node = nodeAdded.node as MasterNode;
if (node != null && masterNode == null)
masterNode = node;
return;
if (m_Graph.removedNodes.Contains(masterNode))
masterNode = null;
var nodeRemoved = change as NodeRemovedGraphChange;
if (nodeRemoved != null)
if (masterNode == null)
if (nodeRemoved.node == masterNode)
masterNode = null;
return;
var addedMasterNode = m_Graph.addedNodes.OfType<MasterNode>().FirstOrDefault();
if (addedMasterNode != null)
masterNode = addedMasterNode;
}
}

10
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphEditWindow.cs


void Update()
{
if (graphObject == null)
return;
if (graphEditorView != null)
graphEditorView.previewSystem.Update();
graphEditorView.previewSystem.HandleGraphChanges();
graphEditorView.previewSystem.RenderPreviews();
graphEditorView.HandleGraphChanges();
graphEditorView.inspectorView.HandleGraphChanges();
graphObject.graph.ClearChanges();
}
void OnDisable()

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphPreviewGenerator.cs


}
}
public void DoRenderPreview(RenderTexture renderTexture, Material mat, PreviewMode mode, bool allowSRP, float time, MaterialPropertyBlock properties = null)
public void DoRenderPreview(RenderTexture renderTexture, Material mat, Mesh mesh, PreviewMode mode, bool allowSRP, float time, MaterialPropertyBlock properties = null)
{
if (mat == null || mat.shader == null)
return;

m_Camera.orthographicSize = 1;
m_Camera.orthographic = true;
}
var ambientProbe = RenderSettings.ambientProbe;
Unsupported.SetOverrideRenderSettings(m_Scene);
RenderSettings.ambientProbe = ambientProbe;
m_Camera.targetTexture = renderTexture;
var previousRenderTexure = RenderTexture.active;

Light1.intensity = 1.0f;
m_Camera.clearFlags = CameraClearFlags.Depth;
Mesh previewMesh = mesh == null ? s_Meshes[0] : mesh;
mode == PreviewMode.Preview3D ? s_Meshes[0] : quad,
mode == PreviewMode.Preview3D ? previewMesh : quad,
Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3.one),
mat,
1,

Unsupported.useScriptableRenderPipeline = oldAllowPipes;
RenderTexture.active = previousRenderTexure;
Unsupported.RestoreOverrideRenderSettings();
Light0.enabled = false;
Light1.enabled = false;

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/NodeInspectors/BasicNodeInspector.cs


var modified = false;
foreach (var slot in node.GetSlots<MaterialSlot>())
modified |= IMGUISlotEditorView.SlotField(slot);
GUILayout.Space(10);
return modified ? ModificationScope.Node : ModificationScope.Nothing;

40
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewSystem.cs


foreach (var node in m_Graph.GetNodes<INode>())
AddPreview(node);
m_Graph.onChange += OnGraphChange;
}
void OnGraphChange(GraphChange change)
{
var nodeAdded = change as NodeAddedGraphChange;
if (nodeAdded != null)
AddPreview(nodeAdded.node);
var nodeRemoved = change as NodeRemovedGraphChange;
if (nodeRemoved != null)
RemovePreview(nodeRemoved.node);
var edgeAdded = change as EdgeAddedGraphChange;
if (edgeAdded != null)
m_DirtyShaders.Add(edgeAdded.edge.inputSlot.nodeGuid);
var edgeRemoved = change as EdgeRemovedGraphChange;
if (edgeRemoved != null)
m_DirtyShaders.Add(edgeRemoved.edge.inputSlot.nodeGuid);
}
void AddPreview(INode node)

HashSet<Guid> m_PropertyNodeGuids = new HashSet<Guid>();
List<PreviewProperty> m_PreviewProperties = new List<PreviewProperty>();
public void Update()
public void HandleGraphChanges()
{
foreach (var node in m_Graph.removedNodes)
RemovePreview(node);
foreach (var node in m_Graph.addedNodes)
AddPreview(node);
foreach (var edge in m_Graph.removedEdges)
m_DirtyShaders.Add(edge.inputSlot.nodeGuid);
foreach (var edge in m_Graph.addedEdges)
m_DirtyShaders.Add(edge.inputSlot.nodeGuid);
}
public void RenderPreviews()
{
if (previewRate == PreviewRate.Off)
return;

}
var node = m_Graph.GetNodeFromGuid(nodeGuid);
m_PreviewMaterial.shader = previewData.shader;
m_PreviewGenerator.DoRenderPreview(previewData.renderTexture, m_PreviewMaterial, previewData.previewMode, node is IMasterNode, time, m_PreviewPropertyBlock);
m_PreviewGenerator.DoRenderPreview(previewData.renderTexture, m_PreviewMaterial, previewData.mesh, previewData.previewMode, node is IMasterNode, time, m_PreviewPropertyBlock);
previewData.texture = previewData.renderTexture;
}

public class PreviewData
{
public Shader shader { get; set; }
public Mesh mesh { get; set; }
public string shaderString { get; set; }
public PreviewMode previewMode { get; set; }
public RenderTexture renderTexture { get; set; }

227
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/GraphEditorView.cs


set { m_PreviewSystem = value; }
}
public GraphInspectorView inspectorView
{
get { return m_GraphInspectorView; }
}
public GraphEditorView(AbstractMaterialGraph graph, Object asset)
{
m_Graph = graph;

m_CopyToClipboardButton = new ToolbarButtonView() { text = "Copy shader to clipboard" };
m_CopyToClipboardButton.AddManipulator(new Clickable(() =>
{
AbstractMaterialNode masterNode = graph.GetNodes<MasterNode>().First();
AbstractMaterialNode copyFromNode = graph.GetNodes<MasterNode>().First();
if (graphView.selection.Count == 1)
{
MaterialNodeView selectedNodeView = graphView.selection[0] as MaterialNodeView;
if (selectedNodeView.node != null && selectedNodeView.node.hasPreview)
{
copyFromNode = selectedNodeView.node;
}
}
string shader = graph.GetShader(masterNode, GenerationMode.ForReals, asset.name, out textureInfo, out previewMode);
string shader = graph.GetShader(copyFromNode, GenerationMode.ForReals, asset.name, out textureInfo, out previewMode);
GUIUtility.systemCopyBuffer = shader;
}
));

}
foreach (var node in graph.GetNodes<INode>())
{
NodeAddedGraphChange change = new NodeAddedGraphChange(node);
var nodeView = new MaterialNodeView(change.node as AbstractMaterialNode, m_PreviewSystem);
nodeView.userData = change.node;
change.node.onModified += OnNodeChanged;
m_GraphView.AddElement(nodeView);
}
foreach (var edge in graph.edges)
{
var edge1 = new EdgeAddedGraphChange(edge).edge;
AddNode(node);
var sourceNode = m_Graph.GetNodeFromGuid(edge1.outputSlot.nodeGuid);
var sourceSlot = sourceNode.FindOutputSlot<ISlot>(edge1.outputSlot.slotId);
var targetNode = m_Graph.GetNodeFromGuid(edge1.inputSlot.nodeGuid);
var targetSlot = targetNode.FindInputSlot<ISlot>(edge1.inputSlot.slotId);
var sourceNodeView = m_GraphView.nodes.ToList().OfType<MaterialNodeView>().FirstOrDefault(x => x.node == sourceNode);
if (sourceNodeView != null)
{
var sourceAnchor = sourceNodeView.outputContainer.Children().OfType<NodeAnchor>().FirstOrDefault(x => x.userData is ISlot && (x.userData as ISlot).Equals(sourceSlot));
var targetNodeView = m_GraphView.nodes.ToList().OfType<MaterialNodeView>().FirstOrDefault(x => x.node == targetNode);
var targetAnchor = targetNodeView.inputContainer.Children().OfType<NodeAnchor>().FirstOrDefault(x => x.userData is ISlot && (x.userData as ISlot).Equals(targetSlot));
var edgeView = new Edge();
edgeView.userData = edge1;
edgeView.output = sourceAnchor;
edgeView.output.Connect(edgeView);
edgeView.input = targetAnchor;
edgeView.input.Connect(edgeView);
m_GraphView.AddElement(edgeView);
sourceNodeView.RefreshAnchors();
}
}
graph.onChange += OnGraphChange;
foreach (var edge in graph.edges)
AddEdge(edge);
Add(content);
}

}
}
void OnGraphChange(GraphChange change)
HashSet<MaterialNodeView> m_NodeViewHashSet = new HashSet<MaterialNodeView>();
public void HandleGraphChanges()
var nodeAdded = change as NodeAddedGraphChange;
if (nodeAdded != null)
foreach (var node in m_Graph.removedNodes)
var nodeView = new MaterialNodeView(nodeAdded.node as AbstractMaterialNode, m_PreviewSystem);
nodeView.userData = nodeAdded.node;
nodeAdded.node.onModified += OnNodeChanged;
m_GraphView.AddElement(nodeView);
node.onModified -= OnNodeChanged;
var nodeView = m_GraphView.nodes.ToList().OfType<MaterialNodeView>().FirstOrDefault(p => p.node != null && p.node.guid == node.guid);
if (nodeView != null)
{
nodeView.Dispose();
nodeView.userData = null;
m_GraphView.RemoveElement(nodeView);
}
var nodeRemoved = change as NodeRemovedGraphChange;
if (nodeRemoved != null)
foreach (var node in m_Graph.addedNodes)
AddNode(node);
var nodesToUpdate = m_NodeViewHashSet;
nodesToUpdate.Clear();
foreach (var edge in m_Graph.removedEdges)
nodeRemoved.node.onModified -= OnNodeChanged;
var edgeView = m_GraphView.graphElements.ToList().OfType<Edge>().FirstOrDefault(p => p.userData is IEdge && Equals((IEdge)p.userData, edge));
if (edgeView != null)
{
var nodeView = edgeView.input.node as MaterialNodeView;
if (nodeView != null && nodeView.node != null)
{
nodesToUpdate.Add(nodeView);
}
edgeView.output.Disconnect(edgeView);
edgeView.input.Disconnect(edgeView);
m_GraphView.RemoveElement(edgeView);
}
}
var nodeView = m_GraphView.nodes.ToList().OfType<MaterialNodeView>().FirstOrDefault(p => p.node != null && p.node.guid == nodeRemoved.node.guid);
if (nodeView != null)
m_GraphView.RemoveElement(nodeView);
foreach (var edge in m_Graph.addedEdges)
{
var edgeView = AddEdge(edge);
if (edgeView != null)
nodesToUpdate.Add((MaterialNodeView)edgeView.input.node);
var edgeAdded = change as EdgeAddedGraphChange;
if (edgeAdded != null)
UpdateEdgeColors(nodesToUpdate);
}
void AddNode(INode node)
{
var nodeView = new MaterialNodeView(node as AbstractMaterialNode, m_PreviewSystem) { userData = node };
node.onModified += OnNodeChanged;
m_GraphView.AddElement(nodeView);
}
Edge AddEdge(IEdge edge)
{
var sourceNode = m_Graph.GetNodeFromGuid(edge.outputSlot.nodeGuid);
var sourceSlot = sourceNode.FindOutputSlot<MaterialSlot>(edge.outputSlot.slotId);
var targetNode = m_Graph.GetNodeFromGuid(edge.inputSlot.nodeGuid);
var targetSlot = targetNode.FindInputSlot<MaterialSlot>(edge.inputSlot.slotId);
var sourceNodeView = m_GraphView.nodes.ToList().OfType<MaterialNodeView>().FirstOrDefault(x => x.node == sourceNode);
if (sourceNodeView != null)
var edge = edgeAdded.edge;
var sourceAnchor = sourceNodeView.outputContainer.Children().OfType<NodeAnchor>().FirstOrDefault(x => x.userData is ISlot && (x.userData as ISlot).Equals(sourceSlot));
var sourceNode = m_Graph.GetNodeFromGuid(edge.outputSlot.nodeGuid);
var sourceSlot = sourceNode.FindOutputSlot<ISlot>(edge.outputSlot.slotId);
var targetNodeView = m_GraphView.nodes.ToList().OfType<MaterialNodeView>().FirstOrDefault(x => x.node == targetNode);
var targetAnchor = targetNodeView.inputContainer.Children().OfType<NodeAnchor>().FirstOrDefault(x => x.userData is ISlot && (x.userData as ISlot).Equals(targetSlot));
var targetNode = m_Graph.GetNodeFromGuid(edge.inputSlot.nodeGuid);
var targetSlot = targetNode.FindInputSlot<ISlot>(edge.inputSlot.slotId);
var edgeView = new GradientEdge
{
userData = edge,
output = sourceAnchor,
input = targetAnchor
};
edgeView.UpdateClasses(sourceSlot.concreteValueType, targetSlot.concreteValueType);
edgeView.output.Connect(edgeView);
edgeView.input.Connect(edgeView);
m_GraphView.AddElement(edgeView);
sourceNodeView.RefreshAnchors();
targetNodeView.RefreshAnchors();
var sourceNodeView = m_GraphView.nodes.ToList().OfType<MaterialNodeView>().FirstOrDefault(x => x.node == sourceNode);
if (sourceNodeView != null)
{
var sourceAnchor = sourceNodeView.outputContainer.Children().OfType<NodeAnchor>().FirstOrDefault(x => x.userData is ISlot && (x.userData as ISlot).Equals(sourceSlot));
return edgeView;
}
var targetNodeView = m_GraphView.nodes.ToList().OfType<MaterialNodeView>().FirstOrDefault(x => x.node == targetNode);
var targetAnchor = targetNodeView.inputContainer.Children().OfType<NodeAnchor>().FirstOrDefault(x => x.userData is ISlot && (x.userData as ISlot).Equals(targetSlot));
return null;
}
var edgeView = new Edge();
edgeView.userData = edge;
edgeView.output = sourceAnchor;
edgeView.output.Connect(edgeView);
edgeView.input = targetAnchor;
edgeView.input.Connect(edgeView);
m_GraphView.AddElement(edgeView);
}
}
Stack<MaterialNodeView> m_NodeStack = new Stack<MaterialNodeView>();
var edgeRemoved = change as EdgeRemovedGraphChange;
if (edgeRemoved != null)
void UpdateEdgeColors(HashSet<MaterialNodeView> nodeViews)
{
var nodeStack = m_NodeStack;
nodeStack.Clear();
foreach (var nodeView in nodeViews)
nodeStack.Push(nodeView);
while (nodeStack.Any())
var edgeView = m_GraphView.graphElements.ToList().OfType<Edge>().FirstOrDefault(p => p.userData is IEdge && Equals((IEdge)p.userData, edgeRemoved.edge));
if (edgeView != null)
var nodeView = nodeStack.Pop();
foreach (var anchorView in nodeView.outputContainer.Children().OfType<NodeAnchor>())
edgeView.output.Disconnect(edgeView);
edgeView.input.Disconnect(edgeView);
m_GraphView.RemoveElement(edgeView);
var sourceSlot = (MaterialSlot)anchorView.userData;
foreach (var edgeView in anchorView.connections.OfType<GradientEdge>())
{
var targetSlot = (MaterialSlot)edgeView.input.userData;
if (targetSlot.valueType == SlotValueType.Dynamic)
{
edgeView.UpdateClasses(sourceSlot.concreteValueType, targetSlot.concreteValueType);
var connectedNodeView = edgeView.input.node as MaterialNodeView;
if (connectedNodeView != null && !nodeViews.Contains(connectedNodeView))
{
nodeStack.Push(connectedNodeView);
nodeViews.Add(connectedNodeView);
}
}
}
}
foreach (var anchorView in nodeView.inputContainer.Children().OfType<NodeAnchor>())
{
var targetSlot = (MaterialSlot)anchorView.userData;
foreach (var edgeView in anchorView.connections.OfType<GradientEdge>())
{
var sourceSlot = (MaterialSlot)edgeView.output.userData;
if (sourceSlot.valueType == SlotValueType.Dynamic)
{
edgeView.UpdateClasses(sourceSlot.concreteValueType, targetSlot.concreteValueType);
var connectedNodeView = edgeView.output.node as MaterialNodeView;
if (connectedNodeView != null && !nodeViews.Contains(connectedNodeView))
{
nodeStack.Push(connectedNodeView);
nodeViews.Add(connectedNodeView);
}
}
}
}
}
}

onUpdateAssetClick = null;
onConvertToSubgraphClick = null;
onShowInProjectClick = null;
m_Graph.onChange -= OnGraphChange;
if (m_GraphView != null)
{
foreach (var node in m_GraphView.Children().OfType<MaterialNodeView>())
node.Dispose();
m_GraphView = null;
}
if (m_GraphInspectorView != null) m_GraphInspectorView.Dispose();
if (previewSystem != null)
{

7
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/MaterialNodeView.cs


{
List<VisualElement> m_ControlViews;
PreviewData m_Preview;
Image m_PreviewImage;
PreviewView m_PreviewImage;
VisualElement m_PreviewToggle;
VisualElement m_ControlsContainer;

previewExpanded = node.previewExpanded;
leftContainer.Add(m_PreviewToggle);
m_PreviewImage = new Image
m_PreviewImage = new PreviewView
{
name = "preview",
pickingMode = PickingMode.Ignore,

UpdateSize();
}
clippingOptions = ClippingOptions.ClipContents;
}
public AbstractMaterialNode node { get; private set; }

public void Dispose()
{
node = null;
if (m_Preview != null)
{
m_Preview.onPreviewChanged -= UpdatePreviewTexture;

4
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Resources/Shaders/Checkerboard.shader


static const float rows = 32;
static const float columns = 32;
static const float4 col1 = float4(0.389297, 0.389297, 0.389297, 1.0);
static const float4 col2 = float4(0.442601, 0.442601, 0.442601, 1.0);
static const float4 col1 = float4(32.0/255.0, 32.0/255.0, 32.0/255.0, 1.0);
static const float4 col2 = float4(42.0/255.0, 42.0/255.0, 42.0/255.0, 1.0);
float4 frag(v2f_img i) : COLOR
{

42
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Resources/Styles/MaterialGraph.uss


background-color: rgb(0, 225, 25);
}
.edge.fromMatrix4, .edge.fromMatrix3, .edge.fromMatrix2 {
edge-output-color: #8FC1DF;
}
.edge.toMatrix4, .edge.toMatrix3, .edge.toMatrix2 {
edge-input-color: #8FC1DF;
}
.edge.fromTexture2D {
edge-output-color: #FF8B8B;
}
.edge.toTexture2D {
edge-input-color: #FF8B8B;
}
.edge.fromVector4 {
edge-output-color: #FBCBF4;
}
.edge.toVector4 {
edge-input-color: #FBCBF4;
}
.edge.fromVector3 {
edge-output-color: #F6FF9A;
}
.edge.toVector3 {
edge-input-color: #F6FF9A;
}
.edge.fromVector2 {
edge-output-color: #9AEF92;
}
.edge.toVector2 {
edge-input-color: #9AEF92;
}
.edge.fromVector1 {
edge-output-color: #84E4E7;
}
.edge.toVector1 {
edge-input-color: #84E4E7;
}
GraphInspectorView {
background-color: rgb(56, 56, 56);
flex-direction: column;

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/IntegrationTests/ShaderGenerationTest.cs


using (var generator = new MaterialGraphPreviewGenerator())
{
var renderTexture = new RenderTexture(res, res, 16, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default) { hideFlags = HideFlags.HideAndDontSave };
generator.DoRenderPreview(renderTexture, m_PreviewMaterial, PreviewMode.Preview3D, true, 10);
generator.DoRenderPreview(renderTexture, m_PreviewMaterial, null, PreviewMode.Preview3D, true, 10);
Assert.IsNotNull(renderTexture, "Render failed");

53
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/GradientEdge.cs


using UnityEditor.Experimental.UIElements.GraphView;
using UnityEngine;
using UnityEngine.Experimental.UIElements.StyleSheets;
using UnityEngine.MaterialGraph;
namespace UnityEditor.MaterialGraph.Drawing
{
public class GradientEdge : Edge
{
const string k_InputColorProperty = "edge-input-color";
const string k_OutputColorProperty = "edge-output-color";
StyleValue<Color> m_InputColor;
StyleValue<Color> m_OutputColor;
public Color inputColor
{
get { return m_InputColor.GetSpecifiedValueOrDefault(defaultColor); }
}
public Color outputColor
{
get { return m_OutputColor.GetSpecifiedValueOrDefault(defaultColor); }
}
public void UpdateClasses(ConcreteSlotValueType outputType, ConcreteSlotValueType inputType)
{
ClearClassList();
AddToClassList("edge");
AddToClassList("from" + outputType);
AddToClassList("to" + inputType);
}
protected override void OnStyleResolved(ICustomStyle styles)
{
base.OnStyleResolved(styles);
styles.ApplyCustomProperty(k_InputColorProperty, ref m_InputColor);
styles.ApplyCustomProperty(k_OutputColorProperty, ref m_OutputColor);
}
protected override void DrawEdge()
{
if (!UpdateEdgeControl())
return;
edgeControl.edgeWidth = edgeWidth;
edgeControl.inputColor = isGhostEdge ? ghostColor : (selected ? selectedColor : inputColor);
edgeControl.outputColor = isGhostEdge ? ghostColor : (selected ? selectedColor : outputColor);
edgeControl.startCapColor = edgeControl.outputColor;
edgeControl.endCapColor = edgeControl.inputColor;
}
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/GradientEdge.cs.meta


fileFormatVersion: 2
guid: 161180898f1d444880080d666ab7be1d
timeCreated: 1509019463

28
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/PreviewView.cs


using UnityEngine;
using UnityEngine.Experimental.UIElements;
using UnityEngine.Rendering;
namespace UnityEditor.MaterialGraph.Drawing
{
public sealed class PreviewView : VisualElement
{
Texture m_Image;
public Texture image
{
get { return m_Image; }
set
{
if (value == m_Image)
return;
m_Image = value;
Dirty(ChangeType.Repaint);
}
}
public override void DoRepaint()
{
EditorGUI.DrawPreviewTexture(contentRect, image);
}
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/PreviewView.cs.meta


fileFormatVersion: 2
guid: 524b1aa8d68043cd81a6514d805eb50d
timeCreated: 1508917442
正在加载...
取消
保存