浏览代码

Merge pull request #317 from Unity-Technologies/copy-property-nodes

Make property nodes into concrete nodes when copied between graphs
/main
GitHub 6 年前
当前提交
a3897b8c
共有 21 个文件被更改,包括 244 次插入16 次删除
  1. 6
      CHANGELOG.md
  2. 28
      com.unity.shadergraph/Editor/Data/Graphs/AbstractMaterialGraph.cs
  3. 1
      com.unity.shadergraph/Editor/Data/Graphs/AbstractShaderProperty.cs
  4. 8
      com.unity.shadergraph/Editor/Data/Graphs/BooleanShaderProperty.cs
  5. 8
      com.unity.shadergraph/Editor/Data/Graphs/ColorShaderProperty.cs
  6. 8
      com.unity.shadergraph/Editor/Data/Graphs/CubemapShaderProperty.cs
  7. 16
      com.unity.shadergraph/Editor/Data/Graphs/GradientShaderProperty.cs
  8. 1
      com.unity.shadergraph/Editor/Data/Graphs/IShaderProperty.cs
  9. 10
      com.unity.shadergraph/Editor/Data/Graphs/Matrix2ShaderProperty.cs
  10. 8
      com.unity.shadergraph/Editor/Data/Graphs/Matrix3ShaderProperty.cs
  11. 8
      com.unity.shadergraph/Editor/Data/Graphs/Matrix4ShaderProperty.cs
  12. 2
      com.unity.shadergraph/Editor/Data/Graphs/MatrixShaderProperty.cs
  13. 10
      com.unity.shadergraph/Editor/Data/Graphs/SamplerStateShaderProperty.cs
  14. 10
      com.unity.shadergraph/Editor/Data/Graphs/TextureShaderProperty.cs
  15. 8
      com.unity.shadergraph/Editor/Data/Graphs/Vector1ShaderProperty.cs
  16. 8
      com.unity.shadergraph/Editor/Data/Graphs/Vector2ShaderProperty.cs
  17. 8
      com.unity.shadergraph/Editor/Data/Graphs/Vector3ShaderProperty.cs
  18. 8
      com.unity.shadergraph/Editor/Data/Graphs/Vector4ShaderProperty.cs
  19. 9
      com.unity.shadergraph/Editor/Drawing/MaterialGraphEditWindow.cs
  20. 36
      com.unity.shadergraph/Editor/Drawing/Views/MaterialGraphView.cs
  21. 59
      com.unity.shadergraph/Editor/Util/CopyPasteGraph.cs

6
CHANGELOG.md


The settings on master nodes now live in a small window that you can toggle on and off. The settings menu allows you to change various rendering settings for your shader.
### Bug fixes
### Bug fixes and minor changes
- Fixed an issue where vector 1 node was not evaluating properly. (#334 and #337)
- Fixed an issue where vector 1 node was not evaluating properly. ([#334](https://github.com/Unity-Technologies/ShaderGraph/issues/334) and [#337](https://github.com/Unity-Technologies/ShaderGraph/issues/337))
- Properties can now be copied and pasted.
- Pasting a property node into another graph will now convert it to a concrete node. ([#300](https://github.com/Unity-Technologies/ShaderGraph/issues/300))

28
com.unity.shadergraph/Editor/Data/Graphs/AbstractMaterialGraph.cs


var nodeGuidMap = new Dictionary<Guid, Guid>();
foreach (var node in graphToPaste.GetNodes<INode>())
{
INode pastedNode = node;
// Check if the property nodes need to be made into a concrete node.
if (node is PropertyNode)
{
PropertyNode propertyNode = (PropertyNode)node;
// If the property is not in the current graph, do check if the
// property can be made into a concrete node.
if (!m_Properties.Select(x => x.guid).Contains(propertyNode.propertyGuid))
{
// If the property is in the serialized paste graph, make the property node into a property node.
var pastedGraphMetaProperties = graphToPaste.metaProperties.Where(x => x.guid == propertyNode.propertyGuid);
if (pastedGraphMetaProperties.Any())
{
pastedNode = pastedGraphMetaProperties.FirstOrDefault().ToConcreteNode();
pastedNode.drawState = node.drawState;
nodeGuidMap[oldGuid] = pastedNode.guid;
}
}
}
var drawState = node.drawState;
var position = drawState.position;
position.x += 30;

remappedNodes.Add(node);
AddNode(node);
remappedNodes.Add(pastedNode);
AddNode(pastedNode);
m_PastedNodes.Add(node);
m_PastedNodes.Add(pastedNode);
}
// only connect edges within pasted elements, discard

1
com.unity.shadergraph/Editor/Data/Graphs/AbstractShaderProperty.cs


public abstract PreviewProperty GetPreviewMaterialProperty();
public abstract INode ToConcreteNode();
public abstract IShaderProperty Copy();
}
}

8
com.unity.shadergraph/Editor/Data/Graphs/BooleanShaderProperty.cs


{
return new BooleanNode { value = new Toggle(value) };
}
public override IShaderProperty Copy()
{
var copied = new BooleanShaderProperty();
copied.displayName = displayName;
copied.value = value;
return copied;
}
}
}

8
com.unity.shadergraph/Editor/Data/Graphs/ColorShaderProperty.cs


{
return new ColorNode { color = new ColorNode.Color(value, colorMode) };
}
public override IShaderProperty Copy()
{
var copied = new ColorShaderProperty();
copied.displayName = displayName;
copied.value = value;
return copied;
}
}
}

