Skip to content

Commit 33b1177

Browse files
committed
feat: SamplingFilter.EdgeAlpha and SamplingFilter.EdgeLuminance now support the Sampling Width property for edge width
1 parent 792db85 commit 33b1177

File tree

4 files changed

+44
-18
lines changed

4 files changed

+44
-18
lines changed

Packages/src/Editor/UIEffectEditor.cs

+8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class UIEffect2Editor : Editor
3535

3636
private SerializedProperty _samplingFilter;
3737
private SerializedProperty _samplingIntensity;
38+
private SerializedProperty _samplingWidth;
3839
private SerializedProperty _samplingScale;
3940

4041
private SerializedProperty _transitionFilter;
@@ -97,6 +98,7 @@ private void OnEnable()
9798

9899
_samplingFilter = serializedObject.FindProperty("m_SamplingFilter");
99100
_samplingIntensity = serializedObject.FindProperty("m_SamplingIntensity");
101+
_samplingWidth = serializedObject.FindProperty("m_SamplingWidth");
100102
_samplingScale = serializedObject.FindProperty("m_SamplingScale");
101103

102104
_transitionFilter = serializedObject.FindProperty("m_TransitionFilter");
@@ -186,6 +188,12 @@ public void DrawProperties()
186188
{
187189
EditorGUI.indentLevel++;
188190
EditorGUILayout.PropertyField(_samplingIntensity);
191+
if (_samplingFilter.intValue == (int)SamplingFilter.EdgeAlpha
192+
|| _samplingFilter.intValue == (int)SamplingFilter.EdgeLuminance)
193+
{
194+
EditorGUILayout.PropertyField(_samplingWidth);
195+
}
196+
189197
EditorGUI.indentLevel--;
190198
}
191199

Packages/src/Runtime/UIEffect.cs

+19
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public class UIEffect : UIEffectBase
3939
[SerializeField]
4040
protected float m_SamplingIntensity = 0.5f;
4141

42+
[Range(0.5f, 10f)]
43+
[SerializeField]
44+
protected float m_SamplingWidth = 1;
45+
4246
[PowerRange(0.01f, 100f, 10f)]
4347
[SerializeField]
4448
protected float m_SamplingScale = 1f;
@@ -278,6 +282,18 @@ public float samplingIntensity
278282
}
279283
}
280284

