Skip to content

Commit

Permalink
release v2.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Takashi Sakai committed Apr 12, 2018
2 parents f17183d + 2c8b994 commit aaf79b7
Show file tree
Hide file tree
Showing 9 changed files with 256 additions and 65 deletions.
111 changes: 98 additions & 13 deletions Assets/UIEffect/Editor/UIEffectCapturedImageEditor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using UnityEditor;
using UnityEditor.UI;
using UnityEngine;
using DesamplingRate = Coffee.UIExtensions.UIEffectCapturedImage.DesamplingRate;

namespace Coffee.UIExtensions
{
Expand All @@ -11,6 +12,41 @@ namespace Coffee.UIExtensions
[CanEditMultipleObjects]
public class UIEffectCapturedImageEditor : RawImageEditor
{
//################################
// Constant or Static Members.
//################################

public enum QualityMode : int
{
Fast = (DesamplingRate.x2 << 0) + (DesamplingRate.x2 << 4) + (FilterMode.Bilinear << 8) + (1 << 10),
Medium = (DesamplingRate.x1 << 0) + (DesamplingRate.x1 << 4) + (FilterMode.Bilinear << 8) + (1 << 10),
Detail = (DesamplingRate.None << 0) + (DesamplingRate.x1 << 4) + (FilterMode.Bilinear << 8) + (1 << 10),
Custom = -1,
}


//################################
// Public/Protected Members.
//################################
/// <summary>
/// This function is called when the object becomes enabled and active.
/// </summary>
protected override void OnEnable()
{
base.OnEnable();
_spTexture = serializedObject.FindProperty("m_Texture");
_spColor = serializedObject.FindProperty("m_Color");
_spRaycastTarget = serializedObject.FindProperty("m_RaycastTarget");
_spDesamplingRate = serializedObject.FindProperty("m_DesamplingRate");
_spReductionRate = serializedObject.FindProperty("m_ReductionRate");
_spFilterMode = serializedObject.FindProperty("m_FilterMode");
_spIterations = serializedObject.FindProperty("m_Iterations");
_spKeepSizeToRootCanvas = serializedObject.FindProperty("m_KeepCanvasSize");


_customAdvancedOption = (qualityMode == QualityMode.Custom);
}

/// <summary>
/// Implement this function to make a custom inspector.
/// </summary>
Expand All @@ -21,9 +57,9 @@ public override void OnInspectorGUI()
//================
// Basic properties.
//================
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_Texture"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_Color"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_RaycastTarget"));
EditorGUILayout.PropertyField(_spTexture);
EditorGUILayout.PropertyField(_spColor);
EditorGUILayout.PropertyField(_spRaycastTarget);

//================
// Capture effect.
Expand All @@ -38,17 +74,24 @@ public override void OnInspectorGUI()
GUILayout.Space(10);
EditorGUILayout.LabelField("Advanced Option", EditorStyles.boldLabel);

// Desampling rate.
DrawDesamplingRate(serializedObject.FindProperty("m_DesamplingRate"));

// Reduction rate.
DrawDesamplingRate(serializedObject.FindProperty("m_ReductionRate"));
EditorGUI.BeginChangeCheck();
QualityMode quality = qualityMode;
quality = (QualityMode)EditorGUILayout.EnumPopup("Quality Mode", quality);
if(EditorGUI.EndChangeCheck())
{
_customAdvancedOption = (quality == QualityMode.Custom);
qualityMode = quality;
}

// Filter Mode.
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_FilterMode"));

// Iterations.
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_Iterations"));
// When qualityMode is `Custom`, show advanced option.
if (_customAdvancedOption)
{
DrawDesamplingRate(_spDesamplingRate);// Desampling rate.
DrawDesamplingRate(_spReductionRate);// Reduction rate.
EditorGUILayout.PropertyField(_spFilterMode);// Filter Mode.
EditorGUILayout.PropertyField(_spIterations);// Iterations.
}
EditorGUILayout.PropertyField(_spKeepSizeToRootCanvas);// Iterations.

serializedObject.ApplyModifiedProperties();

Expand All @@ -67,6 +110,48 @@ public override void OnInspectorGUI()
}
}

//################################
// Private Members.
//################################
const int Bits4 = (1 << 4) - 1;
const int Bits2 = (1 << 2) - 1;
bool _customAdvancedOption = false;
SerializedProperty _spTexture;
SerializedProperty _spColor;
SerializedProperty _spRaycastTarget;
SerializedProperty _spDesamplingRate;
SerializedProperty _spReductionRate;
SerializedProperty _spFilterMode;
SerializedProperty _spIterations;
SerializedProperty _spKeepSizeToRootCanvas;

QualityMode qualityMode
{
get
{
if (_customAdvancedOption)
return QualityMode.Custom;

int qualityValue = (_spDesamplingRate.intValue << 0)
+ (_spReductionRate.intValue << 4)
+ (_spFilterMode.intValue << 8)
+ (_spIterations.intValue << 10);

return System.Enum.IsDefined(typeof(QualityMode), qualityValue) ? (QualityMode)qualityValue : QualityMode.Custom;
}
set
{
if (value != QualityMode.Custom)
{
int qualityValue = (int)value;
_spDesamplingRate.intValue = (qualityValue >> 0) & Bits4;
_spReductionRate.intValue = (qualityValue >> 4) & Bits4;
_spFilterMode.intValue = (qualityValue >> 8) & Bits2;
_spIterations.intValue = (qualityValue >> 10) & Bits4;
}
}
}

