浏览代码

Add Slider

ShaderProperty, Control, Node and property list entry
/main
Matt Dean 7 年前
当前提交
cfb57499
共有 9 个文件被更改,包括 273 次插入0 次删除
  1. 13
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/PropertyNode.cs
  2. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorView.cs
  3. 15
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/ShaderPropertyView.cs
  4. 61
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SliderShaderProperty.cs
  5. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SliderShaderProperty.cs.meta
  6. 86
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/SliderNode.cs
  7. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/SliderNode.cs.meta
  8. 71
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/SliderControl.cs
  9. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/SliderControl.cs.meta

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


AddSlot(new IntegerMaterialSlot(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 });
}
}
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)

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

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


gm.AddItem(new GUIContent("Color"), false, () => AddProperty(new ColorShaderProperty()));
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("Texture"), false, () => AddProperty(new TextureShaderProperty()));
gm.AddItem(new GUIContent("Cubemap"), false, () => AddProperty(new CubemapShaderProperty()));
gm.ShowAsContext();

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


m_ValueAction = Vector4Field;
else if (property is IntegerShaderProperty)
m_ValueAction = IntegerField;
else if (property is SliderShaderProperty)
m_ValueAction = SliderField;
if (m_ValueAction != null)
{

{
var fProp = (IntegerShaderProperty)property;
fProp.value = EditorGUILayout.IntField(fProp.value);
}
void SliderField()
{
var fProp = (SliderShaderProperty)property;
var value = fProp.value;
EditorGUILayout.BeginHorizontal();
GUILayoutOption[] options = { GUILayout.MaxWidth(48.0f) };
value.x = EditorGUILayout.Slider(fProp.value.x, fProp.value.y, fProp.value.z);
value.y = EditorGUILayout.FloatField(fProp.value.y, options);
value.z = EditorGUILayout.FloatField(fProp.value.z, options);
EditorGUILayout.EndHorizontal();
fProp.value = value;
}
}
}

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

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


using System.Collections.Generic;
using UnityEditor.ShaderGraph.Drawing.Controls;
using UnityEngine;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
[Title("Input", "Basic", "Slider")]
public class SliderNode : AbstractMaterialNode, IGeneratesBodyCode, IPropertyFromNode
{
[SerializeField]
private Vector3 m_Value = new Vector3(0f, 0f, 1f);
public const int OutputSlotId = 0;
private const string kOutputSlotName = "Out";
public SliderNode()
{
name = "Slider";
UpdateNodeAfterDeserialization();
}
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new Vector1MaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, 0));
RemoveSlotsNameNotMatching(new[] { OutputSlotId });
}
[SliderControl("")]
public Vector3 value
{
get { return m_Value; }
set
{
if (m_Value == value)
return;
m_Value = value;
Dirty(ModificationScope.Node);
}
}
public override void CollectShaderProperties(PropertyCollector properties, GenerationMode generationMode)
{
if (!generationMode.IsPreview())
return;
properties.AddShaderProperty(new SliderShaderProperty()
{
overrideReferenceName = GetVariableNameForNode(),
generatePropertyBlock = false,
value = value
});
}
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)
{
if (generationMode.IsPreview())
return;
visitor.AddShaderChunk(precision + " " + GetVariableNameForNode() + " = " + m_Value.x + ";", true);
}
public override string GetVariableNameForSlot(int slotId)
{
return GetVariableNameForNode();
}
public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
{
properties.Add(new PreviewProperty(PropertyType.Float)
{
name = GetVariableNameForNode(),
floatValue = m_Value.x
});
}
public IShaderProperty AsShaderProperty()
{
return new SliderShaderProperty { value = value };
}
public int outputSlotId { get { return OutputSlotId; } }
}
}

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


fileFormatVersion: 2
guid: 39d5ba890306e584c9fb4fede8c37f9c
timeCreated: 1445864587
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

71
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/SliderControl.cs


using System;
using System.Reflection;
using UnityEditor.Experimental.UIElements;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph.Drawing.Controls
{
[AttributeUsage(AttributeTargets.Property)]
public class SliderControlAttribute : Attribute, IControlAttribute
{
string m_Label;
public SliderControlAttribute(string label = null)
{
m_Label = label;
}
public VisualElement InstantiateControl(AbstractMaterialNode node, PropertyInfo propertyInfo)
{
return new SliderControlView(m_Label, node, propertyInfo);
}
}
public class SliderControlView : VisualElement, INodeModificationListener
{
GUIContent m_Label;
AbstractMaterialNode m_Node;
PropertyInfo m_PropertyInfo;
IMGUIContainer m_Container;
public SliderControlView(string label, AbstractMaterialNode node, PropertyInfo propertyInfo)
{
m_Node = node;
m_PropertyInfo = propertyInfo;
if (propertyInfo.PropertyType != typeof(Vector3))
throw new ArgumentException("Property must be of type Vector3.", "propertyInfo");
m_Label = new GUIContent(label ?? ObjectNames.NicifyVariableName(propertyInfo.Name));
m_Container = new IMGUIContainer(OnGUIHandler);
Add(m_Container);
}
public void OnNodeModified(ModificationScope scope)
{
if (scope == ModificationScope.Graph)
m_Container.Dirty(ChangeType.Repaint);
}
void OnGUIHandler()
{
var value = (Vector3)m_PropertyInfo.GetValue(m_Node, null);
using (var changeCheckScope = new EditorGUI.ChangeCheckScope())
{
var slider = EditorGUILayout.Slider(value.x, value.y, value.z);
GUILayout.BeginHorizontal();
EditorGUIUtility.labelWidth = 32f;
var minField = EditorGUILayout.FloatField("Min", value.y);
var maxField = EditorGUILayout.FloatField("Max", value.z);
GUILayout.EndHorizontal();
if (changeCheckScope.changed)
{
m_Node.owner.owner.RegisterCompleteObjectUndo("Change " + m_Node.name);
m_PropertyInfo.SetValue(m_Node, new Vector3(slider, minField, maxField), null);
}
}
}
}
}

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/SliderControl.cs.meta


fileFormatVersion: 2
guid: c74a50771637c2d49980f713bbb7f4ea
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存