浏览代码

Vector1 property refactor and types

/main
Matt Dean 7 年前
当前提交
4ad43705
共有 21 个文件被更改,包括 132 次插入85 次删除
  1. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/DynamicVectorMaterialSlot.cs
  2. 18
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/FloatShaderProperty.cs
  3. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/MaterialSlot.cs
  4. 10
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/PreviewProperty.cs
  5. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector1MaterialSlot.cs
  6. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Channel/SwizzleNode.cs
  7. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/IntegerNode.cs
  8. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/SliderNode.cs
  9. 40
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/Vector1Node.cs
  10. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/PBR/DielectricSpecularNode.cs
  11. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/PropertyNode.cs
  12. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/PropertyType.cs
  13. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Utility/SubGraphNode.cs
  14. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/GenerationResults.cs
  15. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/GraphUtil.cs
  16. 89
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Blackboard/BlackboardFieldPropertyView.cs
  17. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Blackboard/BlackboardProvider.cs
  18. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorView.cs
  19. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/ShaderPropertyView.cs
  20. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphEditWindow.cs
  21. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/MaterialNodeTests.cs

4
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/DynamicVectorMaterialSlot.cs


{
var propType = ConvertConcreteSlotValueTypeToPropertyType(concreteValueType);
var pp = new PreviewProperty(propType) { name = name };
if (propType == PropertyType.Float)
if (propType == PropertyType.Vector1)
pp.floatValue = value.x;
else
pp.vector4Value = new Vector4(value.x, value.y, value.z, value.w);

property = new Vector2ShaderProperty();
break;
case ConcreteSlotValueType.Vector1:
property = new FloatShaderProperty();
property = new Vector1ShaderProperty();
break;
default:
throw new ArgumentOutOfRangeException();

18
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/FloatShaderProperty.cs


{
public enum FloatType
{
Float,
Default,
public class FloatShaderProperty : AbstractShaderProperty<float>
public class Vector1ShaderProperty : AbstractShaderProperty<float>
public FloatShaderProperty()
public Vector1ShaderProperty()
displayName = "Float";
displayName = "Vector1";
get { return PropertyType.Float; }
get { return PropertyType.Vector1; }
}
public override Vector4 defaultValue

private FloatType m_FloatType = FloatType.Float;
[SerializeField]
private FloatType m_FloatType = FloatType.Default;
public FloatType floatType
{

}
}
[SerializeField]
private Vector2 m_RangeValues = new Vector2(0, 1);
public Vector2 rangeValues

result.Append(referenceName);
result.Append("(\"");
result.Append(displayName);
switch(m_FloatType)
switch(floatType)
{
case FloatType.Slider:
result.Append("\", Range(");

public override PreviewProperty GetPreviewMaterialProperty()
{
return new PreviewProperty(PropertyType.Float)
return new PreviewProperty(PropertyType.Vector1)
{
name = referenceName,
floatValue = value

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


case ConcreteSlotValueType.Boolean:
return PropertyType.Boolean;
case ConcreteSlotValueType.Vector1:
return PropertyType.Float;
return PropertyType.Vector1;
case ConcreteSlotValueType.Vector2:
return PropertyType.Vector2;
case ConcreteSlotValueType.Vector3:

10
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/PreviewProperty.cs


{
get
{
if (propType != PropertyType.Float)
throw new ArgumentException(string.Format(k_GetErrorMessage, PropertyType.Float, propType));
if (propType != PropertyType.Vector1)
throw new ArgumentException(string.Format(k_GetErrorMessage, PropertyType.Vector1, propType));
if (propType != PropertyType.Float)
throw new ArgumentException(string.Format(k_SetErrorMessage, PropertyType.Float, propType));
if (propType != PropertyType.Vector1)
throw new ArgumentException(string.Format(k_SetErrorMessage, PropertyType.Vector1, propType));
m_Data.floatValue = value;
}
}

block.SetColor(name, m_Data.colorValue);
else if (propType == PropertyType.Vector2 || propType == PropertyType.Vector3 || propType == PropertyType.Vector4)
block.SetVector(name, m_Data.vector4Value);
else if (propType == PropertyType.Float)
else if (propType == PropertyType.Vector1)
block.SetFloat(name, m_Data.floatValue);
else if (propType == PropertyType.Boolean)
block.SetFloat(name, m_Data.booleanValue ? 1 : 0);

4
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector1MaterialSlot.cs


if (matOwner == null)
throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
var property = new FloatShaderProperty()
var property = new Vector1ShaderProperty()
{
overrideReferenceName = matOwner.GetVariableNameForSlot(id),
generatePropertyBlock = false,

public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty(PropertyType.Float)
var pp = new PreviewProperty(PropertyType.Vector1)
{
name = name,
floatValue = value

4
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Channel/SwizzleNode.cs


base.CollectShaderProperties(properties, generationMode);
if (generationMode != GenerationMode.Preview)
return;
properties.AddShaderProperty(new FloatShaderProperty
properties.AddShaderProperty(new Vector1ShaderProperty
{
overrideReferenceName = GetVariableNameForNode(),
generatePropertyBlock = false

base.CollectPreviewMaterialProperties(properties);
// Encode swizzle values into an integer
var value = ((int)redChannel) | ((int)greenChannel << 2) | ((int)blueChannel << 4) | ((int)alphaChannel << 6);
properties.Add(new PreviewProperty(PropertyType.Float)
properties.Add(new PreviewProperty(PropertyType.Vector1)
{
name = GetVariableNameForNode(),
floatValue = value

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/IntegerNode.cs


if (!generationMode.IsPreview())
return;
properties.AddShaderProperty(new FloatShaderProperty()
properties.AddShaderProperty(new Vector1ShaderProperty()
{
overrideReferenceName = GetVariableNameForNode(),
generatePropertyBlock = false,

public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
{
properties.Add(new PreviewProperty(PropertyType.Float)
properties.Add(new PreviewProperty(PropertyType.Vector1)
{
name = GetVariableNameForNode(),
floatValue = m_Value

public IShaderProperty AsShaderProperty()
{
return new FloatShaderProperty { value = value, floatType = FloatType.Integer };
return new Vector1ShaderProperty { value = value, floatType = FloatType.Integer };
}
public int outputSlotId { get { return OutputSlotId; } }

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/SliderNode.cs


if (!generationMode.IsPreview())
return;
properties.AddShaderProperty(new FloatShaderProperty()
properties.AddShaderProperty(new Vector1ShaderProperty()
{
overrideReferenceName = GetVariableNameForNode(),
generatePropertyBlock = false,

public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
{
properties.Add(new PreviewProperty(PropertyType.Float)
properties.Add(new PreviewProperty(PropertyType.Vector1)
{
name = GetVariableNameForNode(),
floatValue = m_Value.x

public IShaderProperty AsShaderProperty()
{
return new FloatShaderProperty
return new Vector1ShaderProperty
{
value = value.x,
rangeValues = new Vector2(value.y, value.z),

40
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/Vector1Node.cs


public const int OutputSlotId = 0;
private const string kOutputSlotName = "Out";
/*[SerializeField]
private FloatPropertyChunk.FloatType m_floatType;*/
// [SerializeField]
//private Vector3 m_rangeValues = new Vector3(0f, 1f, 2f);
public Vector1Node()
{
name = "Vector 1";

}
}
/* public FloatPropertyChunk.FloatType floatType
{
get { return m_floatType; }
set
{
if (m_floatType == value)
return;
m_floatType = value;
Dirty(ModificationScope.Node);
}
}*/
/* public Vector3 rangeValues
{
get { return m_rangeValues; }
set
{
if (m_rangeValues == value)
return;
m_rangeValues = value;
Dirty(ModificationScope.Node);
}
}
*/
properties.AddShaderProperty(new FloatShaderProperty()
properties.AddShaderProperty(new Vector1ShaderProperty()
{
overrideReferenceName = GetVariableNameForNode(),
generatePropertyBlock = false,

public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
{
properties.Add(new PreviewProperty(PropertyType.Float)
properties.Add(new PreviewProperty(PropertyType.Vector1)
{
name = GetVariableNameForNode(),
floatValue = m_Value

public IShaderProperty AsShaderProperty()
{
return new FloatShaderProperty { value = value };
return new Vector1ShaderProperty { value = value };
}
public int outputSlotId { get { return OutputSlotId; } }

8
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/PBR/DielectricSpecularNode.cs


if(material.type == DielectricMaterialType.Common)
{
properties.Add(new PreviewProperty(PropertyType.Float)
properties.Add(new PreviewProperty(PropertyType.Vector1)
{
name = string.Format("_{0}_Range", GetVariableNameForNode()),
floatValue = material.range

{
properties.Add(new PreviewProperty(PropertyType.Float)
properties.Add(new PreviewProperty(PropertyType.Vector1)
{
name = string.Format("_{0}_IOR", GetVariableNameForNode()),
floatValue = material.indexOfRefraction

if (material.type == DielectricMaterialType.Common)
{
properties.AddShaderProperty(new FloatShaderProperty()
properties.AddShaderProperty(new Vector1ShaderProperty()
{
overrideReferenceName = string.Format("_{0}_Range", GetVariableNameForNode()),
generatePropertyBlock = false

{
properties.AddShaderProperty(new FloatShaderProperty()
properties.AddShaderProperty(new Vector1ShaderProperty()
{
overrideReferenceName = string.Format("_{0}_IOR", GetVariableNameForNode()),
generatePropertyBlock = false

4
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/PropertyNode.cs


if (property == null)
return;
if (property is FloatShaderProperty)
if (property is Vector1ShaderProperty)
{
AddSlot(new Vector1MaterialSlot(OutputSlotId, "Out", "Out", SlotType.Output, 0));
RemoveSlotsNameNotMatching(new[] {OutputSlotId});

if (property == null)
return;
if (property is FloatShaderProperty)
if (property is Vector1ShaderProperty)
{
var result = string.Format("{0} {1} = {2};"
, precision

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/PropertyType.cs


Texture,
Cubemap,
Boolean,
Float,
Vector1,
Vector2,
Vector3,
Vector4,

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Utility/SubGraphNode.cs


case PropertyType.Cubemap:
slotType = SlotValueType.Cubemap;
break;
case PropertyType.Float:
case PropertyType.Vector1:
slotType = SlotValueType.Vector1;
break;
case PropertyType.Vector2:

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/GenerationResults.cs


public string shader { get; set; }
public List<PropertyCollector.TextureInfo> configuredTextures;
public PreviewMode previewMode { get; set; }
public FloatShaderProperty outputIdProperty { get; set; }
public Vector1ShaderProperty outputIdProperty { get; set; }
public ShaderSourceMap sourceMap { get; set; }
public GenerationResults()

4
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/GraphUtil.cs


GenerateSurfaceDescriptionStruct(surfaceDescriptionStruct, slots, !isUber);
var shaderProperties = new PropertyCollector();
results.outputIdProperty = new FloatShaderProperty
results.outputIdProperty = new Vector1ShaderProperty
{
displayName = "OutputId",
generatePropertyBlock = false,

GenerationMode mode,
string functionName = "PopulateSurfaceData",
string surfaceDescriptionName = "SurfaceDescription",
FloatShaderProperty outputIdProperty = null,
Vector1ShaderProperty outputIdProperty = null,
IEnumerable<MaterialSlot> slots = null)
{
if (graph == null)

89
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Blackboard/BlackboardFieldPropertyView.cs


public BlackboardFieldPropertyView(AbstractMaterialGraph graph, IShaderProperty property)
{
m_Graph = graph;
if (property is FloatShaderProperty)
if (property is Vector1ShaderProperty)
var floatProperty = (FloatShaderProperty)property;
VisualElement floatRow = new VisualElement();
VisualElement intRow = new VisualElement();
VisualElement modeRow = new VisualElement();
VisualElement minRow = new VisualElement();
VisualElement maxRow = new VisualElement();
var floatProperty = (Vector1ShaderProperty)property;
var field = new FloatField { value = floatProperty.value };
field.OnValueChanged(evt =>
{

AddRow("Default", field);
floatRow = AddRow("Default", field);
floatModeField.value = floatProperty.floatType;
if(floatProperty.floatType == (FloatType)evt.newValue)
return;
floatProperty = (Vector1ShaderProperty)property;
switch(floatProperty.floatType)
{
case FloatType.Slider:
RemoveElements(new VisualElement[]{floatRow, intRow, modeRow, minRow, maxRow});
field = new FloatField { value = Mathf.Max(Mathf.Min(floatProperty.value, floatProperty.rangeValues.y), floatProperty.rangeValues.x) };
floatProperty.value = (float)field.value;
field.OnValueChanged(defaultEvt =>
{
floatProperty.value = Mathf.Max(Mathf.Min((float)defaultEvt.newValue, floatProperty.rangeValues.y), floatProperty.rangeValues.x);
field.value = floatProperty.value;
DirtyNodes();
});
floatRow = AddRow("Default", field);
field.value = Mathf.Max(Mathf.Min(floatProperty.value, floatProperty.rangeValues.y), floatProperty.rangeValues.x);
modeRow = AddRow("Mode", floatModeField);
var minField = new FloatField { value = floatProperty.rangeValues.x };
minField.OnValueChanged(minEvt =>
{
floatProperty.rangeValues = new Vector2((float)minEvt.newValue, floatProperty.rangeValues.y);
floatProperty.value = Mathf.Max(Mathf.Min(floatProperty.value, floatProperty.rangeValues.y), floatProperty.rangeValues.x);
field.value = floatProperty.value;
DirtyNodes();
});
minRow = AddRow("Min", minField);
var maxField = new FloatField { value = floatProperty.rangeValues.y };
maxField.OnValueChanged(maxEvt =>
{
floatProperty.rangeValues = new Vector2(floatProperty.rangeValues.x, (float)maxEvt.newValue);
floatProperty.value = Mathf.Max(Mathf.Min(floatProperty.value, floatProperty.rangeValues.y), floatProperty.rangeValues.x);
field.value = floatProperty.value;
DirtyNodes();
});
maxRow = AddRow("Max", maxField);
break;
case FloatType.Integer:
RemoveElements(new VisualElement[]{floatRow, intRow, modeRow, minRow, maxRow});
var intField = new IntegerField { value = (int)floatProperty.value };
intField.OnValueChanged(intEvt =>
{
floatProperty.value = (float)intEvt.newValue;
DirtyNodes();
});
intRow = AddRow("Default", intField);
modeRow = AddRow("Mode", floatModeField);
break;
default:
RemoveElements(new VisualElement[]{floatRow, intRow, modeRow, minRow, maxRow});
field = new FloatField { value = floatProperty.value };
field.OnValueChanged(defaultEvt =>
{
floatProperty.value = (float)defaultEvt.newValue;
DirtyNodes();
});
floatRow = AddRow("Default", field);
modeRow = AddRow("Mode", floatModeField);
break;
}
AddRow("Mode", floatModeField);
modeRow = AddRow("Mode", floatModeField);
}
else if (property is Vector2ShaderProperty)
{

var colorModeField = new EnumField((Enum)colorProperty.colorMode);
colorModeField.OnValueChanged(evt =>
{
if(colorProperty.colorMode == (ColorMode)evt.newValue)
return;
colorProperty.colorMode = (ColorMode)evt.newValue;
colorField.hdr = colorProperty.colorMode == ColorMode.HDR;
colorField.DoRepaint();

AddToClassList("sgblackboardFieldPropertyView");
}
void AddRow(string labelText, VisualElement control)
VisualElement AddRow(string labelText, VisualElement control)
{
VisualElement rowView = new VisualElement();

rowView.Add(control);
Add(rowView);
return rowView;
}
void RemoveElements(VisualElement[] elements)
{
for(int i = 0; i < elements.Length; i++)
{
if(elements[i].parent == this)
Remove(elements[i]);
}
}
void DirtyNodes()

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Blackboard/BlackboardProvider.cs


void AddItemRequested(Blackboard blackboard)
{
var gm = new GenericMenu();
gm.AddItem(new GUIContent("Float"), false, () => AddProperty(new FloatShaderProperty(), true));
gm.AddItem(new GUIContent("Vector1"), false, () => AddProperty(new Vector1ShaderProperty(), true));
gm.AddItem(new GUIContent("Vector2"), false, () => AddProperty(new Vector2ShaderProperty(), true));
gm.AddItem(new GUIContent("Vector3"), false, () => AddProperty(new Vector3ShaderProperty(), true));
gm.AddItem(new GUIContent("Vector4"), false, () => AddProperty(new Vector4ShaderProperty(), true));

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorView.cs


void OnAddProperty()
{
var gm = new GenericMenu();
gm.AddItem(new GUIContent("Float"), false, () => AddProperty(new FloatShaderProperty()));
gm.AddItem(new GUIContent("Vector1"), false, () => AddProperty(new Vector1ShaderProperty()));
gm.AddItem(new GUIContent("Vector2"), false, () => AddProperty(new Vector2ShaderProperty()));
gm.AddItem(new GUIContent("Vector3"), false, () => AddProperty(new Vector3ShaderProperty()));
gm.AddItem(new GUIContent("Vector4"), false, () => AddProperty(new Vector4ShaderProperty()));

4
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/ShaderPropertyView.cs


Add(displayNameField);
m_ValueAction = null;
if (property is FloatShaderProperty)
if (property is Vector1ShaderProperty)
m_ValueAction = FloatField;
else if (property is Vector2ShaderProperty)
m_ValueAction = Vector2Field;

void FloatField()
{
var fProp = (FloatShaderProperty)property;
var fProp = (Vector1ShaderProperty)property;
fProp.value = EditorGUILayout.FloatField(fProp.value);
}

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


prop = new Vector2ShaderProperty();
break;
case ConcreteSlotValueType.Vector1:
prop = new FloatShaderProperty();
prop = new Vector1ShaderProperty();
break;
case ConcreteSlotValueType.Boolean:
prop = new BooleanShaderProperty();

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


var pp = properties.FirstOrDefault();
Assert.AreEqual(m_NodeA.GetVariableNameForSlot(slot.id), pp.name);
Assert.AreEqual(PropertyType.Float, pp.propType);
Assert.AreEqual(PropertyType.Vector1, pp.propType);
Assert.AreEqual(slot.value, pp.vector4Value);
}

正在加载...
取消
保存