Skip to content

Commit

Permalink
Merge From UIEffect v2.4.0
Browse files Browse the repository at this point in the history
----
close #3 : Feature: Iteration count
close #4 : When iteration count is even, the result image is flipped vertically on Windows
close #5 : Keep canvas size
close #6 : Add `Quality Type` to easy setup
  • Loading branch information
Takashi Sakai committed May 23, 2018
1 parent 265e61a commit 079a04d
Show file tree
Hide file tree
Showing 9 changed files with 825 additions and 148 deletions.
4 changes: 2 additions & 2 deletions Assets/StaticBluredScreen/Demo/Demo_StaticBluredScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ public class Demo_StaticBluredScreen : MonoBehaviour
public void ToggleBlured(bool flag)
{
if (flag)
m_StaticBluredScreen.UpdateTexture();
m_StaticBluredScreen.Capture();
else
m_StaticBluredScreen.texture = null;
}

public void UpdateTex()
{
m_StaticBluredScreen.UpdateTexture();
m_StaticBluredScreen.Capture();
}

public void OpenDialog(Animator anim)
Expand Down
34 changes: 29 additions & 5 deletions Assets/StaticBluredScreen/Demo/Demo_StaticBluredScreen.unity
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,15 @@ MonoBehaviour:
y: 0
width: 1
height: 1
m_Shader: {fileID: 4800000, guid: c7bb8dee750384818a922a942cd437f9, type: 3}
m_Blur: 1
m_BlurMode: 3
m_DesamplingRate: 1
m_ReductionRate: 1
m_FilterMode: 1
m_EffectMaterial: {fileID: 21807421563859802, guid: bfc07c11f98194f59b81443af0968a5f,
type: 2}
m_Iterations: 3
m_KeepCanvasSize: 1
--- !u!222 &602972759
CanvasRenderer:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -1453,7 +1461,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 1, y: 1}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: -0.000015258789, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 160, y: 20}
m_Pivot: {x: 1, y: 1}
--- !u!114 &1268722441
Expand Down Expand Up @@ -2363,7 +2371,7 @@ ParticleSystem:
moveWithTransform: 0
moveWithCustomTransform: {fileID: 0}
scalingMode: 1
randomSeed: 1848250909
randomSeed: 430800314
InitialModule:
serializedVersion: 3
enabled: 1
Expand Down Expand Up @@ -5019,7 +5027,15 @@ MonoBehaviour:
y: 0
width: 1
height: 1
m_Shader: {fileID: 4800000, guid: c7bb8dee750384818a922a942cd437f9, type: 3}
m_Blur: 1
m_BlurMode: 3
m_DesamplingRate: 1
m_ReductionRate: 1
m_FilterMode: 1
m_EffectMaterial: {fileID: 21807421563859802, guid: bfc07c11f98194f59b81443af0968a5f,
type: 2}
m_Iterations: 3
m_KeepCanvasSize: 1
--- !u!222 &1869998896
CanvasRenderer:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -5239,7 +5255,15 @@ MonoBehaviour:
y: 0
width: 1
height: 1
m_Shader: {fileID: 4800000, guid: c7bb8dee750384818a922a942cd437f9, type: 3}
m_Blur: 1
m_BlurMode: 3
m_DesamplingRate: 1
m_ReductionRate: 2
m_FilterMode: 1
m_EffectMaterial: {fileID: 21807421563859802, guid: bfc07c11f98194f59b81443af0968a5f,
type: 2}
m_Iterations: 3
m_KeepCanvasSize: 1
--- !u!222 &2009106049
CanvasRenderer:
m_ObjectHideFlags: 0
Expand Down
9 changes: 9 additions & 0 deletions Assets/StaticBluredScreen/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

226 changes: 226 additions & 0 deletions Assets/StaticBluredScreen/Editor/UIEffectCapturedImageEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
using UnityEditor;
using UnityEditor.UI;
using UnityEngine;
using DesamplingRate = StaticBluredScreen.DesamplingRate;

