Skip to content

Commit

Permalink
feat: change the minimum value of SoftMaskable.power from 1 to 0.5,…
Browse files Browse the repository at this point in the history
… allowing for smoother softness transitions
  • Loading branch information
mob-sakai committed Dec 27, 2024
1 parent 7677a49 commit 3d59755
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
56 changes: 56 additions & 0 deletions Packages/src/Runtime/Internal/Utilities/PowerRangeAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using UnityEditor;
using UnityEngine;

namespace Coffee.UISoftMaskInternal
{
[AttributeUsage(AttributeTargets.Field)]
public sealed class PowerRangeAttribute : PropertyAttribute
{
public readonly float min;
public readonly float max;
public readonly float power;

public PowerRangeAttribute(float min, float max, float power)
{
this.min = min;
this.max = max;
this.power = power;
}
}

#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(PowerRangeAttribute))]
internal sealed class RangeDrawer : PropertyDrawer
{
public override void OnGUI(Rect r, SerializedProperty property, GUIContent label)
{
var labelWidth = EditorGUIUtility.labelWidth;
var attr = (PowerRangeAttribute)attribute;
label = EditorGUI.BeginProperty(r, label, property);

EditorGUI.BeginChangeCheck();
var rSlider = new Rect(r.x + labelWidth + 1, r.y, r.width - labelWidth - 57, r.height);
var powValue = Mathf.Log(property.floatValue, attr.power);
var powMin = Mathf.Log(attr.min, attr.power);
var powMax = Mathf.Log(attr.max, attr.power);
powValue = GUI.HorizontalSlider(rSlider, powValue, powMin, powMax);
if (EditorGUI.EndChangeCheck())
{
property.floatValue = Mathf.Clamp(Mathf.Pow(attr.power, powValue), attr.min, attr.max);
}

EditorGUI.BeginChangeCheck();
EditorGUIUtility.labelWidth = r.width - 53;
var newValue = EditorGUI.FloatField(r, label, property.floatValue);
if (EditorGUI.EndChangeCheck())
{
property.floatValue = Mathf.Clamp(newValue, attr.min, attr.max);
}

EditorGUI.EndProperty();
EditorGUIUtility.labelWidth = labelWidth;
}
}
#endif
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Packages/src/Runtime/SoftMaskable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class SoftMaskable : MonoBehaviour, IMaterialModifier, IMaskable
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_PowerMin = 0.5f;
private const float k_PowerMax = 5f;

[Tooltip("The graphic is ignored when soft-masking.")]
Expand All @@ -31,7 +31,7 @@ public class SoftMaskable : MonoBehaviour, IMaterialModifier, IMaskable
"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)]
[PowerRange(k_PowerMin, k_PowerMax, 10)]
private float m_Power = 1f;

/// <summary>
Expand Down

0 comments on commit 3d59755

Please sign in to comment.