285+
public float samplingWidth
286+
{
287+
get => m_SamplingWidth;
288+
set
289+
{
290+
value = Mathf.Clamp(value, 0.5f, 10);
291+
if (Mathf.Approximately(m_SamplingWidth, value)) return;
292+
context.samplingWidth = m_SamplingWidth = value;
293+
SetMaterialDirty();
294+
}
295+
}
296+
281297
public float samplingScale
282298
{
283299
get => m_SamplingScale;
@@ -792,6 +808,7 @@ protected override void UpdateContext(UIEffectContext c)
792808

793809
c.samplingFilter = m_SamplingFilter;
794810
c.samplingIntensity = m_SamplingIntensity;
811+
c.samplingWidth = m_SamplingWidth;
795812

796813
c.transitionFilter = m_TransitionFilter;
797814
c.transitionRate = m_TransitionRate;
@@ -917,6 +934,7 @@ public void LoadPreset(UIEffect preset)
917934

918935
m_SamplingFilter = preset.m_SamplingFilter;
919936
m_SamplingIntensity = preset.m_SamplingIntensity;
937+
m_SamplingWidth = preset.m_SamplingWidth;
920938

921939
m_TransitionFilter = preset.m_TransitionFilter;
922940
m_TransitionRate = preset.m_TransitionRate;
@@ -977,6 +995,7 @@ internal void CopyFrom(UIEffectContext c)
977995

978996
m_SamplingFilter = c.samplingFilter;
979997
m_SamplingIntensity = c.samplingIntensity;
998+
m_SamplingWidth = c.samplingWidth;
980999

9811000
m_TransitionFilter = c.transitionFilter;
9821001
m_TransitionRate = c.transitionRate;

Packages/src/Runtime/UIEffectContext.cs

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class UIEffectContext
2020
private static readonly int s_ColorIntensity = Shader.PropertyToID("_ColorIntensity");
2121
private static readonly int s_ColorGlow = Shader.PropertyToID("_ColorGlow");
2222
private static readonly int s_SamplingIntensity = Shader.PropertyToID("_SamplingIntensity");
23+
private static readonly int s_SamplingWidth = Shader.PropertyToID("_SamplingWidth");
2324
private static readonly int s_SamplingScale = Shader.PropertyToID("_SamplingScale");
2425
private static readonly int s_TransitionRate = Shader.PropertyToID("_TransitionRate");
2526
private static readonly int s_TransitionReverse = Shader.PropertyToID("_TransitionReverse");
@@ -142,6 +143,7 @@ public class UIEffectContext
142143

143144
public SamplingFilter samplingFilter = SamplingFilter.None;
144145
public float samplingIntensity = 0.5f;
146+
public float samplingWidth;
145147

146148
public TransitionFilter transitionFilter = TransitionFilter.None;
147149
public float transitionRate = 0.5f;
@@ -211,6 +213,7 @@ public void CopyFrom(UIEffectContext preset)
211213

212214
samplingFilter = preset.samplingFilter;
213215
samplingIntensity = preset.samplingIntensity;
216+
samplingWidth = preset.samplingWidth;
214217

215218
transitionFilter = preset.transitionFilter;
216219
transitionRate = preset.transitionRate;
@@ -275,6 +278,7 @@ public void ApplyToMaterial(Material material, float actualSamplingScale = 1f)
275278
material.SetInt(s_ColorGlow, colorGlow ? 1 : 0);
276279

277280
material.SetFloat(s_SamplingIntensity, Mathf.Clamp01(samplingIntensity));
281+
material.SetFloat(s_SamplingWidth, samplingWidth);
278282
material.SetFloat(s_SamplingScale, actualSamplingScale);
279283

280284
material.SetFloat(s_TransitionRate, Mathf.Clamp01(transitionRate));

Packages/src/Shaders/UIEffect.cginc

+13-18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ uniform half4 _ColorValue;
77
uniform float _ColorIntensity;
88
uniform int _ColorGlow;
99
uniform float _SamplingIntensity;
10+
uniform float _SamplingWidth;
1011
uniform float _SamplingScale;
1112
uniform sampler2D _TransitionTex;
1213
uniform float4 _TransitionTex_ST;
@@ -52,6 +53,9 @@ uniform int _ShadowColorGlow;
5253
#define UIEFFECT_SAMPLE_CLAMP(uv, uvMask) uieffect_frag(uv) \
5354
* step(uvMask.x, uv.x) * step(uv.x, uvMask.z) \
5455
* step(uvMask.y, uv.y) * step(uv.y, uvMask.w)
56+
#define TEX_SAMPLE_CLAMP(uv, uvMask) tex2D(_MainTex, uv) \
57+
* step(uvMask.x, uv.x) * step(uv.x, uvMask.z) \
58+
* step(uvMask.y, uv.y) * step(uv.y, uvMask.w)
5559

5660
float2 texel_size()
5761
{
@@ -475,26 +479,17 @@ half4 uieffect(float2 uv, const float4 uvMask, const float2 uvLocal)
475479
#elif SAMPLING_EDGE_LUMINANCE || SAMPLING_EDGE_ALPHA
476480
{
477481
// Pixel size
478-
const float dx = texel_size().x;
479-
const float dy = texel_size().y;
482+
const float2 d = texel_size() * _SamplingWidth;
480483

481484
// Pixel values around the current pixel (3x3, 8 neighbors)
482-
const float2 uv00 = uv + half2(-dx, -dy);
483-
const half v00 = to_value(UIEFFECT_SAMPLE_CLAMP(uv00, uvMask));
484-
const float2 uv01 = uv + half2(-dx, 0.0);
485-
const half v01 = to_value(UIEFFECT_SAMPLE_CLAMP(uv01, uvMask));
486-
const float2 uv02 = uv + half2(-dx, +dy);
487-
const half v02 = to_value(UIEFFECT_SAMPLE_CLAMP(uv02, uvMask));
488-
const float2 uv10 = uv + half2(0.0, -dy);
489-
const half v10 = to_value(UIEFFECT_SAMPLE_CLAMP(uv10, uvMask));
490-
const float2 uv12 = uv + half2(0.0, +dy);
491-
const half v12 = to_value(UIEFFECT_SAMPLE_CLAMP(uv12, uvMask));
492-
const float2 uv20 = uv + half2(+dx, -dy);
493-
const half v20 = to_value(UIEFFECT_SAMPLE_CLAMP(uv20, uvMask));
494-
const float2 uv21 = uv + half2(+dx, 0.0);
495-
const half v21 = to_value(UIEFFECT_SAMPLE_CLAMP(uv21, uvMask));
496-
const float2 uv22 = uv + half2(+dx, +dy);
497-
const half v22 = to_value(UIEFFECT_SAMPLE_CLAMP(uv22, uvMask));
485+
const half v00 = to_value(TEX_SAMPLE_CLAMP((uv + half2(-d.x, -d.y)), uvMask));
486+
const half v01 = to_value(TEX_SAMPLE_CLAMP((uv + half2(-d.x, 0.0)), uvMask));
487+
const half v02 = to_value(TEX_SAMPLE_CLAMP((uv + half2(-d.x, +d.y)), uvMask));
488+
const half v10 = to_value(TEX_SAMPLE_CLAMP((uv + half2(0.0, -d.y)), uvMask));
489+
const half v12 = to_value(TEX_SAMPLE_CLAMP((uv + half2(0.0, +d.y)), uvMask));
490+
const half v20 = to_value(TEX_SAMPLE_CLAMP((uv + half2(+d.x, -d.y)), uvMask));
491+
const half v21 = to_value(TEX_SAMPLE_CLAMP((uv + half2(+d.x, 0.0)), uvMask));
492+
const half v22 = to_value(TEX_SAMPLE_CLAMP((uv + half2(+d.x, +d.y)), uvMask));
498493

499494
// Apply Sobel operator
500495
half sobel_h = v00 * -1.0 + v01 * -2.0 + v02 * -1.0 + v20 * 1.0 + v21 * 2.0 + v22 * 1.0;

0 commit comments

Comments
 (0)