浏览代码

Squash `SerializableSlot` into `MaterialSlot`

/main
Peter Bay Bastian 7 年前
当前提交
5fe33b9e
共有 8 个文件被更改,包括 184 次插入179 次删除
  1. 128
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/MaterialSlot.cs
  2. 15
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/IntegrationTests/SerializationTests.cs
  3. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/MaterialNodeTests.cs
  4. 36
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/SerializedGraphTests.cs
  5. 33
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/TestSlot.cs
  6. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/TestSlot.cs.meta
  7. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/SerializableGraph/Implementation/SerializableSlot.cs.meta
  8. 134
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/SerializableGraph/Implementation/SerializableSlot.cs

128
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/MaterialSlot.cs


using System;
using System.Linq;
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("com.unity.shadergraph.EditorTests")]
public abstract class MaterialSlot : SerializableSlot
public abstract class MaterialSlot : ISlot
const string k_NotInit = "Not Initilaized";
[SerializeField]
int m_Id;
[SerializeField]
string m_DisplayName = k_NotInit;
[SerializeField]
SlotType m_SlotType = SlotType.Input;
[SerializeField]
int m_Priority = int.MaxValue;
[SerializeField]
bool m_Hidden;
[SerializeField]
string m_ShaderOutputName;

private bool m_HasError;
bool m_HasError;
: base(slotId, displayName, slotType, hidden)
m_Id = slotId;
m_DisplayName = displayName;
m_SlotType = slotType;
m_Hidden = hidden;
m_ShaderOutputName = shaderOutputName;
this.shaderStage = shaderStage;
}
protected MaterialSlot(int slotId, string displayName, string shaderOutputName, SlotType slotType, int priority, ShaderStage shaderStage = ShaderStage.Dynamic, bool hidden = false)
{
m_Id = slotId;
m_DisplayName = displayName;
m_SlotType = slotType;
m_Priority = priority;
m_Hidden = hidden;
m_ShaderOutputName = shaderOutputName;
this.shaderStage = shaderStage;
}

}
}
public override string displayName
public virtual string displayName
get { return base.displayName + ConcreteSlotValueTypeAsString(concreteValueType); }
set { base.displayName = value; }
get { return m_DisplayName + ConcreteSlotValueTypeAsString(concreteValueType); }
set { m_DisplayName = value; }
return base.displayName;
return m_DisplayName;
}
public static MaterialSlot CreateMaterialSlot(SlotValueType type, int slotId, string displayName, string shaderOutputName, SlotType slotType, Vector4 defaultValue, ShaderStage shaderStage = ShaderStage.Dynamic, bool hidden = false)

throw new ArgumentOutOfRangeException("type", type, null);
}
public SlotReference slotReference
{
get { return new SlotReference(owner.guid, m_Id); }
}
public INode owner { get; set; }
public bool hidden
{
get { return m_Hidden; }
set { m_Hidden = value; }
}
public int id
{
get { return m_Id; }
}
public int priority
{
get { return m_Priority; }
set { m_Priority = value; }
}
public bool isInputSlot
{
get { return m_SlotType == SlotType.Input; }
}
public bool isOutputSlot
{
get { return m_SlotType == SlotType.Output; }
}
public SlotType slotType
{
get { return m_SlotType; }
}
public bool isConnected
{
get
{
// node and graph respectivly
if (owner == null || owner.owner == null)
return false;
var graph = owner.owner;
var edges = graph.GetEdges(slotReference);
return edges.Any();
}
}
public abstract SlotValueType valueType { get; }
public abstract ConcreteSlotValueType concreteValueType { get; }

}
public abstract void CopyValuesFrom(MaterialSlot foundSlot);
bool Equals(MaterialSlot other)
{
return m_Id == other.m_Id && owner.guid.Equals(other.owner.guid);
}
public bool Equals(ISlot other)
{
return Equals(other as object);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((MaterialSlot)obj);
}
public override int GetHashCode()
{
unchecked
{
return (m_Id * 397) ^ (owner != null ? owner.GetHashCode() : 0);
}
}
}
}

15
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/IntegrationTests/SerializationTests.cs


