浏览代码

compiling

/main
Tim Cooper 7 年前
当前提交
2c1b60e0
共有 17 个文件被更改,包括 345 次插入218 次删除
  1. 5
      MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Implementation/SerializableSlot.cs
  2. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/AbstractMaterialGraphEditWindow.cs
  3. 56
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/IMGUISlotEditorView.cs
  4. 9
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/NodeInspectors/BasicNodeInspector.cs
  5. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/NodeInspectors/SubgraphOutputNodeInspector.cs
  6. 14
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/DynamicSlotTests.cs
  7. 16
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/FixedSlotTests.cs
  8. 15
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/MaterialNodeTests.cs
  9. 90
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/MaterialSlotTests.cs
  10. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/ShaderGeneratorTests.cs
  11. 178
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/MaterialSlot.cs
  12. 149
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbstractMaterialNode.cs
  13. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/HLSLNode.cs
  14. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Input/Matrix/Matrix3Node.cs
  15. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Input/Vector/Vector4Node.cs
  16. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/SlotValue.cs
  17. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/AbstractSubGraphNode.cs

5
MaterialGraphProject/Assets/GraphFramework/SerializableGraph/Runtime/Implementation/SerializableSlot.cs


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()

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/AbstractMaterialGraphEditWindow.cs


break;
case ConcreteSlotValueType.Vector1:
break;
case ConcreteSlotValueType.Error:
break;
default:
throw new ArgumentOutOfRangeException();
}

56
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/IMGUISlotEditorView.cs


using System;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
using UnityEngine.Experimental.UIElements;
using UnityEngine.Graphing;
using UnityEngine.MaterialGraph;

return;
var previousWideMode = EditorGUIUtility.wideMode;
EditorGUIUtility.wideMode = true;
var newValue = SlotField(m_Slot);
if (newValue != m_Slot.currentValue)
{
m_Slot.currentValue = newValue;
m_Slot.owner.onModified(m_Slot.owner, ModificationScope.Node);
}
var modified = SlotField(m_Slot);
if (modified)
m_Slot.owner.onModified(m_Slot.owner, ModificationScope.Node);
static Vector4 SlotField(MaterialSlot slot)
public static bool SlotField(MaterialSlot slot)
if (slot.concreteValueType == ConcreteSlotValueType.Vector1)
return new Vector4(EditorGUILayout.FloatField(slot.displayName, slot.currentValue.x), 0, 0, 0);
if (slot.concreteValueType == ConcreteSlotValueType.Vector2)
return EditorGUILayout.Vector2Field(slot.displayName, slot.currentValue);
if (slot.concreteValueType == ConcreteSlotValueType.Vector3)
return EditorGUILayout.Vector3Field(slot.displayName, slot.currentValue);
if (slot.concreteValueType == ConcreteSlotValueType.Vector4)
return EditorGUILayout.Vector4Field(slot.displayName, slot.currentValue);
return Vector4.zero;
EditorGUI.BeginChangeCheck();
if (slot is DynamicVectorMaterialSlot)
{
var dynSlot = slot as DynamicVectorMaterialSlot;
dynSlot.value = EditorGUILayout.Vector4Field(slot.displayName, dynSlot.value);
}
if (slot is Vector1MaterialSlot)
{
var dynSlot = slot as Vector1MaterialSlot;
dynSlot.value = EditorGUILayout.FloatField(slot.displayName, dynSlot.value);
}
if (slot is Vector2MaterialSlot)
{
var dynSlot = slot as Vector2MaterialSlot;
dynSlot.value = EditorGUILayout.Vector2Field(slot.displayName, dynSlot.value);
}
if (slot is Vector3MaterialSlot)
{
var dynSlot = slot as Vector3MaterialSlot;
dynSlot.value = EditorGUILayout.Vector3Field(slot.displayName, dynSlot.value);
}
if (slot is Vector4MaterialSlot)
{
var dynSlot = slot as Vector4MaterialSlot;
dynSlot.value = EditorGUILayout.Vector4Field(slot.displayName, dynSlot.value);
}
return EditorGUI.EndChangeCheck();
}
}
}

9
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/NodeInspectors/BasicNodeInspector.cs


using System.Linq;
using UnityEditor.MaterialGraph.Drawing.Inspector;
using UnityEngine;
using UnityEngine.Graphing;
using UnityEngine.MaterialGraph;