8
com.unity.shadergraph/Editor/Data/Graphs/CubemapShaderProperty.cs


{
return new CubemapAssetNode { cubemap = value.cubemap };
}
public override IShaderProperty Copy()
{
var copied = new CubemapShaderProperty();
copied.displayName = displayName;
copied.value = value;
return copied;
}
}
}

16
com.unity.shadergraph/Editor/Data/Graphs/GradientShaderProperty.cs


public static Vector4 ColorKeyToVector(GradientColorKey key)
{
return new Vector4( key.color.r, key.color.g, key.color.b, key.time);
return new Vector4( key.color.r, key.color.g, key.color.b, key.time);
return new Vector2( key.alpha, key.time);
return new Vector2( key.alpha, key.time);
}
}

m_OverrideMembers = true;
m_OverrideSlotName = slotName;
}
public override string GetPropertyDeclarationString(string delimiter = ";")
{
if(m_OverrideMembers)

{
return new GradientNode { gradient = value };
}
public override IShaderProperty Copy()
{
return new GradientShaderProperty
{
value = value
};
}
}
}

1
com.unity.shadergraph/Editor/Data/Graphs/IShaderProperty.cs


PreviewProperty GetPreviewMaterialProperty();
INode ToConcreteNode();
IShaderProperty Copy();
}
}

10
com.unity.shadergraph/Editor/Data/Graphs/Matrix2ShaderProperty.cs


row0 = new Vector2(value.m00, value.m01),
row1 = new Vector2(value.m10, value.m11)
};
}
}
public override IShaderProperty Copy()
{
var copied = new Matrix2ShaderProperty();
copied.displayName = displayName;
copied.value = value;
return copied;
}
}
}

8
com.unity.shadergraph/Editor/Data/Graphs/Matrix3ShaderProperty.cs


row2 = new Vector3(value.m20, value.m21, value.m22)
};
}
public override IShaderProperty Copy()
{
var copied = new Matrix3ShaderProperty();
copied.displayName = displayName;
copied.value = value;
return copied;
}
}
}

8
com.unity.shadergraph/Editor/Data/Graphs/Matrix4ShaderProperty.cs


row3 = new Vector4(value.m30, value.m31, value.m32, value.m33)
};
}
public override IShaderProperty Copy()
{
var copied = new Matrix4ShaderProperty();
copied.displayName = displayName;
copied.value = value;
return copied;
}
}
}

2
com.unity.shadergraph/Editor/Data/Graphs/MatrixShaderProperty.cs


public override PreviewProperty GetPreviewMaterialProperty()
{
return default(PreviewProperty);
}
}
}
}

10
com.unity.shadergraph/Editor/Data/Graphs/SamplerStateShaderProperty.cs


using System;
using System;
using UnityEditor.Graphing;
using UnityEngine;

public override INode ToConcreteNode()
{
return new SamplerStateNode();
}
public override IShaderProperty Copy()
{
var copied = new SamplerStateShaderProperty();
copied.displayName = displayName;
copied.value = value;
return copied;
}
}
}

10
com.unity.shadergraph/Editor/Data/Graphs/TextureShaderProperty.cs


using System;
using System;
using System.Text;
using UnityEditor.Graphing;
using UnityEngine;

