浏览代码

Cleanup and fixes (part 1)

- Fix not updating edges when validation changes
- Generate correct function headers
- Calculate whether matrix slot connections are error'd
- Fix validation in subgraphs
- Various cleanup
/main
Matt Dean 7 年前
当前提交
22c13f89
共有 3 个文件被更改,包括 58 次插入140 次删除
  1. 195
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Basic/MultiplyNode.cs
  2. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/SubGraph.cs
  3. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/GraphEditorView.cs

195
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Basic/MultiplyNode.cs


const string kInput2SlotName = "B";
const string kOutputSlotName = "Out";
public enum MultiplyType
{
Vector,
Matrix,
Mixed
}
MultiplyType m_MultiplyType;
//get { return false; }
string GetFunctionName()
string GetFunctionHeader()
return string.Format("Unity_Multiply_{0}_{1}",
FindInputSlot<MaterialSlot>(Input1SlotId).concreteValueType.ToString(precision),
FindInputSlot<MaterialSlot>(Input2SlotId).concreteValueType.ToString(precision));
return string.Format("Unity_Multiply_{0}", precision);
}
public sealed override void UpdateNodeAfterDeserialization()

var outputValue = GetSlotValue(OutputSlotId, generationMode);
sb.AppendLine("{0} {1};", NodeUtils.ConvertConcreteSlotValueTypeToString(precision, FindOutputSlot<MaterialSlot>(OutputSlotId).concreteValueType), GetVariableNameForSlot(OutputSlotId));
sb.AppendLine("{0}({1}, {2}, {3});", GetFunctionName(), input1Value, input2Value, outputValue);
sb.AppendLine("{0}({1}, {2}, {3});", GetFunctionHeader(), input1Value, input2Value, outputValue);
string GetFunctionName()
{
return string.Format("{0}_{1}_{2}",
GetFunctionHeader(),
FindInputSlot<MaterialSlot>(Input1SlotId).concreteValueType.ToString(precision),
FindInputSlot<MaterialSlot>(Input2SlotId).concreteValueType.ToString(precision));
}
GetFunctionName(),
GetFunctionHeader(),
FindInputSlot<MaterialSlot>(Input1SlotId).concreteValueType.ToString(precision),
FindInputSlot<MaterialSlot>(Input2SlotId).concreteValueType.ToString(precision),
FindOutputSlot<MaterialSlot>(OutputSlotId).concreteValueType.ToString(precision));