if (!slots.Any())
return ModificationScope.Nothing;
EditorGUI.BeginChangeCheck();
var modified = false;
slot.currentValue = EditorGUILayout.Vector4Field(slot.displayName, slot.currentValue);
modified |= IMGUISlotEditorView.SlotField(slot);
return EditorGUI.EndChangeCheck() ? ModificationScope.Node : ModificationScope.Nothing;
return modified ? ModificationScope.Node : ModificationScope.Nothing;
}
}
}

5
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/NodeInspectors/SubgraphOutputNodeInspector.cs


{
EditorGUI.BeginChangeCheck();
var result = (UISlotValueType)EditorGUILayout.EnumPopup(ToUISlot(slot.valueType));
slot.valueType = ToSlotValueType(result);
var chantedTo = ToSlotValueType(result);
{
MaterialSlot.CreateMaterialSlot(chantedTo, slot.id, slot.RawDisplayName(), slot.shaderOutputName, slot.slotType, Vector4.zero);
}
}
GUILayout.Space(10);

14
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/DynamicSlotTests.cs


public DynamicNode()
{
AddSlot(new MaterialSlot(DynIn1, "DynIn1", "DynIn1", SlotType.Input, SlotValueType.Dynamic, Vector4.zero));
AddSlot(new MaterialSlot(DynIn2, "DynIn2", "DynIn2", SlotType.Input, SlotValueType.Dynamic, Vector4.zero));
AddSlot(new MaterialSlot(DynOut, "DynOut", "DynOut", SlotType.Output, SlotValueType.Dynamic, Vector4.zero));
AddSlot(new DynamicVectorMaterialSlot(DynIn1, "DynIn1", "DynIn1", SlotType.Input, Vector4.zero));
AddSlot(new DynamicVectorMaterialSlot(DynIn2, "DynIn2", "DynIn2", SlotType.Input, Vector4.zero));
AddSlot(new DynamicVectorMaterialSlot(DynOut, "DynOut", "DynOut", SlotType.Output, Vector4.zero));
}
}

public TestNode()
{
AddSlot(new MaterialSlot(V1Out, "V1Out", "V1Out", SlotType.Output, SlotValueType.Vector1, Vector4.zero));
AddSlot(new MaterialSlot(V2Out, "V2Out", "V2Out", SlotType.Output, SlotValueType.Vector2, Vector4.zero));
AddSlot(new MaterialSlot(V3Out, "V3Out", "V3Out", SlotType.Output, SlotValueType.Vector3, Vector4.zero));
AddSlot(new MaterialSlot(V4Out, "V4Out", "V4Out", SlotType.Output, SlotValueType.Vector4, Vector4.zero));
AddSlot(new Vector1MaterialSlot(V1Out, "V1Out", "V1Out", SlotType.Output, 0));
AddSlot(new Vector2MaterialSlot(V2Out, "V2Out", "V2Out", SlotType.Output, Vector4.zero));
AddSlot(new Vector3MaterialSlot(V3Out, "V3Out", "V3Out", SlotType.Output, Vector4.zero));
AddSlot(new Vector4MaterialSlot(V4Out, "V4Out", "V4Out", SlotType.Output, Vector4.zero));
}
}

16
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/FixedSlotTests.cs


public TestNode()
{
AddSlot(new MaterialSlot(V1Out, "V1Out", "V1Out", SlotType.Output, SlotValueType.Vector1, Vector4.zero));
AddSlot(new MaterialSlot(V2Out, "V2Out", "V2Out", SlotType.Output, SlotValueType.Vector2, Vector4.zero));
AddSlot(new MaterialSlot(V3Out, "V3Out", "V3Out", SlotType.Output, SlotValueType.Vector3, Vector4.zero));
AddSlot(new MaterialSlot(V4Out, "V4Out", "V4Out", SlotType.Output, SlotValueType.Vector4, Vector4.zero));
AddSlot(new Vector1MaterialSlot(V1Out, "V1Out", "V1Out", SlotType.Output, 0));
AddSlot(new Vector2MaterialSlot(V2Out, "V2Out", "V2Out", SlotType.Output, Vector4.zero));
AddSlot(new Vector3MaterialSlot(V3Out, "V3Out", "V3Out", SlotType.Output, Vector4.zero));
AddSlot(new Vector4MaterialSlot(V4Out, "V4Out", "V4Out", SlotType.Output, Vector4.zero));
AddSlot(new MaterialSlot(V1In, "V1In", "V1In", SlotType.Input, SlotValueType.Vector1, Vector4.zero));
AddSlot(new MaterialSlot(V2In, "V2In", "V2In", SlotType.Input, SlotValueType.Vector2, Vector4.zero));
AddSlot(new MaterialSlot(V3In, "V3In", "V3In", SlotType.Input, SlotValueType.Vector3, Vector4.zero));
AddSlot(new MaterialSlot(V4In, "V4In", "V4In", SlotType.Input, SlotValueType.Vector4, Vector4.zero));
AddSlot(new Vector1MaterialSlot(V1In, "V1In", "V1In", SlotType.Input, 0));
AddSlot(new Vector2MaterialSlot(V2In, "V2In", "V2In", SlotType.Input, Vector4.zero));
AddSlot(new Vector3MaterialSlot(V3In, "V3In", "V3In", SlotType.Input, Vector4.zero));
AddSlot(new Vector4MaterialSlot(V4In, "V4In", "V4In", SlotType.Input, Vector4.zero));
}
}

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