public override INode ToConcreteNode()
{
return new Texture2DAssetNode { texture = value.texture };
}
public override IShaderProperty Copy()
{
var copied = new TextureShaderProperty();
copied.displayName = displayName;
copied.value = value;
return copied;
}
}
}

8
com.unity.shadergraph/Editor/Data/Graphs/Vector1ShaderProperty.cs


return node;
}
}
public override IShaderProperty Copy()
{
var copied = new Vector1ShaderProperty();
copied.displayName = displayName;
copied.value = value;
return copied;
}
}
}

8
com.unity.shadergraph/Editor/Data/Graphs/Vector2ShaderProperty.cs


node.FindInputSlot<Vector1MaterialSlot>(Vector2Node.InputSlotYId).value = value.y;
return node;
}
public override IShaderProperty Copy()
{
var copied = new Vector2ShaderProperty();
copied.displayName = displayName;
copied.value = value;
return copied;
}
}
}

8
com.unity.shadergraph/Editor/Data/Graphs/Vector3ShaderProperty.cs


node.FindInputSlot<Vector1MaterialSlot>(Vector3Node.InputSlotZId).value = value.z;
return node;
}
public override IShaderProperty Copy()
{
var copied = new Vector3ShaderProperty();
copied.displayName = displayName;
copied.value = value;
return copied;
}
}
}

8
com.unity.shadergraph/Editor/Data/Graphs/Vector4ShaderProperty.cs


node.FindInputSlot<Vector1MaterialSlot>(Vector4Node.InputSlotWId).value = value.w;
return node;
}
public override IShaderProperty Copy()
{
var copied = new Vector4ShaderProperty();
copied.displayName = displayName;
copied.value = value;
return copied;
}
}
}

9
com.unity.shadergraph/Editor/Drawing/MaterialGraphEditWindow.cs


#if UNITY_2018_1
using GeometryChangedEvent = UnityEngine.Experimental.UIElements.PostLayoutEvent;
#else
using UnityEditor.Experimental.UIElements.GraphView;
using GeometryChangedEvent = UnityEngine.Experimental.UIElements.GeometryChangedEvent;
#endif

var middle = bounds.center;
bounds.center = Vector2.zero;
// Collect the property nodes and get the corresponding properties
var propertyNodeGuids = graphView.selection.OfType<MaterialNodeView>().Where(x => (x.node is PropertyNode)).Select(x => ((PropertyNode)x.node).propertyGuid);
var metaProperties = graphView.graph.properties.Where(x => propertyNodeGuids.Contains(x.guid));
graphView.selection.OfType<Edge>().Select(x => x.userData as IEdge));
graphView.selection.OfType<Edge>().Select(x => x.userData as IEdge),
graphView.selection.OfType<BlackboardField>().Select(x => x.userData as IShaderProperty),
metaProperties);
var deserialized = CopyPasteGraph.FromJson(JsonUtility.ToJson(copyPasteGraph, false));
if (deserialized == null)

36
com.unity.shadergraph/Editor/Drawing/Views/MaterialGraphView.cs


using UnityEditor.Graphing;
using UnityEngine.Experimental.UIElements;
using Edge = UnityEditor.Experimental.UIElements.GraphView.Edge;
using Node = UnityEditor.Experimental.UIElements.GraphView.Node;
#if !UNITY_2018_1
using UnityEditor.Graphs;
#endif
namespace UnityEditor.ShaderGraph.Drawing
{

canPasteSerializedData = CanPasteSerializedDataImplementation;
unserializeAndPaste = UnserializeAndPasteImplementation;
deleteSelection = DeleteSelectionImplementation;
}
protected override bool canCopySelection
{
get { return selection.OfType<Node>().Any() || selection.OfType<GroupNode>().Any() || selection.OfType<BlackboardField>().Any(); }
}
public MaterialGraphView(AbstractMaterialGraph graph) : this()

string SerializeGraphElementsImplementation(IEnumerable<GraphElement> elements)
{
var graph = new CopyPasteGraph(elements.OfType<MaterialNodeView>().Select(x => (INode)x.node), elements.OfType<Edge>().Select(x => x.userData).OfType<IEdge>());
var nodes = elements.OfType<MaterialNodeView>().Select(x => (INode)x.node);
var edges = elements.OfType<Edge>().Select(x => x.userData).OfType<IEdge>();
var properties = selection.OfType<BlackboardField>().Select(x => x.userData as IShaderProperty);
// Collect the property nodes and get the corresponding properties
var propertyNodeGuids = nodes.OfType<PropertyNode>().Select(x => x.propertyGuid);
var metaProperties = this.graph.properties.Where(x => propertyNodeGuids.Contains(x.guid));
var graph = new CopyPasteGraph(nodes, edges, properties, metaProperties);
return JsonUtility.ToJson(graph, true);
}

