浏览代码

Refactor float based shader properties

/main
Matt Dean 7 年前
当前提交
78db5ef1
共有 11 个文件被更改,包括 79 次插入179 次删除
  1. 72
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/FloatShaderProperty.cs
  2. 7
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/IntegerNode.cs
  3. 13
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/SliderNode.cs
  4. 26
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/PropertyNode.cs
  5. 7
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Blackboard/BlackboardFieldPropertyView.cs
  6. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorView.cs
  7. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/ShaderPropertyView.cs
  8. 56
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/IntegerShaderProperty.cs
  9. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/IntegerShaderProperty.cs.meta
  10. 61
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SliderShaderProperty.cs
  11. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SliderShaderProperty.cs.meta

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


namespace UnityEditor.ShaderGraph
{
public enum FloatType
{
Float,
Slider,
Integer
}
[Serializable]
public class FloatShaderProperty : AbstractShaderProperty<float>
{

get { return new Vector4(value, value, value, value); }
}
private FloatType m_FloatType = FloatType.Float;
public FloatType floatType
{
get { return m_FloatType; }
set
{
if (m_FloatType == value)
return;
m_FloatType = value;
}
}
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(m_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 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 };
}
}
}
}

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


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

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

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


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

public IShaderProperty AsShaderProperty()
{
return new SliderShaderProperty { value = value };
return new FloatShaderProperty
{
value = value.x,
rangeValues = new Vector2(value.y, value.z),
floatType = FloatType.Slider
};
}
public int outputSlotId { get { return OutputSlotId; } }

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


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

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

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


DirtyNodes();
});
AddRow("Default", field);
var floatModeField = new EnumField((Enum)floatProperty.floatType);
floatModeField.OnValueChanged(evt =>
{
floatProperty.floatType = (FloatType)evt.newValue;
DirtyNodes();
});
AddRow("Mode", floatModeField);
}
else if (property is Vector2ShaderProperty)
{

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


gm.AddItem(new GUIContent("Vector4"), false, () => AddProperty(new Vector4ShaderProperty()));
gm.AddItem(new GUIContent("Color"), false, () => AddProperty(new ColorShaderProperty()));
gm.AddItem(new GUIContent("HDR Color"), false, () => AddProperty(new ColorShaderProperty() {colorMode = ColorMode.HDR}));
gm.AddItem(new GUIContent("Integer"), false, () => AddProperty(new IntegerShaderProperty()));
gm.AddItem(new GUIContent("Slider"), false, () => AddProperty(new SliderShaderProperty()));
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()));

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


m_ValueAction = Vector3Field;
else if (property is Vector4ShaderProperty)
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;

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

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
正在加载...
取消
保存