浏览代码

Allow sahder properties to be copied and pasted between graphs

/main
Jens Holm 6 年前
当前提交
74039530
共有 3 个文件被更改,包括 84 次插入3 次删除
  1. 3
      com.unity.shadergraph/Editor/Drawing/MaterialGraphEditWindow.cs
  2. 56
      com.unity.shadergraph/Editor/Drawing/Views/MaterialGraphView.cs
  3. 28
      com.unity.shadergraph/Editor/Util/CopyPasteGraph.cs

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


var copyPasteGraph = new CopyPasteGraph(
graphView.selection.OfType<MaterialNodeView>().Where(x => !(x.node is PropertyNode)).Select(x => x.node as INode),
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));
var deserialized = CopyPasteGraph.FromJson(JsonUtility.ToJson(copyPasteGraph, false));
if (deserialized == null)

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


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);
var graph = new CopyPasteGraph(nodes, edges, properties);
return JsonUtility.ToJson(graph, true);
}

{
if (copyGraph == null)
return;
foreach (IShaderProperty property in copyGraph.properties)
{
string propertyName = graphView.graph.SanitizePropertyName(property.displayName);
IShaderProperty copiedProperty = null;
if (property is Vector1ShaderProperty)
{
copiedProperty = new Vector1ShaderProperty();
(copiedProperty as Vector1ShaderProperty).value = (property as Vector1ShaderProperty).value;
}
else if (property is Vector2ShaderProperty)
{
copiedProperty = new Vector2ShaderProperty();
(copiedProperty as Vector2ShaderProperty).value = (property as Vector2ShaderProperty).value;
}
else if (property is Vector3ShaderProperty)
{
copiedProperty = new Vector3ShaderProperty();
(copiedProperty as Vector3ShaderProperty).value = (property as Vector3ShaderProperty).value;
}
else if (property is Vector4ShaderProperty)
{
copiedProperty = new Vector4ShaderProperty();
(copiedProperty as Vector4ShaderProperty).value = (property as Vector4ShaderProperty).value;
}
else if (property is ColorShaderProperty)
{
copiedProperty = new ColorShaderProperty();
(copiedProperty as ColorShaderProperty).value = (property as ColorShaderProperty).value;
}
else if (property is TextureShaderProperty)
{
copiedProperty = new TextureShaderProperty();
(copiedProperty as TextureShaderProperty).value = (property as TextureShaderProperty).value;
}
else if (property is CubemapShaderProperty)
{
copiedProperty = new CubemapShaderProperty();
(copiedProperty as CubemapShaderProperty).value = (property as CubemapShaderProperty).value;
}
else if (property is BooleanShaderProperty)
{
copiedProperty = new BooleanShaderProperty();
(copiedProperty as BooleanShaderProperty).value = (property as BooleanShaderProperty).value;
}
copiedProperty.displayName = propertyName;
graphView.graph.AddShaderProperty(copiedProperty);
}
using (var remappedNodesDisposable = ListPool<INode>.GetDisposable())
using (var remappedEdgesDisposable = ListPool<IEdge>.GetDisposable())

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


[NonSerialized]
HashSet<INode> m_Nodes = new HashSet<INode>();
[NonSerialized]
HashSet<IShaderProperty> m_Properties = new HashSet<IShaderProperty>();
[SerializeField]
List<SerializationHelper.JSONSerializedElement> m_SerializableNodes = new List<SerializationHelper.JSONSerializedElement>();

[SerializeField]
List<SerializationHelper.JSONSerializedElement> m_SerilaizeableProperties = new List<SerializationHelper.JSONSerializedElement>();
public CopyPasteGraph(IEnumerable<INode> nodes, IEnumerable<IEdge> edges)
public CopyPasteGraph(IEnumerable<INode> nodes, IEnumerable<IEdge> edges, IEnumerable<IShaderProperty> properties)
{
foreach (var node in nodes)
{

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

m_Edges.Add(edge);
}
public void AddProperty(IShaderProperty property)
{
m_Properties.Add(property);
}
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 void OnBeforeSerialize()

m_SerilaizeableProperties = SerializationHelper.Serialize<IShaderProperty>(m_Properties);
}
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;
}
internal static CopyPasteGraph FromJson(string copyBuffer)

正在加载...
取消
保存