refactor: refactor
parent
9961c90914
commit
a5abcdaa20
|
@ -5,7 +5,6 @@ using UnityEditor;
|
|||
using UnityEditor.SceneManagement;
|
||||
using UnityEngine;
|
||||
|
||||
|
||||
namespace Coffee.UISoftMask
|
||||
{
|
||||
internal static class EditorUtils
|
||||
|
@ -36,7 +35,7 @@ namespace Coffee.UISoftMask
|
|||
var so = new SerializedObject(target);
|
||||
so.Update();
|
||||
|
||||
bool oldEnable = target.enabled;
|
||||
var oldEnable = target.enabled;
|
||||
target.enabled = false;
|
||||
|
||||
// Find MonoScript of the specified component.
|
||||
|
|
|
@ -14,9 +14,9 @@ namespace Coffee.UISoftMask
|
|||
[CanEditMultipleObjects]
|
||||
public class SoftMaskEditor : Editor
|
||||
{
|
||||
const string k_PrefsPreview = "SoftMaskEditor_Preview";
|
||||
static readonly List<Graphic> s_Graphics = new List<Graphic>();
|
||||
static bool s_Preview;
|
||||
private const string k_PrefsPreview = "SoftMaskEditor_Preview";
|
||||
private static readonly List<Graphic> s_Graphics = new List<Graphic>();
|
||||
private static bool s_Preview;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
|
@ -35,9 +35,7 @@ namespace Coffee.UISoftMask
|
|||
if (0 < fixTargets.Count)
|
||||
{
|
||||
GUILayout.BeginHorizontal();
|
||||
EditorGUILayout.HelpBox(
|
||||
"There are child Graphics that does not have a SoftMaskable component.\nAdd SoftMaskable component to them.",
|
||||
MessageType.Warning);
|
||||
EditorGUILayout.HelpBox("There are child Graphics that does not have a SoftMaskable component.\nAdd SoftMaskable component to them.", MessageType.Warning);
|
||||
GUILayout.BeginVertical();
|
||||
if (GUILayout.Button("Fix"))
|
||||
{
|
||||
|
@ -69,8 +67,8 @@ namespace Coffee.UISoftMask
|
|||
if (s_Preview)
|
||||
{
|
||||
var tex = current.softMaskBuffer;
|
||||
var width = tex.width * 64 / tex.height;
|
||||
EditorGUI.DrawPreviewTexture(GUILayoutUtility.GetRect(width, 64), tex, null, ScaleMode.ScaleToFit);
|
||||
var width = tex.width * 128 / tex.height;
|
||||
EditorGUI.DrawPreviewTexture(GUILayoutUtility.GetRect(width, 128), tex, null, ScaleMode.ScaleToFit);
|
||||
Repaint();
|
||||
}
|
||||
|
||||
|
@ -81,25 +79,25 @@ namespace Coffee.UISoftMask
|
|||
|
||||
//%%%% Context menu for editor %%%%
|
||||
[MenuItem("CONTEXT/Mask/Convert To SoftMask", true)]
|
||||
static bool _ConvertToSoftMask(MenuCommand command)
|
||||
private static bool _ConvertToSoftMask(MenuCommand command)
|
||||
{
|
||||
return EditorUtils.CanConvertTo<SoftMask>(command.context);
|
||||
}
|
||||
|
||||
[MenuItem("CONTEXT/Mask/Convert To SoftMask", false)]
|
||||
static void ConvertToSoftMask(MenuCommand command)
|
||||
private static void ConvertToSoftMask(MenuCommand command)
|
||||
{
|
||||
EditorUtils.ConvertTo<SoftMask>(command.context);
|
||||
}
|
||||
|
||||
[MenuItem("CONTEXT/Mask/Convert To Mask", true)]
|
||||
static bool _ConvertToMask(MenuCommand command)
|
||||
private static bool _ConvertToMask(MenuCommand command)
|
||||
{
|
||||
return EditorUtils.CanConvertTo<Mask>(command.context);
|
||||
}
|
||||
|
||||
[MenuItem("CONTEXT/Mask/Convert To Mask", false)]
|
||||
static void ConvertToMask(MenuCommand command)
|
||||
private static void ConvertToMask(MenuCommand command)
|
||||
{
|
||||
EditorUtils.ConvertTo<Mask>(command.context);
|
||||
}
|
||||
|
|
|
@ -3,14 +3,17 @@ using UnityEngine;
|
|||
using UnityEngine.UI;
|
||||
using UnityEditor;
|
||||
using System.Linq;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using Object = UnityEngine.Object;
|
||||
using MaskIntr = UnityEngine.SpriteMaskInteraction;
|
||||
using System.IO;
|
||||
|
||||
namespace Coffee.UISoftMask
|
||||
{
|
||||
internal enum MaskInteraction : int
|
||||
{
|
||||
VisibleInsideMask = (1 << 0) + (1 << 2) + (1 << 4) + (1 << 6),
|
||||
VisibleOutsideMask = (2 << 0) + (2 << 2) + (2 << 4) + (2 << 6),
|
||||
Custom = -1,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SoftMaskable editor.
|
||||
/// </summary>
|
||||
|
@ -18,18 +21,16 @@ namespace Coffee.UISoftMask
|
|||
[CanEditMultipleObjects]
|
||||
public class SoftMaskableEditor : Editor
|
||||
{
|
||||
public enum MaskInteraction : int
|
||||
{
|
||||
VisibleInsideMask = (1 << 0) + (1 << 2) + (1 << 4) + (1 << 6),
|
||||
VisibleOutsideMask = (2 << 0) + (2 << 2) + (2 << 4) + (2 << 6),
|
||||
Custom = -1,
|
||||
}
|
||||
private static readonly List<Mask> s_TmpMasks = new List<Mask>();
|
||||
private static GUIContent s_MaskWarning;
|
||||
private SerializedProperty _spMaskInteraction;
|
||||
private bool _custom;
|
||||
|
||||
MaskInteraction maskInteraction
|
||||
private MaskInteraction maskInteraction
|
||||
{
|
||||
get
|
||||
{
|
||||
int value = _spMaskInteraction.intValue;
|
||||
var value = _spMaskInteraction.intValue;
|
||||
return _custom
|
||||
? MaskInteraction.Custom
|
||||
: System.Enum.IsDefined(typeof(MaskInteraction), value)
|
||||
|
@ -46,31 +47,26 @@ namespace Coffee.UISoftMask
|
|||
}
|
||||
}
|
||||
|
||||
bool _custom = false;
|
||||
|
||||
static readonly List<Graphic> s_Graphics = new List<Graphic>();
|
||||
SerializedProperty _spMaskInteraction;
|
||||
List<Mask> tmpMasks = new List<Mask>();
|
||||
static GUIContent s_MaskWarning;
|
||||
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
_spMaskInteraction = serializedObject.FindProperty("m_MaskInteraction");
|
||||
_custom = (maskInteraction == MaskInteraction.Custom);
|
||||
s_MaskWarning = new GUIContent(EditorGUIUtility.FindTexture("console.warnicon.sml"),
|
||||
"This is not a SoftMask component.");
|
||||
}
|
||||
|
||||
if (s_MaskWarning == null)
|
||||
{
|
||||
s_MaskWarning = new GUIContent(EditorGUIUtility.FindTexture("console.warnicon.sml"), "This is not a SoftMask component.");
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawMaskInteractions()
|
||||
{
|
||||
var softMaskable = target as SoftMaskable;
|
||||
if (softMaskable == null) return;
|
||||
|
||||
softMaskable.GetComponentsInParent<Mask>(true, tmpMasks);
|
||||
tmpMasks.RemoveAll(x => !x.enabled);
|
||||
tmpMasks.Reverse();
|
||||
softMaskable.GetComponentsInParent<Mask>(true, s_TmpMasks);
|
||||
s_TmpMasks.RemoveAll(x => !x.enabled);
|
||||
s_TmpMasks.Reverse();
|
||||
|
||||
maskInteraction = (MaskInteraction) EditorGUILayout.EnumPopup("Mask Interaction", maskInteraction);
|
||||
if (!_custom) return;
|
||||
|
@ -80,10 +76,10 @@ namespace Coffee.UISoftMask
|
|||
|
||||
using (var ccs = new EditorGUI.ChangeCheckScope())
|
||||
{
|
||||
int intr0 = DrawMaskInteraction(0);
|
||||
int intr1 = DrawMaskInteraction(1);
|
||||
int intr2 = DrawMaskInteraction(2);
|
||||
int intr3 = DrawMaskInteraction(3);
|
||||
var intr0 = DrawMaskInteraction(0);
|
||||
var intr1 = DrawMaskInteraction(1);
|
||||
var intr2 = DrawMaskInteraction(2);
|
||||
var intr3 = DrawMaskInteraction(3);
|
||||
|
||||
if (ccs.changed)
|
||||
{
|
||||
|
@ -94,24 +90,24 @@ namespace Coffee.UISoftMask
|
|||
EditorGUIUtility.labelWidth = l;
|
||||
}
|
||||
|
||||
|
||||
private int DrawMaskInteraction(int layer)
|
||||
{
|
||||
Mask mask = layer < tmpMasks.Count ? tmpMasks[layer] : null;
|
||||
MaskIntr intr = (MaskIntr) ((_spMaskInteraction.intValue >> layer * 2) & 0x3);
|
||||
var mask = layer < s_TmpMasks.Count ? s_TmpMasks[layer] : null;
|
||||
var intr = (MaskIntr) ((_spMaskInteraction.intValue >> layer * 2) & 0x3);
|
||||
if (!mask)
|
||||
{
|
||||
return (int) intr;
|
||||
}
|
||||
|
||||
using (new EditorGUILayout.HorizontalScope())
|
||||
{
|
||||
EditorGUILayout.LabelField(mask is SoftMask ? GUIContent.none : s_MaskWarning, GUILayout.Width(16));
|
||||
GUILayout.Space(-5);
|
||||
EditorGUILayout.ObjectField("Mask " + layer, mask, typeof(Mask), false);
|
||||
GUILayout.Space(-15);
|
||||
return (int) (MaskIntr) EditorGUILayout.EnumPopup(intr);
|
||||
}
|
||||
GUILayout.BeginHorizontal();
|
||||
EditorGUILayout.LabelField(mask is SoftMask ? GUIContent.none : s_MaskWarning, GUILayout.Width(16));
|
||||
GUILayout.Space(-5);
|
||||
EditorGUILayout.ObjectField("Mask " + layer, mask, typeof(Mask), false);
|
||||
GUILayout.Space(-15);
|
||||
intr = (MaskIntr) EditorGUILayout.EnumPopup(intr);
|
||||
GUILayout.EndHorizontal();
|
||||
|
||||
return (int) intr;
|
||||
}
|
||||
|
||||
public override void OnInspectorGUI()
|
||||
|
@ -124,61 +120,20 @@ namespace Coffee.UISoftMask
|
|||
serializedObject.ApplyModifiedProperties();
|
||||
|
||||
var current = target as SoftMaskable;
|
||||
if (current == null) return;
|
||||
|
||||
current.GetComponentsInChildren<Graphic>(true, s_Graphics);
|
||||
var fixTargets = s_Graphics.Where(x =>
|
||||
x.gameObject != current.gameObject && !x.GetComponent<SoftMaskable>() &&
|
||||
(!x.GetComponent<Mask>() || x.GetComponent<Mask>().showMaskGraphic)).ToList();
|
||||
if (0 < fixTargets.Count)
|
||||
var mask = current.softMask;
|
||||
if (mask) return;
|
||||
|
||||
GUILayout.BeginHorizontal();
|
||||
EditorGUILayout.HelpBox("This is unnecessary SoftMaskable.\nCan't find any SoftMask components above.", MessageType.Warning);
|
||||
if (GUILayout.Button("Remove", GUILayout.Height(40)))
|
||||
{
|
||||
GUILayout.BeginHorizontal();
|
||||
EditorGUILayout.HelpBox(
|
||||
"There are child Graphics that does not have a SoftMaskable component.\nAdd SoftMaskable component to them.",
|
||||
MessageType.Warning);
|
||||
GUILayout.BeginVertical();
|
||||
if (GUILayout.Button("Fix"))
|
||||
{
|
||||
foreach (var p in fixTargets)
|
||||
{
|
||||
p.gameObject.AddComponent<SoftMaskable>();
|
||||
}
|
||||
}
|
||||
|
||||
if (GUILayout.Button("Ping"))
|
||||
{
|
||||
EditorGUIUtility.PingObject(fixTargets[0]);
|
||||
}
|
||||
|
||||
GUILayout.EndVertical();
|
||||
GUILayout.EndHorizontal();
|
||||
DestroyImmediate(current);
|
||||
EditorUtils.MarkPrefabDirty();
|
||||
}
|
||||
|
||||
if (!DetectMask(current.transform.parent))
|
||||
{
|
||||
GUILayout.BeginHorizontal();
|
||||
EditorGUILayout.HelpBox("This is unnecessary SoftMaskable.\nCan't find any SoftMask components above.",
|
||||
MessageType.Warning);
|
||||
if (GUILayout.Button("Remove", GUILayout.Height(40)))
|
||||
{
|
||||
DestroyImmediate(current);
|
||||
|
||||
EditorUtils.MarkPrefabDirty();
|
||||
}
|
||||
|
||||
GUILayout.EndHorizontal();
|
||||
}
|
||||
}
|
||||
|
||||
static bool DetectMask(Transform transform)
|
||||
{
|
||||
while (transform)
|
||||
{
|
||||
if (transform.GetComponent<SoftMask>()) return true;
|
||||
|
||||
transform = transform.parent;
|
||||
}
|
||||
|
||||
return false;
|
||||
GUILayout.EndHorizontal();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,9 +17,20 @@ namespace Coffee.UISoftMask
|
|||
GraphicConnector.FindConnector(graphic).SetMaterialDirty(graphic);
|
||||
}
|
||||
|
||||
public static Shader FindEffectShader(this Graphic graphic)
|
||||
public static T GetComponentInParentEx<T>(this Component component, bool includeInactive = false) where T : MonoBehaviour
|
||||
{
|
||||
return GraphicConnector.FindConnector(graphic).FindEffectShader(graphic);
|
||||
if (!component) return null;
|
||||
var trans = component.transform;
|
||||
|
||||
while (trans)
|
||||
{
|
||||
var c = trans.GetComponent<T>();
|
||||
if (c && (includeInactive || c.isActiveAndEnabled)) return c;
|
||||
|
||||
trans = trans.parent;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -27,10 +38,7 @@ namespace Coffee.UISoftMask
|
|||
public class GraphicConnector
|
||||
{
|
||||
private static readonly List<GraphicConnector> s_Connectors = new List<GraphicConnector>();
|
||||
|
||||
private static readonly Dictionary<Type, GraphicConnector> s_ConnectorMap =
|
||||
new Dictionary<Type, GraphicConnector>();
|
||||
|
||||
private static readonly Dictionary<Type, GraphicConnector> s_ConnectorMap = new Dictionary<Type, GraphicConnector>();
|
||||
private static readonly GraphicConnector s_EmptyConnector = new GraphicConnector();
|
||||
|
||||
#if UNITY_EDITOR
|
||||
|
@ -75,15 +83,6 @@ namespace Coffee.UISoftMask
|
|||
get { return -1; }
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Find effect shader.
|
||||
/// </summary>
|
||||
public virtual Shader FindEffectShader(Graphic graphic)
|
||||
{
|
||||
return Shader.Find("Hidden/UI/SoftMaskable");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The connector is valid for the component.
|
||||
/// </summary>
|
||||
|
|
|
@ -1,42 +1,44 @@
|
|||
using System.Collections.Generic;
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Coffee.UISoftMask
|
||||
{
|
||||
internal class MaterialCache
|
||||
internal class MaterialEntry
|
||||
{
|
||||
public delegate void ModifyAction(Material material, Graphic graphic);
|
||||
public Material material;
|
||||
public int referenceCount;
|
||||
|
||||
static Dictionary<Hash128, MaterialEntry> materialMap = new Dictionary<Hash128, MaterialEntry>();
|
||||
|
||||
private class MaterialEntry
|
||||
public void Release()
|
||||
{
|
||||
public Material material;
|
||||
public int referenceCount;
|
||||
|
||||
public void Release()
|
||||
if (material)
|
||||
{
|
||||
if (material)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (!Application.isPlaying)
|
||||
UnityEngine.Object.DestroyImmediate(material, false);
|
||||
}
|
||||
|
||||
material = null;
|
||||
else
|
||||
#endif
|
||||
UnityEngine.Object.Destroy(material);
|
||||
}
|
||||
|
||||
material = null;
|
||||
}
|
||||
}
|
||||
|
||||
internal static class MaterialCache
|
||||
{
|
||||
static readonly Dictionary<Hash128, MaterialEntry> s_MaterialMap = new Dictionary<Hash128, MaterialEntry>();
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[UnityEditor.InitializeOnLoadMethod]
|
||||
private static void ClearCache()
|
||||
{
|
||||
foreach (var entry in materialMap.Values)
|
||||
foreach (var entry in s_MaterialMap.Values)
|
||||
{
|
||||
entry.Release();
|
||||
}
|
||||
|
||||
materialMap.Clear();
|
||||
s_MaterialMap.Clear();
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -45,7 +47,7 @@ namespace Coffee.UISoftMask
|
|||
if (!hash.isValid) return null;
|
||||
|
||||
MaterialEntry entry;
|
||||
if (!materialMap.TryGetValue(hash, out entry))
|
||||
if (!s_MaterialMap.TryGetValue(hash, out entry))
|
||||
{
|
||||
entry = new MaterialEntry()
|
||||
{
|
||||
|
@ -56,7 +58,7 @@ namespace Coffee.UISoftMask
|
|||
};
|
||||
|
||||
onModify(entry.material);
|
||||
materialMap.Add(hash, entry);
|
||||
s_MaterialMap.Add(hash, entry);
|
||||
}
|
||||
|
||||
entry.referenceCount++;
|
||||
|
@ -67,13 +69,13 @@ namespace Coffee.UISoftMask
|
|||
public static void Unregister(Hash128 hash)
|
||||
{
|
||||
MaterialEntry entry;
|
||||
if (!hash.isValid || !materialMap.TryGetValue(hash, out entry)) return;
|
||||
if (!hash.isValid || !s_MaterialMap.TryGetValue(hash, out entry)) return;
|
||||
//Debug.LogFormat("Unregister: {0}, {1}", hash, entry.referenceCount -1);
|
||||
|
||||
if (--entry.referenceCount > 0) return;
|
||||
|
||||
entry.Release();
|
||||
materialMap.Remove(hash);
|
||||
s_MaterialMap.Remove(hash);
|
||||
//Debug.LogFormat("Unregister: Release Emtry: {0}, {1} (Total: {2})", hash, entry.referenceCount, materialMap.Count);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ namespace Coffee.UISoftMask
|
|||
x8 = 8,
|
||||
}
|
||||
|
||||
static readonly List<SoftMask>[] s_TmpSoftMasks = new List<SoftMask>[]
|
||||
private static readonly List<SoftMask>[] s_TmpSoftMasks = new List<SoftMask>[]
|
||||
{
|
||||
new List<SoftMask>(),
|
||||
new List<SoftMask>(),
|
||||
|
@ -33,7 +33,7 @@ namespace Coffee.UISoftMask
|
|||
new List<SoftMask>(),
|
||||
};
|
||||
|
||||
static readonly Color[] s_ClearColors = new Color[]
|
||||
private static readonly Color[] s_ClearColors = new Color[]
|
||||
{
|
||||
new Color(0, 0, 0, 0),
|
||||
new Color(1, 0, 0, 0),
|
||||
|
@ -41,50 +41,46 @@ namespace Coffee.UISoftMask
|
|||
new Color(1, 1, 1, 0),
|
||||
};
|
||||
|
||||
static bool s_UVStartsAtTop;
|
||||
|
||||
static Shader s_SoftMaskShader;
|
||||
static Texture2D s_ReadTexture;
|
||||
static readonly List<SoftMask> s_ActiveSoftMasks = new List<SoftMask>();
|
||||
static readonly List<SoftMask> s_TempRelatables = new List<SoftMask>();
|
||||
static readonly Dictionary<int, Matrix4x4> s_previousViewProjectionMatrices = new Dictionary<int, Matrix4x4>();
|
||||
static readonly Dictionary<int, Matrix4x4> s_nowViewProjectionMatrices = new Dictionary<int, Matrix4x4>();
|
||||
static int s_StencilCompId;
|
||||
static int s_ColorMaskId;
|
||||
static int s_MainTexId;
|
||||
static int s_SoftnessId;
|
||||
static int s_GameVPId;
|
||||
static int s_GameTVPId;
|
||||
static int s_Alpha;
|
||||
MaterialPropertyBlock _mpb;
|
||||
CommandBuffer _cb;
|
||||
Material _material;
|
||||
RenderTexture _softMaskBuffer;
|
||||
int _stencilDepth;
|
||||
Mesh _mesh;
|
||||
SoftMask _parent;
|
||||
readonly List<SoftMask> _children = new List<SoftMask>();
|
||||
bool _hasChanged = false;
|
||||
bool _hasStencilStateChanged = false;
|
||||
private static bool s_UVStartsAtTop;
|
||||
private static Shader s_SoftMaskShader;
|
||||
private static Texture2D s_ReadTexture;
|
||||
private static readonly List<SoftMask> s_ActiveSoftMasks = new List<SoftMask>();
|
||||
private static readonly List<SoftMask> s_TempRelatables = new List<SoftMask>();
|
||||
private static readonly Dictionary<int, Matrix4x4> s_PreviousViewProjectionMatrices = new Dictionary<int, Matrix4x4>();
|
||||
private static readonly Dictionary<int, Matrix4x4> s_NowViewProjectionMatrices = new Dictionary<int, Matrix4x4>();
|
||||
private static int s_StencilCompId;
|
||||
private static int s_ColorMaskId;
|
||||
private static int s_MainTexId;
|
||||
private static int s_SoftnessId;
|
||||
private static int s_GameVPId;
|
||||
private static int s_GameTVPId;
|
||||
private static int s_Alpha;
|
||||
private MaterialPropertyBlock _mpb;
|
||||
private CommandBuffer _cb;
|
||||
private Material _material;
|
||||
private RenderTexture _softMaskBuffer;
|
||||
private int _stencilDepth;
|
||||
private Mesh _mesh;
|
||||
private SoftMask _parent;
|
||||
private readonly List<SoftMask> _children = new List<SoftMask>();
|
||||
private bool _hasChanged = false;
|
||||
private bool _hasStencilStateChanged = false;
|
||||
|
||||
|
||||
[Tooltip("The desampling rate for soft mask buffer.")] [SerializeField]
|
||||
DesamplingRate m_DesamplingRate = DesamplingRate.None;
|
||||
[SerializeField, Tooltip("The desampling rate for soft mask buffer.")]
|
||||
private DesamplingRate m_DesamplingRate = DesamplingRate.None;
|
||||
|
||||
[Tooltip(
|
||||
"The value used by the soft mask to select the area of influence defined over the soft mask's graphic.")]
|
||||
[SerializeField]
|
||||
[Range(0.01f, 1)]
|
||||
float m_Softness = 1;
|
||||
[SerializeField, Range(0.01f, 1), Tooltip("The value used by the soft mask to select the area of influence defined over the soft mask's graphic.")]
|
||||
private float m_Softness = 1;
|
||||
|
||||
[Tooltip("The transparency of the whole masked graphic.")] [SerializeField] [Range(0f, 1f)]
|
||||
float m_Alpha = 1;
|
||||
[SerializeField, Range(0f, 1f), Tooltip("The transparency of the whole masked graphic.")]
|
||||
private float m_Alpha = 1;
|
||||
|
||||
[Tooltip("Should the soft mask ignore parent soft masks?")] [SerializeField]
|
||||
bool m_IgnoreParent = false;
|
||||
[SerializeField, Tooltip("Should the soft mask ignore parent soft masks?")]
|
||||
private bool m_IgnoreParent = false;
|
||||
|
||||
[Tooltip("Is the soft mask a part of parent soft mask?")] [SerializeField]
|
||||
bool m_PartOfParent = false;
|
||||
[SerializeField, Tooltip("Is the soft mask a part of parent soft mask?")]
|
||||
private bool m_PartOfParent = false;
|
||||
|
||||
|
||||
/// <summary>
|
||||
|
@ -336,7 +332,7 @@ namespace Coffee.UISoftMask
|
|||
_mpb = new MaterialPropertyBlock();
|
||||
_cb = new CommandBuffer();
|
||||
|
||||
graphic.SetVerticesDirty();
|
||||
graphic.SetVerticesDirtyEx();
|
||||
|
||||
base.OnEnable();
|
||||
_hasStencilStateChanged = false;
|
||||
|
@ -411,7 +407,7 @@ namespace Coffee.UISoftMask
|
|||
/// </summary>
|
||||
protected override void OnValidate()
|
||||
{
|
||||
graphic.SetMaterialDirty();
|
||||
graphic.SetMaterialDirtyEx();
|
||||
OnTransformParentChanged();
|
||||
base.OnValidate();
|
||||
_hasStencilStateChanged = false;
|
||||
|
@ -441,8 +437,8 @@ namespace Coffee.UISoftMask
|
|||
var nowVP = cam.projectionMatrix * cam.worldToCameraMatrix;
|
||||
var previousVP = default(Matrix4x4);
|
||||
var id = cam.GetInstanceID();
|
||||
s_previousViewProjectionMatrices.TryGetValue(id, out previousVP);
|
||||
s_nowViewProjectionMatrices[id] = nowVP;
|
||||
s_PreviousViewProjectionMatrices.TryGetValue(id, out previousVP);
|
||||
s_NowViewProjectionMatrices[id] = nowVP;
|
||||
|
||||
if (previousVP != nowVP)
|
||||
{
|
||||
|
@ -478,13 +474,13 @@ namespace Coffee.UISoftMask
|
|||
MaskUtilities.NotifyStencilStateChanged(sm);
|
||||
}
|
||||
|
||||
s_previousViewProjectionMatrices.Clear();
|
||||
foreach (var id in s_nowViewProjectionMatrices.Keys)
|
||||
s_PreviousViewProjectionMatrices.Clear();
|
||||
foreach (var id in s_NowViewProjectionMatrices.Keys)
|
||||
{
|
||||
s_previousViewProjectionMatrices[id] = s_nowViewProjectionMatrices[id];
|
||||
s_PreviousViewProjectionMatrices[id] = s_NowViewProjectionMatrices[id];
|
||||
}
|
||||
|
||||
s_nowViewProjectionMatrices.Clear();
|
||||
s_NowViewProjectionMatrices.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -15,40 +15,37 @@ namespace Coffee.UISoftMask
|
|||
#else
|
||||
[ExecuteInEditMode]
|
||||
# endif
|
||||
[RequireComponent(typeof(Graphic))]
|
||||
public class SoftMaskable : MonoBehaviour, IMaterialModifier, ICanvasRaycastFilter
|
||||
#if UNITY_EDITOR
|
||||
, ISerializationCallbackReceiver
|
||||
# endif
|
||||
{
|
||||
const int kVisibleInside = (1 << 0) + (1 << 2) + (1 << 4) + (1 << 6);
|
||||
const int kVisibleOutside = (2 << 0) + (2 << 2) + (2 << 4) + (2 << 6);
|
||||
static readonly Hash128 k_InvalidHash = new Hash128();
|
||||
private const int kVisibleInside = (1 << 0) + (1 << 2) + (1 << 4) + (1 << 6);
|
||||
private const int kVisibleOutside = (2 << 0) + (2 << 2) + (2 << 4) + (2 << 6);
|
||||
private static readonly Hash128 k_InvalidHash = new Hash128();
|
||||
|
||||
static int s_SoftMaskTexId;
|
||||
static int s_StencilCompId;
|
||||
static int s_MaskInteractionId;
|
||||
static List<SoftMaskable> s_ActiveSoftMaskables;
|
||||
static int[] s_Interactions = new int[4];
|
||||
private static int s_SoftMaskTexId;
|
||||
private static int s_StencilCompId;
|
||||
private static int s_MaskInteractionId;
|
||||
private static List<SoftMaskable> s_ActiveSoftMaskables;
|
||||
private static int[] s_Interactions = new int[4];
|
||||
|
||||
[Tooltip("The graphic will be visible only in areas where no mask is present.")]
|
||||
[System.Obsolete]
|
||||
[HideInInspector]
|
||||
[SerializeField]
|
||||
bool m_Inverse = false;
|
||||
[SerializeField, HideInInspector, System.Obsolete]
|
||||
private bool m_Inverse;
|
||||
|
||||
[Tooltip("The interaction for each masks.")] [HideInInspector] [SerializeField]
|
||||
int m_MaskInteraction = kVisibleInside;
|
||||
[SerializeField, Tooltip("The interaction for each masks."), HideInInspector]
|
||||
private int m_MaskInteraction = kVisibleInside;
|
||||
|
||||
[Tooltip("Use stencil to mask.")] [SerializeField]
|
||||
bool m_UseStencil = false;
|
||||
[SerializeField, Tooltip("Use stencil to mask.")]
|
||||
private bool m_UseStencil;
|
||||
|
||||
[Tooltip("Use soft-masked raycast target.\n\nNote: This option is expensive.")] [SerializeField]
|
||||
bool m_RaycastFilter = false;
|
||||
[SerializeField, Tooltip("Use soft-masked raycast target.\n\nNote: This option is expensive.")]
|
||||
private bool m_RaycastFilter;
|
||||
|
||||
Graphic _graphic = null;
|
||||
SoftMask _softMask = null;
|
||||
Material _maskMaterial = null;
|
||||
Hash128 _effectMaterialHash;
|
||||
private Graphic _graphic;
|
||||
private SoftMask _softMask;
|
||||
private Hash128 _effectMaterialHash;
|
||||
|
||||
/// <summary>
|
||||
/// The graphic will be visible only in areas where no mask is present.
|
||||
|
@ -82,6 +79,11 @@ namespace Coffee.UISoftMask
|
|||
get { return _graphic ? _graphic : _graphic = GetComponent<Graphic>(); }
|
||||
}
|
||||
|
||||
public SoftMask softMask
|
||||
{
|
||||
get { return _softMask ? _softMask : _softMask = this.GetComponentInParentEx<SoftMask>(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Perform material modification in this function.
|
||||
/// </summary>
|
||||
|
@ -98,27 +100,13 @@ namespace Coffee.UISoftMask
|
|||
// If this component is disabled, the material is returned as is.
|
||||
if (!isActiveAndEnabled) return baseMaterial;
|
||||
|
||||
// Find the nearest parent softmask.
|
||||
var parentTransform = transform.parent;
|
||||
while (parentTransform)
|
||||
{
|
||||
var sm = parentTransform.GetComponent<SoftMask>();
|
||||
if (sm && sm.enabled)
|
||||
{
|
||||
_softMask = sm;
|
||||
break;
|
||||
}
|
||||
|
||||
parentTransform = parentTransform.parent;
|
||||
}
|
||||
|
||||
// If the parents do not have a soft mask component, the material is returned as is.
|
||||
if (!_softMask) return baseMaterial;
|
||||
if (!softMask) return baseMaterial;
|
||||
|
||||
// Generate soft maskable material.
|
||||
_effectMaterialHash = new Hash128(
|
||||
(uint) baseMaterial.GetInstanceID(),
|
||||
(uint) _softMask.GetInstanceID(),
|
||||
(uint) softMask.GetInstanceID(),
|
||||
(uint) m_MaskInteraction,
|
||||
(uint) (m_UseStencil ? 1 : 0)
|
||||
);
|
||||
|
@ -130,7 +118,7 @@ namespace Coffee.UISoftMask
|
|||
#if UNITY_EDITOR
|
||||
mat.EnableKeyword("SOFTMASK_EDITOR");
|
||||
#endif
|
||||
mat.SetTexture(s_SoftMaskTexId, _softMask.softMaskBuffer);
|
||||
mat.SetTexture(s_SoftMaskTexId, softMask.softMaskBuffer);
|
||||
mat.SetInt(s_StencilCompId,
|
||||
m_UseStencil ? (int) CompareFunction.Equal : (int) CompareFunction.Always);
|
||||
mat.SetVector(s_MaskInteractionId, new Vector4(
|
||||
|
@ -140,7 +128,6 @@ namespace Coffee.UISoftMask
|
|||
((m_MaskInteraction >> 6) & 0x3)
|
||||
));
|
||||
});
|
||||
_maskMaterial = modifiedMaterial;
|
||||
|
||||
return modifiedMaterial;
|
||||
}
|
||||
|
@ -153,21 +140,21 @@ namespace Coffee.UISoftMask
|
|||
/// <param name="eventCamera">Raycast camera.</param>
|
||||
bool ICanvasRaycastFilter.IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
|
||||
{
|
||||
if (!isActiveAndEnabled || !_softMask)
|
||||
if (!isActiveAndEnabled || !softMask)
|
||||
return true;
|
||||
if (!RectTransformUtility.RectangleContainsScreenPoint(transform as RectTransform, sp, eventCamera))
|
||||
return false;
|
||||
if (!m_RaycastFilter)
|
||||
return true;
|
||||
|
||||
var sm = _softMask;
|
||||
var sm = softMask;
|
||||
for (var i = 0; i < 4; i++)
|
||||
{
|
||||
s_Interactions[i] = sm ? ((m_MaskInteraction >> i * 2) & 0x3) : 0;
|
||||
sm = sm ? sm.parent : null;
|
||||
}
|
||||
|
||||
return _softMask.IsRaycastLocationValid(sp, eventCamera, graphic, s_Interactions);
|
||||
return softMask.IsRaycastLocationValid(sp, eventCamera, graphic, s_Interactions);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -243,7 +230,7 @@ namespace Coffee.UISoftMask
|
|||
if (m_Inverse)
|
||||
{
|
||||
m_Inverse = false;
|
||||
m_MaskInteraction = (2 << 0) + (2 << 2) + (2 << 4) + (2 << 6);
|
||||
m_MaskInteraction = kVisibleOutside;
|
||||
}
|
||||
#pragma warning restore 0612
|
||||
|
||||
|
|
Loading…
Reference in New Issue