public TestNode()
{
AddSlot(new MaterialSlot(V1Out, "V1Out", "V1Out", SlotType.Output, SlotValueType.Vector1, Vector4.zero));
AddSlot(new MaterialSlot(V1In, "V1In", "V1In", SlotType.Input, SlotValueType.Vector1, Vector4.zero));
AddSlot(new Vector1MaterialSlot(V1Out, "V1Out", "V1Out", SlotType.Output, 0));
AddSlot(new Vector1MaterialSlot(V1In, "V1In", "V1In", SlotType.Input, 0));
}
}

[Test]
public void ReplacingMaterialSlotPreservesTheOldCurrentValue()
{
m_NodeA.AddSlot(new MaterialSlot(TestNode.V1In, "V1In", "V1In", SlotType.Input, SlotValueType.Vector1, Vector4.one));
m_NodeA.AddSlot(new Vector1MaterialSlot(TestNode.V1In, "V1In", "V1In", SlotType.Input, 0));
var slot = m_NodeA.GetInputSlots<MaterialSlot>().FirstOrDefault();
var slot = m_NodeA.GetInputSlots<Vector1MaterialSlot>().FirstOrDefault();
Assert.AreEqual(Vector4.zero, slot.currentValue);
Assert.AreEqual(Vector4.zero, slot.value);
}
[Test]

Assert.AreEqual("3x3", AbstractMaterialNode.ConvertConcreteSlotValueTypeToString(ConcreteSlotValueType.Matrix3));
Assert.AreEqual("4x4", AbstractMaterialNode.ConvertConcreteSlotValueTypeToString(ConcreteSlotValueType.Matrix4));
Assert.AreEqual("SamplerState", AbstractMaterialNode.ConvertConcreteSlotValueTypeToString(ConcreteSlotValueType.SamplerState));
Assert.AreEqual("Error", AbstractMaterialNode.ConvertConcreteSlotValueTypeToString(ConcreteSlotValueType.Error));
}
[Test]

m_NodeA.CollectPreviewMaterialProperties(properties);
var slot = m_NodeA.GetInputSlots<MaterialSlot>().FirstOrDefault();
var slot = m_NodeA.GetInputSlots<Vector1MaterialSlot>().FirstOrDefault();
Assert.AreEqual(1, properties.Count);
var pp = properties.FirstOrDefault();

Assert.AreEqual(slot.currentValue, pp.m_Vector4);
Assert.AreEqual(slot.value, pp.m_Vector4);
}
[Test]

90
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/MaterialSlotTests.cs


using System;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.Graphing;

class TestNode : AbstractMaterialNode
{
public const int V1In = 1;
public const int V2In = 2;
public const int V3In = 3;
public const int V4In = 4;
public readonly MaterialSlot slot;
public readonly Vector1MaterialSlot slot1;
public readonly Vector2MaterialSlot slot2;
public readonly Vector3MaterialSlot slot3;
public readonly Vector4MaterialSlot slot4;
slot = new MaterialSlot(V1In, "V1In", "V1In", SlotType.Input, SlotValueType.Vector1, Vector4.zero);
AddSlot(slot);
slot1 = new Vector1MaterialSlot(V1In, "V1In", "V1In", SlotType.Input, 0);
AddSlot(slot1);
slot2 = new Vector2MaterialSlot(V2In, "V2In", "V2In", SlotType.Input, Vector2.zero);
AddSlot(slot2);
slot3 = new Vector3MaterialSlot(V3In, "V3In", "V3In", SlotType.Input, Vector3.zero);
AddSlot(slot3);
slot4 = new Vector4MaterialSlot(V4In, "V4In", "V4In", SlotType.Input, Vector4.zero);
AddSlot(slot4);
}
}

}
[Test]
public void CanUpdateMaterialSlotDisplayName()
public void SlotDisplayNameIsCorrect()
var slot = m_NodeA.slot;
slot.displayName = displayName;
slot.valueType = SlotValueType.Vector1;
Assert.AreEqual(displayName + "(1)", slot.displayName);
slot.valueType = SlotValueType.Vector2;
Assert.AreEqual(displayName + "(2)", slot.displayName);
var slot = m_NodeA.slot1;
Assert.AreEqual(displayName + "(1)", m_NodeA.slot1.displayName);
slot.valueType = SlotValueType.Vector3;
Assert.AreEqual(displayName + "(3)", slot.displayName);
Assert.AreEqual(displayName + "(2)", m_NodeA.slot2.displayName);
slot.valueType = SlotValueType.Vector4;
Assert.AreEqual(displayName + "(4)", slot.displayName);
Assert.AreEqual(displayName + "(3)", m_NodeA.slot3.displayName);
slot.valueType = SlotValueType.Dynamic;
Assert.AreEqual(displayName + "(4)", slot.displayName);
Assert.AreEqual(displayName + "(4)", m_NodeA.slot4.displayName);
var slot = m_NodeA.slot;
slot.defaultValue = Vector4.one;
var slot = m_NodeA.slot1;
slot.value = 1;
Assert.AreEqual(Vector4.one, slot.defaultValue);
}

