浏览代码

Add Integer

Node, ShaderProperty, MaterialSlot
Update Property node and properties list
/main
Matt Dean 7 年前
当前提交
2f6e839e
共有 11 个文件被更改,包括 337 次插入0 次删除
  1. 13
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/PropertyNode.cs
  2. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/GraphInspectorView.cs
  3. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Inspector/ShaderPropertyView.cs
  4. 91
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/IntegerMaterialSlot.cs
  5. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/IntegerMaterialSlot.cs.meta
  6. 56
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/IntegerShaderProperty.cs
  7. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/IntegerShaderProperty.cs.meta
  8. 86
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/IntegerNode.cs
  9. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/IntegerNode.cs.meta
  10. 53
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/IntegerControl.cs
  11. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/IntegerControl.cs.meta

13
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 IntegerMaterialSlot(OutputSlotId, "Out", "Out", SlotType.Output, 0));
RemoveSlotsNameNotMatching(new[] { OutputSlotId });
}
}
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)

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

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


gm.AddItem(new GUIContent("Vector3"), false, () => AddProperty(new Vector3ShaderProperty()));
gm.AddItem(new GUIContent("Vector4"), false, () => AddProperty(new Vector4ShaderProperty()));
gm.AddItem(new GUIContent("Color"), false, () => AddProperty(new ColorShaderProperty()));
gm.AddItem(new GUIContent("Integer"), false, () => AddProperty(new IntegerShaderProperty()));
gm.AddItem(new GUIContent("Texture"), false, () => AddProperty(new TextureShaderProperty()));
gm.AddItem(new GUIContent("Cubemap"), false, () => AddProperty(new CubemapShaderProperty()));
gm.ShowAsContext();

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;
if (m_ValueAction != null)
{

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

91
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/IntegerMaterialSlot.cs


using System;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Drawing.Slots;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
namespace UnityEditor.ShaderGraph
{
[Serializable]
public class IntegerMaterialSlot : MaterialSlot, IMaterialSlotHasValue<int>
{
[SerializeField]
private int m_Value;
[SerializeField]
private int m_DefaultValue;
public IntegerMaterialSlot()
{}
public IntegerMaterialSlot(
int slotId,
string displayName,
string shaderOutputName,
SlotType slotType,
int value,
ShaderStage shaderStage = ShaderStage.Dynamic,
bool hidden = false)
: base(slotId, displayName, shaderOutputName, slotType, shaderStage, hidden)
{
m_DefaultValue = value;
m_Value = value;
}
public int defaultValue { get { return m_DefaultValue; } }
public int value
{
get { return m_Value; }
set { m_Value = value; }
}
public override VisualElement InstantiateControl()
{
return new MultiFloatSlotControlView(owner, 1, () => new Vector4(value, 0f, 0f, 0f), (newValue) => value = (int)newValue.x);
}
protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
return value.ToString();
}
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{
if (!generationMode.IsPreview())
return;
var matOwner = owner as AbstractMaterialNode;
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 IntegerShaderProperty()
{
overrideReferenceName = matOwner.GetVariableNameForSlot(id),
generatePropertyBlock = false,
value = value
};
properties.AddShaderProperty(property);
}
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(PropertyType.Float)
{
name = name,
floatValue = value
};
return pp;
}
public override void CopyValuesFrom(MaterialSlot foundSlot)
{
var slot = foundSlot as IntegerMaterialSlot;
if (slot != null)
value = slot.value;
}
}
}

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/IntegerMaterialSlot.cs.meta


fileFormatVersion: 2
guid: 51c110f32bfa9764295488324d94d836
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

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

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


using System.Collections.Generic;
using UnityEditor.ShaderGraph.Drawing.Controls;
using UnityEngine;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
[Title("Input", "Basic", "Integer")]
public class IntegerNode : AbstractMaterialNode, IGeneratesBodyCode, IPropertyFromNode
{
[SerializeField]
private int m_Value;
public const int OutputSlotId = 0;
private const string kOutputSlotName = "Out";
public IntegerNode()
{
name = "Integer";
UpdateNodeAfterDeserialization();
}
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new IntegerMaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, 0));
RemoveSlotsNameNotMatching(new[] { OutputSlotId });
}
[IntegerControl("")]
public int 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 IntegerShaderProperty()
{
overrideReferenceName = GetVariableNameForNode(),
generatePropertyBlock = false,
value = value
});
}
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)
{
if (generationMode.IsPreview())
return;
visitor.AddShaderChunk(precision + " " + GetVariableNameForNode() + " = " + m_Value + ";", 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
});
}
public IShaderProperty AsShaderProperty()
{
return new IntegerShaderProperty { value = value };
}
public int outputSlotId { get { return OutputSlotId; } }
}
}

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


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

53
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/IntegerControl.cs


using System;
using System.Reflection;
using UnityEditor.Experimental.UIElements;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
namespace UnityEditor.ShaderGraph.Drawing.Controls
{
[AttributeUsage(AttributeTargets.Property)]
public class IntegerControlAttribute : Attribute, IControlAttribute
{
string m_Label;
public IntegerControlAttribute(string label = null)
{
m_Label = label;
}
public VisualElement InstantiateControl(AbstractMaterialNode node, PropertyInfo propertyInfo)
{
return new IntegerControlView(m_Label, node, propertyInfo);
}
}
public class IntegerControlView : VisualElement
{
AbstractMaterialNode m_Node;
PropertyInfo m_PropertyInfo;
public IntegerControlView(string label, AbstractMaterialNode node, PropertyInfo propertyInfo)
{
m_Node = node;
m_PropertyInfo = propertyInfo;
if (propertyInfo.PropertyType != typeof(int))
throw new ArgumentException("Property must be of type integer.", "propertyInfo");
label = label ?? ObjectNames.NicifyVariableName(propertyInfo.Name);
if (!string.IsNullOrEmpty(label))
Add(new Label(label));
var intField = new IntegerField { value = (int)m_PropertyInfo.GetValue(m_Node, null) };
intField.OnValueChanged(OnChange);
Add(intField);
}
void OnChange(ChangeEvent<long> evt)
{
m_Node.owner.owner.RegisterCompleteObjectUndo("Integer Change");
m_PropertyInfo.SetValue(m_Node, (int)evt.newValue, null);
Dirty(ChangeType.Repaint);
}
}
}

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/IntegerControl.cs.meta


fileFormatVersion: 2
guid: 57d18a57866821542806ab5253cf4310
timeCreated: 1507817885
正在加载...
取消
保存