From 26e0be247e4c36c6002a369a9575b29d86383080 Mon Sep 17 00:00:00 2001 From: mob-sakai Date: Fri, 16 Feb 2024 16:41:39 +0900 Subject: [PATCH] feat: (editor) use stencil outside screen in scene view for development --- .../src/Runtime/Coffee.SoftMaskForUGUI.asmdef | 9 +++++- .../src/Runtime/MaskingShape/MaskingShape.cs | 15 ++++++++++ .../UISoftMaskProjectSettings.cs | 14 ++++++++++ Packages/src/Runtime/SoftMask.cs | 28 +++++++++++++++++++ Packages/src/Runtime/SoftMaskable.cs | 9 ++++++ .../Hidden-TMP_SDF-Mobile-SoftMaskable.shader | 2 +- .../Hidden-TMP_SDF-SoftMaskable.shader | 2 +- .../Hidden-TMP_Sprite-SoftMaskable.shader | 2 +- .../Hidden-TMP_SDF-Mobile-SoftMaskable.shader | 2 +- .../Hidden-TMP_SDF-SoftMaskable.shader | 2 +- .../Hidden-TMP_Sprite-SoftMaskable.shader | 2 +- 11 files changed, 80 insertions(+), 7 deletions(-) diff --git a/Packages/src/Runtime/Coffee.SoftMaskForUGUI.asmdef b/Packages/src/Runtime/Coffee.SoftMaskForUGUI.asmdef index be1ef98..b1cf632 100644 --- a/Packages/src/Runtime/Coffee.SoftMaskForUGUI.asmdef +++ b/Packages/src/Runtime/Coffee.SoftMaskForUGUI.asmdef @@ -1,6 +1,8 @@ { "name": "Coffee.SoftMaskForUGUI", - "references": [], + "references": [ + "Unity.TextMeshPro" + ], "includePlatforms": [], "excludePlatforms": [], "versionDefines": [ @@ -8,6 +10,11 @@ "name": "com.unity.modules.vr", "expression": "1.0.0", "define": "UNITY_MODULE_VR" + }, + { + "name": "com.unity.textmeshpro", + "expression": "1.0.0", + "define": "TMP_ENABLE" } ] } diff --git a/Packages/src/Runtime/MaskingShape/MaskingShape.cs b/Packages/src/Runtime/MaskingShape/MaskingShape.cs index 963bb92..ddfa844 100644 --- a/Packages/src/Runtime/MaskingShape/MaskingShape.cs +++ b/Packages/src/Runtime/MaskingShape/MaskingShape.cs @@ -6,6 +6,9 @@ using UnityEngine.Profiling; using UnityEngine.Rendering; using UnityEngine.UI; +#if TMP_ENABLE +using TMPro; +#endif namespace Coffee.UISoftMask { @@ -375,6 +378,18 @@ internal void DrawSoftMaskBuffer(CommandBuffer cb, int depth) { var texture = graphic.mainTexture; var mesh = _mesh; +#if TMP_ENABLE + if (graphic is TextMeshProUGUI textMeshPro) + { + mesh = textMeshPro.mesh; + texture = textMeshPro.fontMaterial.mainTexture; + } + else if (graphic is TMP_SubMeshUI subMeshUI) + { + mesh = subMeshUI.mesh; + texture = subMeshUI.material.mainTexture; + } +#endif if (!mesh) return; if (!graphic.IsInScreen()) return; diff --git a/Packages/src/Runtime/ProjectSettings/UISoftMaskProjectSettings.cs b/Packages/src/Runtime/ProjectSettings/UISoftMaskProjectSettings.cs index ff780b3..a21286d 100644 --- a/Packages/src/Runtime/ProjectSettings/UISoftMaskProjectSettings.cs +++ b/Packages/src/Runtime/ProjectSettings/UISoftMaskProjectSettings.cs @@ -12,6 +12,12 @@ namespace Coffee.UISoftMask { public class UISoftMaskProjectSettings : PreloadedProjectSettings { + public enum SceneViewBehavior + { + UseStencilOutsideScreen, + SameAsGameView + } + public enum FallbackBehavior { DefaultSoftMaskable, @@ -44,6 +50,9 @@ public enum TransformSensitivity [SerializeField] private bool m_EnabledInEditMode = true; + [SerializeField] + private SceneViewBehavior m_SceneViewBehavior = SceneViewBehavior.UseStencilOutsideScreen; + [Header("Shader")] [SerializeField] private bool m_AutoIncludeShaders = true; @@ -63,7 +72,12 @@ public static bool softMaskEnabled } public static bool useStencil => +#if UNITY_EDITOR + instance.m_SceneViewBehavior == SceneViewBehavior.UseStencilOutsideScreen; +#else false; +#endif + #if UNITY_MODULE_VR public static bool stereoEnabled => softMaskEnabled && instance.m_StereoEnabled && XRSettings.enabled; #else diff --git a/Packages/src/Runtime/SoftMask.cs b/Packages/src/Runtime/SoftMask.cs index 6735ab6..f89cf38 100644 --- a/Packages/src/Runtime/SoftMask.cs +++ b/Packages/src/Runtime/SoftMask.cs @@ -7,6 +7,9 @@ using UnityEngine.Profiling; using UnityEngine.Rendering; using UnityEngine.UI; +#if TMP_ENABLE +using TMPro; +#endif namespace Coffee.UISoftMask { @@ -738,6 +741,18 @@ private void RenderSoftMaskBuffer(CommandBuffer cb, Camera.MonoOrStereoscopicEye Profiler.BeginSample("(SM4UI)[SoftMask] RenderSoftMaskBuffer > ApplyMaterialPropertyBlock"); var mat = graphic.canvasRenderer.GetMaterial(0); var texture = graphic.mainTexture; +#if TMP_ENABLE + { + if (graphic is TextMeshProUGUI textMeshPro) + { + texture = textMeshPro.fontMaterial.mainTexture; + } + else if (graphic is TMP_SubMeshUI subMeshUI) + { + texture = subMeshUI.material.mainTexture; + } + } +#endif SoftMaskUtils.ApplyMaterialPropertyBlock(_mpb, softMaskDepth, texture, softMaskingRange); Profiler.EndSample(); } @@ -763,6 +778,19 @@ private void RenderSoftMaskBuffer(CommandBuffer cb, Camera.MonoOrStereoscopicEye } var mesh = _mesh; +#if TMP_ENABLE + { + if (graphic is TextMeshProUGUI textMeshPro) + { + mesh = textMeshPro.mesh; + _mpb.SetFloat(ShaderPropertyIds.alphaAdd, -0.25f); + } + else if (graphic is TMP_SubMeshUI subMeshUI) + { + mesh = subMeshUI.mesh; + } + } +#endif if (mesh) { diff --git a/Packages/src/Runtime/SoftMaskable.cs b/Packages/src/Runtime/SoftMaskable.cs index e897203..95b7e4f 100755 --- a/Packages/src/Runtime/SoftMaskable.cs +++ b/Packages/src/Runtime/SoftMaskable.cs @@ -3,6 +3,9 @@ using UnityEngine; using UnityEngine.Profiling; using UnityEngine.UI; +#if TMP_ENABLE +using TMPro; +#endif namespace Coffee.UISoftMask { @@ -158,6 +161,12 @@ Material IMaterialModifier.GetModifiedMaterial(Material baseMaterial) ? 0 : (shape.softnessRange.max + shape.softnessRange.min) / 2; } +#if TMP_ENABLE + if (_graphic is TextMeshProUGUI) + { + threshold -= 0.25f; + } +#endif _maskableMaterial.SetFloat(ShaderPropertyIds.alphaClipThreshold, threshold); } diff --git a/Packages/src/Samples~/TextMeshPro Support v3.2 or v4.0~/Hidden-TMP_SDF-Mobile-SoftMaskable.shader b/Packages/src/Samples~/TextMeshPro Support v3.2 or v4.0~/Hidden-TMP_SDF-Mobile-SoftMaskable.shader index 066122d..60e70c5 100644 --- a/Packages/src/Samples~/TextMeshPro Support v3.2 or v4.0~/Hidden-TMP_SDF-Mobile-SoftMaskable.shader +++ b/Packages/src/Samples~/TextMeshPro Support v3.2 or v4.0~/Hidden-TMP_SDF-Mobile-SoftMaskable.shader @@ -244,7 +244,7 @@ SubShader { c *= SoftMask(input.vertex, mul(unity_ObjectToWorld, input.worldPosition)); #if UNITY_UI_ALPHACLIP - clip(c.a - 0.001); + SoftMaskClip(c.a - 0.001); #endif return c; diff --git a/Packages/src/Samples~/TextMeshPro Support v3.2 or v4.0~/Hidden-TMP_SDF-SoftMaskable.shader b/Packages/src/Samples~/TextMeshPro Support v3.2 or v4.0~/Hidden-TMP_SDF-SoftMaskable.shader index 5a2dac9..8f1d4ab 100644 --- a/Packages/src/Samples~/TextMeshPro Support v3.2 or v4.0~/Hidden-TMP_SDF-SoftMaskable.shader +++ b/Packages/src/Samples~/TextMeshPro Support v3.2 or v4.0~/Hidden-TMP_SDF-SoftMaskable.shader @@ -320,7 +320,7 @@ SubShader { faceColor *= SoftMask(input.position, mul(unity_ObjectToWorld, input.worldPosition)); #if UNITY_UI_ALPHACLIP - clip(faceColor.a - 0.001); + SoftMaskClip(faceColor.a - 0.001); #endif return faceColor * input.color.a; diff --git a/Packages/src/Samples~/TextMeshPro Support v3.2 or v4.0~/Hidden-TMP_Sprite-SoftMaskable.shader b/Packages/src/Samples~/TextMeshPro Support v3.2 or v4.0~/Hidden-TMP_Sprite-SoftMaskable.shader index 24336af..6a78a67 100644 --- a/Packages/src/Samples~/TextMeshPro Support v3.2 or v4.0~/Hidden-TMP_Sprite-SoftMaskable.shader +++ b/Packages/src/Samples~/TextMeshPro Support v3.2 or v4.0~/Hidden-TMP_Sprite-SoftMaskable.shader @@ -126,7 +126,7 @@ Shader "Hidden/TextMeshPro/Sprite (SoftMaskable)" color.a *= SoftMask(IN.vertex, mul(unity_ObjectToWorld, IN.worldPosition)); #ifdef UNITY_UI_ALPHACLIP - clip (color.a - 0.001); + SoftMaskClip (color.a - 0.001); #endif return color; diff --git a/Packages/src/Samples~/TextMeshPro Support~/Hidden-TMP_SDF-Mobile-SoftMaskable.shader b/Packages/src/Samples~/TextMeshPro Support~/Hidden-TMP_SDF-Mobile-SoftMaskable.shader index cd14c28..5d4b28b 100644 --- a/Packages/src/Samples~/TextMeshPro Support~/Hidden-TMP_SDF-Mobile-SoftMaskable.shader +++ b/Packages/src/Samples~/TextMeshPro Support~/Hidden-TMP_SDF-Mobile-SoftMaskable.shader @@ -237,7 +237,7 @@ SubShader { c *= SoftMask(input.vertex, mul(unity_ObjectToWorld, input.worldPosition)); #if UNITY_UI_ALPHACLIP - clip(c.a - 0.001); + SoftMaskClip(c.a - 0.001); #endif return c; diff --git a/Packages/src/Samples~/TextMeshPro Support~/Hidden-TMP_SDF-SoftMaskable.shader b/Packages/src/Samples~/TextMeshPro Support~/Hidden-TMP_SDF-SoftMaskable.shader index b2a4373..0594b3f 100644 --- a/Packages/src/Samples~/TextMeshPro Support~/Hidden-TMP_SDF-SoftMaskable.shader +++ b/Packages/src/Samples~/TextMeshPro Support~/Hidden-TMP_SDF-SoftMaskable.shader @@ -315,7 +315,7 @@ SubShader { faceColor *= SoftMask(input.position, mul(unity_ObjectToWorld, input.worldPosition)); #if UNITY_UI_ALPHACLIP - clip(faceColor.a - 0.001); + SoftMaskClip(faceColor.a - 0.001); #endif return faceColor * input.color.a; diff --git a/Packages/src/Samples~/TextMeshPro Support~/Hidden-TMP_Sprite-SoftMaskable.shader b/Packages/src/Samples~/TextMeshPro Support~/Hidden-TMP_Sprite-SoftMaskable.shader index f9123f2..55f2081 100644 --- a/Packages/src/Samples~/TextMeshPro Support~/Hidden-TMP_Sprite-SoftMaskable.shader +++ b/Packages/src/Samples~/TextMeshPro Support~/Hidden-TMP_Sprite-SoftMaskable.shader @@ -121,7 +121,7 @@ Shader "Hidden/TextMeshPro/Sprite (SoftMaskable)" color.a *= SoftMask(IN.vertex, mul(unity_ObjectToWorld, IN.worldPosition)); #ifdef UNITY_UI_ALPHACLIP - clip (color.a - 0.001); + SoftMaskClip (color.a - 0.001); #endif return color;