Skip to content

Commit

Permalink
feat: (editor) use stencil outside screen in scene view for development
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Feb 16, 2024
1 parent c2f1eee commit ab946e3
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 7 deletions.
9 changes: 8 additions & 1 deletion Packages/src/Runtime/Coffee.SoftMaskForUGUI.asmdef
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
{
"name": "Coffee.SoftMaskForUGUI",
"references": [],
"references": [
"Unity.TextMeshPro"
],
"includePlatforms": [],
"excludePlatforms": [],
"versionDefines": [
{
"name": "com.unity.modules.vr",
"expression": "1.0.0",
"define": "UNITY_MODULE_VR"
},
{
"name": "com.unity.textmeshpro",
"expression": "1.0.0",
"define": "TMP_ENABLE"
}
]
}
15 changes: 15 additions & 0 deletions Packages/src/Runtime/MaskingShape/MaskingShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
using UnityEngine.Profiling;
using UnityEngine.Rendering;
using UnityEngine.UI;
#if TMP_ENABLE
using TMPro;
#endif

namespace Coffee.UISoftMask
{
Expand Down Expand Up @@ -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;

Expand Down
14 changes: 14 additions & 0 deletions Packages/src/Runtime/ProjectSettings/UISoftMaskProjectSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ namespace Coffee.UISoftMask
{
public class UISoftMaskProjectSettings : PreloadedProjectSettings<UISoftMaskProjectSettings>
{
public enum SceneViewBehavior
{
UseStencilOutsideScreen,
SameAsGameView
}

public enum FallbackBehavior
{
DefaultSoftMaskable,
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
28 changes: 28 additions & 0 deletions Packages/src/Runtime/SoftMask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
using UnityEngine.Profiling;
using UnityEngine.Rendering;
using UnityEngine.UI;
#if TMP_ENABLE
using TMPro;
#endif

namespace Coffee.UISoftMask
{
Expand Down Expand Up @@ -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();
}
Expand All @@ -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)
{
Expand Down
9 changes: 9 additions & 0 deletions Packages/src/Runtime/SoftMaskable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
using UnityEngine;
using UnityEngine.Profiling;
using UnityEngine.UI;
#if TMP_ENABLE
using TMPro;
#endif

namespace Coffee.UISoftMask
{
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ab946e3

Please sign in to comment.