Skip to content

Commit

Permalink
feat: add power option for SoftMaskable element
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Oct 31, 2024
1 parent 315b86c commit 1362b18
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Packages/src/Editor/SoftMaskableEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ public class SoftMaskableEditor : AutoGeneratedEditor
{
private SerializedProperty _ignoreSelf;
private SerializedProperty _ignoreChildren;
private SerializedProperty _power;

private void OnEnable()
{
_ignoreSelf = serializedObject.FindProperty("m_IgnoreSelf");
_ignoreChildren = serializedObject.FindProperty("m_IgnoreChildren");
_power = serializedObject.FindProperty("m_Power");
}

public override void OnInspectorGUI()
Expand All @@ -27,6 +29,7 @@ public override void OnInspectorGUI()
serializedObject.Update();
DrawProperty(_ignoreSelf, x => x.SetMaterialDirty());
DrawProperty(_ignoreChildren, x => x.SetMaterialDirtyForChildren());
DrawProperty(_power, x => x.SetMaterialDirty());
serializedObject.ApplyModifiedProperties();

if (targets.Length == 1 && target is SoftMaskable softMaskable && softMaskable.ignored)
Expand Down
36 changes: 33 additions & 3 deletions Packages/src/Runtime/SoftMaskable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public class SoftMaskable : MonoBehaviour, IMaterialModifier, IMaskable
#endif
private static readonly int s_AllowDynamicResolution = Shader.PropertyToID("_AllowDynamicResolution");
private static readonly int s_AllowRenderScale = Shader.PropertyToID("_AllowRenderScale");
private static readonly int s_SoftMaskingPower = Shader.PropertyToID("_SoftMaskingPower");
private const float k_PowerMin = 1f;
private const float k_PowerMax = 5f;

[Tooltip("The graphic is ignored when soft-masking.")]
[SerializeField]
Expand All @@ -24,6 +27,13 @@ public class SoftMaskable : MonoBehaviour, IMaterialModifier, IMaskable
[SerializeField]
private bool m_IgnoreChildren;

[Tooltip("Soft masking power.\n" +
"The higher this value, the faster it becomes transparent.\n" +
"If overlapping objects appear see-through, please adjust this value.")]
[SerializeField]
[Range(k_PowerMin, k_PowerMax)]
private float m_Power = 1f;

/// <summary>
/// The graphic is ignored when soft-masking.
/// </summary>
Expand Down Expand Up @@ -81,6 +91,23 @@ public bool ignored
}
}

/// <summary>
/// Soft masking power.
/// The higher this value, the faster it becomes transparent.
/// If overlapping objects appear see-through, please adjust this value.
/// </summary>
public float power
{
get => m_Power;
set
{
value = Mathf.Clamp(value, k_PowerMin, k_PowerMax);
if (Mathf.Approximately(m_Power, value)) return;
m_Power = value;
SetMaterialDirty();
}
}

private Action _checkGraphic;
private MaskableGraphic _graphic;
private Material _maskableMaterial;
Expand Down Expand Up @@ -183,7 +210,7 @@ Material IMaterialModifier.GetModifiedMaterial(Material baseMaterial)
Profiler.BeginSample("(SM4UI)[SoftMaskable] GetModifiedMaterial");
var isStereo = UISoftMaskProjectSettings.stereoEnabled && _graphic.canvas.IsStereoCanvas();
var useStencil = UISoftMaskProjectSettings.useStencilOutsideScreen;
var localId = 0u;
var localId = (uint)(Mathf.InverseLerp(k_PowerMin, k_PowerMax, power) * (1 << 10));
#if UNITY_EDITOR
var threshold = 0f;
var subtract = false;
Expand All @@ -200,7 +227,7 @@ Material IMaterialModifier.GetModifiedMaterial(Material baseMaterial)
}
}

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

var hash = new Hash128(
Expand All @@ -220,6 +247,7 @@ Material IMaterialModifier.GetModifiedMaterial(Material baseMaterial)
#endif
_maskableMaterial.SetInt(s_AllowDynamicResolution, _softMask.allowDynamicResolution ? 1 : 0);
_maskableMaterial.SetInt(s_AllowRenderScale, _softMask.allowRenderScale ? 1 : 0);
_maskableMaterial.SetFloat(s_SoftMaskingPower, power);
return _maskableMaterial;
}

Expand Down Expand Up @@ -270,7 +298,9 @@ public void SetMaterialDirtyForChildren()

private void UpdateHideFlags()
{
hideFlags = ignoreSelf || ignoreChildren ? HideFlags.None : HideFlags.DontSave;
hideFlags = ignoreSelf || ignoreChildren || !Mathf.Approximately(power, 1f)
? HideFlags.None
: HideFlags.DontSave;
}

#if UNITY_EDITOR
Expand Down
3 changes: 2 additions & 1 deletion Packages/src/Shaders/SoftMask.cginc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ uniform float _RenderScale;
uniform float2 _DynamicResolutionScale;
uniform int _AllowRenderScale;
uniform int _AllowDynamicResolution;
uniform float _SoftMaskingPower;

float Approximately(float4x4 a, float4x4 b)
{
Expand Down Expand Up @@ -116,7 +117,7 @@ float SoftMaskSample(float2 uv, float a)
#endif
#endif

return alpha.x * alpha.y * alpha.z * alpha.w;
return pow(alpha.x * alpha.y * alpha.z * alpha.w, _SoftMaskingPower);
}

void SoftMaskForGraph_float(float4 ScreenPos, float4 WorldPos, float InAlpha, out float A)
Expand Down

0 comments on commit 1362b18

Please sign in to comment.