Skip to content

Commit

Permalink
fix: subtract mode MaskingShape does not display correctly in the e…
Browse files Browse the repository at this point in the history
…ditor
  • Loading branch information
mob-sakai committed Sep 13, 2024
1 parent 82c38dd commit f83a647
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
35 changes: 23 additions & 12 deletions Packages/src/Runtime/SoftMaskable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ namespace Coffee.UISoftMask
[ExecuteAlways]
public class SoftMaskable : MonoBehaviour, IMaterialModifier, IMaskable
{
#if UNITY_EDITOR
private static readonly int s_AlphaClipThreshold = Shader.PropertyToID("_AlphaClipThreshold");
private static readonly int s_MaskingShapeSubtract = Shader.PropertyToID("_MaskingShapeSubtract");
#endif
private Action _checkGraphic;
private MaskableGraphic _graphic;
private Material _maskableMaterial;
Expand Down Expand Up @@ -112,32 +115,40 @@ Material IMaterialModifier.GetModifiedMaterial(Material baseMaterial)
Profiler.BeginSample("(SM4UI)[SoftMaskable] GetModifiedMaterial");
var isStereo = UISoftMaskProjectSettings.stereoEnabled && _graphic.canvas.IsStereoCanvas();
var useStencil = UISoftMaskProjectSettings.useStencilOutsideScreen;
var hash = new Hash128(
(uint)baseMaterial.GetInstanceID(),
(uint)_softMask.softMaskBuffer.GetInstanceID(),
(uint)(_stencilBits + (isStereo ? 1 << 8 : 0) + (useStencil ? 1 << 9 : 0) + (_softMaskDepth << 10)),
0);
MaterialRepository.Get(hash, ref _maskableMaterial,
x => SoftMaskUtils.CreateSoftMaskable(x.baseMaterial, x.softMaskBuffer, x._softMaskDepth,
x._stencilBits, x.isStereo, UISoftMaskProjectSettings.fallbackBehavior),
(baseMaterial, _softMask.softMaskBuffer, _softMaskDepth, _stencilBits, isStereo));
Profiler.EndSample();

var localId = 0u;
#if UNITY_EDITOR
var threshold = 0f;
if (UISoftMaskProjectSettings.useStencilOutsideScreen)
var subtract = false;
if (useStencil)
{
if (TryGetComponent(out MaskingShape s) && s.maskingMethod == MaskingShape.MaskingMethod.Subtract)
{
threshold = s.softnessRange.average;
subtract = true;
}
else if (_softMask)
{
threshold = _softMask.softnessRange.average;
}
}

localId = (uint)(Mathf.Clamp01(threshold) * (1 << 8) + (subtract ? 1 << 9 : 0));
#endif

var hash = new Hash128(
(uint)baseMaterial.GetInstanceID(),
(uint)_softMask.softMaskBuffer.GetInstanceID(),
(uint)(_stencilBits + (isStereo ? 1 << 8 : 0) + (useStencil ? 1 << 9 : 0) + (_softMaskDepth << 10)),
localId);
MaterialRepository.Get(hash, ref _maskableMaterial,
x => SoftMaskUtils.CreateSoftMaskable(x.baseMaterial, x.softMaskBuffer, x._softMaskDepth,
x._stencilBits, x.isStereo, UISoftMaskProjectSettings.fallbackBehavior),
(baseMaterial, _softMask.softMaskBuffer, _softMaskDepth, _stencilBits, isStereo));
Profiler.EndSample();

#if UNITY_EDITOR
_maskableMaterial.SetFloat(s_AlphaClipThreshold, threshold);
_maskableMaterial.SetInt(s_MaskingShapeSubtract, subtract ? 1 : 0);
#endif

return _maskableMaterial;
Expand Down
16 changes: 12 additions & 4 deletions Packages/src/Shaders/SoftMask.cginc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ uniform float4x4 _GameVP_2;
uniform float4x4 _GameTVP_2;
uniform int _SoftMaskInGameView;
uniform int _SoftMaskInSceneView;
uniform int _MaskingShapeSubtract;

float Approximately(float4x4 a, float4x4 b)
{
Expand Down Expand Up @@ -87,12 +88,19 @@ float SoftMaskSample(float2 uv, float a)
lerp(mask, half4(1, 1, 1, 1) - mask, _SoftMaskColor - half4(1, 1, 1, 1)),
_SoftMaskColor));
#if SOFTMASK_EDITOR
int inScreen = step(0, uv.x) * step(uv.x, 1) * step(0, uv.y) * step(uv.y, 1);
alpha = lerp(_SoftMaskOutsideColor, alpha, inScreen);
if (inScreen == 0)
int inScreen = step(0, uv.x) * step(uv.x, 1) * step(0, uv.y) * step(uv.y, 1);
alpha = lerp(_SoftMaskOutsideColor, alpha, inScreen);
#ifdef UNITY_UI_ALPHACLIP
if (_MaskingShapeSubtract == 1)
{
if (_SoftMaskInSceneView == 1)
{
clip (a * alpha.x * alpha.y * alpha.z * alpha.w - _AlphaClipThreshold - 0.001);
clip (a - _AlphaClipThreshold - 0.001);
return 1;
}
clip (a * alpha.x * alpha.y * alpha.z * alpha.w - _AlphaClipThreshold - 0.001);
}
#endif
#endif

return alpha.x * alpha.y * alpha.z * alpha.w;
Expand Down

0 comments on commit f83a647

Please sign in to comment.