{
if (copyGraph == null)
return;
// Make new properties from the copied graph
foreach (IShaderProperty property in copyGraph.properties)
{
string propertyName = graphView.graph.SanitizePropertyName(property.displayName);
IShaderProperty copiedProperty = property.Copy();
copiedProperty.displayName = propertyName;
graphView.graph.AddShaderProperty(copiedProperty);
// Update the property nodes that depends on the copied node
var dependentPropertyNodes = copyGraph.GetNodes<PropertyNode>().Where(x => x.propertyGuid == property.guid);
foreach (var node in dependentPropertyNodes)
{
node.owner = graphView.graph;
node.propertyGuid = copiedProperty.guid;
}
}
using (var remappedNodesDisposable = ListPool<INode>.GetDisposable())
using (var remappedEdgesDisposable = ListPool<IEdge>.GetDisposable())

59
com.unity.shadergraph/Editor/Util/CopyPasteGraph.cs


[NonSerialized]
HashSet<INode> m_Nodes = new HashSet<INode>();
[NonSerialized]
HashSet<IShaderProperty> m_Properties = new HashSet<IShaderProperty>();
// The meta properties are properties that are not copied into the tatget graph
// but sent along to allow property nodes to still hvae the data from the original
// property present.
[NonSerialized]
HashSet<IShaderProperty> m_MetaProperties = new HashSet<IShaderProperty>();
[SerializeField]
List<SerializationHelper.JSONSerializedElement> m_SerilaizeableProperties = new List<SerializationHelper.JSONSerializedElement>();
[SerializeField]
List<SerializationHelper.JSONSerializedElement> m_SerializableMetaProperties = new List<SerializationHelper.JSONSerializedElement>();
public CopyPasteGraph(IEnumerable<INode> nodes, IEnumerable<IEdge> edges)
public CopyPasteGraph(IEnumerable<INode> nodes, IEnumerable<IEdge> edges, IEnumerable<IShaderProperty> properties, IEnumerable<IShaderProperty> metaProperties)
{
foreach (var node in nodes)
{

foreach (var edge in edges)
AddEdge(edge);
foreach (var property in properties)
AddProperty(property);
foreach (var metaProperty in metaProperties)
AddMetaProperty(metaProperty);
}
public void AddNode(INode node)

m_Edges.Add(edge);
}
public void AddProperty(IShaderProperty property)
{
m_Properties.Add(property);
}
public void AddMetaProperty(IShaderProperty metaProperty)
{
m_MetaProperties.Add(metaProperty);
}
public IEnumerable<T> GetNodes<T>() where T : INode
{
return m_Nodes.OfType<T>();

get { return m_Edges; }
}
public IEnumerable<IShaderProperty> properties
{
get { return m_Properties; }
}
public IEnumerable<IShaderProperty> metaProperties
{
get { return m_MetaProperties; }
}
m_SerilaizeableProperties = SerializationHelper.Serialize<IShaderProperty>(m_Properties);
m_SerializableMetaProperties = SerializationHelper.Serialize<IShaderProperty>(m_MetaProperties);
}
public void OnAfterDeserialize()

foreach (var edge in edges)
m_Edges.Add(edge);
m_SerializableEdges = null;
var properties = SerializationHelper.Deserialize<IShaderProperty>(m_SerilaizeableProperties, GraphUtil.GetLegacyTypeRemapping());
m_Properties.Clear();
foreach (var property in properties)
m_Properties.Add(property);
m_SerilaizeableProperties = null;
var metaProperties = SerializationHelper.Deserialize<IShaderProperty>(m_SerializableMetaProperties, GraphUtil.GetLegacyTypeRemapping());
m_MetaProperties.Clear();
foreach (var metaProperty in metaProperties)
{
m_MetaProperties.Add(metaProperty);
}
m_SerializableMetaProperties = null;
}
internal static CopyPasteGraph FromJson(string copyBuffer)

正在加载...
取消
保存