Skip to content

Commit

Permalink
fix: when using dynamic resolution, soft masking will not be rendered…
Browse files Browse the repository at this point in the history
… correctly
  • Loading branch information
mob-sakai committed Sep 25, 2024
1 parent 0e128e5 commit e7f4539
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 4 deletions.
46 changes: 46 additions & 0 deletions Packages/src/Runtime/SoftMask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
using UnityEngine.Events;
using UnityEngine.Profiling;
using UnityEngine.Rendering;
#if URP_ENABLE
using UnityEngine.Rendering.Universal;
#endif
using UnityEngine.UI;

namespace Coffee.UISoftMask
Expand Down Expand Up @@ -292,6 +295,49 @@ public bool isDirty
private set;
}

public bool allowRenderScale
{
get
{
#if URP_ENABLE
if (_rootCanvas
&& _rootCanvas.renderMode != RenderMode.ScreenSpaceOverlay
&& _rootCanvas.worldCamera
&& GraphicsSettings.currentRenderPipeline is UniversalRenderPipelineAsset)
{
return true;
}

#endif
return false;
}
}

public bool allowDynamicResolution
{
get
{
var isSupported =
#if UNITY_XBOXONE || UNITY_PS5 || UNITY_PS4 || UNITY_SWITCH || UNITY_IOS || UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
true;
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_TVOS
SystemInfo.graphicsDeviceType == GraphicsDeviceType.Metal;
#elif UNITY_ANDROID
SystemInfo.graphicsDeviceType == GraphicsDeviceType.Vulkan;
#elif UNITY_WSA
SystemInfo.graphicsDeviceType == GraphicsDeviceType.Direct3D12;
#else
false;
#endif

return isSupported
&& _rootCanvas
&& _rootCanvas.renderMode != RenderMode.ScreenSpaceOverlay
&& _rootCanvas.worldCamera
&& _rootCanvas.worldCamera.allowDynamicResolution;
}
}

/// <summary>
/// Called when the component is enabled.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions Packages/src/Runtime/SoftMaskable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public class SoftMaskable : MonoBehaviour, IMaterialModifier, IMaskable
private static readonly int s_AlphaClipThreshold = Shader.PropertyToID("_AlphaClipThreshold");
private static readonly int s_MaskingShapeSubtract = Shader.PropertyToID("_MaskingShapeSubtract");
#endif
private static readonly int s_AllowDynamicResolution = Shader.PropertyToID("_AllowDynamicResolution");
private static readonly int s_AllowRenderScale = Shader.PropertyToID("_AllowRenderScale");

private Action _checkGraphic;
private MaskableGraphic _graphic;
private Material _maskableMaterial;
Expand Down Expand Up @@ -151,6 +154,9 @@ Material IMaterialModifier.GetModifiedMaterial(Material baseMaterial)
_maskableMaterial.SetInt(s_MaskingShapeSubtract, subtract ? 1 : 0);
#endif

_maskableMaterial.SetInt(s_AllowDynamicResolution, _softMask.allowDynamicResolution ? 1 : 0);
_maskableMaterial.SetInt(s_AllowRenderScale, _softMask.allowRenderScale ? 1 : 0);

return _maskableMaterial;
}

Expand Down
14 changes: 14 additions & 0 deletions Packages/src/Runtime/Utilities/SoftMaskUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ internal static class SoftMaskUtils
private static readonly int s_ThresholdMin = Shader.PropertyToID("_ThresholdMin");
private static readonly int s_ThresholdMax = Shader.PropertyToID("_ThresholdMax");
private static readonly int s_RenderScale = Shader.PropertyToID("_RenderScale");
private static readonly int s_DynamicResolutionScale = Shader.PropertyToID("_DynamicResolutionScale");
private static float s_CurrentRenderScale = 1;
private static Vector2 s_CurrentDynamicResolutionScale = Vector2.one;

private static readonly string[] s_SoftMaskableShaderNameFormats =
{
Expand Down Expand Up @@ -89,6 +91,18 @@ private static void InitializeOnLoadMethod()
}
};
#endif
UIExtraCallbacks.onBeforeCanvasRebuild += () =>
{
var s = new Vector2(ScalableBufferManager.widthScaleFactor, ScalableBufferManager.heightScaleFactor);
s.x = Mathf.Clamp(Mathf.CeilToInt(s.x / 0.05f) * 0.05f, 0.25f, 1.0f);
s.y = Mathf.Clamp(Mathf.CeilToInt(s.y / 0.05f) * 0.05f, 0.25f, 1.0f);

if (s_CurrentDynamicResolutionScale != s)
{
s_CurrentDynamicResolutionScale = s;
Shader.SetGlobalVector(s_DynamicResolutionScale, s_CurrentDynamicResolutionScale);
}
};

#if TMP_ENABLE
TMPro_EventManager.TEXT_CHANGED_EVENT.Add(obj =>
Expand Down
17 changes: 13 additions & 4 deletions Packages/src/Shaders/SoftMask.cginc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ uniform int _SoftMaskInGameView;
uniform int _SoftMaskInSceneView;
uniform int _MaskingShapeSubtract;
uniform float _RenderScale;
uniform float2 _DynamicResolutionScale;
uniform int _AllowRenderScale;
uniform int _AllowDynamicResolution;

float Approximately(float4x4 a, float4x4 b)
{
Expand Down Expand Up @@ -84,11 +87,17 @@ float SoftMaskSample(float2 uv, float a)
uv = lerp(half2(uv.x / 2, uv.y), half2(uv.x / 2 + 0.5, uv.y), unity_StereoEyeIndex);
}

#if SOFTMASK_EDITOR
#if !SOFTMASK_EDITOR
if (_AllowRenderScale == 1)
{
uv /= _RenderScale;
}
if (_AllowDynamicResolution == 1)
{
uv /= _DynamicResolutionScale;
}
#endif
half4 mask = tex2D(_SoftMaskTex, uv);
#else
half4 mask = tex2D(_SoftMaskTex, uv / _RenderScale);
#endif
half4 alpha = saturate(lerp(half4(1, 1, 1, 1),
lerp(mask, half4(1, 1, 1, 1) - mask, _SoftMaskColor - half4(1, 1, 1, 1)),
_SoftMaskColor));
Expand Down

0 comments on commit e7f4539

Please sign in to comment.