From 649589b1cbd09af540d37a2b9cbb034dc5554e08 Mon Sep 17 00:00:00 2001 From: iizzaya Date: Mon, 14 Oct 2019 23:50:51 +0800 Subject: [PATCH 1/6] Add the parameter to control mask transparency --- .../SoftMaskForUGUI/Scripts/SoftMask.cs | 22 +++++++++++++++++++ .../Shaders/Resources/SoftMask.shader | 3 ++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/SoftMask.cs b/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/SoftMask.cs index fecb688..c35b4fa 100755 --- a/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/SoftMask.cs +++ b/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/SoftMask.cs @@ -53,6 +53,8 @@ namespace Coffee.UIExtensions [SerializeField] 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; + [Tooltip("The transparency of the whole masked graphic.")] + [SerializeField][Range(0f, 1f)] float m_Alpha = 1; [Tooltip("Should the soft mask ignore parent soft masks?")] [SerializeField] bool m_IgnoreParent = false; [Tooltip("Is the soft mask a part of parent soft mask?")] @@ -94,6 +96,23 @@ namespace Coffee.UIExtensions } } } + + /// + /// The transparency of the whole masked graphic. + /// + public float alpha + { + get { return m_Alpha; } + set + { + value = Mathf.Clamp01(value); + if (m_Alpha != value) + { + m_Alpha = value; + hasChanged = true; + } + } + } /// /// Should the soft mask ignore parent soft masks? @@ -271,6 +290,7 @@ namespace Coffee.UIExtensions s_ColorMaskId = Shader.PropertyToID("_ColorMask"); s_MainTexId = Shader.PropertyToID("_MainTex"); s_SoftnessId = Shader.PropertyToID("_Softness"); + s_Alpha = Shader.PropertyToID("_Alpha"); } } s_ActiveSoftMasks.Add(this); @@ -378,6 +398,7 @@ namespace Coffee.UIExtensions static int s_ColorMaskId; static int s_MainTexId; static int s_SoftnessId; + static int s_Alpha; MaterialPropertyBlock _mpb; CommandBuffer _cb; Material _material; @@ -539,6 +560,7 @@ namespace Coffee.UIExtensions sm.material.SetInt(s_ColorMaskId, (int)1 << (3 - _stencilDepth - i)); sm._mpb.SetTexture(s_MainTexId, sm.graphic.mainTexture); sm._mpb.SetFloat(s_SoftnessId, sm.m_Softness); + sm._mpb.SetFloat(s_Alpha, sm.m_Alpha); // Draw mesh. _cb.DrawMesh(sm.mesh, sm.transform.localToWorldMatrix, sm.material, 0, 0, sm._mpb); diff --git a/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Shaders/Resources/SoftMask.shader b/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Shaders/Resources/SoftMask.shader index ced2a86..a8988f0 100644 --- a/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Shaders/Resources/SoftMask.shader +++ b/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Shaders/Resources/SoftMask.shader @@ -19,10 +19,11 @@ SubShader { sampler2D _MainTex; float _Softness; + float _Alpha; fixed4 frag (v2f_img i) : SV_Target { - return saturate(tex2D(_MainTex, i.uv).a/_Softness); + return saturate(tex2D(_MainTex, i.uv).a/_Softness) * _Alpha; } ENDCG } From c7e2fa27106cd2a345748882764c1ed68726f26a Mon Sep 17 00:00:00 2001 From: mob-sakai Date: Tue, 28 Jan 2020 18:46:48 +0900 Subject: [PATCH 2/6] fix: Update softmask not working when canvas component was deactivated Close #66 --- .../Coffee/UIExtensions/SoftMaskForUGUI/Scripts/SoftMask.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/SoftMask.cs b/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/SoftMask.cs index c35b4fa..953cd68 100755 --- a/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/SoftMask.cs +++ b/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/SoftMask.cs @@ -429,9 +429,15 @@ namespace Coffee.UIExtensions continue; var canvas = sm.graphic.canvas; + if(!canvas) + continue; + if (canvas.renderMode == RenderMode.WorldSpace) { var cam = canvas.worldCamera; + if(!cam) + continue; + Matrix4x4 nowsVP = cam.projectionMatrix * cam.worldToCameraMatrix; #if UNITY_2018_1_OR_NEWER From 82c1de9f35a2fdc1c0c8c5824785df6b331f8abe Mon Sep 17 00:00:00 2001 From: mob-sakai Date: Tue, 28 Jan 2020 19:11:35 +0900 Subject: [PATCH 3/6] fix: Projection Matrix check always true when using world space canvas Close #67 --- .../UIExtensions/SoftMaskForUGUI/Scripts/SoftMask.cs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/SoftMask.cs b/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/SoftMask.cs index 953cd68..cf4530d 100755 --- a/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/SoftMask.cs +++ b/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/SoftMask.cs @@ -409,10 +409,8 @@ namespace Coffee.UIExtensions List _children = new List(); bool _hasChanged = false; bool _hasStencilStateChanged = false; -#if !UNITY_2018_1_OR_NEWER static readonly Dictionary s_previousViewProjectionMatrices = new Dictionary (); static readonly Dictionary s_nowViewProjectionMatrices = new Dictionary (); -#endif Material material { get { return _material ? _material : _material = new Material(s_SoftMaskShader ? s_SoftMaskShader : s_SoftMaskShader = Resources.Load("SoftMask")){ hideFlags = HideFlags.HideAndDontSave }; } } @@ -438,18 +436,14 @@ namespace Coffee.UIExtensions if(!cam) continue; - Matrix4x4 nowsVP = cam.projectionMatrix * cam.worldToCameraMatrix; + Matrix4x4 nowVP = cam.projectionMatrix * cam.worldToCameraMatrix; -#if UNITY_2018_1_OR_NEWER - Matrix4x4 previousVP = cam.previousViewProjectionMatrix; -#else Matrix4x4 previousVP = default(Matrix4x4); int id = cam.GetInstanceID (); s_previousViewProjectionMatrices.TryGetValue (id, out previousVP); - s_nowViewProjectionMatrices[id] = nowsVP; -#endif + s_nowViewProjectionMatrices[id] = nowVP; - if (previousVP != nowsVP) + if (previousVP != nowVP) { sm.hasChanged = true; } From 99bceaf6192b86f674976b79867c400d6776c9a8 Mon Sep 17 00:00:00 2001 From: mob-sakai Date: Tue, 28 Jan 2020 19:23:10 +0900 Subject: [PATCH 4/6] fix: Raycast coordinates are incorrect Close #52 --- .../UIExtensions/SoftMaskForUGUI/Scripts/SoftMask.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/SoftMask.cs b/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/SoftMask.cs index cf4530d..a65fbfa 100755 --- a/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/SoftMask.cs +++ b/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/SoftMask.cs @@ -45,6 +45,8 @@ namespace Coffee.UIExtensions new Color(1, 1, 1, 0), }; + static bool s_UVStartsAtTop; + //################################ // Serialize Members. @@ -259,7 +261,9 @@ namespace Coffee.UIExtensions } int x = (int)((softMaskBuffer.width - 1) * Mathf.Clamp01(sp.x / Screen.width)); - int y = (int)((softMaskBuffer.height - 1) * Mathf.Clamp01(sp.y / Screen.height)); + int y = s_UVStartsAtTop + ? (int)((softMaskBuffer.height - 1) * Mathf.Clamp01(sp.y / Screen.height)) + : (int)((softMaskBuffer.height - 1) * (1 - Mathf.Clamp01(sp.y / Screen.height))); return 0.5f < GetPixelValue(x, y, interactions); } @@ -282,6 +286,7 @@ namespace Coffee.UIExtensions // Register. if (s_ActiveSoftMasks.Count == 0) { + s_UVStartsAtTop = SystemInfo.graphicsUVStartsAtTop; Canvas.willRenderCanvases += UpdateMaskTextures; if (s_StencilCompId == 0) From 0c5578bda36b1c530cccfb19456c47182444faf2 Mon Sep 17 00:00:00 2001 From: mob-sakai Date: Tue, 28 Jan 2020 19:23:33 +0900 Subject: [PATCH 5/6] fix: Projection Matrix check always true when using world space canvas --- .../Coffee/UIExtensions/SoftMaskForUGUI/Scripts/SoftMask.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/SoftMask.cs b/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/SoftMask.cs index a65fbfa..af94d4f 100755 --- a/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/SoftMask.cs +++ b/Assets/Coffee/UIExtensions/SoftMaskForUGUI/Scripts/SoftMask.cs @@ -485,15 +485,12 @@ namespace Coffee.UIExtensions } } - -#if !UNITY_2018_1_OR_NEWER s_previousViewProjectionMatrices.Clear (); - foreach (int id in s_previousViewProjectionMatrices.Keys) + foreach (int id in s_nowViewProjectionMatrices.Keys) { s_previousViewProjectionMatrices [id] = s_nowViewProjectionMatrices [id]; } s_nowViewProjectionMatrices.Clear (); -#endif } /// From c192d5714971f809eb06cc05a9885391bcd68ec9 Mon Sep 17 00:00:00 2001 From: mob-sakai Date: Tue, 28 Jan 2020 20:53:37 +0900 Subject: [PATCH 6/6] update documents for v0.9.1 --- .../UIExtensions/SoftMaskForUGUI/CHANGELOG.md | 14 ++++++++++++++ .../UIExtensions/SoftMaskForUGUI/package.json | 2 +- CHANGELOG.md | 14 ++++++++++++++ package.json | 2 +- 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Assets/Coffee/UIExtensions/SoftMaskForUGUI/CHANGELOG.md b/Assets/Coffee/UIExtensions/SoftMaskForUGUI/CHANGELOG.md index 2e33567..4936d9c 100644 --- a/Assets/Coffee/UIExtensions/SoftMaskForUGUI/CHANGELOG.md +++ b/Assets/Coffee/UIExtensions/SoftMaskForUGUI/CHANGELOG.md @@ -1,5 +1,19 @@ # Changelog +## [v0.9.1](https://github.com/mob-sakai/SoftMaskForUGUI/tree/v0.9.1) (2020-01-28) + +[Full Changelog](https://github.com/mob-sakai/SoftMaskForUGUI/compare/v0.9.0...v0.9.1) + +**Implemented enhancements:** + +- Add the parameter to control mask transparency [\#62](https://github.com/mob-sakai/SoftMaskForUGUI/pull/62) ([IIzzaya](https://github.com/IIzzaya)) + +**Fixed bugs:** + +- Projection Matrix check always true when using world space canvas [\#67](https://github.com/mob-sakai/SoftMaskForUGUI/issues/67) +- Update softmask not working when canvas component was deactivated [\#66](https://github.com/mob-sakai/SoftMaskForUGUI/issues/66) +- Raycast coordinates are incorrect [\#52](https://github.com/mob-sakai/SoftMaskForUGUI/issues/52) + ## [v0.9.0](https://github.com/mob-sakai/SoftMaskForUGUI/tree/v0.9.0) (2019-08-27) [Full Changelog](https://github.com/mob-sakai/SoftMaskForUGUI/compare/v0.8.1...v0.9.0) diff --git a/Assets/Coffee/UIExtensions/SoftMaskForUGUI/package.json b/Assets/Coffee/UIExtensions/SoftMaskForUGUI/package.json index a5cd789..c58cf8b 100644 --- a/Assets/Coffee/UIExtensions/SoftMaskForUGUI/package.json +++ b/Assets/Coffee/UIExtensions/SoftMaskForUGUI/package.json @@ -2,7 +2,7 @@ "name": "com.coffee.softmask-for-ugui", "displayName": "Soft Mask For uGUI", "description": "SoftMask is a smooth masking component for uGUI elements in Unity.\nBy using SoftMask instead of default Mask, rounded edges of UI elements can be expressed beautifully.", - "version": "0.9.0", + "version": "0.9.1", "unity": "2017.1", "license": "MIT", "repository": { diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e33567..4936d9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,19 @@ # Changelog +## [v0.9.1](https://github.com/mob-sakai/SoftMaskForUGUI/tree/v0.9.1) (2020-01-28) + +[Full Changelog](https://github.com/mob-sakai/SoftMaskForUGUI/compare/v0.9.0...v0.9.1) + +**Implemented enhancements:** + +- Add the parameter to control mask transparency [\#62](https://github.com/mob-sakai/SoftMaskForUGUI/pull/62) ([IIzzaya](https://github.com/IIzzaya)) + +**Fixed bugs:** + +- Projection Matrix check always true when using world space canvas [\#67](https://github.com/mob-sakai/SoftMaskForUGUI/issues/67) +- Update softmask not working when canvas component was deactivated [\#66](https://github.com/mob-sakai/SoftMaskForUGUI/issues/66) +- Raycast coordinates are incorrect [\#52](https://github.com/mob-sakai/SoftMaskForUGUI/issues/52) + ## [v0.9.0](https://github.com/mob-sakai/SoftMaskForUGUI/tree/v0.9.0) (2019-08-27) [Full Changelog](https://github.com/mob-sakai/SoftMaskForUGUI/compare/v0.8.1...v0.9.0) diff --git a/package.json b/package.json index a5cd789..c58cf8b 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "com.coffee.softmask-for-ugui", "displayName": "Soft Mask For uGUI", "description": "SoftMask is a smooth masking component for uGUI elements in Unity.\nBy using SoftMask instead of default Mask, rounded edges of UI elements can be expressed beautifully.", - "version": "0.9.0", + "version": "0.9.1", "unity": "2017.1", "license": "MIT", "repository": {