using NUnit.Framework;
using UnityEngine;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph;
namespace UnityEditor.Graphing.IntegrationTests
{

[Test]
public void TestSerializableSlotCanSerialize()
{
var toSerialize = new List<SerializableSlot>()
var toSerialize = new List<MaterialSlot>()
new SerializableSlot(0, "InSlot", SlotType.Input, 0),
new SerializableSlot(1, "OutSlot", SlotType.Output, 5),
new TestSlot(0, "InSlot", SlotType.Input, 0),
new TestSlot(1, "OutSlot", SlotType.Output, 5),
var serialized = SerializationHelper.Serialize<SerializableSlot>(toSerialize);
var loaded = SerializationHelper.Deserialize<SerializableSlot>(serialized, null);
var serialized = SerializationHelper.Serialize<MaterialSlot>(toSerialize);
var loaded = SerializationHelper.Deserialize<MaterialSlot>(serialized, null);
Assert.IsInstanceOf<SerializableSlot>(loaded[0]);
Assert.IsInstanceOf<SerializableSlot>(loaded[1]);
Assert.IsInstanceOf<MaterialSlot>(loaded[0]);
Assert.IsInstanceOf<MaterialSlot>(loaded[1]);
Assert.AreEqual(0, loaded[0].id);
Assert.AreEqual("InSlot", loaded[0].displayName);

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/MaterialNodeTests.cs


[Test]
public void AddingNonMaterialSlotToNodeThrows()
{
Assert.Throws<ArgumentException>(() => m_NodeA.AddSlot(new SerializableSlot(0, string.Empty, SlotType.Input)));
Assert.Throws<ArgumentException>(() => m_NodeA.AddSlot(new TestSlot(0, string.Empty, SlotType.Input)));
}
[Test]

36
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/SerializedGraphTests.cs


public TestableNode()
{
AddSlot(new SerializableSlot(Input0, "Input", SlotType.Input));
AddSlot(new SerializableSlot(Input1, "Input", SlotType.Input));
AddSlot(new SerializableSlot(Input2, "Input", SlotType.Input));
AddSlot(new TestSlot(Input0, "Input", SlotType.Input));
AddSlot(new TestSlot(Input1, "Input", SlotType.Input));
AddSlot(new TestSlot(Input2, "Input", SlotType.Input));
AddSlot(new SerializableSlot(Output0, "Output", SlotType.Output));
AddSlot(new SerializableSlot(Output1, "Output", SlotType.Output));
AddSlot(new SerializableSlot(Output2, "Output", SlotType.Output));
AddSlot(new TestSlot(Output0, "Output", SlotType.Output));
AddSlot(new TestSlot(Output1, "Output", SlotType.Output));
AddSlot(new TestSlot(Output2, "Output", SlotType.Output));
}
}

{
var graph = new TestMaterialGraph();
var node = new TestNode();
node.AddSlot(new SerializableSlot(0, "output", SlotType.Output));
node.AddSlot(new SerializableSlot(1, "input", SlotType.Input));
node.AddSlot(new TestSlot(0, "output", SlotType.Output));
node.AddSlot(new TestSlot(1, "input", SlotType.Input));
node.name = "Test Node";
graph.AddNode(node);

{
var graph = new TestMaterialGraph();
var node = new TestNode();
node.AddSlot(new SerializableSlot(0, "output", SlotType.Output));
node.AddSlot(new SerializableSlot(1, "input", SlotType.Input));
node.AddSlot(new TestSlot(0, "output", SlotType.Output));
node.AddSlot(new TestSlot(1, "input", SlotType.Input));
graph.AddNode(node);
Assert.AreEqual(2, node.GetSlots<ISlot>().Count());

{
var graph = new TestMaterialGraph();
var node = new TestNode();
node.AddSlot(new SerializableSlot(0, "output", SlotType.Output));
node.AddSlot(new SerializableSlot(0, "output", SlotType.Output));
node.AddSlot(new TestSlot(0, "output", SlotType.Output));
node.AddSlot(new TestSlot(0, "output", SlotType.Output));
node.name = "Test Node";
graph.AddNode(node);

{
var graph = new TestMaterialGraph();
var node = new TestNode();
node.AddSlot(new SerializableSlot(0, "output", SlotType.Output));
node.AddSlot(new SerializableSlot(0, "output_updated", SlotType.Output));
node.AddSlot(new TestSlot(0, "output", SlotType.Output));
node.AddSlot(new TestSlot(0, "output_updated", SlotType.Output));
node.name = "Test Node";
graph.AddNode(node);

{
var graph = new TestMaterialGraph();
var node = new TestNode();
node.AddSlot(new SerializableSlot(0, "output", SlotType.Output, 0));
node.AddSlot(new TestSlot(0, "output", SlotType.Output, 0));
node.name = "Test Node";
graph.AddNode(node);

{
var graph = new TestMaterialGraph();
var node = new TestNode();
node.AddSlot(new SerializableSlot(0, "output", SlotType.Output, 0));
node.AddSlot(new SerializableSlot(0, "output", SlotType.Output, 5));
node.AddSlot(new TestSlot(0, "output", SlotType.Output, 0));
node.AddSlot(new TestSlot(0, "output", SlotType.Output, 5));
node.name = "Test Node";
graph.AddNode(node);

public void TestCanUpdateSlotDisplayName()
{
var node = new TestNode();
node.AddSlot(new SerializableSlot(0, "output", SlotType.Output));
node.AddSlot(new TestSlot(0, "output", SlotType.Output));
node.name = "Test Node";
Assert.AreEqual(0, node.GetInputSlots<ISlot>().Count());

33
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/TestSlot.cs


using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
public class TestSlot : MaterialSlot
{
public TestSlot(int slotId, string displayName, SlotType slotType, ShaderStage shaderStage = ShaderStage.Dynamic, bool hidden = false)
: base(slotId, displayName, displayName, slotType, shaderStage, hidden) {}
public TestSlot(int slotId, string displayName, SlotType slotType, int priority, ShaderStage shaderStage = ShaderStage.Dynamic, bool hidden = false)
: base(slotId, displayName, displayName, slotType, priority, shaderStage, hidden) {}
public override SlotValueType valueType
{
get { throw new System.NotImplementedException(); }
}
public override ConcreteSlotValueType concreteValueType
{
get { throw new System.NotImplementedException(); }
}
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{
throw new System.NotImplementedException();
}
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
throw new System.NotImplementedException();
}
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/TestSlot.cs.meta


fileFormatVersion: 2
guid: e7e4d50a46ff471096f3c60fb06a364d
timeCreated: 1513346708

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/SerializableGraph/Implementation/SerializableSlot.cs.meta


fileFormatVersion: 2
guid: 236912145b2fff1409d2b69157f5de23
timeCreated: 1464264924
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

134
MaterialGraphProject/Assets/UnityShaderEditor/Editor/SerializableGraph/Implementation/SerializableSlot.cs


using System;
using System.Linq;
using UnityEngine;
namespace UnityEditor.Graphing
{
[Serializable]
public class SerializableSlot : ISlot
{
private const string kNotInit = "Not Initilaized";
[SerializeField]
private int m_Id;
[SerializeField]
private string m_DisplayName = kNotInit;
[SerializeField]
private SlotType m_SlotType = SlotType.Input;
[SerializeField]
private int m_Priority = int.MaxValue;
[SerializeField]
private bool m_Hidden;
public SlotReference slotReference
{
get { return new SlotReference(owner.guid, m_Id); }
}
public INode owner { get; set; }
public bool hidden
{
get { return m_Hidden; }
set { m_Hidden = value; }
}
public int id
{
get { return m_Id; }
}
public virtual string displayName
{
get { return m_DisplayName; }
set { m_DisplayName = value; }
}
public int priority
{
get { return m_Priority; }
set { m_Priority = value; }
}
public bool isInputSlot
{
get { return m_SlotType == SlotType.Input; }
}
public bool isOutputSlot
{
get { return m_SlotType == SlotType.Output; }
}
public SlotType slotType
{
get { return m_SlotType; }
}
// used via reflection / serialization after deserialize
// to reconstruct this slot.
public SerializableSlot()
{}
public SerializableSlot(int id, string displayName, SlotType slotType, int priority, bool hidden = false)
{
m_Id = id;
m_DisplayName = displayName;
m_SlotType = slotType;
m_Priority = priority;
m_Hidden = hidden;
}
public SerializableSlot(int id, string displayName, SlotType slotType, bool hidden = false)
{
m_Id = id;
m_DisplayName = displayName;
m_SlotType = slotType;
m_Hidden = hidden;
}
protected bool Equals(SerializableSlot other)
{
return m_Id == other.m_Id && owner.guid.Equals(other.owner.guid);
}
public bool Equals(ISlot other)
{
return Equals(other as object);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((SerializableSlot)obj);
}
public override int GetHashCode()
{
unchecked
{
return (m_Id * 397) ^ (owner != null ? owner.GetHashCode() : 0);
}
}
public bool isConnected
{
get
{
// node and graph respectivly
if (owner == null || owner.owner == null)
return false;
var graph = owner.owner;
var edges = graph.GetEdges(slotReference);
return edges.Any();
}
}
}
}
正在加载...
取消
保存