diff --git a/Packages/SoftMaskForUGUI/Scripts/Editor/SoftMaskEditor.cs b/Packages/SoftMaskForUGUI/Scripts/Editor/SoftMaskEditor.cs index ce32fd7..be40b77 100644 --- a/Packages/SoftMaskForUGUI/Scripts/Editor/SoftMaskEditor.cs +++ b/Packages/SoftMaskForUGUI/Scripts/Editor/SoftMaskEditor.cs @@ -14,6 +14,7 @@ namespace Coffee.UISoftMask [CanEditMultipleObjects] public class SoftMaskEditor : Editor { + private const int k_PreviewSize = 128; private const string k_PrefsPreview = "SoftMaskEditor_Preview"; private static readonly List s_Graphics = new List(); private static bool s_Preview; @@ -29,9 +30,10 @@ namespace Coffee.UISoftMask var current = target as SoftMask; current.GetComponentsInChildren(true, s_Graphics); - var fixTargets = s_Graphics.Where(x => - x.gameObject != current.gameObject && !x.GetComponent() && - (!x.GetComponent() || x.GetComponent().showMaskGraphic)).ToList(); + var fixTargets = s_Graphics + .Where(x => x.gameObject != current.gameObject) + .Where(x => !x.GetComponent() && (!x.GetComponent() || x.GetComponent().showMaskGraphic)) + .ToList(); if (0 < fixTargets.Count) { GUILayout.BeginHorizontal(); @@ -58,7 +60,7 @@ namespace Coffee.UISoftMask // Preview buffer. GUILayout.BeginVertical(EditorStyles.helpBox); - if (s_Preview != (s_Preview = EditorGUILayout.ToggleLeft("Preview Buffer", s_Preview))) + if (s_Preview != (s_Preview = EditorGUILayout.ToggleLeft("Preview Soft Mask Buffer", s_Preview))) { EditorPrefs.SetBool(k_PrefsPreview, s_Preview); } @@ -66,10 +68,11 @@ namespace Coffee.UISoftMask if (s_Preview) { var tex = current.softMaskBuffer; - var width = tex.width * 128 / tex.height; - EditorGUI.DrawPreviewTexture(GUILayoutUtility.GetRect(width, 128), tex, null, ScaleMode.ScaleToFit); + var width = tex.width * k_PreviewSize / tex.height; + EditorGUI.DrawPreviewTexture(GUILayoutUtility.GetRect(width, k_PreviewSize), tex, null, ScaleMode.ScaleToFit); Repaint(); } + GUILayout.EndVertical(); } diff --git a/Packages/SoftMaskForUGUI/Scripts/SoftMask.cs b/Packages/SoftMaskForUGUI/Scripts/SoftMask.cs index 4439477..6f40c31 100644 --- a/Packages/SoftMaskForUGUI/Scripts/SoftMask.cs +++ b/Packages/SoftMaskForUGUI/Scripts/SoftMask.cs @@ -76,7 +76,7 @@ namespace Coffee.UISoftMask [SerializeField, Range(0f, 1f), Tooltip("The transparency of the whole masked graphic.")] private float m_Alpha = 1; - [SerializeField, Tooltip("Should the soft mask ignore parent soft masks?")] + [Header("Advanced Options")] [SerializeField, Tooltip("Should the soft mask ignore parent soft masks?")] private bool m_IgnoreParent = false; [SerializeField, Tooltip("Is the soft mask a part of parent soft mask?")] @@ -187,8 +187,7 @@ namespace Coffee.UISoftMask if (!_softMaskBuffer) { - _softMaskBuffer = RenderTexture.GetTemporary(w, h, 0, RenderTextureFormat.ARGB32, - RenderTextureReadWrite.Default); + _softMaskBuffer = RenderTexture.GetTemporary(w, h, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default); hasChanged = true; _hasStencilStateChanged = true; } @@ -242,7 +241,6 @@ namespace Coffee.UISoftMask } - Material material { get @@ -251,9 +249,8 @@ namespace Coffee.UISoftMask ? _material : _material = new Material(s_SoftMaskShader - ? s_SoftMaskShader - : s_SoftMaskShader = Resources.Load("SoftMask")) - {hideFlags = HideFlags.HideAndDontSave}; + ? s_SoftMaskShader + : s_SoftMaskShader = Resources.Load("SoftMask")) {hideFlags = HideFlags.HideAndDontSave}; } } @@ -558,8 +555,7 @@ namespace Coffee.UISoftMask Profiler.BeginSample("UpdateMaskTexture"); - _stencilDepth = - MaskUtilities.GetStencilDepth(transform, MaskUtilities.FindRootSortOverrideCanvas(transform)); + _stencilDepth = MaskUtilities.GetStencilDepth(transform, MaskUtilities.FindRootSortOverrideCanvas(transform)); // Collect children soft masks. Profiler.BeginSample("Collect children soft masks"); @@ -570,7 +566,7 @@ namespace Coffee.UISoftMask var count = s_TmpSoftMasks[depth].Count; for (var i = 0; i < count; i++) { - List children = s_TmpSoftMasks[depth][i]._children; + var children = s_TmpSoftMasks[depth][i]._children; var childCount = children.Count; for (var j = 0; j < childCount; j++) { @@ -598,11 +594,11 @@ namespace Coffee.UISoftMask var cam = c.worldCamera ?? Camera.main; if (c && c.renderMode != RenderMode.ScreenSpaceOverlay && cam) { - _cb.SetViewProjectionMatrices(cam.worldToCameraMatrix, - GL.GetGPUProjectionMatrix(cam.projectionMatrix, false)); + var p = GL.GetGPUProjectionMatrix(cam.projectionMatrix, false); + _cb.SetViewProjectionMatrices(cam.worldToCameraMatrix, p); #if UNITY_EDITOR - var pv = GL.GetGPUProjectionMatrix(cam.projectionMatrix, false) * cam.worldToCameraMatrix; + var pv = p * cam.worldToCameraMatrix; _cb.SetGlobalMatrix(s_GameVPId, pv); _cb.SetGlobalMatrix(s_GameTVPId, pv); #endif @@ -611,19 +607,16 @@ namespace Coffee.UISoftMask { var pos = c.transform.position; var vm = Matrix4x4.TRS(new Vector3(-pos.x, -pos.y, -1000), Quaternion.identity, new Vector3(1, 1, -1f)); - var pm = Matrix4x4.TRS(new Vector3(0, 0, -1), Quaternion.identity, - new Vector3(1 / pos.x, 1 / pos.y, -2 / 10000f)); + var pm = Matrix4x4.TRS(new Vector3(0, 0, -1), Quaternion.identity, new Vector3(1 / pos.x, 1 / pos.y, -2 / 10000f)); _cb.SetViewProjectionMatrices(vm, pm); #if UNITY_EDITOR var scale = c.transform.localScale.x; var size = (c.transform as RectTransform).sizeDelta; - _cb.SetGlobalMatrix(s_GameVPId, - Matrix4x4.TRS(new Vector3(0, 0, 0.5f), Quaternion.identity, - new Vector3(2 / size.x, 2 / size.y, 0.0005f * scale))); - _cb.SetGlobalMatrix(s_GameTVPId, - Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, - new Vector3(1 / pos.x, 1 / pos.y, -2 / 2000f)) * Matrix4x4.Translate(-pos)); + var gameVp = Matrix4x4.TRS(new Vector3(0, 0, 0.5f), Quaternion.identity, new Vector3(2 / size.x, 2 / size.y, 0.0005f * scale)); + var gameTvp = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(1 / pos.x, 1 / pos.y, -2 / 2000f)) * Matrix4x4.Translate(-pos); + _cb.SetGlobalMatrix(s_GameVPId, gameVp); + _cb.SetGlobalMatrix(s_GameTVPId, gameTvp); #endif } @@ -640,8 +633,7 @@ namespace Coffee.UISoftMask if (i != 0) { - sm._stencilDepth = MaskUtilities.GetStencilDepth(sm.transform, - MaskUtilities.FindRootSortOverrideCanvas(sm.transform)); + sm._stencilDepth = MaskUtilities.GetStencilDepth(sm.transform, MaskUtilities.FindRootSortOverrideCanvas(sm.transform)); } // Set material property. @@ -700,6 +692,7 @@ namespace Coffee.UISoftMask private static void ReleaseRt(ref RenderTexture tmpRT) { if (!tmpRT) return; + tmpRT.Release(); RenderTexture.ReleaseTemporary(tmpRT); tmpRT = null; @@ -712,13 +705,13 @@ namespace Coffee.UISoftMask private static void ReleaseObject(Object obj) { if (!obj) return; + #if UNITY_EDITOR if (!Application.isPlaying) DestroyImmediate(obj); else #endif Destroy(obj); - obj = null; } diff --git a/Packages/SoftMaskForUGUI/Scripts/SoftMaskable.cs b/Packages/SoftMaskForUGUI/Scripts/SoftMaskable.cs index 9659268..4c8157c 100755 --- a/Packages/SoftMaskForUGUI/Scripts/SoftMaskable.cs +++ b/Packages/SoftMaskForUGUI/Scripts/SoftMaskable.cs @@ -133,8 +133,7 @@ namespace Coffee.UISoftMask mat.EnableKeyword("SOFTMASK_EDITOR"); #endif mat.SetTexture(s_SoftMaskTexId, softMask.softMaskBuffer); - mat.SetInt(s_StencilCompId, - m_UseStencil ? (int) CompareFunction.Equal : (int) CompareFunction.Always); + mat.SetInt(s_StencilCompId, m_UseStencil ? (int) CompareFunction.Equal : (int) CompareFunction.Always); var root = MaskUtilities.FindRootSortOverrideCanvas(transform); var stencil = MaskUtilities.GetStencilDepth(transform, root); @@ -179,15 +178,19 @@ namespace Coffee.UISoftMask for (var i = 0; i < 4; i++) { if (!sm) break; + s_Interactions[i] = sm ? ((m_MaskInteraction >> i * 2) & 0x3) : 0; var interaction = s_Interactions[i] == 1; - var inRect = RectTransformUtility.RectangleContainsScreenPoint(sm.transform as RectTransform, sp, eventCamera); + var rt = sm.transform as RectTransform; + var inRect = RectTransformUtility.RectangleContainsScreenPoint(rt, sp, eventCamera); if (!sm.ignoreSelfGraphic && interaction != inRect) return false; foreach (var child in sm._children) { - if (!child) break; - var inRectChild = RectTransformUtility.RectangleContainsScreenPoint(child.transform as RectTransform, sp, eventCamera); + if (!child) continue; + + var childRt = child.transform as RectTransform; + var inRectChild = RectTransformUtility.RectangleContainsScreenPoint(childRt, sp, eventCamera); if (!child.ignoreSelfGraphic && interaction != inRectChild) return false; } @@ -201,7 +204,7 @@ namespace Coffee.UISoftMask /// /// Set the interaction for each mask. /// - public void SetMaskInteraction(SpriteMaskInteraction intr) + public void SetMaskInteraction(MaskIntr intr) { SetMaskInteraction(intr, intr, intr, intr); } @@ -209,8 +212,7 @@ namespace Coffee.UISoftMask /// /// Set the interaction for each mask. /// - public void SetMaskInteraction(SpriteMaskInteraction layer0, SpriteMaskInteraction layer1, - SpriteMaskInteraction layer2, SpriteMaskInteraction layer3) + public void SetMaskInteraction(MaskIntr layer0, MaskIntr layer1, MaskIntr layer2, MaskIntr layer3) { m_MaskInteraction = (int) layer0 + ((int) layer1 << 2) + ((int) layer2 << 4) + ((int) layer3 << 6); graphic.SetMaterialDirtyEx(); @@ -278,12 +280,14 @@ namespace Coffee.UISoftMask var current = this; UnityEditor.EditorApplication.delayCall += () => { - if (current && graphic && graphic.material && graphic.material.shader && - graphic.material.shader.name == "Hidden/UI/Default (SoftMaskable)") - { - graphic.material = null; - graphic.SetMaterialDirtyEx(); - } + if (!current) return; + if (!graphic) return; + if (!graphic.material) return; + if (!graphic.material.shader) return; + if (graphic.material.shader.name != "Hidden/UI/Default (SoftMaskable)") return; + + graphic.material = null; + graphic.SetMaterialDirtyEx(); }; } #endif