var slot = m_NodeA.slot;
slot.currentValue = Vector4.one;
Assert.AreEqual(Vector4.one, slot.currentValue);
}
[Test]
public void CanUpdateMaterialSlotValueType()
{
var slot = m_NodeA.slot;
slot.valueType = SlotValueType.Vector3;
Assert.AreEqual(SlotValueType.Vector3, slot.valueType);
var slot = m_NodeA.slot1;
slot.value = 1;
Assert.AreEqual(Vector4.one, 1);
}
/* [Test]

public void MaterialSlotReturnsValidDefaultValue()
{
string expected = string.Format("{0}", m_NodeA.GetVariableNameForSlot(TestNode.V1In));
var slot = m_NodeA.slot;
var result = slot.GetDefaultValue(GenerationMode.Preview);
var result = m_NodeA.slot1.GetDefaultValue(GenerationMode.Preview);
slot.valueType = SlotValueType.Vector1;
slot.currentValue = new Vector4(6, 6, 6, 1);
result = slot.GetDefaultValue(GenerationMode.ForReals);
m_NodeA.slot1.value = 6;
result = m_NodeA.slot1.GetDefaultValue(GenerationMode.ForReals);
slot.valueType = SlotValueType.Vector2;
slot.currentValue = new Vector4(6, 6, 6, 1);
result = slot.GetDefaultValue(GenerationMode.ForReals);
m_NodeA.slot2.value = new Vector4(6, 6, 6, 1);
result = m_NodeA.slot1.GetDefaultValue(GenerationMode.ForReals);
slot.valueType = SlotValueType.Vector3;
slot.currentValue = new Vector4(6, 6, 6, 1);
result = slot.GetDefaultValue(GenerationMode.ForReals);
m_NodeA.slot3.value = new Vector4(6, 6, 6, 1);
result = m_NodeA.slot3.GetDefaultValue(GenerationMode.ForReals);
slot.valueType = SlotValueType.Vector4;
slot.currentValue = new Vector4(6, 6, 6, 1);
result = slot.GetDefaultValue(GenerationMode.ForReals);
m_NodeA.slot4.value = new Vector4(6, 6, 6, 1);
result = m_NodeA.slot3.GetDefaultValue(GenerationMode.ForReals);
Assert.AreEqual("half4 (6,6,6,1)", result);
}

8
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/ShaderGeneratorTests.cs


public TestNode()
{
AddSlot(new MaterialSlot(V1Out, "V1Out", "V1Out", SlotType.Output, SlotValueType.Vector1, Vector4.zero));
AddSlot(new MaterialSlot(V2Out, "V2Out", "V2Out", SlotType.Output, SlotValueType.Vector2, Vector4.zero));
AddSlot(new MaterialSlot(V3Out, "V3Out", "V3Out", SlotType.Output, SlotValueType.Vector3, Vector4.zero));
AddSlot(new MaterialSlot(V4Out, "V4Out", "V4Out", SlotType.Output, SlotValueType.Vector4, Vector4.zero));
AddSlot(new Vector1MaterialSlot(V1Out, "V1Out", "V1Out", SlotType.Output, 0));
AddSlot(new Vector2MaterialSlot(V2Out, "V2Out", "V2Out", SlotType.Output, Vector4.zero));
AddSlot(new Vector3MaterialSlot(V3Out, "V3Out", "V3Out", SlotType.Output, Vector4.zero));
AddSlot(new Vector4MaterialSlot(V4Out, "V4Out", "V4Out", SlotType.Output, Vector4.zero));
}
}

178
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/MaterialSlot.cs


namespace UnityEngine.MaterialGraph
{
public interface IMaterialSlotHasVaule<T>
{
T defaultValue { get; }
T value { get; }
}
public class Vector1MaterialSlot : MaterialSlot
public class Vector1MaterialSlot : MaterialSlot, IMaterialSlotHasVaule<float>
[SerializeField]
private float m_DefaultValue;
public Vector1MaterialSlot(
int slotId,
string displayName,

bool hidden = false)
:base(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden)
{
m_DefaultValue = value;
public float defaultValue { get { return m_DefaultValue; } }
public float value
{
get { return m_Value; }

public override SlotValueType valueType { get { return SlotValueType.Vector1; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Vector1; } }
public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty
{
m_Name = name,
m_PropType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType),
m_Vector4 = new Vector4(value, value, value, value),
m_Float = value,
m_Color = new Vector4(value, value, value, value),
};
return pp;
}
public class Vector2MaterialSlot : MaterialSlot
public class Vector2MaterialSlot : MaterialSlot, IMaterialSlotHasVaule<Vector2>
{
[SerializeField]
private Vector2 m_Value;

m_Value = value;
}
public Vector2 defaultValue { get { return m_DefaultValue; } }
public Vector2 value
{
get { return m_Value; }

return precision + "2 (" + value.x + "," + value.y + ")";
}
public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty
{
m_Name = name,
m_PropType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType),
m_Vector4 = new Vector4(value.x, value.y, 0, 0),
m_Float = value.x,
m_Color = new Vector4(value.x, value.x, 0, 0),
};
return pp;
}
public class Vector3MaterialSlot : MaterialSlot
public class Vector3MaterialSlot : MaterialSlot, IMaterialSlotHasVaule<Vector3>
{
[SerializeField]
private Vector3 m_Value;

m_Value = value;
}
public Vector3 defaultValue { get { return m_DefaultValue; } }
public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty
{
m_Name = name,
m_PropType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType),
m_Vector4 = new Vector4(value.x, value.y, value.z, 0),
m_Float = value.x,
m_Color = new Vector4(value.x, value.x, value.z, 0),
};
return pp;
}
public override SlotValueType valueType { get { return SlotValueType.Vector3; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Vector3; } }

public class Vector4MaterialSlot : MaterialSlot
public class Vector4MaterialSlot : MaterialSlot, IMaterialSlotHasVaule<Vector4>
{
[SerializeField]
private Vector4 m_Value;

{
m_Value = value;
}
public Vector4 defaultValue { get { return m_DefaultValue; } }
public Vector4 value
{

return precision + "4 (" + value.x + "," + value.y + "," + value.z + "," + value.w + ")";
}
public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty
{
m_Name = name,
m_PropType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType),
m_Vector4 = new Vector4(value.x, value.y, value.z, value.w),
m_Float = value.x,
m_Color = new Vector4(value.x, value.x, value.z, value.w),
};
return pp;
}
public override SlotValueType valueType { get { return SlotValueType.Vector4; } }
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Vector4; } }
}

}
[Serializable]
public class DynamicVectorMaterialSlot : MaterialSlot
public class DynamicVectorMaterialSlot : MaterialSlot, IMaterialSlotHasVaule<Vector4>
{
[SerializeField]
private Vector4 m_Value;

private ConcreteSlotValueType m_ConcreteValueType = ConcreteSlotValueType.Vector4;
public DynamicVectorMaterialSlot(
int slotId,

m_Value = value;
}
public Vector4 defaultValue { get { return m_DefaultValue; } }
public Vector4 value
{
get { return m_Value; }
set { m_Value = value; }
}
public override ConcreteSlotValueType concreteValueType { get { return ConcreteSlotValueType.Error; } }
public override ConcreteSlotValueType concreteValueType
{
get { return m_ConcreteValueType; }
}
public void SetConcreteType(ConcreteSlotValueType valueType)
{
m_ConcreteValueType = valueType;
}
}
[Serializable]

[SerializeField]
ShaderStage m_ShaderStage;
private bool m_HasError;
protected MaterialSlot() { }

set { base.displayName = value; }
}
public string RawDisplayName()
{
return displayName;
}
public static MaterialSlot CreateMaterialSlot(SlotValueType type, int slotId, string displayName, string shaderOutputName, SlotType slotType, Vector4 defaultValue, ShaderStage shaderStage = ShaderStage.Dynamic, bool hidden = false)
{
switch (type)
{
case SlotValueType.SamplerState:
return new SamplerStateMaterialSlot(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden);
case SlotValueType.Matrix4:
return new Matrix4MaterialSlot(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden);
case SlotValueType.Matrix3:
return new Matrix3MaterialSlot(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden);
case SlotValueType.Matrix2:
return new Matrix2MaterialSlot(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden);
case SlotValueType.Texture2D:
return new Texture2DMaterialSlot(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden);
case SlotValueType.Dynamic:
return new DynamicVectorMaterialSlot(slotId, displayName, shaderOutputName, slotType, defaultValue, shaderStage, hidden);
case SlotValueType.Vector4:
return new Vector3MaterialSlot(slotId, displayName, shaderOutputName, slotType, defaultValue, shaderStage, hidden);
case SlotValueType.Vector3:
return new Vector3MaterialSlot(slotId, displayName, shaderOutputName, slotType, defaultValue, shaderStage, hidden);
case SlotValueType.Vector2:
return new Vector2MaterialSlot(slotId, displayName, shaderOutputName, slotType, defaultValue, shaderStage, hidden);
case SlotValueType.Vector1:
return new Vector1MaterialSlot(slotId, displayName, shaderOutputName, slotType, defaultValue.x, shaderStage, hidden);
}
throw new ArgumentOutOfRangeException("type", type, null);
}
public abstract SlotValueType valueType { get; }
public abstract ConcreteSlotValueType concreteValueType { get; }

{
get { return m_ShaderStage; }
set { m_ShaderStage = value; }
}
public bool hasError
{
get { return m_HasError; }
set { m_HasError = value; }
}
public bool IsCompatibleWithInputSlotType(SlotValueType inputType)

property.generatePropertyBlock = false;
properties.AddShaderProperty(property);
}
protected static PropertyType ConvertConcreteSlotValueTypeToPropertyType(ConcreteSlotValueType slotValue)
{
switch (slotValue)
{
case ConcreteSlotValueType.Texture2D:
return PropertyType.Texture;
case ConcreteSlotValueType.Vector1:
return PropertyType.Float;
case ConcreteSlotValueType.Vector2:
return PropertyType.Vector2;
case ConcreteSlotValueType.Vector3:
return PropertyType.Vector3;
case ConcreteSlotValueType.Vector4:
return PropertyType.Vector4;
case ConcreteSlotValueType.Matrix2:
return PropertyType.Matrix2;
case ConcreteSlotValueType.Matrix3:
return PropertyType.Matrix3;
case ConcreteSlotValueType.Matrix4:
return PropertyType.Matrix4;
case ConcreteSlotValueType.SamplerState:
return PropertyType.SamplerState;
default:
return PropertyType.Vector4;
}
}
public virtual PreviewProperty GetPreviewProperty(string name)
{
return null;
}
}
}

149
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbstractMaterialNode.cs


return inputSlot.GetDefaultValue(generationMode);
}
private ConcreteSlotValueType FindCommonChannelType(ConcreteSlotValueType from, ConcreteSlotValueType to)
{
if (ImplicitConversionExists(from, to))
return to;
return ConcreteSlotValueType.Error;
}
private static ConcreteSlotValueType ToConcreteType(SlotValueType svt)
{
switch (svt)
{
case SlotValueType.Vector1:
return ConcreteSlotValueType.Vector1;
case SlotValueType.Vector2:
return ConcreteSlotValueType.Vector2;
case SlotValueType.Vector3:
return ConcreteSlotValueType.Vector3;
case SlotValueType.Vector4:
return ConcreteSlotValueType.Vector4;
case SlotValueType.Texture2D:
return ConcreteSlotValueType.Texture2D;
case SlotValueType.Matrix2:
return ConcreteSlotValueType.Matrix2;
case SlotValueType.Matrix3:
return ConcreteSlotValueType.Matrix3;
case SlotValueType.Matrix4:
return ConcreteSlotValueType.Matrix4;
case SlotValueType.SamplerState:
return ConcreteSlotValueType.SamplerState;
}
return ConcreteSlotValueType.Error;
}
private static bool ImplicitConversionExists(ConcreteSlotValueType from, ConcreteSlotValueType to)
{
if (from == to)

private ConcreteSlotValueType ConvertDynamicInputTypeToConcrete(IEnumerable<ConcreteSlotValueType> inputTypes)
{
var concreteSlotValueTypes = inputTypes as IList<ConcreteSlotValueType> ?? inputTypes.ToList();
if (concreteSlotValueTypes.Any(x => x == ConcreteSlotValueType.Error))
return ConcreteSlotValueType.Error;
var inputTypesDistinct = concreteSlotValueTypes.Distinct().ToList();
switch (inputTypesDistinct.Count)
{

return ordered.FirstOrDefault();
break;
}
return ConcreteSlotValueType.Error;
return ConcreteSlotValueType.Vector1;
}
public override void ValidateNode()

// so do that here
foreach (var inputSlot in GetInputSlots<MaterialSlot>())
{
inputSlot.hasError = false;
var edges = owner.GetEdges(inputSlot.slotReference);
foreach (var edge in edges)
{

}
}
var dynamicInputSlotsToCompare = DictionaryPool<MaterialSlot, ConcreteSlotValueType>.Get();
var skippedDynamicSlots = ListPool<MaterialSlot>.Get();
var dynamicInputSlotsToCompare = DictionaryPool<DynamicVectorMaterialSlot, ConcreteSlotValueType>.Get();
var skippedDynamicSlots = ListPool<DynamicVectorMaterialSlot>.Get();
var inputType = inputSlot.valueType;
if (inputType != SlotValueType.Dynamic)
inputSlot.concreteValueType = ToConcreteType(inputType);
else
skippedDynamicSlots.Add(inputSlot);
if (inputSlot is DynamicVectorMaterialSlot)
skippedDynamicSlots.Add(inputSlot as DynamicVectorMaterialSlot);
continue;
}

if (outputSlot == null)
continue;
var outputConcreteType = outputSlot.concreteValueType;
// if we have a standard connection... just check the types work!
if (inputType != SlotValueType.Dynamic)
if (outputSlot.hasError)
var inputConcreteType = ToConcreteType(inputType);
inputSlot.concreteValueType = FindCommonChannelType(outputConcreteType, inputConcreteType);
inputSlot.hasError = true;
var outputConcreteType = outputSlot.concreteValueType;
dynamicInputSlotsToCompare.Add(inputSlot, outputConcreteType);
if (inputSlot is DynamicVectorMaterialSlot)
{
dynamicInputSlotsToCompare.Add((DynamicVectorMaterialSlot) inputSlot, outputConcreteType);
continue;
}
// if we have a standard connection... just check the types work!
if (!ImplicitConversionExists(outputConcreteType, inputSlot.concreteValueType))
inputSlot.hasError = true;
}
// we can now figure out the dynamic slotType

dynamicKvP.Key.concreteValueType = dynamicType;
dynamicKvP.Key.SetConcreteType(dynamicType);
skippedSlot.concreteValueType = dynamicType;
skippedSlot.SetConcreteType(dynamicType);
var inputError = GetInputSlots<MaterialSlot>().Any(x => x.concreteValueType == ConcreteSlotValueType.Error);
var inputError = GetInputSlots<MaterialSlot>().Any(x => x.hasError);
// configure the output slots now
// their slotType will either be the default output slotType

{
outputSlot.hasError = false;
outputSlot.concreteValueType = ConcreteSlotValueType.Error;
outputSlot.hasError = true;
if (outputSlot.valueType == SlotValueType.Dynamic)
if (outputSlot is DynamicVectorMaterialSlot)
outputSlot.concreteValueType = dynamicType;
(outputSlot as DynamicVectorMaterialSlot).SetConcreteType(dynamicType);
outputSlot.concreteValueType = ToConcreteType(outputSlot.valueType);
isInError |= GetOutputSlots<MaterialSlot>().Any(x => x.concreteValueType == ConcreteSlotValueType.Error);
isInError |= GetOutputSlots<MaterialSlot>().Any(x => x.hasError);
isInError |= CalculateNodeHasError();
hasError = isInError;

}
ListPool<MaterialSlot>.Release(skippedDynamicSlots);
DictionaryPool<MaterialSlot, ConcreteSlotValueType>.Release(dynamicInputSlotsToCompare);
ListPool<DynamicVectorMaterialSlot>.Release(skippedDynamicSlots);
DictionaryPool<DynamicVectorMaterialSlot, ConcreteSlotValueType>.Release(dynamicInputSlotsToCompare);
}

}
}
public static PropertyType ConvertConcreteSlotValueTypeToPropertyType(ConcreteSlotValueType slotValue)
{
switch (slotValue)
{
case ConcreteSlotValueType.Texture2D:
return PropertyType.Texture;
case ConcreteSlotValueType.Vector1:
return PropertyType.Float;
case ConcreteSlotValueType.Vector2:
return PropertyType.Vector2;
case ConcreteSlotValueType.Vector3:
return PropertyType.Vector3;
case ConcreteSlotValueType.Vector4:
return PropertyType.Vector4;
case ConcreteSlotValueType.Matrix2:
return PropertyType.Matrix2;
case ConcreteSlotValueType.Matrix3:
return PropertyType.Matrix3;
case ConcreteSlotValueType.Matrix4:
return PropertyType.Matrix4;
case ConcreteSlotValueType.SamplerState:
return PropertyType.SamplerState;
default:
return PropertyType.Vector4;
}
}
public virtual void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
{
var validSlots = GetInputSlots<MaterialSlot>().ToArray();

if (edges.Any())
continue;
var pp = new PreviewProperty
{
m_Name = GetVariableNameForSlot(s.id),
m_PropType = ConvertConcreteSlotValueTypeToPropertyType(s.concreteValueType),
m_Vector4 = s.currentValue,
m_Float = s.currentValue.x,
m_Color = s.currentValue
};
properties.Add(pp);
var item = s.GetPreviewProperty(GetVariableNameForSlot(s.id));
if (item == null)
continue;
properties.Add(item);
}
}

if (foundSlot == null)
return;
// preserve the old current value.
addingSlot.currentValue = foundSlot.currentValue;
// now copy over values :)
if (addingSlot is DynamicVectorMaterialSlot && foundSlot is DynamicVectorMaterialSlot)
(addingSlot as DynamicVectorMaterialSlot).value = (foundSlot as DynamicVectorMaterialSlot).value;
if (addingSlot is Vector1MaterialSlot && foundSlot is Vector1MaterialSlot)
(addingSlot as Vector1MaterialSlot).value = (foundSlot as Vector1MaterialSlot).value;
if (addingSlot is Vector2MaterialSlot && foundSlot is Vector2MaterialSlot)
(addingSlot as Vector2MaterialSlot).value = (foundSlot as Vector2MaterialSlot).value;
if (addingSlot is Vector3MaterialSlot && foundSlot is Vector3MaterialSlot)
(addingSlot as Vector3MaterialSlot).value = (foundSlot as Vector3MaterialSlot).value;
if (addingSlot is Vector4MaterialSlot && foundSlot is Vector4MaterialSlot)
(addingSlot as Vector4MaterialSlot).value = (foundSlot as Vector4MaterialSlot).value;
}
}
}

5
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/HLSLNode.cs


{
var attribute = GetSlotAttribute(par);
slots.Add(new MaterialSlot(attribute.slotId, par.Name, par.Name, par.IsOut ? SlotType.Output : SlotType.Input,
ConvertTypeToSlotValueType(par), attribute.defaultValue ?? Vector4.zero, hidden: attribute.hidden));
slots.Add(MaterialSlot.CreateMaterialSlot(ConvertTypeToSlotValueType(par), attribute.slotId, par.Name, par.Name, par.IsOut ? SlotType.Output : SlotType.Input,
attribute.defaultValue ?? Vector4.zero, hidden: attribute.hidden));
m_Slots.Add(attribute);
}

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Input/Matrix/Matrix3Node.cs


public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new MaterialSlot(kOutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, SlotValueType.Matrix3, Vector4.zero));
AddSlot(new Matrix3MaterialSlot(kOutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output));
RemoveSlotsNameNotMatching(new[] { kOutputSlotId });
}

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Input/Vector/Vector4Node.cs


public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new MaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, SlotValueType.Vector4, Vector4.zero));
AddSlot(new Vector4MaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, Vector4.zero));
RemoveSlotsNameNotMatching(new[] { OutputSlotId });
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/SlotValue.cs


Vector4,
Vector3,
Vector2,
Vector1,
Error
Vector1
}
public static class SlotValueHelper

4
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SubGraph/AbstractSubGraphNode.cs


}
var id = prop.guid.GetHashCode();
AddSlot(new MaterialSlot(id, prop.displayName, prop.referenceName, SlotType.Input, slotType, prop.defaultValue));
AddSlot(MaterialSlot.CreateMaterialSlot(slotType, id, prop.displayName, prop.referenceName, SlotType.Input, prop.defaultValue));
validNames.Add(id);
}

foreach (var slot in subGraphOutputNode.GetInputSlots<MaterialSlot>())
{
AddSlot(new MaterialSlot(slot.id, slot.displayName, slot.shaderOutputName, SlotType.Output, slot.valueType, slot.defaultValue));
AddSlot(MaterialSlot.CreateMaterialSlot( slot.valueType,slot.id, slot.displayName, slot.shaderOutputName, SlotType.Output, Vector4.zero));
validNames.Add(slot.id);
}
}

正在加载...
取消
保存