浏览代码

Merge pull request #192 from Unity-Technologies/node-bugfixes

Property type refactor and Blackboard extension
/main
GitHub 7 年前
当前提交
d2f86285
共有 33 个文件被更改,包括 367 次插入417 次删除
  1. 20
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/ColorShaderProperty.cs
  2. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/DynamicVectorMaterialSlot.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. 85
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector1ShaderProperty.cs
  7. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Channel/SwizzleNode.cs
  8. 52
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/ColorNode.cs
  9. 9
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/IntegerNode.cs
  10. 15
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/SliderNode.cs
  11. 40
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/Vector1Node.cs
  12. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/PBR/DielectricSpecularNode.cs
  13. 30
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/PropertyNode.cs
  14. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/PropertyType.cs
  15. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Utility/SubGraphNode.cs
  16. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/GenerationResults.cs
  17. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/GraphUtil.cs
  18. 164
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Blackboard/BlackboardFieldPropertyView.cs
  19. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Blackboard/BlackboardProvider.cs
  20. 50
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/ColorControl.cs
  21. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorView.cs
  22. 14
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/ShaderPropertyView.cs
  23. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphEditWindow.cs
  24. 25
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Resources/Styles/MaterialGraph.uss
  25. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/MaterialNodeTests.cs
  26. 56
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/IntegerShaderProperty.cs
  27. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/IntegerShaderProperty.cs.meta
  28. 61
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SliderShaderProperty.cs
  29. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SliderShaderProperty.cs.meta
  30. 94
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/HDRColorNode.cs
  31. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/HDRColorNode.cs.meta
  32. 0
      /MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector1ShaderProperty.cs.meta
  33. 0
      /MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector1ShaderProperty.cs

20
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/ColorShaderProperty.cs


public class ColorShaderProperty : AbstractShaderProperty<Color>
{
[SerializeField]
private bool m_HDR;
private ColorMode m_ColorMode;
public bool HDR
public ColorMode colorMode
get { return m_HDR; }
get { return m_ColorMode; }
if (m_HDR == value)
if (m_ColorMode == value)
m_HDR = value;
m_ColorMode = value;
}
}

}
public ColorShaderProperty(bool hdr)
{
displayName = "HDR Color";
HDR = hdr;
}
public override PropertyType propertyType
{
get { return PropertyType.Color; }

return string.Empty;
var result = new StringBuilder();
if (HDR)
if (colorMode == ColorMode.HDR)
result.Append("[HDR]");
result.Append(referenceName);
result.Append("(\"");

public override INode ToConcreteNode()
{
return new ColorNode { color = value };
return new ColorNode { color = new ColorNode.Color(value, colorMode) };
}
}
}

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

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

85
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector1ShaderProperty.cs


namespace UnityEditor.ShaderGraph
{
public enum FloatType
{
Default,
Slider,
Integer
}
public class FloatShaderProperty : AbstractShaderProperty<float>
[FormerName("UnityEditor.ShaderGraph.FloatShaderProperty")]
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

[SerializeField]
private FloatType m_FloatType = FloatType.Default;
public FloatType floatType
{
get { return m_FloatType; }
set
{
if (m_FloatType == value)
return;
m_FloatType = value;
}
}
[SerializeField]
private Vector2 m_RangeValues = new Vector2(0, 1);
public Vector2 rangeValues
{
get { return m_RangeValues; }
set
{
if (m_RangeValues == value)
return;
m_RangeValues = value;
}
}
// if (m_FloatType == FloatPropertyChunk.FloatType.Toggle)
// result.Append("[Toggle]");
// else if (m_FloatType == FloatPropertyChunk.FloatType.PowerSlider)
// result.Append("[PowerSlider(" + m_rangeValues.z + ")]");
//if (m_FloatType == FloatPropertyChunk.FloatType.Float || m_FloatType == FloatPropertyChunk.FloatType.Toggle)
//{
result.Append("\", Float) = ");
/* }
else if (m_FloatType == FloatPropertyChunk.FloatType.Range || m_FloatType == FloatPropertyChunk.FloatType.PowerSlider)
{
result.Append("\", Range(");
result.Append(m_rangeValues.x + ", " + m_rangeValues.y);
result.Append(")) = ");
}*/
switch(floatType)
{
case FloatType.Slider:
result.Append("\", Range(");
result.Append(m_RangeValues.x + ", " + m_RangeValues.y);
result.Append(")) = ");
break;
case FloatType.Integer:
result.Append("\", Int) = ");
break;
default:
result.Append("\", Float) = ");
break;
}
result.Append(value);
return result.ToString();
}

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

