浏览代码

Refactor Dielectric Specular node for UI changes

/main
Matt Dean 7 年前
当前提交
93aa1de8
共有 4 个文件被更改,包括 311 次插入50 次删除
  1. 84
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/PBR/DielectricSpecularNode.cs
  2. 55
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Resources/Styles/MaterialGraph.uss
  3. 219
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/DielectricSpecularControl.cs
  4. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/DielectricSpecularControl.cs.meta

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


namespace UnityEditor.ShaderGraph
{
public enum DielectricMaterial
public enum DielectricMaterialType
{
Common,
RustedMetal,

}
[SerializeField]
private DielectricMaterial m_Material = DielectricMaterial.Common;
DielectricMaterial m_Material = new DielectricMaterial(DielectricMaterialType.Common, 0.5f, 1.0f);
[EnumControl("Material")]
public DielectricMaterial material
[Serializable]
public struct DielectricMaterial
get { return m_Material; }
set
{
if (m_Material == value)
return;
m_Material = value;
Dirty(ModificationScope.Graph);
}
}
public DielectricMaterialType type;
public float range;
public float indexOfRefraction;
[SerializeField]
private float m_Range = 0.5f;
[MultiFloatControl("Range")]
public float range
{
get { return m_Range; }
set
public DielectricMaterial(DielectricMaterialType type, float range, float indexOfRefraction)
if (m_Range == value)
return;
m_Range = value;
Dirty(ModificationScope.Node);
this.type = type;
this.range = range;
this.indexOfRefraction = indexOfRefraction;
[SerializeField]
private float m_IndexOfRefraction = 1.0f;
[MultiFloatControl("IOR")]
public float indexOfRefraction
[DielectricSpecularControl()]
public DielectricMaterial material
get { return m_IndexOfRefraction; }
get { return m_Material; }
if (m_IndexOfRefraction == value)
if ((value.type == m_Material.type) && (value.range == m_Material.range) && (value.indexOfRefraction == m_Material.indexOfRefraction))
m_IndexOfRefraction = value;
Dirty(ModificationScope.Node);
DielectricMaterialType previousType = m_Material.type;
m_Material = value;
if(value.type != previousType)
Dirty(ModificationScope.Graph);
else
Dirty(ModificationScope.Node);
}
}

public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)
{
switch (m_Material)
switch (material.type)
case DielectricMaterial.RustedMetal:
case DielectricMaterialType.RustedMetal:
case DielectricMaterial.Water:
case DielectricMaterialType.Water:
case DielectricMaterial.Ice:
case DielectricMaterialType.Ice:
case DielectricMaterial.Glass:
case DielectricMaterialType.Glass:
case DielectricMaterial.Custom:
case DielectricMaterialType.Custom:
visitor.AddShaderChunk(string.Format("{0}3 {1} = pow({2} - 1, 2) / pow({2} + 1, 2);", precision, GetVariableNameForSlot(kOutputSlotId),
string.Format("_{0}_IOR", GetVariableNameForNode())), true);
break;

{
base.CollectPreviewMaterialProperties(properties);
if(material == DielectricMaterial.Common)
if(material.type == DielectricMaterialType.Common)
floatValue = range
floatValue = material.range
else if (material == DielectricMaterial.Custom)
else if (material.type == DielectricMaterialType.Custom)
floatValue = indexOfRefraction
floatValue = material.indexOfRefraction
});
}
}

