浏览代码

[Material Graph]Material graph does not need reference to owner + more tests.

/main
Tim Cooper 8 年前
当前提交
2630ebd1
共有 7 个文件被更改,包括 138 次插入40 次删除
  1. 18
      UnityProject/Assets/UnityShaderEditor/Editor/Source/AbstractMaterialGraph.cs
  2. 2
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/MaterialGraphDataSource.cs
  3. 12
      UnityProject/Assets/UnityShaderEditor/Editor/Source/MaterialGraph.cs
  4. 2
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/AbstractMaterialNode.cs
  5. 6
      UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/PixelShaderNode.cs
  6. 25
      UnityProject/Assets/UnityShaderEditor/Editor/Source/PixelGraph.cs
  7. 113
      UnityProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/AbstractMaterialGraphTests.cs

18
UnityProject/Assets/UnityShaderEditor/Editor/Source/AbstractMaterialGraph.cs


{
[NonSerialized]
private PreviewRenderUtility m_PreviewUtility;
[NonSerialized]
private MaterialGraph m_Owner;
protected AbstractMaterialGraph(MaterialGraph owner)
{
m_Owner = owner;
}
public IEnumerable<AbstractMaterialNode> materialNodes
{
get { return nodes.OfType<AbstractMaterialNode>(); }

if (m_PreviewUtility == null)
{
m_PreviewUtility = new PreviewRenderUtility();
// EditorUtility.SetCameraAnimateMaterials(m_PreviewUtility.m_Camera, true);
EditorUtility.SetCameraAnimateMaterials(m_PreviewUtility.m_Camera, true);
}
return m_PreviewUtility;

public bool requiresRepaint
{
get { return nodes.Any(x => x is IRequiresTime); }
}
public MaterialGraph owner
{
get { return m_Owner; }
set { m_Owner = value; }
}
public override void AddNode(SerializableNode node)

2
UnityProject/Assets/UnityShaderEditor/Editor/Source/Drawing/MaterialGraphDataSource.cs


{
var baseNode = drawableMaterialNode.m_Node;
// grab the input slots where there are no edges
foreach (var slot in baseNode.GetDrawableInputProxies())
foreach (var slot in baseNode.GetInputsWithNoConnection())
{
// if there is no anchor, continue
// this can happen if we are in collapsed mode

12
UnityProject/Assets/UnityShaderEditor/Editor/Source/MaterialGraph.cs


namespace UnityEditor.MaterialGraph
{
[Serializable]
public class MaterialGraph : ISerializationCallbackReceiver
public class MaterialGraph
{
[SerializeField]
private MaterialOptions m_MaterialOptions = new MaterialOptions();

public MaterialGraph()
{
m_PixelGraph = new PixelGraph(this);
m_PixelGraph = new PixelGraph();
}
public MaterialOptions materialOptions

public AbstractMaterialGraph currentGraph
{
get { return m_PixelGraph; }
}
public void OnBeforeSerialize()
{}
public void OnAfterDeserialize()
{
m_PixelGraph.owner = this;
}
public Material GetMaterial()

2
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/AbstractMaterialNode.cs


return inputSlot.OnGUI(rect, inputSlotType);
}
public virtual IEnumerable<MaterialSlot> GetDrawableInputProxies()
public virtual IEnumerable<MaterialSlot> GetInputsWithNoConnection()
{
return materialInputSlots.Where(x => !owner.GetEdges(GetSlotReference(x.name)).Any());
}

6
UnityProject/Assets/UnityShaderEditor/Editor/Source/Nodes/PixelShaderNode.cs


}
return GUIModificationType.None;
}
public override IEnumerable<MaterialSlot> GetDrawableInputProxies()
public override IEnumerable<MaterialSlot> GetInputsWithNoConnection()
{
return new List<MaterialSlot>();
}

var shaderName = "Hidden/PreviewShader/" + name + "_" + guid.ToString().Replace("-","_");
List<PropertyGenerator.TextureInfo> defaultTextures;
var resultShader = ShaderGenerator.GenerateSurfaceShader(materialGraphOwner.owner, shaderName, true, out defaultTextures);
//TODO: Fix me
var resultShader = string.Empty;//ShaderGenerator.GenerateSurfaceShader(materialGraphOwner.owner, shaderName, true, out defaultTextures);
m_GeneratedShaderMode = PreviewMode.Preview3D;
hasError = !InternalUpdatePreviewShader(resultShader);
return true;

25
UnityProject/Assets/UnityShaderEditor/Editor/Source/PixelGraph.cs


[NonSerialized]
private PixelShaderNode m_PixelMasterNode;
public PixelGraph(MaterialGraph owner) : base (owner)
{}
public PixelShaderNode pixelMasterNode
{
get

m_PixelMasterNode = nodes.FirstOrDefault(x => x.GetType() == typeof(PixelShaderNode)) as PixelShaderNode;
// none exists, so create!
if (m_PixelMasterNode == null)
{
m_PixelMasterNode = new PixelShaderNode(this);
AddNode(m_PixelMasterNode);
m_PixelMasterNode.position = new Rect(700, m_PixelMasterNode.position.y, m_PixelMasterNode.position.width, m_PixelMasterNode.position.height);
}
return m_PixelMasterNode;
}
}

NodeUtils.DepthFirstCollectNodesFromNode(m_ActiveNodes, pixelMasterNode);
return m_ActiveNodes.OfType<AbstractMaterialNode>();
}
}
public override void OnAfterDeserialize()
{
m_PixelMasterNode = null;
}
public override void AddNode(SerializableNode node)
{
if (pixelMasterNode != null && node is PixelShaderNode)
{
Debug.LogWarning("Attempting to add second PixelShaderNode to PixelGraph. This is not allowed.");
return;
}
base.AddNode(node);
}
public void GenerateSurfaceShader(

113
UnityProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/AbstractMaterialGraphTests.cs


using System.Linq;
using NUnit.Framework;
namespace UnityEditor.MaterialGraph.Tests
{
[TestFixture]
public class AbstractMaterialGraphTests
{
private class TestableMGraph : AbstractMaterialGraph
{}
private class TestableMNode : AbstractMaterialNode
{
public TestableMNode(AbstractMaterialGraph theOwner) : base(theOwner)
{}
}
private class TimeTestableMNode : AbstractMaterialNode, IRequiresTime
{
public TimeTestableMNode(AbstractMaterialGraph theOwner) : base(theOwner)
{}
}
[Test]
public void TestCanCreateMaterialGraph()
{
TestableMGraph graph = new TestableMGraph();
Assert.AreEqual(0, graph.edges.Count());
Assert.AreEqual(0, graph.nodes.Count());
}
[Test]
public void TestCanAddMaterialNodeToMaterialGraph()
{
TestableMGraph graph = new TestableMGraph();
var node = new TestableMNode(graph);
graph.AddNode(node);
Assert.AreEqual(0, graph.edges.Count());
Assert.AreEqual(1, graph.nodes.Count());
Assert.AreEqual(1, graph.materialNodes.Count());
}
[Test]
public void TestCanNotAddSerializableNodeToMaterialGraph()
{
TestableMGraph graph = new TestableMGraph();
var node = new SerializableNode(graph);
graph.AddNode(node);
Assert.AreEqual(0, graph.edges.Count());
Assert.AreEqual(0, graph.nodes.Count());
Assert.AreEqual(0, graph.materialNodes.Count());
}
[Test]
public void TestCanGetMaterialNodeFromMaterialGraph()
{
TestableMGraph graph = new TestableMGraph();
var node = new TestableMNode(graph);
graph.AddNode(node);
Assert.AreEqual(0, graph.edges.Count());
Assert.AreEqual(1, graph.nodes.Count());
Assert.AreEqual(node, graph.GetMaterialNodeFromGuid(node.guid));
}
[Test]
public void TestMaterialGraphNeedsRepaintWhenTimeNodePresent()
{
TestableMGraph graph = new TestableMGraph();
graph.AddNode(new TestableMNode(graph));
Assert.AreEqual(0, graph.edges.Count());
Assert.AreEqual(1, graph.nodes.Count());
Assert.IsFalse(graph.requiresRepaint);
graph.AddNode(new TimeTestableMNode(graph));
Assert.IsTrue(graph.requiresRepaint);
}
[Test]
public void TestCreatePixelShaderGraphWorks()
{
var graph = new PixelGraph();
Assert.AreEqual(0, graph.nodes.Count());
var psn = new PixelShaderNode(graph);
graph.AddNode(psn);
Assert.AreEqual(0, graph.edges.Count());
Assert.AreEqual(1, graph.nodes.Count());
Assert.IsInstanceOf(typeof(PixelShaderNode), graph.nodes.FirstOrDefault());
Assert.IsNotNull(graph.pixelMasterNode);
Assert.AreEqual(1, graph.activeNodes.Count());
}
[Test]
public void TestCanOnlyAddOnePixelShaderNode()
{
var graph = new PixelGraph();
Assert.AreEqual(0, graph.nodes.Count());
var psn = new PixelShaderNode(graph);
graph.AddNode(psn);
Assert.AreEqual(0, graph.edges.Count());
Assert.AreEqual(1, graph.nodes.Count());
var psn2 = new PixelShaderNode(graph);
graph.AddNode(psn2);
Assert.AreEqual(0, graph.edges.Count());
Assert.AreEqual(1, graph.nodes.Count());
}
}
}
正在加载...
取消
保存