public override INode ToConcreteNode()
{
return new Vector1Node { value = value };
switch(m_FloatType)
{
case FloatType.Slider:
return new SliderNode { value = new Vector3(value, m_RangeValues.x, m_RangeValues.y) };
case FloatType.Integer:
return new IntegerNode { value = (int)value };
default:
return new Vector1Node { value = 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

52
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/ColorNode.cs


using System;
using System.Collections.Generic;
using UnityEditor.ShaderGraph.Drawing.Controls;
using UnityEngine;

{
public enum ColorMode
{
Default,
HDR
}
[SerializeField]
private Color m_Color;
public const int OutputSlotId = 0;
private const string kOutputSlotName = "Out";

UpdateNodeAfterDeserialization();
}
public sealed override void UpdateNodeAfterDeserialization()
[SerializeField]
Color m_Color = new Color(UnityEngine.Color.clear, ColorMode.Default);
[Serializable]
public struct Color
AddSlot(new Vector4MaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, Vector4.zero));
RemoveSlotsNameNotMatching(new[] { OutputSlotId });
public UnityEngine.Color color;
public ColorMode mode;
public Color(UnityEngine.Color color, ColorMode mode)
{
this.color = color;
this.mode = mode;
}
}
[ColorControl("")]

set
{
if (m_Color == value)
if ((value.color == m_Color.color) && (value.mode == m_Color.mode))
Dirty(ModificationScope.Node);
Dirty(ModificationScope.Graph);
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new Vector4MaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, Vector4.zero));
RemoveSlotsNameNotMatching(new[] { OutputSlotId });
}
public override void CollectShaderProperties(PropertyCollector properties, GenerationMode generationMode)
{

{
overrideReferenceName = GetVariableNameForNode(),
generatePropertyBlock = false,
value = color,
HDR = false
value = color.color,
colorMode = color.mode
});
}

@"{0}4 {1} = IsGammaSpace() ? {0}4({2}, {3}, {4}, {5}) : {0}4(SRGBToLinear({0}3({2}, {3}, {4})), {5});"
, precision
, GetVariableNameForNode()
, color.r
, color.g
, color.b
, color.a), true);
, color.color.r
, color.color.g
, color.color.b
, color.color.a), true);
}
public override string GetVariableNameForSlot(int slotId)

properties.Add(new PreviewProperty(PropertyType.Color)
{
name = GetVariableNameForNode(),
colorValue = color
colorValue = color.color
return new ColorShaderProperty { value = color };
return new ColorShaderProperty { value = color.color, colorMode = color.mode };
}
public int outputSlotId { get { return OutputSlotId; } }

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


if (!generationMode.IsPreview())
return;
properties.AddShaderProperty(new IntegerShaderProperty()
properties.AddShaderProperty(new Vector1ShaderProperty()
value = value
value = value,
floatType = FloatType.Integer
});
}

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 IntegerShaderProperty { value = value };
return new Vector1ShaderProperty { value = value, floatType = FloatType.Integer };
}
public int outputSlotId { get { return OutputSlotId; } }

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


if (!generationMode.IsPreview())
return;
properties.AddShaderProperty(new SliderShaderProperty()
properties.AddShaderProperty(new Vector1ShaderProperty()
value = value
value = value.x,
rangeValues = new Vector2(value.y, value.z),
floatType = FloatType.Slider
});
}

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 SliderShaderProperty { value = value };
return new Vector1ShaderProperty
{
value = value.x,
rangeValues = new Vector2(value.y, value.z),
floatType = FloatType.Slider
};
}
public int outputSlotId { get { return OutputSlotId; } }

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

30
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, property.displayName, "Out", SlotType.Output, 0));
RemoveSlotsNameNotMatching(new[] {OutputSlotId});

