Skip to content

Commit

Permalink
refactor and format
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Jul 17, 2024
1 parent c0f9da5 commit e8582a0
Show file tree
Hide file tree
Showing 21 changed files with 337 additions and 296 deletions.
2 changes: 1 addition & 1 deletion Packages/src/Editor/AlphaHitTestTargetEditor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using UnityEditor;
using UnityEditor;

namespace Coffee.UISoftMask
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using Coffee.UISoftMaskInternal.AssetModification;
using UnityEditor;
using UnityEngine;

#pragma warning disable CS0612 // Type or member is obsolete
#pragma warning disable CS0612, CS0618 // Type or member is obsolete

namespace Coffee.UISoftMask
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Coffee.UISoftMaskInternal;
using Coffee.UISoftMaskInternal.AssetModification;
using UnityEditor;

#pragma warning disable CS0612 // Type or member is obsolete
#pragma warning disable CS0612, CS0618 // Type or member is obsolete

namespace Coffee.UISoftMask
{
Expand All @@ -14,7 +14,7 @@ protected override bool ModifyComponent(SoftMask softMask, bool dryRun)
if (!dryRun)
{
var go = softMask.gameObject;
softMask.softMaskingRange = new MinMax01(0, softMask.softness);
softMask.softnessRange = new MinMax01(0, softMask.softness);
softMask.softness = -1;
EditorUtility.SetDirty(go);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,16 @@ public static Modifier Create(string path)
var shader = AssetDatabase.LoadAssetAtPath<Shader>(path);
if (!shader || !shader.name.Contains(" (SoftMaskable)")) return null;

var cginc = AssetDatabase.GUIDToAssetPath("c0f7e0d8262ac42cc9ec30a5aea12d72");
return new SoftMaskableShaderModifier
{
path = path,
textModifiers = new ITextModifier[]
{
// color.a *= SoftMask(input.IN.vertex, IN.worldPosition);
// -> color.a *= SoftMask(input.IN.vertex, IN.worldPosition, color.a);
new TextReplaceModifier(
@"#include.*/SoftMask.cginc.*$",
$"#include \"{cginc}\" // Add for soft mask"),
new TextReplaceModifier(
@"^(\s+)#pragma\s+shader_feature(_local)?\s+_+\s+SOFTMASK_EDITOR.*$",
@"$1#pragma multi_compile_local UI_SOFT_MASKABLE UI_SOFT_MASKABLE_EDITOR // Add for soft mask
$1#pragma multi_compile_local _ UI_SOFT_MASKABLE_STEREO // Add for soft mask (stereo)")
@"(([^. \t]+).*\*=\s*SoftMask\(.*[^.][^a])\);.*$",
$"$1, $2.a); // Add for soft mask")
}
};
}
Expand Down
29 changes: 4 additions & 25 deletions Packages/src/Editor/MaskingShapeEditor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using UnityEditor;
using UnityEditor;
using UnityEngine;

namespace Coffee.UISoftMask
Expand All @@ -11,15 +11,15 @@ public class MaskingShapeEditor : Editor
private SerializedProperty _antiAliasingThreshold;
private SerializedProperty _maskingMethod;
private SerializedProperty _showMaskGraphic;
private SerializedProperty _softMaskingRange;
private SerializedProperty _softnessRange;

protected void OnEnable()
{
_maskingMethod = serializedObject.FindProperty("m_MaskingMethod");
_showMaskGraphic = serializedObject.FindProperty("m_ShowMaskGraphic");
_alphaHitTest = serializedObject.FindProperty("m_AlphaHitTest");
_antiAliasingThreshold = serializedObject.FindProperty("m_AntiAliasingThreshold");
_softMaskingRange = serializedObject.FindProperty("m_SoftMaskingRange");
_softnessRange = serializedObject.FindProperty("m_SoftnessRange");
}

public override void OnInspectorGUI()
Expand All @@ -34,17 +34,14 @@ public override void OnInspectorGUI()
EditorGUILayout.EnumPopup(new GUIContent("Masking Mode"), maskingMode);
EditorGUI.EndDisabledGroup();


EditorGUILayout.PropertyField(_maskingMethod);
// EditorGUILayout.PropertyField(_showMaskGraphic);
// EditorGUILayout.PropertyField(_alphaHitTest);

switch (maskingMode)
{
case SoftMask.MaskingMode.SoftMasking:
EditorGUILayout.PropertyField(_showMaskGraphic);
EditorGUILayout.PropertyField(_alphaHitTest);
EditorGUILayout.PropertyField(_softMaskingRange);
EditorGUILayout.PropertyField(_softnessRange);
break;
case SoftMask.MaskingMode.AntiAliasing:
EditorGUILayout.PropertyField(_alphaHitTest);
Expand All @@ -55,24 +52,6 @@ public override void OnInspectorGUI()
break;
}

// AntiAliasing is only available in Mask
// EditorGUI.BeginDisabledGroup(mask is SoftMask);
// {
// EditorGUILayout.PropertyField(_antiAliasing);
// EditorGUI.BeginDisabledGroup(!_antiAliasing.boolValue);
// {
// EditorGUILayout.PropertyField(_antiAliasingThreshold);
// }
// }
// EditorGUI.EndDisabledGroup();
//
// // Softness is only available in SoftMask
// EditorGUI.BeginDisabledGroup(!(mask is SoftMask));
// {
// EditorGUILayout.PropertyField(_softnessRange);
// }
// EditorGUI.EndDisabledGroup();

serializedObject.ApplyModifiedProperties();

// Draw alpha hit test warning
Expand Down
46 changes: 6 additions & 40 deletions Packages/src/Editor/ShaderPreProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using Coffee.UISoftMaskInternal;
using UnityEditor.Rendering;
using UnityEngine;
Expand All @@ -19,21 +19,13 @@ internal class ShaderPreProcessor : ShaderPreProcessorBase
public override void OnProcessShader(Shader shader, ShaderSnippetData snippet,
IList<ShaderCompilerData> data)
{
// If the shader is not SoftMask/softMaskable, do nothing.
var type = GetShaderType(shader);
if (type == ShaderType.None) return;
// If the shader is not SoftMask/SoftMaskable, do nothing.
if (shader.name != "Hidden/UI/SoftMask" && !shader.name.EndsWith(" (SoftMaskable)")) return;

// Remove the 'UI_SOFT_MASKABLE_EDITOR' shader variants.
var editor = new ShaderKeyword(shader, "UI_SOFT_MASKABLE_EDITOR");
// Remove the 'SOFTMASK_EDITOR' shader variants.
var editor = new ShaderKeyword(shader, "SOFTMASK_EDITOR");
StripUnusedVariantsIf(data, d => d.shaderKeywordSet.IsEnabled(editor));

// If the shader is separated soft-maskable, remove non-soft-maskable variants.
if (type == ShaderType.SeparatedSoftMaskable)
{
var softMaskable = new ShaderKeyword(shader, "UI_SOFT_MASKABLE");
StripUnusedVariantsIf(data, d => !d.shaderKeywordSet.IsEnabled(softMaskable));
}

// If strip shader variants is disabled in the project, do nothing.
if (!UISoftMaskProjectSettings.instance.m_StripShaderVariants) return;

Expand All @@ -45,37 +37,11 @@ public override void OnProcessShader(Shader shader, ShaderSnippetData snippet,
return;
}

// If stereo is disabled in the project, remove the 'UI_SOFT_MASKABLE_STEREO' shader variants.
if (!UISoftMaskProjectSettings.stereoEnabled)
{
var stereo = new ShaderKeyword(shader, "UI_SOFT_MASKABLE_STEREO");
StripUnusedVariantsIf(data, d => d.shaderKeywordSet.IsEnabled(stereo));
}

// Log
if (snippet.shaderType == UnityEditor.Rendering.ShaderType.Fragment)
if (snippet.shaderType == ShaderType.Fragment)
{
Log(shader, data, s_IgnoredKeywords);
}
}

private static ShaderType GetShaderType(Shader shader)
{
if (!shader) return ShaderType.None;
var name = shader.name;
if (name == "Hidden/UI/SoftMask") return ShaderType.SoftMask;
if (!name.EndsWith(" (SoftMaskable)")) return ShaderType.None;
return name.StartsWith("Hidden/")
? ShaderType.SeparatedSoftMaskable
: ShaderType.HybridSoftMaskable;
}

private enum ShaderType
{
None,
SoftMask,
HybridSoftMaskable,
SeparatedSoftMaskable
}
}
}
42 changes: 26 additions & 16 deletions Packages/src/Editor/SoftMaskEditor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Coffee.UISoftMaskInternal;
using Coffee.UISoftMaskInternal;
using UnityEditor;
using UnityEditor.U2D;
using UnityEngine;
Expand All @@ -20,7 +20,7 @@ public class SoftMaskEditor : Editor
private SerializedProperty _maskingMode;
private bool _preview;
private SerializedProperty _showMaskGraphic;
private SerializedProperty _softMaskingRange;
private SerializedProperty _softnessRange;