// Internal validation
// -------------------------------------------------
public enum MultiplyType
{
Vector,
Matrix,
Mixed
}
MultiplyType m_MultiplyType;
public override void ValidateNode()
{

ListPool<DynamicValueMaterialSlot>.Release(skippedDynamicSlots);
DictionaryPool<DynamicValueMaterialSlot, ConcreteSlotValueType>.Release(dynamicInputSlotsToCompare);
List<MaterialSlot> inSlots = new List<MaterialSlot>();
List<MaterialSlot> outSlots = new List<MaterialSlot>();
GetInputSlots<MaterialSlot>(inSlots);
GetOutputSlots<MaterialSlot>(outSlots);
}
// Debugs
foreach(MaterialSlot i in inSlots)
Debug.Log("Node: "+this.guid +" slot "+i.displayName+" to type "+i.concreteValueType);
foreach(MaterialSlot o in outSlots)
Debug.Log("Node: "+this.guid +" slot "+o.displayName+" to type "+o.concreteValueType);
protected override bool CalculateNodeHasError()
{
if(m_MultiplyType == MultiplyType.Matrix)
{
foreach (var slot in this.GetOutputSlots<ISlot>())
{
foreach (var edge in owner.GetEdges(slot.slotReference))
{
var inputNode = owner.GetNodeFromGuid(edge.inputSlot.nodeGuid);
List<MaterialSlot> slots = new List<MaterialSlot>();
inputNode.GetInputSlots(slots);
foreach(var s in slots)
{
foreach(var inputEdge in inputNode.owner.GetEdges(s.slotReference))
{
if(inputEdge == edge)
{
if(s as DynamicValueMaterialSlot == null)
{
if(s.concreteValueType != ConcreteSlotValueType.Matrix4
&& s.concreteValueType != ConcreteSlotValueType.Matrix3
&& s.concreteValueType != ConcreteSlotValueType.Matrix2)
{
Debug.Log("ERROR: slot "+s.displayName+" cannot accept a Matrix type input");
return true;
}
}
}
}
}
}
}
}
return false;
}
private static bool ImplicitConversionExists(ConcreteSlotValueType from, ConcreteSlotValueType to)

}
return ConcreteSlotValueType.Matrix2;
}
/*private ConcreteSlotValueType ConvertDynamicOutputType(List<ConcreteSlotValueType> inputTypesDistinct)
{
// If dynamics contain vectors return a vector
if(DynamicsContainVectors(inputTypesDistinct))
{
int vectorLength;
// If mul(matrix, vector) return matrix length
if(DynamicsContainMatrices(inputTypesDistinct, out vectorLength))
{
switch(vectorLength)
{
case 2:
return ConcreteSlotValueType.Vector2;
case 3:
return ConcreteSlotValueType.Vector3;
case 4:
return ConcreteSlotValueType.Vector4;
default:
return ConcreteSlotValueType.Vector1;
}
}
// find the 'minumum' channel width excluding 1 as it can promote
inputTypesDistinct.RemoveAll(x => x == ConcreteSlotValueType.Vector1);
var ordered = inputTypesDistinct.OrderByDescending(x => x);
if (ordered.Any())
return ordered.FirstOrDefault();
return ConcreteSlotValueType.Vector1;
}
// Otherwise return a matrix
else
{
var ordered = inputTypesDistinct.OrderByDescending(x => x);
if (ordered.Any())
return ordered.FirstOrDefault();
return ConcreteSlotValueType.Vector1;
}
}
private bool DynamicsContainVectors(IList<ConcreteSlotValueType> inputTypes)
{
for (int i = 0; i < inputTypes.Count; i++)
{
if(inputTypes[i] == ConcreteSlotValueType.Vector4
|| inputTypes[i] == ConcreteSlotValueType.Vector3
|| inputTypes[i] == ConcreteSlotValueType.Vector2
|| inputTypes[i] == ConcreteSlotValueType.Vector1)
{
return true;
}
}
return false;
}
private bool DynamicsContainMatrices(IList<ConcreteSlotValueType> inputTypes, out int vectorLength)
{
for (int i = 0; i < inputTypes.Count; i++)
{
if(DynamicIsMatrix(inputTypes[i], out vectorLength))
return true;
}
vectorLength = 0;
return false;
}
private bool DynamicIsMatrix(ConcreteSlotValueType type, out int vectorLength)
{
if(type == ConcreteSlotValueType.Matrix4)
{
vectorLength = 4;
return true;
}
else if(type == ConcreteSlotValueType.Matrix3)
{
vectorLength = 3;
return true;
}
else if(type == ConcreteSlotValueType.Matrix2)
{
vectorLength = 2;
return true;
}
else
vectorLength = 1;
return false;
}*/
/*public MultiplyNode()
{
name = "Multiply";
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("Unity_Multiply", BindingFlags.Static | BindingFlags.NonPublic);
}
static string Unity_Multiply(
[Slot(0, Binding.None, 0, 0, 0, 0)] DynamicDimensionVector A,
[Slot(1, Binding.None, 2, 2, 2, 2)] DynamicDimensionVector B,
[Slot(2, Binding.None)] out DynamicDimensionVector Out)
{
return
@"
{
Out = A * B;
}
";
}*/
}

1
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/SubGraph/SubGraph.cs


{
foreach (var node in activeNodes)
{
node.ValidateNode();
if (node is IGeneratesFunction)
(node as IGeneratesFunction).GenerateNodeFunction(registry, generationMode);
}

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/GraphEditorView.cs


foreach (var edgeView in anchorView.connections.OfType<Edge>())
{
var targetSlot = edgeView.input.GetSlot();
if (targetSlot.valueType == SlotValueType.DynamicVector)
if (targetSlot.valueType == SlotValueType.DynamicVector || targetSlot.valueType == SlotValueType.DynamicMatrix || targetSlot.valueType == SlotValueType.Dynamic)
{
var connectedNodeView = edgeView.input.node as MaterialNodeView;
if (connectedNodeView != null && !nodeViews.Contains(connectedNodeView))

正在加载...
取消
保存