AddSlot(new CubemapMaterialSlot(OutputSlotId, property.displayName, "Out", SlotType.Output));
RemoveSlotsNameNotMatching(new[] { OutputSlotId });
}
else if (property is IntegerShaderProperty)
{
AddSlot(new Vector1MaterialSlot(OutputSlotId, property.displayName, "Out", SlotType.Output, 0));
RemoveSlotsNameNotMatching(new[] { OutputSlotId });
}
else if (property is SliderShaderProperty)
{
AddSlot(new Vector1MaterialSlot(OutputSlotId, property.displayName, "Out", SlotType.Output, 0));
RemoveSlotsNameNotMatching(new[] { OutputSlotId });
}
else if (property is BooleanShaderProperty)
{
AddSlot(new BooleanMaterialSlot(OutputSlotId, property.displayName, "Out", SlotType.Output, false));

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

else if (property is ColorShaderProperty)
{
var result = string.Format("{0}4 {1} = {2};"
, precision
, GetVariableNameForSlot(OutputSlotId)
, property.referenceName);
visitor.AddShaderChunk(result, true);
}
else if (property is IntegerShaderProperty)
{
var result = string.Format("{0} {1} = {2};"
, precision
, GetVariableNameForSlot(OutputSlotId)
, property.referenceName);
visitor.AddShaderChunk(result, true);
}
else if (property is SliderShaderProperty)
{
var result = string.Format("{0} {1} = {2};"
, precision
, GetVariableNameForSlot(OutputSlotId)
, property.referenceName);

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)

164
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;
var field = new FloatField { value = floatProperty.value };
field.OnValueChanged(evt =>
VisualElement floatRow = new VisualElement();
VisualElement intRow = new VisualElement();
VisualElement modeRow = new VisualElement();
VisualElement minRow = new VisualElement();
VisualElement maxRow = new VisualElement();
FloatField floatField = null;
var floatProperty = (Vector1ShaderProperty)property;
if(floatProperty.floatType == FloatType.Integer)
{
var field = new IntegerField { value = (int)floatProperty.value };
field.OnValueChanged(intEvt =>
{
floatProperty.value = (float)intEvt.newValue;
DirtyNodes();
});
intRow = AddRow("Default", field);
}
else
floatProperty.value = (float)evt.newValue;
floatField = new FloatField { value = floatProperty.value };
floatField.OnValueChanged(evt =>
{
floatProperty.value = (float)evt.newValue;
DirtyNodes();
});
floatRow = AddRow("Default", floatField);
}
var floatModeField = new EnumField((Enum)floatProperty.floatType);
floatModeField.value = floatProperty.floatType;
floatModeField.OnValueChanged(evt =>
{
if(floatProperty.floatType == (FloatType)evt.newValue)
return;
floatProperty = (Vector1ShaderProperty)property;
floatProperty.floatType = (FloatType)evt.newValue;
switch(floatProperty.floatType)
{
case FloatType.Slider:
RemoveElements(new VisualElement[]{floatRow, intRow, modeRow, minRow, maxRow});
var 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("Default", field);
modeRow = AddRow("Mode", floatModeField);
if(floatProperty.floatType == FloatType.Slider)
{
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);
floatField.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);
floatField.value = floatProperty.value;
DirtyNodes();
});
maxRow = AddRow("Max", maxField);
}
}
else if (property is Vector2ShaderProperty)
{

else if (property is ColorShaderProperty)
{
var colorProperty = (ColorShaderProperty)property;
var field = new ColorField { value = property.defaultValue, hdr = colorProperty.HDR };
field.OnValueChanged(evt =>
var colorField = new ColorField { value = property.defaultValue, showEyeDropper = false, hdr = colorProperty.colorMode == ColorMode.HDR };
colorField.OnValueChanged(evt =>
AddRow("Default", field);
AddRow("Default", colorField);
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();
DirtyNodes();
});
AddRow("Mode", colorModeField);
}
else if (property is TextureShaderProperty)
{

});
AddRow("Default", field);
}
else if (property is BooleanShaderProperty)
{
var booleanProperty = (BooleanShaderProperty)property;
Action onBooleanChanged = () =>
{
booleanProperty.value = !booleanProperty.value;
DirtyNodes();
};
var field = new Toggle(onBooleanChanged) { on = booleanProperty.value };
AddRow("Default", field);
}
// AddRow("Type", new TextField());
// AddRow("Exposed", new Toggle(null));
// AddRow("Range", new Toggle(null));

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

3
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));

gm.AddItem(new GUIContent("Boolean"), false, () => AddProperty(new BooleanShaderProperty(), true));
gm.ShowAsContext();
}

50
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/ColorControl.cs