return;
base.CollectShaderProperties(properties, generationMode);
if (material == DielectricMaterial.Common)
if (material.type == DielectricMaterialType.Common)
{
properties.AddShaderProperty(new FloatShaderProperty()
{

}
else if (material == DielectricMaterial.Custom)
else if (material.type == DielectricMaterialType.Custom)
{
properties.AddShaderProperty(new FloatShaderProperty()
{

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


max-width: 30;
}
DielectricSpecularControlView {
padding-left: 8;
padding-right: 8;
padding-top: 4;
padding-bottom: 4;
}
DielectricSpecularControlView > #enumPanel {
flex-direction: row;
flex-grow: 1;
padding-top: 0;
padding-bottom: 0;
}
DielectricSpecularControlView > #enumPanel > Label {
margin-left: 0;
margin-right: 0;
padding-left: 0;
padding-right: 0;
width: 60;
}
DielectricSpecularControlView > #enumPanel > EnumField {
flex-grow: 1;
margin-left: 0;
margin-right: 0;
}
DielectricSpecularControlView > #sliderPanel {
flex-direction: row;
flex-grow: 1;
padding-top: 0;
padding-bottom: 0;
}
DielectricSpecularControlView > #sliderPanel > Label {
margin-left: 0;
margin-right: 0;
padding-left: 0;
padding-right: 0;
width: 60;
}
DielectricSpecularControlView > #sliderPanel > Slider {
flex-grow: 1;
margin-left: 0;
margin-right: 0;
}
DielectricSpecularControlView > #sliderPanel > FloatField {
width: 30;
margin-left: 4;
margin-right: 0;
}
/* TEMP STUFF THAT SHOULD ACTUALLY STAY IN GRAPHVIEW */
FloatField {

219
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Controls/DielectricSpecularControl.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 DielectricSpecularControlAttribute : Attribute, IControlAttribute
{
public DielectricSpecularControlAttribute()
{
}
public VisualElement InstantiateControl(AbstractMaterialNode node, PropertyInfo propertyInfo)
{
return new DielectricSpecularControlView(node, propertyInfo);
}
}
public class DielectricSpecularControlView : VisualElement
{
AbstractMaterialNode m_Node;
PropertyInfo m_PropertyInfo;
DielectricSpecularNode.DielectricMaterial m_DielectricMaterial;
VisualElement m_RangePanel;
Slider m_RangeSlider;
FloatField m_RangeField;
VisualElement m_IORPanel;
Slider m_IORSlider;
FloatField m_IORField;
int m_UndoGroup = -1;
public DielectricSpecularControlView(AbstractMaterialNode node, PropertyInfo propertyInfo)
{
m_Node = node;
m_PropertyInfo = propertyInfo;
m_DielectricMaterial = (DielectricSpecularNode.DielectricMaterial)m_PropertyInfo.GetValue(m_Node, null);
if (propertyInfo.PropertyType != typeof(DielectricSpecularNode.DielectricMaterial))
throw new ArgumentException("Property must be of type DielectricMaterial.", "propertyInfo");
var enumPanel = new VisualElement { name = "enumPanel" };
enumPanel.Add(new Label("Material"));
var enumField = new EnumField(m_DielectricMaterial.type);
enumField.OnValueChanged(OnEnumChanged);
enumPanel.Add(enumField);
Add(enumPanel);
m_RangePanel = new VisualElement { name = "sliderPanel" };
m_RangePanel.Add(new Label("Range"));
Action<float> changedRangeSlider = (s) => { OnChangeRangeSlider(s); };
m_RangeSlider = new Slider(0.01f, 1, changedRangeSlider);
m_RangeSlider.value = 0.5f;
m_RangePanel.Add(m_RangeSlider);
m_RangeField = AddField(m_RangePanel, m_RangeSlider, 0, m_DielectricMaterial);
m_RangePanel.SetEnabled(true);
Add(m_RangePanel);
m_IORPanel = new VisualElement { name = "sliderPanel" };
m_IORPanel.Add(new Label("IOR"));
Action<float> changedIORSlider = (s) => { OnChangeIORSlider(s); };
m_IORSlider = new Slider(1, 5, changedIORSlider);
m_IORSlider.value = 1;
m_IORPanel.Add(m_IORSlider);
m_IORField = AddField(m_IORPanel, m_IORSlider, 1, m_DielectricMaterial);
m_IORPanel.SetEnabled(false);
Add(m_IORPanel);
}
void OnEnumChanged(ChangeEvent<Enum> evt)
{
if (!evt.newValue.Equals(m_DielectricMaterial.type))
{
m_Node.owner.owner.RegisterCompleteObjectUndo("Change " + m_Node.name);
m_DielectricMaterial.type = (DielectricMaterialType)evt.newValue;
m_PropertyInfo.SetValue(m_Node, m_DielectricMaterial, null);
switch(m_DielectricMaterial.type)
{
case DielectricMaterialType.Common:
m_RangePanel.SetEnabled(true);
m_IORPanel.SetEnabled(false);
break;
case DielectricMaterialType.Custom:
m_RangePanel.SetEnabled(false);
m_IORPanel.SetEnabled(true);
break;
default:
m_RangePanel.SetEnabled(false);
m_IORPanel.SetEnabled(false);
break;
}
}
}
void OnChangeRangeSlider(float newValue)
{
m_Node.owner.owner.RegisterCompleteObjectUndo("Slider Change");
m_DielectricMaterial.range = newValue;
m_PropertyInfo.SetValue(m_Node, m_DielectricMaterial, null);
if(m_RangeField != null)
m_RangeField.value = newValue;
Dirty(ChangeType.Repaint);
}
void OnChangeIORSlider(float newValue)
{
m_Node.owner.owner.RegisterCompleteObjectUndo("Slider Change");
m_DielectricMaterial.indexOfRefraction = newValue;
m_PropertyInfo.SetValue(m_Node, m_DielectricMaterial, null);
if(m_IORField != null)
m_IORField.value = newValue;
Dirty(ChangeType.Repaint);
}
FloatField AddField(VisualElement panel, Slider slider, int index, DielectricSpecularNode.DielectricMaterial initMaterial)
{
float initValue;
if(index == 1)
initValue = initMaterial.indexOfRefraction;
else
initValue = initMaterial.range;
var field = new FloatField { userData = index, value = initValue };
field.RegisterCallback<MouseDownEvent>(Repaint);
field.RegisterCallback<MouseMoveEvent>(Repaint);
field.OnValueChanged(evt =>
{
var value = (DielectricSpecularNode.DielectricMaterial)m_PropertyInfo.GetValue(m_Node, null);
var fieldValue = (float)evt.newValue;
if(index == 1)
{
value.indexOfRefraction = fieldValue;
RedrawIORControls(fieldValue);
}
else
{
value.range = fieldValue;
RedrawRangeControls(fieldValue);
}
m_PropertyInfo.SetValue(m_Node, value, null);
m_UndoGroup = -1;
Dirty(ChangeType.Repaint);
});
field.RegisterCallback<InputEvent>(evt =>
{
if (m_UndoGroup == -1)
{
m_UndoGroup = Undo.GetCurrentGroup();
m_Node.owner.owner.RegisterCompleteObjectUndo("Change " + m_Node.name);
}
float newValue;
if (!float.TryParse(evt.newData, out newValue))
newValue = 0f;
var value = (DielectricSpecularNode.DielectricMaterial)m_PropertyInfo.GetValue(m_Node, null);
if(index == 1)
value.indexOfRefraction = newValue;
else
value.range = newValue;
m_PropertyInfo.SetValue(m_Node, value, null);
Dirty(ChangeType.Repaint);
});
field.RegisterCallback<KeyDownEvent>(evt =>
{
if (evt.keyCode == KeyCode.Escape && m_UndoGroup > -1)
{
Undo.RevertAllDownToGroup(m_UndoGroup);
m_UndoGroup = -1;
var value = (DielectricSpecularNode.DielectricMaterial)m_PropertyInfo.GetValue(m_Node, null);
if(index == 1)
RedrawIORControls(value.indexOfRefraction);
else
RedrawRangeControls(value.range);
evt.StopPropagation();
}
Dirty(ChangeType.Repaint);
});
panel.Add(field);
return field;
}
void RedrawRangeControls(float value)
{
value = Mathf.Max(Mathf.Min(value, 1), 0.01f);
m_RangePanel.Remove(m_RangeSlider);
Action<float> changedSlider = (s) => { OnChangeRangeSlider(s); };
m_RangeSlider = new Slider(0.01f, 1, changedSlider);
m_RangeSlider.value = value;
m_RangePanel.Add(m_RangeSlider);
m_RangePanel.Remove(m_RangeField);
m_RangeField.value = value;
m_RangePanel.Add(m_RangeField);
}
void RedrawIORControls(float value)
{
value = Mathf.Max(Mathf.Min(value, 5), 1);
m_IORPanel.Remove(m_IORSlider);
Action<float> changedSlider = (s) => { OnChangeIORSlider(s); };
m_IORSlider = new Slider(1, 5, changedSlider);
m_IORSlider.value = value;
m_IORPanel.Add(m_IORSlider);
m_IORPanel.Remove(m_IORField);
m_IORField.value = value;
m_IORPanel.Add(m_IORField);
}
void Repaint<T>(MouseEventBase<T> evt) where T : MouseEventBase<T>, new()
{
evt.StopPropagation();
Dirty(ChangeType.Repaint);
}
}
}

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


fileFormatVersion: 2
guid: 20b1492eed8154d8f9a454e3563d9c01
timeCreated: 1507817885
正在加载...
取消
保存