浏览代码

Fix undo bug caused by property node -> concrete node conversion happening during undo

/main
Peter Bay Bastian 7 年前
当前提交
c2d9f15c
共有 5 个文件被更改,包括 96 次插入75 次删除
  1. 28
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Editor/Implementation/SerializableGraph.cs
  2. 75
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/AbstractMaterialGraph.cs
  3. 61
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/PropertyNode.cs
  4. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Manipulators/NodeCreator.cs
  5. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewManager.cs

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


ValidateGraph();
}
void AddNodeNoValidate(INode node)
protected void AddNodeNoValidate(INode node)
{
m_Nodes.Add(node.guid, node);
node.owner = this;

ValidateGraph();
}
void RemoveNodeNoValidate(INode node)
protected void RemoveNodeNoValidate(INode node)
{
if (!node.canDeleteNode)
return;

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

m_AddedEdges.Add(newEdge);
AddEdgeToNodeEdges(newEdge);
Debug.Log("Connected edge: " + newEdge);
Debug.LogFormat("Connected edge: {0} -> {1} ({2} -> {3})\n{4}", newEdge.outputSlot.nodeGuid, newEdge.inputSlot.nodeGuid, fromNode.name, toNode.name, Environment.StackTrace);
return newEdge;
}

ValidateGraph();
}
void RemoveEdgeNoValidate(IEdge e)
protected void RemoveEdgeNoValidate(IEdge e)
{
e = m_Edges.FirstOrDefault(x => x.Equals(e));
if (e == null)

foreach (var node in GetNodes<INode>())
node.ValidateNode();
foreach (var edge in m_AddedEdges.ToList())
{
if (!ContainsNodeGuid(edge.outputSlot.nodeGuid) || !ContainsNodeGuid(edge.inputSlot.nodeGuid))
{
Debug.LogWarningFormat("Added edge is invalid: {0} -> {1}", edge.outputSlot.nodeGuid, edge.inputSlot.nodeGuid);
m_AddedEdges.Remove(edge);
}
}
other.ValidateGraph();
ValidateGraph();
foreach (var edge in m_Edges)
removedNodeEdges.Add(edge);
removedNodeEdges.AddRange(m_Edges);
foreach (var edge in removedNodeEdges)
RemoveEdgeNoValidate(edge);
}

var removedNodeGuids = removedNodesPooledObject.value;
foreach (var node in m_Nodes.Values)
removedNodeGuids.Add(node.guid);
removedNodeGuids.AddRange(m_Nodes.Keys);
foreach (var nodeGuid in removedNodeGuids)
RemoveNodeNoValidate(m_Nodes[nodeGuid]);
}

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