using UnityEngine.Experimental.UIElements;
using UnityEngine.Experimental.UIElements.StyleSheets;
using UnityEditor.ShaderGraph;
using Color = UnityEditor.ShaderGraph.ColorNode.Color;
public enum ColorMode
{
Default,
HDR
}
[AttributeUsage(AttributeTargets.Property)]
public class ColorControlAttribute : Attribute, IControlAttribute
{

AbstractMaterialNode m_Node;
PropertyInfo m_PropertyInfo;
Color m_Color;
ColorField m_ColorField;
public ColorControlView(string label, ColorMode colorMode, AbstractMaterialNode node, PropertyInfo propertyInfo)
{
m_Node = node;

label = label ?? ObjectNames.NicifyVariableName(propertyInfo.Name);
m_Color = (Color)m_PropertyInfo.GetValue(m_Node, null);
ColorField colorField;
switch(colorMode)
{
case ColorMode.HDR:
colorField = new ColorField { value = (Color)m_PropertyInfo.GetValue(m_Node, null), hdr = true, showEyeDropper = false };
break;
default:
colorField = new ColorField { value = (Color)m_PropertyInfo.GetValue(m_Node, null), showEyeDropper = false };
break;
}
colorField.OnValueChanged(OnChange);
Add(colorField);
m_ColorField = new ColorField { value = m_Color.color, hdr = m_Color.mode == ColorMode.HDR, showEyeDropper = false };
m_ColorField.OnValueChanged(OnChange);
Add(m_ColorField);
VisualElement enumPanel = new VisualElement { name = "enumPanel" };
enumPanel.Add(new Label("Mode"));
var enumField = new EnumField(m_Color.mode);
enumField.OnValueChanged(OnModeChanged);
enumPanel.Add(enumField);
Add(enumPanel);
void OnChange(ChangeEvent<Color> evt)
void OnChange(ChangeEvent<UnityEngine.Color> evt)
m_PropertyInfo.SetValue(m_Node, evt.newValue, null);
m_Color.color = evt.newValue;
m_PropertyInfo.SetValue(m_Node, m_Color, null);
}
void OnModeChanged(ChangeEvent<Enum> evt)
{
if (!evt.newValue.Equals(m_Color.mode))
{
m_Node.owner.owner.RegisterCompleteObjectUndo("Change " + m_Node.name);
m_Color.mode = (ColorMode)evt.newValue;
m_ColorField.hdr = m_Color.mode == ColorMode.HDR;
m_PropertyInfo.SetValue(m_Node, m_Color, null);
}
}
}
}

6
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("HDR Color"), false, () => AddProperty(new ColorShaderProperty(true)));
gm.AddItem(new GUIContent("Integer"), false, () => AddProperty(new IntegerShaderProperty()));
gm.AddItem(new GUIContent("Slider"), false, () => AddProperty(new SliderShaderProperty()));
gm.AddItem(new GUIContent("HDR Color"), false, () => AddProperty(new ColorShaderProperty() {colorMode = ColorMode.HDR}));
gm.AddItem(new GUIContent("Boolean"), false, () => AddProperty(new BooleanShaderProperty()));
gm.AddItem(new GUIContent("Texture"), false, () => AddProperty(new TextureShaderProperty()));
gm.AddItem(new GUIContent("Cubemap"), false, () => AddProperty(new CubemapShaderProperty()));

14
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;

m_ValueAction = Vector4Field;
else if (property is IntegerShaderProperty)
m_ValueAction = IntegerField;
else if (property is SliderShaderProperty)
m_ValueAction = SliderField;
else if (property is BooleanShaderProperty)
m_ValueAction = BooleanField;

{
var fProp = (ColorShaderProperty)property;
ColorField colorField;
if(fProp.HDR)
if(fProp.colorMode == ColorMode.HDR)
colorField = new ColorField { name = "value", value = fProp.value, hdr = true };
else
colorField = new ColorField { name = "value", value = fProp.value };

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

fProp.value = EditorGUILayout.Vector4Field("", fProp.value);
}
void IntegerField()
/*void IntegerField()
{
var fProp = (IntegerShaderProperty)property;
fProp.value = EditorGUILayout.IntField(fProp.value);

EditorGUIUtility.labelWidth = previousLabelWidth;
EditorGUILayout.EndHorizontal();
fProp.value = value;
}
}*/
void BooleanField()
{

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

25
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Resources/Styles/MaterialGraph.uss


padding-bottom: 2;
}
ColorControlView {
ColorControlView > ColorField {
flex-direction: row;
}
ColorControlView > #enumPanel {
}
ColorControlView > #enumPanel > EnumField {
flex: 1;
margin-left: 0;
margin-right: 8;
margin-top: 4;
margin-bottom: 4;
}
ColorControlView > #enumPanel > Label {
padding-left: 0;
padding-right: 0;
padding-top: 0;
padding-bottom: 0;
margin-left: 8;
margin-right: 8;
margin-top: 4;
margin-bottom: 4;
}
MultiFloatControlView {

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);
}

56
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/IntegerShaderProperty.cs