/// <summary>
/// Draws the desampling rate.
/// </summary>
Expand Down
84 changes: 62 additions & 22 deletions Assets/UIEffect/Editor/UIEffectEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ namespace Coffee.UIExtensions
[CanEditMultipleObjects]
public class UIEffectEditor : Editor
{
ReorderableList roAdditionalShadows;
SerializedProperty spAdditionalShadows;
SerializedProperty spBlurMode;

//################################
// Constant or Static Members.
//################################
/// <summary>
/// Implement this function to make a custom inspector.
/// Draw effect properties.
/// </summary>
public static void DrawEffectProperties(string shaderName, SerializedObject serializedObject)
{
Expand Down Expand Up @@ -93,23 +92,46 @@ public static void DrawEffectProperties(string shaderName, SerializedObject seri
}
}

//################################
// Private Members.
//################################
ReorderableList _roAdditionalShadows;
SerializedProperty _spAdditionalShadows;
SerializedProperty _spBlurMode;
SerializedProperty _spCustomEffect;
SerializedProperty _spEffectMaterial;
SerializedProperty _spEffectColor;
SerializedProperty _spCustomFactorX;
SerializedProperty _spCustomFactorY;
SerializedProperty _spCustomFactorZ;
SerializedProperty _spCustomFactorW;

void OnEnable()
{
spAdditionalShadows = serializedObject.FindProperty("m_AdditionalShadows");
spBlurMode = serializedObject.FindProperty("m_BlurMode");

roAdditionalShadows = new ReorderableList(serializedObject, spAdditionalShadows, true, true, true, true);
roAdditionalShadows.drawElementCallback = DrawElementCallback;
roAdditionalShadows.drawHeaderCallback = (rect) => EditorGUI.LabelField(rect, "Additional Shadows");
roAdditionalShadows.onAddCallback = OnAddCallback;
roAdditionalShadows.elementHeightCallback = ElementHeightCallback;
_spAdditionalShadows = serializedObject.FindProperty("m_AdditionalShadows");
_spBlurMode = serializedObject.FindProperty("m_BlurMode");
_spEffectColor = serializedObject.FindProperty("m_EffectColor");

_spCustomEffect = serializedObject.FindProperty("m_CustomEffect");
_spEffectMaterial = serializedObject.FindProperty("m_EffectMaterial");
var spFactor = serializedObject.FindProperty("m_CustomFactor");
_spCustomFactorX = spFactor.FindPropertyRelative("x");
_spCustomFactorY = spFactor.FindPropertyRelative("y");
_spCustomFactorZ = spFactor.FindPropertyRelative("z");
_spCustomFactorW = spFactor.FindPropertyRelative("w");

_roAdditionalShadows = new ReorderableList(serializedObject, _spAdditionalShadows, true, true, true, true);
_roAdditionalShadows.drawElementCallback = DrawElementCallback;
_roAdditionalShadows.drawHeaderCallback = (rect) => EditorGUI.LabelField(rect, "Additional Shadows");
_roAdditionalShadows.onAddCallback = OnAddCallback;
_roAdditionalShadows.elementHeightCallback = ElementHeightCallback;

}

void OnAddCallback(ReorderableList ro)
{
spAdditionalShadows.InsertArrayElementAtIndex(ro.count);
var element = spAdditionalShadows.GetArrayElementAtIndex(ro.count - 1);
_spAdditionalShadows.InsertArrayElementAtIndex(ro.count);
var element = _spAdditionalShadows.GetArrayElementAtIndex(ro.count - 1);
element.FindPropertyRelative("shadowMode").intValue = (int)UIEffect.ShadowStyle.Shadow;
element.FindPropertyRelative("shadowColor").colorValue = Color.black;
element.FindPropertyRelative("effectDistance").vector2Value = new Vector2(1f, -1f);
Expand All @@ -119,19 +141,19 @@ void OnAddCallback(ReorderableList ro)

float ElementHeightCallback(int index)
{
var element = spAdditionalShadows.GetArrayElementAtIndex(index);
var element = _spAdditionalShadows.GetArrayElementAtIndex(index);
if (element.FindPropertyRelative("shadowMode").intValue == (int)UIEffect.ShadowStyle.None)
return 16;

return (spBlurMode.intValue == (int)UIEffect.BlurMode.None ? 66 : 84) + (EditorGUIUtility.wideMode ? 0 : 18);
return (_spBlurMode.intValue == (int)UIEffect.BlurMode.None ? 66 : 84) + (EditorGUIUtility.wideMode ? 0 : 18);
}

/// <summary>
///
/// </summary>
void DrawElementCallback(Rect rect, int index, bool isActive, bool isFocused)
{
var sp = roAdditionalShadows.serializedProperty.GetArrayElementAtIndex(index);
var sp = _roAdditionalShadows.serializedProperty.GetArrayElementAtIndex(index);

Rect r = new Rect(rect);
r.height = EditorGUIUtility.singleLineHeight;
Expand All @@ -147,7 +169,7 @@ void DrawElementCallback(Rect rect, int index, bool isActive, bool isFocused)
r.y += EditorGUIUtility.wideMode ? r.height : r.height * 2;
EditorGUI.PropertyField(r, sp.FindPropertyRelative("useGraphicAlpha"));

if (spBlurMode.intValue != (int)UIEffect.BlurMode.None)
if (_spBlurMode.intValue != (int)UIEffect.BlurMode.None)
{
r.y += r.height;
EditorGUI.PropertyField(r, sp.FindPropertyRelative("shadowBlur"));
Expand All @@ -160,7 +182,25 @@ void DrawElementCallback(Rect rect, int index, bool isActive, bool isFocused)
public override void OnInspectorGUI()
{
serializedObject.Update();
DrawEffectProperties(UIEffect.shaderName, serializedObject);

// Custom effect.
EditorGUILayout.PropertyField(_spCustomEffect);
if(_spCustomEffect.boolValue)
{
EditorGUILayout.PropertyField(_spEffectMaterial);

EditorGUI.indentLevel++;
EditorGUILayout.Slider(_spCustomFactorX, 0, 1, new GUIContent("Effect Factor X"));
EditorGUILayout.Slider(_spCustomFactorY, 0, 1, new GUIContent("Effect Factor Y"));
EditorGUILayout.Slider(_spCustomFactorZ, 0, 1, new GUIContent("Effect Factor Z"));
EditorGUILayout.Slider(_spCustomFactorW, 0, 1, new GUIContent("Effect Factor W"));
EditorGUILayout.PropertyField(_spEffectColor);
EditorGUI.indentLevel--;
}
else
{
DrawEffectProperties(UIEffect.shaderName, serializedObject);
}

//================
// Shadow setting.
Expand All @@ -176,7 +216,7 @@ public override void OnInspectorGUI()
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_ShadowColor"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_UseGraphicAlpha"));

if (spBlurMode.intValue != (int)UIEffect.BlurMode.None)
if (_spBlurMode.intValue != (int)UIEffect.BlurMode.None)
{
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_ShadowBlur"));
}
Expand All @@ -186,7 +226,7 @@ public override void OnInspectorGUI()
//================
// Additional shadow setting.
//================
roAdditionalShadows.DoLayoutList();
_roAdditionalShadows.DoLayoutList();

serializedObject.ApplyModifiedProperties();

Expand Down
15 changes: 9 additions & 6 deletions Assets/UIEffect/UI-Effect.shader
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ Shader "UI/Hidden/UI-Effect"
#endif

#if defined (UI_TONE) || defined (UI_BLUR)
half3 effectFactor : TEXCOORD2;
half4 effectFactor : TEXCOORD2;
#endif
#if UI_TONE_HUE || UI_TONE_PIXEL
half4 extraFactor : TEXCOORD3;
#endif
};

Expand All @@ -112,10 +115,10 @@ Shader "UI/Hidden/UI-Effect"
#endif

#if UI_TONE_HUE
OUT.effectFactor.y = sin(OUT.effectFactor.x*3.14159265359*2);
OUT.effectFactor.x = cos(OUT.effectFactor.x*3.14159265359*2);
OUT.extraFactor.x = cos(OUT.effectFactor.x*3.14159265359 * 2);
OUT.extraFactor.y = sin(OUT.effectFactor.x*3.14159265359 * 2);
#elif UI_TONE_PIXEL
OUT.effectFactor.xy = max(2, (1-OUT.effectFactor.x) * _MainTex_TexelSize.zw);
OUT.extraFactor.xy = max(2, (1-OUT.effectFactor.x*0.98) * _MainTex_TexelSize.zw);
#endif

#if defined (UI_COLOR)
Expand All @@ -129,7 +132,7 @@ Shader "UI/Hidden/UI-Effect"
fixed4 frag(v2f IN) : SV_Target
{
#if UI_TONE_PIXEL
IN.texcoord = round(IN.texcoord * IN.effectFactor.xy) / IN.effectFactor.xy;
IN.texcoord = round(IN.texcoord * IN.extraFactor.xy) / IN.extraFactor.xy;
#endif

#if defined (UI_BLUR)
Expand All @@ -149,7 +152,7 @@ Shader "UI/Hidden/UI-Effect"
color.rgb = IN.color.rgb;
color.a = color.a * tex2D(_MainTex, IN.texcoord).a + IN.effectFactor.x * 2 - 1;
#elif UI_TONE_HUE
color.rgb = shift_hue(color.rgb, IN.effectFactor.x, IN.effectFactor.y);
color.rgb = shift_hue(color.rgb, IN.extraFactor.x, IN.extraFactor.y);
#elif defined (UI_TONE) & !UI_TONE_CUTOFF
color = ApplyToneEffect(color, IN.effectFactor.x);
#endif
Expand Down
Loading

0 comments on commit aaf79b7

Please sign in to comment.