public void RemoveShaderProperty(Guid guid)
{
var propertyNodes = GetNodes<PropertyNode>().Where(x => x.propertyGuid == guid).ToArray();
foreach (var pNode in propertyNodes)
pNode.ReplaceWithConcreteNode();
RemoveShaderPropertyNoValidate(guid);
ValidateGraph();
}
void RemoveShaderPropertyNoValidate(Guid guid)
{
public void ReplacePropertyNodeWithConcreteNode(PropertyNode propertyNode)
{
var property = properties.FirstOrDefault(x => x.guid == propertyNode.propertyGuid);
if (property != null)
{
AbstractMaterialNode node = null;
int slotId = -1;
if (property is FloatShaderProperty)
{
var createdNode = new Vector1Node();
createdNode.value = ((FloatShaderProperty) property).value;
slotId = Vector1Node.OutputSlotId;
node = createdNode;
}
else if (property is Vector2ShaderProperty)
{
var createdNode = new Vector2Node();
createdNode.value = ((Vector2ShaderProperty) property).value;
slotId = Vector2Node.OutputSlotId;
node = createdNode;
}
else if (property is Vector3ShaderProperty)
{
var createdNode = new Vector3Node();
createdNode.value = ((Vector3ShaderProperty) property).value;
slotId = Vector3Node.OutputSlotId;
node = createdNode;
}
else if (property is Vector4ShaderProperty)
{
var createdNode = new Vector4Node();
createdNode.value = ((Vector4ShaderProperty) property).value;
slotId = Vector4Node.OutputSlotId;
node = createdNode;
}
else if (property is ColorShaderProperty)
{
var createdNode = new ColorNode();
createdNode.color = ((ColorShaderProperty) property).value;
slotId = ColorNode.OutputSlotId;
node = createdNode;
}
if (node == null)
return;
var slot = propertyNode.FindOutputSlot<MaterialSlot>(PropertyNode.OutputSlotId);
node.drawState = propertyNode.drawState;
AddNodeNoValidate(node);
foreach (var edge in GetEdges(slot.slotReference).ToArray())
ConnectNoValidate(node.GetSlotReference(slotId), edge.inputSlot);
RemoveNodeNoValidate(propertyNode);
}
}
public override void ValidateGraph()
{
var propertyNodes = GetNodes<PropertyNode>().Where(n => !m_Properties.Any(p => p.guid == n.propertyGuid)).ToArray();
foreach (var pNode in propertyNodes)
ReplacePropertyNodeWithConcreteNode(pNode);
base.ValidateGraph();
}
public override Dictionary<SerializationHelper.TypeSerializationInfo, SerializationHelper.TypeSerializationInfo> GetLegacyTypeRemapping()
{
var result = base.GetLegacyTypeRemapping();

foreach (var property in m_Properties)
removedPropertyGuids.Add(property.guid);
foreach (var propertyGuid in removedPropertyGuids)
RemoveShaderProperty(propertyGuid);
RemoveShaderPropertyNoValidate(propertyGuid);
}
foreach (var otherProperty in otherMG.properties)
{

61
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/PropertyNode.cs


{
UpdateNode();
}
public void ReplaceWithConcreteNode()
{
var matGraph = owner as MaterialGraph;
if (matGraph == null)
return;
var property = matGraph.properties.FirstOrDefault(x => x.guid == propertyGuid);
if (property != null)
{
AbstractMaterialNode node = null;
int slotId = -1;
if (property is FloatShaderProperty)
{
var createdNode = new Vector1Node();
createdNode.value = ((FloatShaderProperty) property).value;
slotId = Vector1Node.OutputSlotId;
node = createdNode;
}
else if (property is Vector2ShaderProperty)
{
var createdNode = new Vector2Node();
createdNode.value = ((Vector2ShaderProperty) property).value;
slotId = Vector2Node.OutputSlotId;
node = createdNode;
}
else if (property is Vector3ShaderProperty)
{
var createdNode = new Vector3Node();
createdNode.value = ((Vector3ShaderProperty) property).value;
slotId = Vector3Node.OutputSlotId;
node = createdNode;
}
else if (property is Vector4ShaderProperty)
{
var createdNode = new Vector4Node();
createdNode.value = ((Vector4ShaderProperty) property).value;
slotId = Vector4Node.OutputSlotId;
node = createdNode;
}
else if (property is ColorShaderProperty)
{
var createdNode = new ColorNode();
createdNode.color = ((ColorShaderProperty) property).value;
slotId = ColorNode.OutputSlotId;
node = createdNode;
}
if (node == null)
return;
var slot = FindOutputSlot<MaterialSlot>(OutputSlotId);
node.drawState = drawState;
owner.AddNode(node);
foreach (var edge in owner.GetEdges(slot.slotReference).ToArray())
owner.Connect(node.GetSlotReference(slotId), edge.inputSlot);
owner.RemoveNode(this);
}
}
}
}

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Manipulators/NodeCreator.cs


.OfType<PropertyNode>();
foreach (var propNode in slected)
propNode.ReplaceWithConcreteNode();
((AbstractMaterialGraph)propNode.owner).ReplacePropertyNodeWithConcreteNode(propNode);
}
private void OnConvertToProperty()

5
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewManager.cs


{
renderTexture = new RenderTexture(256, 256, 16, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default) { hideFlags = HideFlags.HideAndDontSave }
};
if (m_Previews.ContainsKey(node.guid))
{
Debug.LogWarningFormat("A preview already exists for {0} {1}", node.name, node.guid);
RemovePreview(node);
}
m_Previews.Add(node.guid, previewData);
m_DirtyShaders.Add(node.guid);
node.onModified += OnNodeModified;

正在加载...
取消
保存