protected void OnEnable()
{
Expand All @@ -29,7 +29,7 @@ protected void OnEnable()
_showMaskGraphic = serializedObject.FindProperty("m_ShowMaskGraphic");
_downSamplingRate = serializedObject.FindProperty("m_DownSamplingRate");
_alphaHitTest = serializedObject.FindProperty("m_AlphaHitTest");
_softMaskingRange = serializedObject.FindProperty("m_SoftMaskingRange");
_softnessRange = serializedObject.FindProperty("m_SoftnessRange");
_preview = EditorPrefs.GetBool(k_PrefsPreview, false);

s_ContentMaskingMode.text = _maskingMode.displayName;
Expand Down Expand Up @@ -57,20 +57,30 @@ public override void OnInspectorGUI()
EditorGUILayout.PropertyField(_maskingMode);
OpenProjectSettings(current);

if (_maskingMode.intValue == (int)SoftMask.MaskingMode.SoftMasking)
switch ((SoftMask.MaskingMode)_maskingMode.intValue)
{
EditorGUILayout.PropertyField(_showMaskGraphic);
EditorGUILayout.PropertyField(_alphaHitTest);
EditorGUILayout.PropertyField(_downSamplingRate);
EditorGUILayout.PropertyField(_softMaskingRange);

FixUiMaskIssue(current); // Fix 'UIMask' issue.
DrawSoftMaskBuffer(); // Preview soft mask buffer.
}
else
{
EditorGUILayout.PropertyField(_alphaHitTest);
EditorGUILayout.PropertyField(_antiAliasingThreshold);
case SoftMask.MaskingMode.SoftMasking:
{
EditorGUILayout.PropertyField(_showMaskGraphic);
EditorGUILayout.PropertyField(_alphaHitTest);
EditorGUILayout.PropertyField(_downSamplingRate);
EditorGUILayout.PropertyField(_softnessRange);

FixUiMaskIssue(current); // Fix 'UIMask' issue.
DrawSoftMaskBuffer(); // Preview soft mask buffer.
break;
}
case SoftMask.MaskingMode.AntiAliasing:
{
EditorGUILayout.PropertyField(_alphaHitTest);
EditorGUILayout.PropertyField(_antiAliasingThreshold);
break;
}
case SoftMask.MaskingMode.Normal:
{
EditorGUILayout.PropertyField(_showMaskGraphic);
break;
}
}

serializedObject.ApplyModifiedProperties();
Expand Down
18 changes: 8 additions & 10 deletions Packages/src/Editor/UISoftMaskProjectSettingsEditor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Coffee.UISoftMaskInternal;
using Coffee.UISoftMaskInternal;
using UnityEditor;
using UnityEngine;

Expand All @@ -7,23 +7,21 @@ namespace Coffee.UISoftMask
[CustomEditor(typeof(UISoftMaskProjectSettings))]
internal class UISoftMaskProjectSettingsEditor : Editor
{
private static readonly GUIContent s_ContentRemove = new GUIContent("-");
private static readonly GUIContent s_ContentReset = new GUIContent("Reset");
private static readonly GUIContent s_ContentIncluded = new GUIContent("Included Shaders");
private static readonly GUIContent s_ContentUpgrade = new GUIContent("Upgrade");
private static readonly GUIContent s_ContentUpgradeButton = new GUIContent("Upgrade All Assets For V2");
private static readonly GUIContent s_ContentUpgradeButton = new GUIContent("Upgrade All Assets V1 to V2");

public override void OnInspectorGUI()
{
var prevEnabled = UISoftMaskProjectSettings.softMaskEnabled;
base.OnInspectorGUI();
if (prevEnabled != UISoftMaskProjectSettings.softMaskEnabled)
{
UISoftMaskProjectSettings.ResetAllSoftMasks();
}

// Draw SoftMask/SoftMaskable/TerminalShape Shaders;
EditorGUILayout.BeginHorizontal();
{
EditorGUILayout.PrefixLabel("Included Shaders");
if (GUILayout.Button("Reset", EditorStyles.miniButton, GUILayout.Width(80)))
EditorGUILayout.PrefixLabel(s_ContentIncluded);
if (GUILayout.Button(s_ContentReset, EditorStyles.miniButton, GUILayout.Width(80)))
{
UISoftMaskProjectSettings.instance.ReloadShaders(true);
}
Expand All @@ -36,7 +34,7 @@ public override void OnInspectorGUI()

EditorGUILayout.BeginHorizontal();
EditorGUILayout.ObjectField(shader, typeof(Shader), false);
if (GUILayout.Button("-", EditorStyles.miniButton, GUILayout.Width(20)))
if (GUILayout.Button(s_ContentRemove, EditorStyles.miniButton, GUILayout.Width(20)))
{
AlwaysIncludedShadersProxy.Remove(shader);
}
Expand Down
2 changes: 1 addition & 1 deletion Packages/src/Runtime/AlphaHitTestTarget.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using UnityEngine;
using UnityEngine;
using UnityEngine.UI;

namespace Coffee.UISoftMask
Expand Down
2 changes: 1 addition & 1 deletion Packages/src/Runtime/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Runtime.CompilerServices;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Coffee.SoftMaskForUGUI.Editor")]
[assembly: InternalsVisibleTo("Coffee.SoftMaskForUGUI.Demo")]
Loading

0 comments on commit e8582a0

Please sign in to comment.