using System;
using System.Text;
using UnityEditor.Graphing;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class IntegerShaderProperty : AbstractShaderProperty<int>
{
public IntegerShaderProperty()
{
displayName = "Integer";
}
public override PropertyType propertyType
{
get { return PropertyType.Float; }
}
public override Vector4 defaultValue
{
get { return new Vector4(value, value, value, value); }
}
public override string GetPropertyBlockString()
{
var result = new StringBuilder();
result.Append(referenceName);
result.Append("(\"");
result.Append(displayName);
result.Append("\", Int) = ");
result.Append(value);
return result.ToString();
}
public override string GetPropertyDeclarationString(string delimiter = ";")
{
return string.Format("float {0}{1}", referenceName, delimiter);
}
public override PreviewProperty GetPreviewMaterialProperty()
{
return new PreviewProperty(PropertyType.Float)
{
name = referenceName,
floatValue = value
};
}
public override INode ToConcreteNode()
{
return new IntegerNode { value = value };
}
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/IntegerShaderProperty.cs.meta


fileFormatVersion: 2
guid: 5980fe29fe1b01c4fa3fb434dd9eb9e2
timeCreated: 1505346922

61
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SliderShaderProperty.cs


using System;
using System.Text;
using UnityEditor.Graphing;
using UnityEngine;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class SliderShaderProperty : AbstractShaderProperty<Vector3>
{
public SliderShaderProperty()
{
displayName = "Slider";
value = new Vector3(0, 0, 1);
}
public override PropertyType propertyType
{
get { return PropertyType.Float; }
}
public override Vector4 defaultValue
{
get { return new Vector4(value.x, value.y, value.z, value.x); }
}
public override string GetPropertyBlockString()
{
var result = new StringBuilder();
result.Append(referenceName);
result.Append("(\"");
result.Append(displayName);
result.Append("\", Range(");
result.Append(value.y);
result.Append(", ");
result.Append(value.z);
result.Append(")) = ");
result.Append(value.x);
return result.ToString();
}
public override string GetPropertyDeclarationString(string delimiter = ";")
{
return string.Format("float {0}{1}", referenceName, delimiter);
}
public override PreviewProperty GetPreviewMaterialProperty()
{
return new PreviewProperty(PropertyType.Float)
{
name = referenceName,
floatValue = value.x
};
}
public override INode ToConcreteNode()
{
return new SliderNode { value = value };
}
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SliderShaderProperty.cs.meta


fileFormatVersion: 2
guid: 116edc957722a90438d51cf6610694d3
timeCreated: 1505346922

94
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/HDRColorNode.cs


using System.Collections.Generic;
using UnityEditor.ShaderGraph.Drawing.Controls;
using UnityEngine;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
[Title("Input", "Basic", "HDR Color")]
public class HDRColorNode : AbstractMaterialNode, IGeneratesBodyCode, IPropertyFromNode
{
[SerializeField]
private Color m_Color;
public const int OutputSlotId = 0;
private const string kOutputSlotName = "Out";
public HDRColorNode()
{
name = "HDR Color";
UpdateNodeAfterDeserialization();
}
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new ColorRGBAMaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, Vector4.zero));
RemoveSlotsNameNotMatching(new[] { OutputSlotId });
}
[ColorControl("", ColorMode.HDR)]
public Color color
{
get { return m_Color; }
set
{
if (m_Color == value)
return;
m_Color = value;
Dirty(ModificationScope.Node);
}
}
public override void CollectShaderProperties(PropertyCollector properties, GenerationMode generationMode)
{
if (!generationMode.IsPreview())
return;
properties.AddShaderProperty(new ColorShaderProperty()
{
overrideReferenceName = GetVariableNameForNode(),
generatePropertyBlock = false,
value = color,
HDR = true
});
}
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)
{
if (generationMode.IsPreview())
return;
visitor.AddShaderChunk(string.Format(
@"{0}4 {1} = IsGammaSpace() ? {0}4({2}, {3}, {4}, {5}) : {0}4(SRGBToLinear({0}3({2}, {3}, {4})), {5});"
, precision
, GetVariableNameForNode()
, color.r
, color.g
, color.b
, color.a), true);
}
public override string GetVariableNameForSlot(int slotId)
{
return GetVariableNameForNode();
}
public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
{
properties.Add(new PreviewProperty(PropertyType.Color)
{
name = GetVariableNameForNode(),
colorValue = color
});
}
public IShaderProperty AsShaderProperty()
{
return new ColorShaderProperty { value = color, HDR = true };
}
public int outputSlotId { get { return OutputSlotId; } }
}
}

8
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/HDRColorNode.cs.meta


fileFormatVersion: 2
guid: 948ed387e8c19c14c82965be937d7284
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/FloatShaderProperty.cs.meta → /MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector1ShaderProperty.cs.meta

/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/FloatShaderProperty.cs → /MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector1ShaderProperty.cs

正在加载...
取消
保存