/// <summary>
/// StaticBluredScreen editor.
/// </summary>
[CustomEditor(typeof(StaticBluredScreen))]
[CanEditMultipleObjects]
public class StaticBluredScreenEditor : 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>
public override void OnInspectorGUI()
{
serializedObject.Update();

//================
// Basic properties.
//================
EditorGUILayout.PropertyField(_spTexture);
EditorGUILayout.PropertyField(_spColor);
EditorGUILayout.PropertyField(_spRaycastTarget);

//================
// Capture effect.
//================
GUILayout.Space(10);
EditorGUILayout.LabelField("Capture Effect", EditorStyles.boldLabel);
DrawEffectProperties(StaticBluredScreen.shaderName, serializedObject);

//================
// Advanced option.
//================
GUILayout.Space(10);
EditorGUILayout.LabelField("Advanced Option", EditorStyles.boldLabel);

EditorGUI.BeginChangeCheck();
QualityMode quality = qualityMode;
quality = (QualityMode)EditorGUILayout.EnumPopup("Quality Mode", quality);
if (EditorGUI.EndChangeCheck())
{
_customAdvancedOption = (quality == QualityMode.Custom);
qualityMode = quality;
}

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

// Debug.
using (new EditorGUILayout.HorizontalScope(EditorStyles.helpBox))
{
GUILayout.Label("Debug");

if (GUILayout.Button("Capture", "ButtonLeft"))
UpdateTexture(true);

EditorGUI.BeginDisabledGroup(!(target as StaticBluredScreen).capturedTexture);
if (GUILayout.Button("Release", "ButtonRight"))
UpdateTexture(false);
EditorGUI.EndDisabledGroup();
}
}

//################################
// 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>
/// Draw effect properties.
/// </summary>
public static void DrawEffectProperties(string shaderName, SerializedObject serializedObject)
{
bool changed = false;

//================
// Effect material.
//================
var spMaterial = serializedObject.FindProperty("m_EffectMaterial");
EditorGUI.BeginDisabledGroup(true);
EditorGUILayout.PropertyField(spMaterial);
EditorGUI.EndDisabledGroup();

//================
// Blur setting.
//================
var spBlurMode = serializedObject.FindProperty("m_BlurMode");
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(spBlurMode);
changed |= EditorGUI.EndChangeCheck();

// When blur is enable, show parameters.
if (spBlurMode.intValue != (int)StaticBluredScreen.BlurMode.None)
{
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_Blur"));
EditorGUI.indentLevel--;
}

// Set effect material.
if (!serializedObject.isEditingMultipleObjects && spBlurMode.intValue == 0)
{
spMaterial.objectReferenceValue = null;
}
else if (changed || !serializedObject.isEditingMultipleObjects)
{
spMaterial.objectReferenceValue = StaticBluredScreen.GetOrGenerateMaterialVariant(Shader.Find(shaderName),
(StaticBluredScreen.BlurMode)spBlurMode.intValue
);
}
}

/// <summary>
/// Draws the desampling rate.
/// </summary>
void DrawDesamplingRate(SerializedProperty sp)
{
using (new EditorGUILayout.HorizontalScope())
{
EditorGUILayout.PropertyField(sp);
int w, h;
(target as StaticBluredScreen).GetDesamplingSize((DesamplingRate)sp.intValue, out w, out h);
GUILayout.Label(string.Format("{0}x{1}", w, h), EditorStyles.miniLabel);
}
}

/// <summary>
/// Updates the texture.
/// </summary>
void UpdateTexture(bool capture)
{
var current = target as StaticBluredScreen;
bool enable = current.enabled;
current.enabled = false;
current.Release();
if (capture)
current.Capture();

EditorApplication.delayCall += () => current.enabled = enable;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 079a04d

Please sign in to comment.