浏览代码

Convolution filter presenter

/main
Florent Guinier 8 年前
当前提交
74c4e0ee
共有 4 个文件被更改,包括 178 次插入3 次删除
  1. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/MaterialGraphPresenter.cs
  2. 16
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Art/Filters/ConvolutionFilterNode.cs
  3. 152
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/ConvolutionFilterNodePresenter.cs
  4. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/ConvolutionFilterNodePresenter.cs.meta

1
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/MaterialGraphPresenter.cs


typeMapper[typeof(AddManyNode)] = typeof(AddManyNodePresenter);
typeMapper[typeof(IfNode)] = typeof(IfNodePresenter);
typeMapper[typeof(CustomCodeNode)] = typeof(CustomCodePresenter);
typeMapper[typeof(ConvolutionFilterNode)] = typeof(ConvolutionFilterNodePresenter);
}
}
}

16
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Art/Filters/ConvolutionFilterNode.cs


namespace UnityEngine.MaterialGraph
{
[Title("Art/Filters/Convolution")]
public class ConvolutionFilter : Function2Input, IGeneratesFunction, IGeneratesBodyCode, IMayRequireMeshUV
public class ConvolutionFilterNode : Function2Input, IGeneratesFunction, IGeneratesBodyCode, IMayRequireMeshUV
{
private const string kUVSlotName = "UV";
private const string kTextureSlotName = "Texture";

new Vector4(1,1,1,1),
new Vector4(1,1,1,1),
new Vector4(1,1,1,1),
new Vector4(1,0,0,1.0f/25.0f) };
new Vector4(1,0,0,25.0f) };
private void GetPositionInData(int row, int col, out int vectorIndex, out int vectorOffset)
{

int valueIndex = col * 5 + row;
vectorIndex = valueIndex / 4;
vectorOffset = valueIndex % 4;
}
public float GetConvolutionDivisor()
{
return m_ConvolutionFilter[6].w;
}
public void SetConvolutionDivisor(float value)
{
m_ConvolutionFilter[6].w = value;
}
public float GetConvolutionWeight(int row, int col)

return new MaterialSlot(OutputSlotId, kOutputSlotShaderName, kOutputSlotShaderName, SlotType.Output, SlotValueType.Vector4, Vector4.zero);
}
public ConvolutionFilter()
public ConvolutionFilterNode()
{
name = "Convolution";
UpdateNodeAfterDeserialization();

152
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/ConvolutionFilterNodePresenter.cs


using System;
using System.Collections.Generic;
using RMGUI.GraphView;
using UnityEditor.Graphing.Drawing;
using UnityEngine.MaterialGraph;
using UnityEngine;
namespace UnityEditor.MaterialGraph.Drawing
{
class ConvolutionFilterControlPresenter : GraphControlPresenter
{
enum KernelPresets
{
Presets,
BoxBlur,
GaussianBlur,
EdgesDetection,
EnhanceEdges,
Sharpen,
Emboss,
};
private void ApplyPreset(KernelPresets preset, ref float[,] values, ref float divisor)
{
if (preset == KernelPresets.BoxBlur)
{
values = new float[,] { {1,1,1,1,1},
{1,1,1,1,1},
{1,1,1,1,1},
{1,1,1,1,1},
{1,1,1,1,1} };
divisor = 25;
}
if (preset == KernelPresets.GaussianBlur)
{
values = new float[,] { { 1, 4, 6, 4, 1},
{ 4,16,24,16, 4},
{ 6,24,36,24, 6},
{ 4,16,24,16, 4},
{ 1, 4, 6, 4, 1} };
divisor = 256;
}
if (preset == KernelPresets.EdgesDetection)
{
values = new float[,] { {-1,-1,-1, -1,-1},
{-1,-2,-2, -2,-1},
{-1,-2, 33,-2,-1},
{-1,-2,-2, -2,-1},
{-1,-1,-1, -1,-1} };
divisor = 1;
}
if (preset == KernelPresets.EnhanceEdges)
{
values = new float[,] { { 0, 0, 0, 0, 0},
{ 0, 1, 1, 1, 0},
{ 0, 1,-7, 1, 0},
{ 0, 1, 1, 1, 0},
{ 0, 0, 0, 0, 0} };
divisor = 1;
}
if (preset == KernelPresets.Sharpen)
{
values = new float[,] { {-1,-1,-1, -1,-1},
{-1, 2, 2, 2,-1},
{-1, 2, 8, 2,-1},
{-1, 2, 2, 2,-1},
{-1,-1,-1, -1,-1} };
divisor = 8;
}
if (preset == KernelPresets.Emboss)
{
values = new float[,] { {-1,-1,-1,-1, 0},
{-1,-1,-1, 0, 1},
{-1,-1, 0, 1, 1},
{-1, 0, 1, 1, 1},
{ 0, 1, 1, 1, 1} };
divisor = 1;
}
}
public override void OnGUIHandler()
{
base.OnGUIHandler();
var tNode = node as ConvolutionFilterNode;
if (tNode == null)
return;
float divisor = tNode.GetConvolutionDivisor();
float[,] values = new float[5,5];
for (int row = 0; row < 5; ++row)
{
for (int col = 0; col < 5; ++col)
{
values[row,col] = tNode.GetConvolutionWeight(row,col);
}
}
EditorGUILayout.BeginVertical();
EditorGUI.BeginChangeCheck();
EditorGUILayout.LabelField("Kernel");
for (int col = 0; col < 5; ++col)
{
EditorGUILayout.BeginHorizontal();
for (int row = 0; row < 5; ++row)
{
values[row, col] = EditorGUILayout.FloatField(values[row, col], GUILayout.Width(35));
}
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.Space();
divisor = EditorGUILayout.FloatField("Divisor", divisor);
EditorGUILayout.Space();
KernelPresets kernelPresets = KernelPresets.Presets;
kernelPresets = (KernelPresets)EditorGUILayout.EnumPopup(kernelPresets);
ApplyPreset(kernelPresets, ref values, ref divisor);
EditorGUILayout.EndVertical();
if (EditorGUI.EndChangeCheck())
{
tNode.SetConvolutionDivisor(divisor);
for (int row = 0; row < 5; ++row)
{
for (int col = 0; col < 5; ++col)
{
tNode.SetConvolutionWeight(row, col, values[row, col]);
}
}
}
}
public override float GetHeight()
{
return (EditorGUIUtility.singleLineHeight * 10 + EditorGUIUtility.standardVerticalSpacing) + EditorGUIUtility.standardVerticalSpacing;
}
}
[Serializable]
public class ConvolutionFilterNodePresenter : PropertyNodePresenter
{
protected override IEnumerable<GraphElementPresenter> GetControlData()
{
var instance = CreateInstance<ConvolutionFilterControlPresenter>();
instance.Initialize(node);
return new List<GraphElementPresenter>(base.GetControlData()) { instance };
}
}
}

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/ConvolutionFilterNodePresenter.cs.meta


fileFormatVersion: 2
guid: f3a416c372d21b6479241e837d4257da
timeCreated: 1495705790
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存